From 7e12222d33e424e7611b2bac701262a4ee5e5c96 Mon Sep 17 00:00:00 2001 From: Damien Churchill Date: Sat, 24 Apr 2010 01:40:34 +0100 Subject: [PATCH] create a plugin registration system to allow for plugin loading on the fly remove the plugin event handlers from the event manager fix enabling/disabling plugins when the preferences page hasn't been rendered yet --- deluge/ui/web/js/deluge-all/Deluge.js | 28 +++++++++++++++++++ deluge/ui/web/js/deluge-all/EventsManager.js | 2 -- deluge/ui/web/js/deluge-all/UI.js | 27 ++++++++++++------ .../js/deluge-all/preferences/PluginsPage.js | 2 ++ 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/deluge/ui/web/js/deluge-all/Deluge.js b/deluge/ui/web/js/deluge-all/Deluge.js index 96810bee8..de82dd7af 100644 --- a/deluge/ui/web/js/deluge-all/Deluge.js +++ b/deluge/ui/web/js/deluge-all/Deluge.js @@ -87,6 +87,9 @@ Ext.USE_NATIVE_JSON = true; // Create the Deluge namespace Ext.apply(Deluge, { + + // private + pluginStore: {}, // private progressTpl: '
' + @@ -117,6 +120,31 @@ Ext.apply(Deluge, { var barWidth = progressWidth - 1; var textWidth = ((progressWidth - modifier) > 0 ? progressWidth - modifier : 0); return String.format(Deluge.progressTpl, text, width, barWidth, textWidth); + }, + + /** + * Constructs a new instance of the specified plugin. + * @param {String} name The plugin name to create + */ + createPlugin: function(name) { + return new Deluge.pluginStore[name](); + }, + + /** + * Check to see if a plugin has been registered. + * @param {String} name The plugin name to check + */ + hasPlugin: function(name) { + return (Deluge.pluginStore[name]) ? true : false; + }, + + /** + * Register a plugin with the Deluge interface. + * @param {String} name The plugin name to register + * @param {Plugin} plugin The plugin to register + */ + registerPlugin: function(name, plugin) { + Deluge.pluginStore[name] = plugin; } }); diff --git a/deluge/ui/web/js/deluge-all/EventsManager.js b/deluge/ui/web/js/deluge-all/EventsManager.js index df669ff45..0981d4393 100644 --- a/deluge/ui/web/js/deluge-all/EventsManager.js +++ b/deluge/ui/web/js/deluge-all/EventsManager.js @@ -88,8 +88,6 @@ Deluge.EventsManager = Ext.extend(Ext.util.Observable, { // private onLogin: function() { this.start(); - this.on('PluginEnabledEvent', this.onPluginEnabled, this); - this.on('PluginDisabledEvent', this.onPluginDisabled, this); }, onGetEventsSuccess: function(events) { diff --git a/deluge/ui/web/js/deluge-all/UI.js b/deluge/ui/web/js/deluge-all/UI.js index 5ec69cc10..a73a8c641 100644 --- a/deluge/ui/web/js/deluge-all/UI.js +++ b/deluge/ui/web/js/deluge-all/UI.js @@ -81,8 +81,9 @@ deluge.ui = { url: deluge.config.base + 'json' }); - for (var plugin in Deluge.plugins) { - plugin = new Deluge.plugins[plugin](); + // enable all the already active plugins + for (var plugin in Deluge.pluginStore) { + plugin = Deluge.createPlugin(plugin); plugin.enable(); deluge.plugins[plugin.name] = plugin; } @@ -161,14 +162,19 @@ deluge.ui = { }, onPluginEnabled: function(pluginName) { - deluge.client.web.get_plugin_resources(pluginName, { - success: this.onGotPluginResources, - scope: this - }) + alert('enabled ' + pluginName); + if (deluge.plugins[pluginName]) { + deluge.plugins[pluginName].enable(); + } else { + deluge.client.web.get_plugin_resources(pluginName, { + success: this.onGotPluginResources, + scope: this + }); + } }, onGotPluginResources: function(resources) { - var scripts = (deluge.debug) ? resources.debug_scripts : resources.scripts; + var scripts = (Deluge.debug) ? resources.debug_scripts : resources.scripts; Ext.each(scripts, function(script) { Ext.ux.JSLoader({ url: script, @@ -179,15 +185,18 @@ deluge.ui = { }, onPluginDisabled: function(pluginName) { + alert('disabled ' + pluginName); deluge.plugins[pluginName].disable(); }, onPluginLoaded: function(options) { // This could happen if the plugin has multiple scripts - if (!deluge.plugins[options.pluginName]) return; + if (!Deluge.hasPlugin(options.pluginName)) return; // Enable the plugin - deluge.plugins[options.pluginName].enable(); + plugin = Deluge.createPlugin(options.pluginName); + plugin.enable(); + deluge.plugins[plugin.name] = plugin; }, /** diff --git a/deluge/ui/web/js/deluge-all/preferences/PluginsPage.js b/deluge/ui/web/js/deluge-all/preferences/PluginsPage.js index 63da922f1..c805cba06 100644 --- a/deluge/ui/web/js/deluge-all/preferences/PluginsPage.js +++ b/deluge/ui/web/js/deluge-all/preferences/PluginsPage.js @@ -310,6 +310,7 @@ Deluge.preferences.Plugins = Ext.extend(Ext.Panel, { onPluginEnabled: function(pluginName) { var index = this.grid.getStore().find('plugin', pluginName); + if (index == -1) return; var plugin = this.grid.getStore().getAt(index); plugin.set('enabled', true); plugin.commit(); @@ -317,6 +318,7 @@ Deluge.preferences.Plugins = Ext.extend(Ext.Panel, { onPluginDisabled: function(pluginName) { var index = this.grid.getStore().find('plugin', pluginName); + if (index == -1) return; var plugin = this.grid.getStore().getAt(index); plugin.set('enabled', false); plugin.commit();