2019-04-01 21:43:13 +02:00
|
|
|
const { Logger } = require('lib/logger.js');
|
|
|
|
|
|
|
|
class PluginManager {
|
|
|
|
constructor() {
|
2019-07-29 15:43:53 +02:00
|
|
|
this.plugins_ = {};
|
2019-04-01 21:43:13 +02:00
|
|
|
this.logger_ = new Logger();
|
|
|
|
}
|
|
|
|
|
|
|
|
setLogger(l) {
|
|
|
|
this.logger_ = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
|
|
|
}
|
|
|
|
|
|
|
|
static instance() {
|
|
|
|
if (this.instance_) return this.instance_;
|
|
|
|
this.instance_ = new PluginManager();
|
|
|
|
return this.instance_;
|
|
|
|
}
|
|
|
|
|
|
|
|
register(classes) {
|
|
|
|
if (!Array.isArray(classes)) classes = [classes];
|
|
|
|
|
|
|
|
for (let i = 0; i < classes.length; i++) {
|
|
|
|
const PluginClass = classes[i];
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2019-04-01 21:43:13 +02:00
|
|
|
if (this.plugins_[PluginClass.manifest.name]) throw new Error('Already registered: ' + PluginClass.manifest.name);
|
|
|
|
|
|
|
|
this.plugins_[PluginClass.manifest.name] = {
|
|
|
|
Class: PluginClass,
|
|
|
|
instance: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginInstance_(name) {
|
|
|
|
const p = this.plugins_[name];
|
|
|
|
if (p.instance) return p.instance;
|
|
|
|
p.instance = new p.Class();
|
2019-07-29 15:43:53 +02:00
|
|
|
p.instance.dispatch = action => this.dispatch_(action);
|
2019-04-01 21:43:13 +02:00
|
|
|
return p.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginClass_(name) {
|
|
|
|
return this.plugins_[name].Class;
|
|
|
|
}
|
|
|
|
|
|
|
|
onPluginMenuItemTrigger_(event) {
|
|
|
|
const p = this.pluginInstance_(event.pluginName);
|
|
|
|
p.onTrigger({
|
|
|
|
itemName: event.itemName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginDialogToShow(pluginStates) {
|
|
|
|
for (const name in pluginStates) {
|
|
|
|
const p = pluginStates[name];
|
|
|
|
if (!p.dialogOpen) continue;
|
|
|
|
|
|
|
|
const Class = this.pluginClass_(name);
|
|
|
|
if (!Class.Dialog) continue;
|
|
|
|
|
|
|
|
return {
|
|
|
|
Dialog: Class.Dialog,
|
|
|
|
props: this.dialogProps_(name),
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2019-04-01 21:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialogProps_(name) {
|
|
|
|
return {
|
2019-07-29 15:43:53 +02:00
|
|
|
dispatch: action => this.dispatch_(action),
|
2019-04-01 21:43:13 +02:00
|
|
|
plugin: this.pluginInstance_(name),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
menuItems() {
|
|
|
|
let output = [];
|
|
|
|
for (const name in this.plugins_) {
|
|
|
|
const menuItems = this.plugins_[name].Class.manifest.menuItems;
|
|
|
|
if (!menuItems) continue;
|
|
|
|
|
|
|
|
for (const item of menuItems) {
|
|
|
|
item.click = () => {
|
|
|
|
this.onPluginMenuItemTrigger_({
|
|
|
|
pluginName: name,
|
|
|
|
itemName: item.name,
|
|
|
|
});
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2019-04-01 21:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
output = output.concat(menuItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = PluginManager;
|