Client: Persistant settings implemented
authorDarren <darren@darrenwhitlen.com>
Mon, 5 Nov 2012 19:39:22 +0000 (19:39 +0000)
committerDarren <darren@darrenwhitlen.com>
Mon, 5 Nov 2012 19:39:22 +0000 (19:39 +0000)
client/assets/dev/app.js
client/assets/dev/applet_settings.js
client/assets/dev/build.js
client/assets/dev/index.html.tmpl
client/assets/dev/model_application.js
client/assets/dev/model_datastore.js [new file with mode: 0644]
client/assets/dev/view.js

index 50a0fc24117620b0bf91f73658c1529f9c3a2dc5..6b887bc1b777a65cff9f66af63b659c7dfbc8999 100644 (file)
@@ -15,6 +15,7 @@ _kiwi.applets = {};
  * and data (think: plugins)\r
  */\r
 _kiwi.global = {\r
+       settings: undefined,\r
        utils: undefined, // Re-usable methods\r
        gateway: undefined,\r
        user: undefined,\r
@@ -28,12 +29,20 @@ _kiwi.global = {
        start: function (opts) {\r
                opts = opts || {};\r
 \r
+        // Load the plugin manager\r
+        _kiwi.global.plugins = new _kiwi.model.PluginManager();\r
+\r
+        // Set up the settings datastore\r
+        _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings');\r
+        _kiwi.global.settings.load();\r
+\r
                _kiwi.app = new _kiwi.model.Application(opts);\r
 \r
                if (opts.kiwi_server) {\r
                        _kiwi.app.kiwi_server = opts.kiwi_server;\r
                }\r
 \r
+               // Start the client up\r
                _kiwi.app.start();\r
 \r
                return true;\r
index 9e821f63883e80115872a4dadbc0253b397363cb..cbb640582a7848d67e15763bd06b9be25376a0ae 100644 (file)
@@ -5,18 +5,19 @@
         },\r
 \r
         initialize: function (options) {\r
+            var settings = _kiwi.global.settings;\r
+\r
             this.$el = $($('#tmpl_applet_settings').html());\r
+\r
+            this.$el.find('.theme').val(settings.get('theme'));\r
         },\r
         \r
         saveSettings: function () {\r
-            var theme = $('.theme', this.$el).val();\r
+            var settings = _kiwi.global.settings;\r
 \r
-            // Clear any current theme\r
-            _kiwi.app.view.$el.removeClass(function (i, css) {\r
-                return (css.match (/\btheme_\S+/g) || []).join(' ');\r
-            });\r
+            settings.set('theme', $('.theme', this.$el).val());\r
 \r
-            if (theme) _kiwi.app.view.$el.addClass('theme_' + theme);\r
+            settings.save();\r
         }\r
     });\r
 \r
index 6cb692204c6be0e9f0584183d51868a76cb50ef8..be386f4ef71264ba610236065f159074983dec81 100644 (file)
@@ -41,6 +41,7 @@ var src = concat([
     __dirname + '/model_server.js',\r
     __dirname + '/model_applet.js',\r
     __dirname + '/model_pluginmanager.js',\r
+    __dirname + '/model_datastore.js',\r
 \r
     __dirname + '/applet_settings.js',\r
     __dirname + '/applet_nickserv.js',\r
index b5b1db3536b5210448a902aa2a45ac56cdff08d4..b1961e8c5ed90f1c1fe4401afdb1f7f64b6f8619 100644 (file)
 \r
                 [\r
                     'dev/model_pluginmanager.js',\r
+                    'dev/model_datastore.js',\r
                     'dev/utils.js',\r
                     'dev/view.js'\r
                 ]\r
index 8e5f823d5a6d95db75869bb512be3f4f58aa2d94..6d90b70aead3cf2f70430ff10321adfe0366d374 100644 (file)
@@ -31,9 +31,6 @@ _kiwi.model.Application = function () {
 \r
             // Best guess at where the kiwi server is\r
             this.detectKiwiServer();\r
-\r
-            // Load the plugin manager\r
-            this.plugins = new _kiwi.model.PluginManager();\r
         };\r
 \r
         this.start = function () {\r
diff --git a/client/assets/dev/model_datastore.js b/client/assets/dev/model_datastore.js
new file mode 100644 (file)
index 0000000..3d508ca
--- /dev/null
@@ -0,0 +1,40 @@
+_kiwi.model.DataStore = Backbone.Model.extend({
+       initialize: function () {
+               this._namespace = '';
+               this.new_data = {};
+       },
+
+       namespace: function (new_namespace) {
+               if (new_namespace) this._namespace = new_namespace;
+               return this._namespace;
+       },
+
+       // Overload the original save() method
+       save: function () {
+               localStorage.setItem(this._namespace, JSON.stringify(this.attributes));
+       },
+
+       // Overload the original load() method
+       load: function () {
+               if (!localStorage) return;
+
+               var data;
+
+               try {
+                       data = JSON.parse(localStorage.getItem(this._namespace)) || {};
+               } catch (error) {
+                       data = {};
+               }
+
+               this.attributes = data;
+       }
+},
+
+{
+       // Generates a new instance of DataStore with a set namespace
+       instance: function (namespace, attributes) {
+               var datastore = new _kiwi.model.DataStore(attributes);
+               datastore.namespace(namespace);
+               return datastore;
+       }
+});
\ No newline at end of file
index 65b7df9bc1027764e7175b909dcde957c473d1bf..a5841a2a7bace2cef4a981ac7bba367c6d79ffd2 100644 (file)
@@ -908,6 +908,10 @@ _kiwi.view.Application = Backbone.View.extend({
         $('#toolbar').resize(this.doLayout);\r
         $('#controlbox').resize(this.doLayout);\r
 \r
+        // Change the theme when the config is changed\r
+        _kiwi.global.settings.on('change:theme', this.updateTheme, this);\r
+        this.updateTheme();\r
+\r
         this.doLayout();\r
 \r
         $(document).keydown(this.setKeyFocus);\r
@@ -921,6 +925,26 @@ _kiwi.view.Application = Backbone.View.extend({
     },\r
 \r
 \r
+\r
+    updateTheme: function (theme_name) {\r
+        // If called by the settings callback, get the correct new_value\r
+        if (theme_name === _kiwi.global.settings) {\r
+            theme_name = arguments[1];\r
+        }\r
+\r
+        // If we have no theme specified, get it from the settings\r
+        if (!theme_name) theme_name = _kiwi.global.settings.get('theme');\r
+\r
+        // Clear any current theme\r
+        this.$el.removeClass(function (i, css) {\r
+            return (css.match (/\btheme_\S+/g) || []).join(' ');\r
+        });\r
+\r
+        // Apply the new theme\r
+        this.$el.addClass('theme_' + (theme_name || 'default'));\r
+    },\r
+\r
+\r
     // Globally shift focus to the command input box on a keypress\r
     setKeyFocus: function (ev) {\r
         // If we're copying text, don't shift focus\r