2020-05-19 14:35:19 -07:00
|
|
|
define(['events', 'globalize'], function (events, globalize) {
|
2019-01-10 05:39:37 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// TODO: replace with each plugin version
|
|
|
|
var cacheParam = new Date().getTime();
|
2018-10-22 15:05:09 -07:00
|
|
|
|
2020-04-27 05:20:32 -07:00
|
|
|
function loadStrings(plugin) {
|
2018-10-22 15:05:09 -07:00
|
|
|
var strings = plugin.getTranslations ? plugin.getTranslations() : [];
|
|
|
|
return globalize.loadStrings({
|
|
|
|
name: plugin.id || plugin.packageName,
|
|
|
|
strings: strings
|
2019-01-10 05:39:37 -07:00
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function definePluginRoute(pluginManager, route, plugin) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
route.contentPath = pluginManager.mapPath(plugin, route.path);
|
|
|
|
route.path = pluginManager.mapRoute(plugin, route);
|
|
|
|
|
|
|
|
Emby.App.defineRoute(route, plugin.id);
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function PluginManager() {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
this.pluginsList = [];
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
PluginManager.prototype.loadPlugin = function(pluginSpec) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
var instance = this;
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
function registerPlugin(plugin) {
|
|
|
|
instance.register(plugin);
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
if (plugin.getRoutes) {
|
|
|
|
plugin.getRoutes().forEach(function (route) {
|
|
|
|
definePluginRoute(instance, route, plugin);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plugin.type === 'skin') {
|
|
|
|
|
|
|
|
// translations won't be loaded for skins until needed
|
|
|
|
return Promise.resolve(plugin);
|
|
|
|
} else {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-04-27 05:20:32 -07:00
|
|
|
loadStrings(plugin)
|
2020-04-27 04:02:25 -07:00
|
|
|
.then(function () {
|
|
|
|
resolve(plugin);
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-05-19 04:51:34 -07:00
|
|
|
if (typeof pluginSpec === 'string') {
|
2020-04-29 15:31:34 -07:00
|
|
|
console.debug('Loading plugin (via deprecated requirejs method): ' + pluginSpec);
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
require([pluginSpec], (pluginFactory) => {
|
2020-05-21 18:39:21 -07:00
|
|
|
var plugin = pluginFactory.default ? new pluginFactory.default() : new pluginFactory();
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
// See if it's already installed
|
|
|
|
var existing = instance.pluginsList.filter(function (p) {
|
|
|
|
return p.id === plugin.id;
|
|
|
|
})[0];
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
if (existing) {
|
|
|
|
resolve(pluginSpec);
|
2019-01-10 05:39:37 -07:00
|
|
|
}
|
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
plugin.installUrl = pluginSpec;
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
var separatorIndex = Math.max(pluginSpec.lastIndexOf('/'), pluginSpec.lastIndexOf('\\'));
|
|
|
|
plugin.baseUrl = pluginSpec.substring(0, separatorIndex);
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
var paths = {};
|
|
|
|
paths[plugin.id] = plugin.baseUrl;
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
requirejs.config({
|
|
|
|
waitSeconds: 0,
|
|
|
|
paths: paths
|
|
|
|
});
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-27 04:02:25 -07:00
|
|
|
registerPlugin(plugin).then(resolve).catch(reject);
|
|
|
|
});
|
2019-01-10 05:39:37 -07:00
|
|
|
});
|
2020-04-27 04:02:25 -07:00
|
|
|
} else if (pluginSpec.then) {
|
|
|
|
return pluginSpec.then(pluginBuilder => {
|
2020-04-27 05:20:32 -07:00
|
|
|
return pluginBuilder();
|
2020-04-27 04:02:25 -07:00
|
|
|
}).then(plugin => {
|
2020-04-29 15:31:34 -07:00
|
|
|
console.debug(`Plugin loaded: ${plugin.id}`);
|
2020-04-27 04:02:25 -07:00
|
|
|
return registerPlugin(plugin);
|
|
|
|
});
|
|
|
|
} else {
|
2020-05-19 04:51:34 -07:00
|
|
|
const err = new Error('Plugins have to be a Promise that resolves to a plugin builder function or a requirejs urls (deprecated)');
|
2020-04-27 04:02:25 -07:00
|
|
|
console.error(err);
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
2019-01-10 05:39:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// In lieu of automatic discovery, plugins will register dynamic objects
|
|
|
|
// Each object will have the following properties:
|
|
|
|
// name
|
|
|
|
// type (skin, screensaver, etc)
|
|
|
|
PluginManager.prototype.register = function (obj) {
|
|
|
|
|
|
|
|
this.pluginsList.push(obj);
|
|
|
|
events.trigger(this, 'registered', [obj]);
|
|
|
|
};
|
|
|
|
|
|
|
|
PluginManager.prototype.ofType = function (type) {
|
|
|
|
|
|
|
|
return this.pluginsList.filter(function (o) {
|
|
|
|
return o.type === type;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
PluginManager.prototype.plugins = function () {
|
|
|
|
return this.pluginsList;
|
|
|
|
};
|
|
|
|
|
|
|
|
PluginManager.prototype.mapRoute = function (plugin, route) {
|
|
|
|
|
|
|
|
if (typeof plugin === 'string') {
|
|
|
|
plugin = this.pluginsList.filter(function (p) {
|
|
|
|
return (p.id || p.packageName) === plugin;
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
route = route.path || route;
|
|
|
|
|
|
|
|
if (route.toLowerCase().indexOf('http') === 0) {
|
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '/plugins/' + plugin.id + '/' + route;
|
|
|
|
};
|
|
|
|
|
|
|
|
PluginManager.prototype.mapPath = function (plugin, path, addCacheParam) {
|
|
|
|
|
|
|
|
if (typeof plugin === 'string') {
|
|
|
|
plugin = this.pluginsList.filter(function (p) {
|
|
|
|
return (p.id || p.packageName) === plugin;
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
var url = plugin.baseUrl + '/' + path;
|
|
|
|
|
|
|
|
if (addCacheParam) {
|
|
|
|
url += url.indexOf('?') === -1 ? '?' : '&';
|
|
|
|
url += 'v=' + cacheParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
|
|
|
return new PluginManager();
|
2020-02-22 09:47:03 -07:00
|
|
|
});
|