2020-09-08 02:05:02 -04:00
|
|
|
import { Events } from 'jellyfin-apiclient';
|
2020-08-14 08:46:34 +02:00
|
|
|
import globalize from '../scripts/globalize';
|
2020-11-18 23:13:56 +00:00
|
|
|
import loading from './loading/loading';
|
|
|
|
import appSettings from '../scripts/settings/appSettings';
|
|
|
|
import { playbackManager } from './playback/playbackmanager';
|
2020-10-18 20:00:39 +01:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
/* eslint-disable indent */
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
// TODO: replace with each plugin version
|
2020-10-07 21:12:14 +09:00
|
|
|
const cacheParam = new Date().getTime();
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
class PluginManager {
|
|
|
|
pluginsList = [];
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
get plugins() {
|
|
|
|
return this.pluginsList;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
#loadStrings(plugin) {
|
2020-10-07 21:12:14 +09:00
|
|
|
const strings = plugin.getTranslations ? plugin.getTranslations() : [];
|
2020-08-01 03:01:32 +02:00
|
|
|
return globalize.loadStrings({
|
|
|
|
name: plugin.id || plugin.packageName,
|
|
|
|
strings: strings
|
|
|
|
});
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
#definePluginRoute(route, plugin) {
|
2020-08-12 15:21:56 +02:00
|
|
|
route.contentPath = this.mapPath(plugin, route.path);
|
|
|
|
route.path = this.#mapRoute(plugin, route);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
Emby.App.defineRoute(route, plugin.id);
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-11-18 23:13:56 +00:00
|
|
|
async #registerPlugin(plugin) {
|
2020-08-01 03:01:32 +02:00
|
|
|
this.#register(plugin);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-27 13:02:25 +02:00
|
|
|
if (plugin.getRoutes) {
|
2020-08-01 03:01:32 +02:00
|
|
|
plugin.getRoutes().forEach((route) => {
|
|
|
|
this.#definePluginRoute(route, plugin);
|
2020-04-27 13:02:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plugin.type === 'skin') {
|
|
|
|
// translations won't be loaded for skins until needed
|
2020-11-18 23:13:56 +00:00
|
|
|
return plugin;
|
2020-04-27 13:02:25 +02:00
|
|
|
} else {
|
2020-11-18 23:13:56 +00:00
|
|
|
return await this.#loadStrings(plugin);
|
2020-04-27 13:02:25 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-11-18 23:13:56 +00:00
|
|
|
async #preparePlugin(pluginSpec, plugin) {
|
2020-08-01 03:01:32 +02:00
|
|
|
if (typeof pluginSpec === 'string') {
|
2020-11-18 23:13:56 +00:00
|
|
|
// See if it's already installed
|
|
|
|
const existing = this.plugins.filter(function (p) {
|
|
|
|
return p.id === plugin.id;
|
|
|
|
})[0];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-11-18 23:13:56 +00:00
|
|
|
if (existing) {
|
|
|
|
return pluginSpec;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-11-18 23:13:56 +00:00
|
|
|
plugin.installUrl = pluginSpec;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-11-18 23:13:56 +00:00
|
|
|
const separatorIndex = Math.max(pluginSpec.lastIndexOf('/'), pluginSpec.lastIndexOf('\\'));
|
|
|
|
plugin.baseUrl = pluginSpec.substring(0, separatorIndex);
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-11-18 23:13:56 +00:00
|
|
|
return this.#registerPlugin(plugin);
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-11-18 23:13:56 +00:00
|
|
|
async loadPlugin(pluginSpec) {
|
|
|
|
let plugin;
|
|
|
|
|
|
|
|
if (typeof pluginSpec === 'string') {
|
|
|
|
if (pluginSpec in window) {
|
|
|
|
console.log(`Loading plugin (via window): ${pluginSpec}`);
|
2020-11-29 14:36:25 +01:00
|
|
|
let pluginInstance = await window[pluginSpec];
|
2020-11-29 14:46:18 +01:00
|
|
|
|
2020-11-29 14:36:25 +01:00
|
|
|
if (typeof pluginInstance === 'function') {
|
2020-12-06 11:09:29 +01:00
|
|
|
pluginInstance = await pluginInstance();
|
2020-11-29 14:36:25 +01:00
|
|
|
}
|
2020-11-18 23:13:56 +00:00
|
|
|
|
|
|
|
// init plugin and pass basic dependencies
|
2020-11-29 14:36:25 +01:00
|
|
|
plugin = new pluginInstance({
|
2020-11-18 23:13:56 +00:00
|
|
|
events: Events,
|
|
|
|
loading,
|
|
|
|
appSettings,
|
|
|
|
playbackManager
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.debug(`Loading plugin (via dynamic import): ${pluginSpec}`);
|
2020-11-30 11:50:00 -05:00
|
|
|
const pluginResult = await import(/* webpackChunkName: "[request]" */ `../plugins/${pluginSpec}`);
|
|
|
|
plugin = new pluginResult.default;
|
2020-11-18 23:13:56 +00:00
|
|
|
}
|
2020-08-01 03:01:32 +02:00
|
|
|
} else if (pluginSpec.then) {
|
2020-11-18 23:13:56 +00:00
|
|
|
console.debug('Loading plugin (via promise/async function)');
|
|
|
|
|
|
|
|
const pluginResult = await pluginSpec;
|
|
|
|
plugin = new pluginResult.default;
|
2020-08-01 03:01:32 +02:00
|
|
|
} else {
|
2020-11-09 22:28:23 +00:00
|
|
|
const err = new TypeError('Plugins have to be a Promise that resolves to a plugin builder function');
|
2020-08-01 03:01:32 +02:00
|
|
|
console.error(err);
|
2020-11-18 23:13:56 +00:00
|
|
|
throw err;
|
2020-08-01 03:01:32 +02:00
|
|
|
}
|
2020-11-18 23:13:56 +00:00
|
|
|
|
|
|
|
return this.#preparePlugin(pluginSpec, plugin);
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
// In lieu of automatic discovery, plugins will register dynamic objects
|
|
|
|
// Each object will have the following properties:
|
|
|
|
// name
|
|
|
|
// type (skin, screensaver, etc)
|
|
|
|
#register(obj) {
|
|
|
|
this.pluginsList.push(obj);
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.trigger(this, 'registered', [obj]);
|
2020-08-01 03:01:32 +02:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
ofType(type) {
|
|
|
|
return this.pluginsList.filter((o) => {
|
|
|
|
return o.type === type;
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
#mapRoute(plugin, route) {
|
|
|
|
if (typeof plugin === 'string') {
|
|
|
|
plugin = this.pluginsList.filter((p) => {
|
|
|
|
return (p.id || p.packageName) === plugin;
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
route = route.path || route;
|
|
|
|
|
|
|
|
if (route.toLowerCase().startsWith('http')) {
|
|
|
|
return route;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
return '/plugins/' + plugin.id + '/' + route;
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2020-08-12 15:21:56 +02:00
|
|
|
mapPath(plugin, path, addCacheParam) {
|
2020-08-01 03:01:32 +02:00
|
|
|
if (typeof plugin === 'string') {
|
|
|
|
plugin = this.pluginsList.filter((p) => {
|
|
|
|
return (p.id || p.packageName) === plugin;
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
2020-10-07 21:12:14 +09:00
|
|
|
let url = plugin.baseUrl + '/' + path;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
if (addCacheParam) {
|
|
|
|
url += url.includes('?') ? '&' : '?';
|
|
|
|
url += 'v=' + cacheParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2020-08-01 03:01:32 +02:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
/* eslint-enable indent */
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-16 20:24:45 +02:00
|
|
|
export const pluginManager = new PluginManager();
|