mirror of
https://github.com/jellyfin/jellyfin-web.git
synced 2024-11-15 18:08:17 -07:00
Merge pull request #1141 from cromefire/promise-plugins
Register plugins with promises
This commit is contained in:
commit
e3cef950f7
@ -1,10 +1,10 @@
|
||||
define(['events'], function (events) {
|
||||
define(['events', 'globalize', 'appRouter'], function (events, globalize, appRouter) {
|
||||
'use strict';
|
||||
|
||||
// TODO: replace with each plugin version
|
||||
var cacheParam = new Date().getTime();
|
||||
|
||||
function loadStrings(plugin, globalize) {
|
||||
function loadStrings(plugin) {
|
||||
var strings = plugin.getTranslations ? plugin.getTranslations() : [];
|
||||
return globalize.loadStrings({
|
||||
name: plugin.id || plugin.packageName,
|
||||
@ -25,68 +25,86 @@ define(['events'], function (events) {
|
||||
this.pluginsList = [];
|
||||
}
|
||||
|
||||
PluginManager.prototype.loadPlugin = function (url) {
|
||||
PluginManager.prototype.loadPlugin = function(pluginSpec) {
|
||||
|
||||
console.debug('Loading plugin: ' + url);
|
||||
var instance = this;
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
function registerPlugin(plugin) {
|
||||
instance.register(plugin);
|
||||
|
||||
require([url, 'globalize', 'appRouter'], function (pluginFactory, globalize, appRouter) {
|
||||
|
||||
var plugin = new pluginFactory();
|
||||
|
||||
// See if it's already installed
|
||||
var existing = instance.pluginsList.filter(function (p) {
|
||||
return p.id === plugin.id;
|
||||
})[0];
|
||||
|
||||
if (existing) {
|
||||
resolve(url);
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.installUrl = url;
|
||||
|
||||
var urlLower = url.toLowerCase();
|
||||
if (urlLower.indexOf('http:') === -1 && urlLower.indexOf('https:') === -1 && urlLower.indexOf('file:') === -1) {
|
||||
if (url.indexOf(appRouter.baseUrl()) !== 0) {
|
||||
|
||||
url = appRouter.baseUrl() + '/' + url;
|
||||
}
|
||||
}
|
||||
|
||||
var separatorIndex = Math.max(url.lastIndexOf('/'), url.lastIndexOf('\\'));
|
||||
plugin.baseUrl = url.substring(0, separatorIndex);
|
||||
|
||||
var paths = {};
|
||||
paths[plugin.id] = plugin.baseUrl;
|
||||
|
||||
requirejs.config({
|
||||
waitSeconds: 0,
|
||||
paths: paths
|
||||
if (plugin.getRoutes) {
|
||||
plugin.getRoutes().forEach(function (route) {
|
||||
definePluginRoute(instance, route, plugin);
|
||||
});
|
||||
}
|
||||
|
||||
instance.register(plugin);
|
||||
if (plugin.type === 'skin') {
|
||||
|
||||
if (plugin.getRoutes) {
|
||||
plugin.getRoutes().forEach(function (route) {
|
||||
definePluginRoute(instance, route, plugin);
|
||||
// translations won't be loaded for skins until needed
|
||||
return Promise.resolve(plugin);
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
loadStrings(plugin)
|
||||
.then(function () {
|
||||
resolve(plugin);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof pluginSpec === "string") {
|
||||
console.debug('Loading plugin (via deprecated requirejs method): ' + pluginSpec);
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
require([pluginSpec], (pluginFactory) => {
|
||||
var plugin = new pluginFactory();
|
||||
|
||||
// See if it's already installed
|
||||
var existing = instance.pluginsList.filter(function (p) {
|
||||
return p.id === plugin.id;
|
||||
})[0];
|
||||
|
||||
if (existing) {
|
||||
resolve(pluginSpec);
|
||||
}
|
||||
|
||||
plugin.installUrl = pluginSpec;
|
||||
|
||||
var urlLower = pluginSpec.toLowerCase();
|
||||
if (urlLower.indexOf('http:') === -1 && urlLower.indexOf('https:') === -1 && urlLower.indexOf('file:') === -1) {
|
||||
if (pluginSpec.indexOf(appRouter.baseUrl()) !== 0) {
|
||||
|
||||
pluginSpec = appRouter.baseUrl() + '/' + pluginSpec;
|
||||
}
|
||||
}
|
||||
|
||||
var separatorIndex = Math.max(pluginSpec.lastIndexOf('/'), pluginSpec.lastIndexOf('\\'));
|
||||
plugin.baseUrl = pluginSpec.substring(0, separatorIndex);
|
||||
|
||||
var paths = {};
|
||||
paths[plugin.id] = plugin.baseUrl;
|
||||
|
||||
requirejs.config({
|
||||
waitSeconds: 0,
|
||||
paths: paths
|
||||
});
|
||||
}
|
||||
|
||||
if (plugin.type === 'skin') {
|
||||
|
||||
// translations won't be loaded for skins until needed
|
||||
resolve(plugin);
|
||||
} else {
|
||||
|
||||
loadStrings(plugin, globalize).then(function () {
|
||||
resolve(plugin);
|
||||
}, 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 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
|
||||
|
Loading…
Reference in New Issue
Block a user