From e6267d94113269b19449302519dccbe6100c1afe Mon Sep 17 00:00:00 2001 From: Chase Sterling Date: Fri, 21 Dec 2012 13:26:58 -0500 Subject: [PATCH] Fix issues with js eventmanager. refs #2046 Fix plugin methods not being available when enabled until refresh. refs #2125 --- deluge/ui/web/js/deluge-all/Client.js | 55 ++++++++++---------- deluge/ui/web/js/deluge-all/EventsManager.js | 16 +++--- deluge/ui/web/js/deluge-all/Plugin.js | 1 + 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/deluge/ui/web/js/deluge-all/Client.js b/deluge/ui/web/js/deluge-all/Client.js index 658cf7127..979099bea 100644 --- a/deluge/ui/web/js/deluge-all/Client.js +++ b/deluge/ui/web/js/deluge-all/Client.js @@ -40,20 +40,20 @@ Ext.namespace('Ext.ux.util'); Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, { _components: [], - + _methods: [], - + _requests: {}, - + _url: null, - + _optionKeys: ['scope', 'success', 'failure'], - + constructor: function(config) { Ext.ux.util.RpcClient.superclass.constructor.call(this, config); this._url = config.url || null; this._id = 0; - + this.addEvents( // raw events /** @@ -62,16 +62,13 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, { * @param {Ext.ux.util.RpcClient} this */ 'connected', - + 'error' ); this.reloadMethods(); }, - + reloadMethods: function() { - Ext.each(this._components, function(component) { - delete this[component]; - }, this); this._execute('system.listMethods', { success: this._setMethods, scope: this @@ -82,14 +79,14 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, { options = options || {}; options.params = options.params || []; options.id = this._id; - + var request = Ext.encode({ method: method, params: options.params, id: options.id }); this._id++; - + return Ext.Ajax.request({ url: this._url, method: 'POST', @@ -100,7 +97,7 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, { options: options }); }, - + _onFailure: function(response, requestOptions) { var options = requestOptions.options; errorObj = { @@ -111,23 +108,23 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, { code: 255 } } - + this.fireEvent('error', errorObj, response, requestOptions) - + if (Ext.type(options.failure) != 'function') return; if (options.scope) { options.failure.call(options.scope, errorObj, response, requestOptions); } else { options.failure(errorObj, response, requestOptions); - } + } }, - + _onSuccess: function(response, requestOptions) { var responseObj = Ext.decode(response.responseText); var options = requestOptions.options; if (responseObj.error) { this.fireEvent('error', responseObj, response, requestOptions); - + if (Ext.type(options.failure) != 'function') return; if (options.scope) { options.failure.call(options.scope, responseObj, response, requestOptions); @@ -143,21 +140,21 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, { } } }, - + _parseArgs: function(args) { var params = []; Ext.each(args, function(arg) { params.push(arg); }); - + var options = params[params.length - 1]; if (Ext.type(options) == 'object') { var keys = Ext.keys(options), isOption = false; - + Ext.each(this._optionKeys, function(key) { if (keys.indexOf(key) > -1) isOption = true; }); - + if (isOption) { params.remove(options) } else { @@ -172,11 +169,11 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, { _setMethods: function(methods) { var components = {}, self = this; - + Ext.each(methods, function(method) { var parts = method.split('.'); var component = components[parts[0]] || {}; - + var fn = function() { var options = self._parseArgs(arguments); return self._execute(method, options); @@ -184,11 +181,15 @@ Ext.ux.util.RpcClient = Ext.extend(Ext.util.Observable, { component[parts[1]] = fn; components[parts[0]] = component; }); - + for (var name in components) { self[name] = components[name]; } - + Ext.each(this._components, function(component) { + if (!component in components) { + delete this[component]; + } + }, this); this._components = Ext.keys(components); this.fireEvent('connected', this); } diff --git a/deluge/ui/web/js/deluge-all/EventsManager.js b/deluge/ui/web/js/deluge-all/EventsManager.js index 9a799d53d..0204b3e7b 100644 --- a/deluge/ui/web/js/deluge-all/EventsManager.js +++ b/deluge/ui/web/js/deluge-all/EventsManager.js @@ -91,13 +91,15 @@ Deluge.EventsManager = Ext.extend(Ext.util.Observable, { }, onGetEventsSuccess: function(events) { - if (!events) return; - Ext.each(events, function(event) { - var name = event[0], args = event[1]; - args.splice(0, 0, name); - this.fireEvent.apply(this, args); - }, this); - if (this.running) this.getEvents(); + if (!this.running) return; + if (events) { + Ext.each(events, function(event) { + var name = event[0], args = event[1]; + args.splice(0, 0, name); + this.fireEvent.apply(this, args); + }, this); + } + this.getEvents(); }, // private diff --git a/deluge/ui/web/js/deluge-all/Plugin.js b/deluge/ui/web/js/deluge-all/Plugin.js index 2453abe05..a3d544e85 100644 --- a/deluge/ui/web/js/deluge-all/Plugin.js +++ b/deluge/ui/web/js/deluge-all/Plugin.js @@ -76,6 +76,7 @@ Deluge.Plugin = Ext.extend(Ext.util.Observable, { * then executes the plugins setup method, onEnabled. */ enable: function() { + deluge.client.reloadMethods(); this.fireEvent("enable", this); if (this.onEnable) this.onEnable(); },