From 5bed05365683d96ea0d034ff61e6eec25d34bf96 Mon Sep 17 00:00:00 2001 From: Darren Date: Mon, 5 Nov 2012 19:39:22 +0000 Subject: [PATCH] Client: Persistant settings implemented --- client/assets/dev/app.js | 9 ++++++ client/assets/dev/applet_settings.js | 13 +++++---- client/assets/dev/build.js | 1 + client/assets/dev/index.html.tmpl | 1 + client/assets/dev/model_application.js | 3 -- client/assets/dev/model_datastore.js | 40 ++++++++++++++++++++++++++ client/assets/dev/view.js | 24 ++++++++++++++++ 7 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 client/assets/dev/model_datastore.js diff --git a/client/assets/dev/app.js b/client/assets/dev/app.js index 50a0fc2..6b887bc 100644 --- a/client/assets/dev/app.js +++ b/client/assets/dev/app.js @@ -15,6 +15,7 @@ _kiwi.applets = {}; * and data (think: plugins) */ _kiwi.global = { + settings: undefined, utils: undefined, // Re-usable methods gateway: undefined, user: undefined, @@ -28,12 +29,20 @@ _kiwi.global = { start: function (opts) { opts = opts || {}; + // Load the plugin manager + _kiwi.global.plugins = new _kiwi.model.PluginManager(); + + // Set up the settings datastore + _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings'); + _kiwi.global.settings.load(); + _kiwi.app = new _kiwi.model.Application(opts); if (opts.kiwi_server) { _kiwi.app.kiwi_server = opts.kiwi_server; } + // Start the client up _kiwi.app.start(); return true; diff --git a/client/assets/dev/applet_settings.js b/client/assets/dev/applet_settings.js index 9e821f6..cbb6405 100644 --- a/client/assets/dev/applet_settings.js +++ b/client/assets/dev/applet_settings.js @@ -5,18 +5,19 @@ }, initialize: function (options) { + var settings = _kiwi.global.settings; + this.$el = $($('#tmpl_applet_settings').html()); + + this.$el.find('.theme').val(settings.get('theme')); }, saveSettings: function () { - var theme = $('.theme', this.$el).val(); + var settings = _kiwi.global.settings; - // Clear any current theme - _kiwi.app.view.$el.removeClass(function (i, css) { - return (css.match (/\btheme_\S+/g) || []).join(' '); - }); + settings.set('theme', $('.theme', this.$el).val()); - if (theme) _kiwi.app.view.$el.addClass('theme_' + theme); + settings.save(); } }); diff --git a/client/assets/dev/build.js b/client/assets/dev/build.js index 6cb6922..be386f4 100644 --- a/client/assets/dev/build.js +++ b/client/assets/dev/build.js @@ -41,6 +41,7 @@ var src = concat([ __dirname + '/model_server.js', __dirname + '/model_applet.js', __dirname + '/model_pluginmanager.js', + __dirname + '/model_datastore.js', __dirname + '/applet_settings.js', __dirname + '/applet_nickserv.js', diff --git a/client/assets/dev/index.html.tmpl b/client/assets/dev/index.html.tmpl index b5b1db3..b1961e8 100644 --- a/client/assets/dev/index.html.tmpl +++ b/client/assets/dev/index.html.tmpl @@ -204,6 +204,7 @@ [ 'dev/model_pluginmanager.js', + 'dev/model_datastore.js', 'dev/utils.js', 'dev/view.js' ] diff --git a/client/assets/dev/model_application.js b/client/assets/dev/model_application.js index 8e5f823..6d90b70 100644 --- a/client/assets/dev/model_application.js +++ b/client/assets/dev/model_application.js @@ -31,9 +31,6 @@ _kiwi.model.Application = function () { // Best guess at where the kiwi server is this.detectKiwiServer(); - - // Load the plugin manager - this.plugins = new _kiwi.model.PluginManager(); }; this.start = function () { diff --git a/client/assets/dev/model_datastore.js b/client/assets/dev/model_datastore.js new file mode 100644 index 0000000..3d508ca --- /dev/null +++ b/client/assets/dev/model_datastore.js @@ -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 diff --git a/client/assets/dev/view.js b/client/assets/dev/view.js index 65b7df9..a5841a2 100644 --- a/client/assets/dev/view.js +++ b/client/assets/dev/view.js @@ -908,6 +908,10 @@ _kiwi.view.Application = Backbone.View.extend({ $('#toolbar').resize(this.doLayout); $('#controlbox').resize(this.doLayout); + // Change the theme when the config is changed + _kiwi.global.settings.on('change:theme', this.updateTheme, this); + this.updateTheme(); + this.doLayout(); $(document).keydown(this.setKeyFocus); @@ -921,6 +925,26 @@ _kiwi.view.Application = Backbone.View.extend({ }, + + updateTheme: function (theme_name) { + // If called by the settings callback, get the correct new_value + if (theme_name === _kiwi.global.settings) { + theme_name = arguments[1]; + } + + // If we have no theme specified, get it from the settings + if (!theme_name) theme_name = _kiwi.global.settings.get('theme'); + + // Clear any current theme + this.$el.removeClass(function (i, css) { + return (css.match (/\btheme_\S+/g) || []).join(' '); + }); + + // Apply the new theme + this.$el.addClass('theme_' + (theme_name || 'default')); + }, + + // Globally shift focus to the command input box on a keypress setKeyFocus: function (ev) { // If we're copying text, don't shift focus -- 2.25.1