mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Added the possibility to register plugins with promises
This commit is contained in:
parent
564ab1f7b4
commit
0ee1c1ec80
1 changed files with 71 additions and 53 deletions
|
@ -1,4 +1,4 @@
|
||||||
define(['events'], function (events) {
|
define(['events', 'globalize', 'appRouter'], function (events, globalize, appRouter) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// TODO: replace with each plugin version
|
// TODO: replace with each plugin version
|
||||||
|
@ -25,68 +25,86 @@ define(['events'], function (events) {
|
||||||
this.pluginsList = [];
|
this.pluginsList = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
PluginManager.prototype.loadPlugin = function (url) {
|
PluginManager.prototype.loadPlugin = function(pluginSpec) {
|
||||||
|
|
||||||
console.debug('Loading plugin: ' + url);
|
|
||||||
var instance = this;
|
var instance = this;
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
function registerPlugin(plugin) {
|
||||||
|
instance.register(plugin);
|
||||||
|
|
||||||
require([url, 'globalize', 'appRouter'], function (pluginFactory, globalize, appRouter) {
|
if (plugin.getRoutes) {
|
||||||
|
plugin.getRoutes().forEach(function (route) {
|
||||||
var plugin = new pluginFactory();
|
definePluginRoute(instance, route, plugin);
|
||||||
|
|
||||||
// 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
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
instance.register(plugin);
|
if (plugin.type === 'skin') {
|
||||||
|
|
||||||
if (plugin.getRoutes) {
|
// translations won't be loaded for skins until needed
|
||||||
plugin.getRoutes().forEach(function (route) {
|
return Promise.resolve(plugin);
|
||||||
definePluginRoute(instance, route, plugin);
|
} else {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
loadStrings(plugin, globalize)
|
||||||
|
.then(function () {
|
||||||
|
resolve(plugin);
|
||||||
|
})
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof pluginSpec === "string") {
|
||||||
|
console.debug('Loading plugin: ' + 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') {
|
registerPlugin(plugin).then(resolve).catch(reject);
|
||||||
|
});
|
||||||
// translations won't be loaded for skins until needed
|
|
||||||
resolve(plugin);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
loadStrings(plugin, globalize).then(function () {
|
|
||||||
resolve(plugin);
|
|
||||||
}, reject);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
} else if (pluginSpec.then) {
|
||||||
|
return pluginSpec.then(pluginBuilder => {
|
||||||
|
const pluginHelper = {}; // TODO: Advance this
|
||||||
|
return pluginBuilder(pluginHelper);
|
||||||
|
}).then(plugin => {
|
||||||
|
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
|
// In lieu of automatic discovery, plugins will register dynamic objects
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue