2020-08-01 03:01:32 +02:00
|
|
|
import events from 'events';
|
|
|
|
import globalize from 'globalize';
|
|
|
|
/* eslint-disable indent */
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
// TODO: replace with each plugin version
|
|
|
|
var 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) {
|
|
|
|
var strings = plugin.getTranslations ? plugin.getTranslations() : [];
|
|
|
|
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) {
|
|
|
|
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-08-01 03:01:32 +02:00
|
|
|
#registerPlugin(plugin) {
|
|
|
|
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
|
|
|
|
return Promise.resolve(plugin);
|
|
|
|
} else {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-08-01 03:01:32 +02:00
|
|
|
this.#loadStrings(plugin)
|
2020-04-27 13:02:25 +02:00
|
|
|
.then(function () {
|
|
|
|
resolve(plugin);
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
loadPlugin(pluginSpec) {
|
|
|
|
if (typeof pluginSpec === 'string') {
|
|
|
|
console.debug('Loading plugin (via deprecated requirejs method): ' + pluginSpec);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
require([pluginSpec], (pluginFactory) => {
|
|
|
|
var plugin = pluginFactory.default ? new pluginFactory.default() : new pluginFactory();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
// See if it's already installed
|
|
|
|
var existing = this.pluginsList.filter(function (p) {
|
|
|
|
return p.id === plugin.id;
|
|
|
|
})[0];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
if (existing) {
|
|
|
|
resolve(pluginSpec);
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
plugin.installUrl = pluginSpec;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
var separatorIndex = Math.max(pluginSpec.lastIndexOf('/'), pluginSpec.lastIndexOf('\\'));
|
|
|
|
plugin.baseUrl = pluginSpec.substring(0, separatorIndex);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
var paths = {};
|
|
|
|
paths[plugin.id] = plugin.baseUrl;
|
|
|
|
|
|
|
|
requirejs.config({
|
|
|
|
waitSeconds: 0,
|
|
|
|
paths: paths
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-01 03:01:32 +02:00
|
|
|
this.#registerPlugin(plugin).then(resolve).catch(reject);
|
|
|
|
});
|
2020-04-27 13:02:25 +02:00
|
|
|
});
|
2020-08-01 03:01:32 +02:00
|
|
|
} else if (pluginSpec.then) {
|
|
|
|
return pluginSpec.then(pluginBuilder => {
|
|
|
|
return pluginBuilder();
|
|
|
|
}).then((plugin) => {
|
|
|
|
console.debug(`Plugin loaded: ${plugin.id}`);
|
|
|
|
return this.#registerPlugin(plugin);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const err = new TypeError('Plugins have to be a Promise that resolves to a plugin builder function or a RequireJS url (deprecated)');
|
|
|
|
console.error(err);
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
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);
|
|
|
|
events.trigger(this, 'registered', [obj]);
|
|
|
|
}
|
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-01 03:01:32 +02:00
|
|
|
#mapPath(plugin, path, addCacheParam) {
|
|
|
|
if (typeof plugin === 'string') {
|
|
|
|
plugin = this.pluginsList.filter((p) => {
|
|
|
|
return (p.id || p.packageName) === plugin;
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
console.dir(plugin);
|
|
|
|
|
|
|
|
var 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-01 03:01:32 +02:00
|
|
|
export default new PluginManager();
|