mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Migrate PluginManager to ES6
This commit is contained in:
parent
267c1582c0
commit
1ef5529244
5 changed files with 117 additions and 107 deletions
|
@ -152,6 +152,7 @@
|
||||||
"src/components/playerstats/playerstats.js",
|
"src/components/playerstats/playerstats.js",
|
||||||
"src/components/playlisteditor/playlisteditor.js",
|
"src/components/playlisteditor/playlisteditor.js",
|
||||||
"src/components/playmenu.js",
|
"src/components/playmenu.js",
|
||||||
|
"src/components/pluginManager.js",
|
||||||
"src/components/prompt/prompt.js",
|
"src/components/prompt/prompt.js",
|
||||||
"src/components/recordingcreator/seriesrecordingeditor.js",
|
"src/components/recordingcreator/seriesrecordingeditor.js",
|
||||||
"src/components/recordingcreator/recordinghelper.js",
|
"src/components/recordingcreator/recordinghelper.js",
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
define(['appSettings', 'pluginManager'], function (appSettings, pluginManager) {
|
define(['appSettings', 'pluginManager'], function (appSettings, pluginManager) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
pluginManager = pluginManager.default || pluginManager;
|
||||||
|
|
||||||
var settingsKey = 'installedpackages1';
|
var settingsKey = 'installedpackages1';
|
||||||
|
|
||||||
function addPackage(packageManager, pkg) {
|
function addPackage(packageManager, pkg) {
|
||||||
|
|
|
@ -1,37 +1,38 @@
|
||||||
define(['events', 'globalize'], function (events, globalize) {
|
import events from 'events';
|
||||||
'use strict';
|
import globalize from 'globalize';
|
||||||
|
/* eslint-disable indent */
|
||||||
|
|
||||||
// TODO: replace with each plugin version
|
// TODO: replace with each plugin version
|
||||||
var cacheParam = new Date().getTime();
|
var cacheParam = new Date().getTime();
|
||||||
|
|
||||||
function loadStrings(plugin) {
|
class PluginManager {
|
||||||
var strings = plugin.getTranslations ? plugin.getTranslations() : [];
|
pluginsList = [];
|
||||||
return globalize.loadStrings({
|
|
||||||
name: plugin.id || plugin.packageName,
|
|
||||||
strings: strings
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function definePluginRoute(pluginManager, route, plugin) {
|
get plugins() {
|
||||||
route.contentPath = pluginManager.mapPath(plugin, route.path);
|
return this.pluginsList;
|
||||||
route.path = pluginManager.mapRoute(plugin, route);
|
}
|
||||||
|
|
||||||
Emby.App.defineRoute(route, plugin.id);
|
#loadStrings(plugin) {
|
||||||
}
|
var strings = plugin.getTranslations ? plugin.getTranslations() : [];
|
||||||
|
return globalize.loadStrings({
|
||||||
|
name: plugin.id || plugin.packageName,
|
||||||
|
strings: strings
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function PluginManager() {
|
#definePluginRoute(route, plugin) {
|
||||||
this.pluginsList = [];
|
route.contentPath = this.#mapPath(plugin, route.path);
|
||||||
}
|
route.path = this.mapRoute(plugin, route);
|
||||||
|
|
||||||
PluginManager.prototype.loadPlugin = function(pluginSpec) {
|
Emby.App.defineRoute(route, plugin.id);
|
||||||
var instance = this;
|
}
|
||||||
|
|
||||||
function registerPlugin(plugin) {
|
#registerPlugin(plugin) {
|
||||||
instance.register(plugin);
|
this.#register(plugin);
|
||||||
|
|
||||||
if (plugin.getRoutes) {
|
if (plugin.getRoutes) {
|
||||||
plugin.getRoutes().forEach(function (route) {
|
plugin.getRoutes().forEach((route) => {
|
||||||
definePluginRoute(instance, route, plugin);
|
this.#definePluginRoute(route, plugin);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +41,7 @@ define(['events', 'globalize'], function (events, globalize) {
|
||||||
return Promise.resolve(plugin);
|
return Promise.resolve(plugin);
|
||||||
} else {
|
} else {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
loadStrings(plugin)
|
this.#loadStrings(plugin)
|
||||||
.then(function () {
|
.then(function () {
|
||||||
resolve(plugin);
|
resolve(plugin);
|
||||||
})
|
})
|
||||||
|
@ -49,103 +50,106 @@ define(['events', 'globalize'], function (events, globalize) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof pluginSpec === 'string') {
|
loadPlugin(pluginSpec) {
|
||||||
console.debug('Loading plugin (via deprecated requirejs method): ' + pluginSpec);
|
var instance = this;
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
if (typeof pluginSpec === 'string') {
|
||||||
require([pluginSpec], (pluginFactory) => {
|
console.debug('Loading plugin (via deprecated requirejs method): ' + pluginSpec);
|
||||||
var plugin = pluginFactory.default ? new pluginFactory.default() : new pluginFactory();
|
|
||||||
|
|
||||||
// See if it's already installed
|
return new Promise((resolve, reject) => {
|
||||||
var existing = instance.pluginsList.filter(function (p) {
|
require([pluginSpec], (pluginFactory) => {
|
||||||
return p.id === plugin.id;
|
var plugin = pluginFactory.default ? new pluginFactory.default() : new pluginFactory();
|
||||||
})[0];
|
|
||||||
|
|
||||||
if (existing) {
|
// See if it's already installed
|
||||||
resolve(pluginSpec);
|
var existing = this.pluginsList.filter(function (p) {
|
||||||
}
|
return p.id === plugin.id;
|
||||||
|
})[0];
|
||||||
|
|
||||||
plugin.installUrl = pluginSpec;
|
if (existing) {
|
||||||
|
resolve(pluginSpec);
|
||||||
|
}
|
||||||
|
|
||||||
var separatorIndex = Math.max(pluginSpec.lastIndexOf('/'), pluginSpec.lastIndexOf('\\'));
|
plugin.installUrl = pluginSpec;
|
||||||
plugin.baseUrl = pluginSpec.substring(0, separatorIndex);
|
|
||||||
|
|
||||||
var paths = {};
|
var separatorIndex = Math.max(pluginSpec.lastIndexOf('/'), pluginSpec.lastIndexOf('\\'));
|
||||||
paths[plugin.id] = plugin.baseUrl;
|
plugin.baseUrl = pluginSpec.substring(0, separatorIndex);
|
||||||
|
|
||||||
requirejs.config({
|
var paths = {};
|
||||||
waitSeconds: 0,
|
paths[plugin.id] = plugin.baseUrl;
|
||||||
paths: paths
|
|
||||||
|
requirejs.config({
|
||||||
|
waitSeconds: 0,
|
||||||
|
paths: paths
|
||||||
|
});
|
||||||
|
|
||||||
|
this.#registerPlugin(plugin).then(resolve).catch(reject);
|
||||||
});
|
});
|
||||||
|
|
||||||
registerPlugin(plugin).then(resolve).catch(reject);
|
|
||||||
});
|
});
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ofType(type) {
|
||||||
|
return this.pluginsList.filter((o) => {
|
||||||
|
return o.type === type;
|
||||||
});
|
});
|
||||||
} else if (pluginSpec.then) {
|
|
||||||
return pluginSpec.then(pluginBuilder => {
|
|
||||||
return pluginBuilder();
|
|
||||||
}).then(plugin => {
|
|
||||||
console.debug(`Plugin loaded: ${plugin.id}`);
|
|
||||||
return registerPlugin(plugin);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
const err = new Error('Plugins have to be a Promise that resolves to a plugin builder function or a requirejs urls (deprecated)');
|
|
||||||
console.error(err);
|
|
||||||
return Promise.reject(err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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;
|
#mapRoute(plugin, route) {
|
||||||
|
if (typeof plugin === 'string') {
|
||||||
|
plugin = this.pluginsList.filter((p) => {
|
||||||
|
return (p.id || p.packageName) === plugin;
|
||||||
|
})[0];
|
||||||
|
}
|
||||||
|
|
||||||
if (route.toLowerCase().indexOf('http') === 0) {
|
route = route.path || route;
|
||||||
return route;
|
|
||||||
|
if (route.toLowerCase().startsWith('http')) {
|
||||||
|
return route;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '/plugins/' + plugin.id + '/' + route;
|
||||||
}
|
}
|
||||||
|
|
||||||
return '/plugins/' + plugin.id + '/' + route;
|
#mapPath(plugin, path, addCacheParam) {
|
||||||
};
|
if (typeof plugin === 'string') {
|
||||||
|
plugin = this.pluginsList.filter((p) => {
|
||||||
|
return (p.id || p.packageName) === plugin;
|
||||||
|
})[0];
|
||||||
|
}
|
||||||
|
|
||||||
PluginManager.prototype.mapPath = function (plugin, path, addCacheParam) {
|
console.dir(plugin);
|
||||||
if (typeof plugin === 'string') {
|
|
||||||
plugin = this.pluginsList.filter(function (p) {
|
var url = plugin.baseUrl + '/' + path;
|
||||||
return (p.id || p.packageName) === plugin;
|
|
||||||
})[0];
|
if (addCacheParam) {
|
||||||
|
url += url.includes('?') ? '&' : '?';
|
||||||
|
url += 'v=' + cacheParam;
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var url = plugin.baseUrl + '/' + path;
|
/* eslint-enable indent */
|
||||||
|
|
||||||
if (addCacheParam) {
|
export default new PluginManager();
|
||||||
url += url.indexOf('?') === -1 ? '?' : '&';
|
|
||||||
url += 'v=' + cacheParam;
|
|
||||||
}
|
|
||||||
|
|
||||||
return url;
|
|
||||||
};
|
|
||||||
|
|
||||||
return new PluginManager();
|
|
||||||
});
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
define(['pluginManager'], function (pluginManager) {
|
define(['pluginManager'], function (pluginManager) {
|
||||||
|
pluginManager = pluginManager.default || pluginManager;
|
||||||
|
|
||||||
return function () {
|
return function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
|
|
@ -476,7 +476,7 @@ function initClient() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadPlugins(appHost, browser, shell) {
|
function loadPlugins(appHost, browser, shell) {
|
||||||
console.debug('loading installed plugins');
|
console.groupCollapsed('loading installed plugins');
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
require(['webSettings'], function (webSettings) {
|
require(['webSettings'], function (webSettings) {
|
||||||
webSettings.getPlugins().then(function (list) {
|
webSettings.getPlugins().then(function (list) {
|
||||||
|
@ -495,6 +495,7 @@ function initClient() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Promise.all(list.map(loadPlugin)).then(function () {
|
Promise.all(list.map(loadPlugin)).then(function () {
|
||||||
|
console.groupEnd('loading installed plugins');
|
||||||
require(['packageManager'], function (packageManager) {
|
require(['packageManager'], function (packageManager) {
|
||||||
packageManager.init().then(resolve, reject);
|
packageManager.init().then(resolve, reject);
|
||||||
});
|
});
|
||||||
|
@ -507,7 +508,7 @@ function initClient() {
|
||||||
function loadPlugin(url) {
|
function loadPlugin(url) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
require(['pluginManager'], function (pluginManager) {
|
require(['pluginManager'], function (pluginManager) {
|
||||||
pluginManager.loadPlugin(url).then(resolve, reject);
|
pluginManager.default.loadPlugin(url).then(resolve, reject);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue