1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/pluginManager.js

152 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import events from 'jellyfin-apiclient';
import globalize from '../scripts/globalize';
2020-08-01 03:01:32 +02:00
/* eslint-disable indent */
// 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;
}
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);
2020-08-01 03:01:32 +02:00
Emby.App.defineRoute(route, plugin.id);
}
2020-08-01 03:01:32 +02:00
#registerPlugin(plugin) {
this.#register(plugin);
if (plugin.getRoutes) {
2020-08-01 03:01:32 +02:00
plugin.getRoutes().forEach((route) => {
this.#definePluginRoute(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-08-01 03:01:32 +02:00
this.#loadStrings(plugin)
.then(function () {
resolve(plugin);
})
.catch(reject);
});
}
}
2020-08-01 03:01:32 +02:00
loadPlugin(pluginSpec) {
if (typeof pluginSpec === 'string') {
console.debug('Loading plugin (via deprecated requirejs method): ' + pluginSpec);
2020-08-01 03:01:32 +02:00
return new Promise((resolve, reject) => {
require([pluginSpec], (pluginFactory) => {
2020-10-07 21:12:14 +09:00
const plugin = pluginFactory.default ? new pluginFactory.default() : new pluginFactory();
2020-08-01 03:01:32 +02:00
// See if it's already installed
2020-10-07 21:12:14 +09:00
const existing = this.pluginsList.filter(function (p) {
2020-08-01 03:01:32 +02:00
return p.id === plugin.id;
})[0];
2020-08-01 03:01:32 +02:00
if (existing) {
resolve(pluginSpec);
}
2020-08-01 03:01:32 +02:00
plugin.installUrl = pluginSpec;
2020-10-07 21:12:14 +09:00
const separatorIndex = Math.max(pluginSpec.lastIndexOf('/'), pluginSpec.lastIndexOf('\\'));
2020-08-01 03:01:32 +02:00
plugin.baseUrl = pluginSpec.substring(0, separatorIndex);
2020-10-07 21:12:14 +09:00
const paths = {};
2020-08-01 03:01:32 +02:00
paths[plugin.id] = plugin.baseUrl;
requirejs.config({
waitSeconds: 0,
paths: paths
});
2020-08-01 03:01:32 +02:00
this.#registerPlugin(plugin).then(resolve).catch(reject);
});
});
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);
}
}
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]);
}
2020-08-01 03:01:32 +02:00
ofType(type) {
return this.pluginsList.filter((o) => {
return o.type === type;
});
}
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;
}
2020-08-01 03:01:32 +02:00
return '/plugins/' + plugin.id + '/' + route;
}
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;
2020-08-01 03:01:32 +02:00
if (addCacheParam) {
url += url.includes('?') ? '&' : '?';
url += 'v=' + cacheParam;
}
return url;
}
2020-08-01 03:01:32 +02:00
}
2020-08-01 03:01:32 +02:00
/* eslint-enable indent */
2020-08-01 03:01:32 +02:00
export default new PluginManager();