From: Darren Date: Thu, 22 Nov 2012 12:25:35 +0000 (+0000) Subject: Client: Simplar applet interface with .loadOnce() #139 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=da848c4f048e26bc7867b17de2477904df086d70;p=KiwiIRC.git Client: Simplar applet interface with .loadOnce() #139 --- diff --git a/client/assets/dev/applet_settings.js b/client/assets/dev/applet_settings.js index 2072fe1..8b9a061 100644 --- a/client/assets/dev/applet_settings.js +++ b/client/assets/dev/applet_settings.js @@ -12,6 +12,8 @@ // Now actually show the current settings this.loadSettings(); + + }, @@ -42,10 +44,13 @@ - _kiwi.applets.Settings = Backbone.Model.extend({ + var Applet = Backbone.Model.extend({ initialize: function () { this.set('title', 'Settings'); this.view = new View(); } }); + + + _kiwi.model.Applet.register('kiwi_settings', Applet); })(); \ No newline at end of file diff --git a/client/assets/dev/model_applet.js b/client/assets/dev/model_applet.js index e447749..07cc5f9 100644 --- a/client/assets/dev/model_applet.js +++ b/client/assets/dev/model_applet.js @@ -17,6 +17,7 @@ _kiwi.model.Applet = _kiwi.model.Panel.extend({ this.loaded_applet = null; }, + // Load an applet within this panel load: function (applet_object, applet_name) { if (typeof applet_object === 'object') { @@ -49,6 +50,7 @@ _kiwi.model.Applet = _kiwi.model.Panel.extend({ return this; }, + loadFromUrl: function(applet_url, applet_name) { var that = this; @@ -65,6 +67,7 @@ _kiwi.model.Applet = _kiwi.model.Panel.extend({ }); }, + close: function () { this.view.$el.remove(); this.destroy(); @@ -78,4 +81,53 @@ _kiwi.model.Applet = _kiwi.model.Panel.extend({ this.closePanel(); } +}, + + +{ + // Load an applet type once only. If it already exists, return that + loadOnce: function (applet_name) { + + // See if we have an instance loaded already + var applet = _.find(_kiwi.app.panels.models, function(panel) { + // Ignore if it's not an applet + if (!panel.isApplet()) return; + + // Ignore if it doesn't have an applet loaded + if (!panel.loaded_applet) return; + + if (panel.loaded_applet.get('_applet_name') === applet_name) { + return true; + } + }); + + if (applet) return applet; + + + // If we didn't find an instance, load a new one up + return this.load(applet_name); + }, + + + load: function (applet_name) { + var applet; + + // Find the applet within the registered applets + if (!_kiwi.applets[applet_name]) return; + + // Create the applet and load the content + applet = new _kiwi.model.Applet(); + applet.load(new _kiwi.applets[applet_name]({_applet_name: applet_name})); + + // Add it into the tab list + _kiwi.app.panels.add(applet); + + + return applet; + }, + + + register: function (applet_name, applet) { + _kiwi.applets[applet_name] = applet; + } }); \ No newline at end of file diff --git a/client/assets/dev/model_application.js b/client/assets/dev/model_application.js index 262383b..156b0f8 100644 --- a/client/assets/dev/model_application.js +++ b/client/assets/dev/model_application.js @@ -648,7 +648,7 @@ _kiwi.model.Application = function () { member.set('away', !(!event.trailing)); } }); - }); + }); gw.on('onlist_start', function (data) { @@ -989,11 +989,8 @@ _kiwi.model.Application = function () { } function settingsCommand (ev) { - var panel = new _kiwi.model.Applet(); - panel.load(new _kiwi.applets.Settings()); - - _kiwi.app.panels.add(panel); - panel.view.show(); + var settings = _kiwi.model.Applet.loadOnce('kiwi_settings'); + settings.view.show(); } function appletCommand (ev) {