From 45243cb5ff2cf3e21b5728c7aac0e876613a7d58 Mon Sep 17 00:00:00 2001 From: rolux Date: Fri, 30 Dec 2011 20:36:55 +0530 Subject: [PATCH] add Ox.localStorage --- source/Ox.UI/js/Core/Ox.App.js | 3 ++ source/Ox.UI/js/Core/Ox.Theme.js | 4 +- source/Ox.UI/js/Form/Ox.Input.js | 3 +- source/Ox.UI/js/Form/Ox.Spreadsheet.js | 2 - source/Ox/js/Core.js | 56 +++++++++++++++++++------- 5 files changed, 48 insertions(+), 20 deletions(-) diff --git a/source/Ox.UI/js/Core/Ox.App.js b/source/Ox.UI/js/Core/Ox.App.js index 4a63fdc7..9eba636f 100644 --- a/source/Ox.UI/js/Core/Ox.App.js +++ b/source/Ox.UI/js/Core/Ox.App.js @@ -18,6 +18,7 @@ Ox.App = function(options) { var self = { options: Ox.extend({ + name: 'App', timeout: 60000, type: 'POST', url: '/api/' @@ -39,6 +40,8 @@ Ox.App = function(options) { }); }); + that.localStorage = Ox.localStorage(self.options.name); + function getUserData() { return { document: { diff --git a/source/Ox.UI/js/Core/Ox.Theme.js b/source/Ox.UI/js/Core/Ox.Theme.js index 7efedfa5..c3e19491 100644 --- a/source/Ox.UI/js/Core/Ox.Theme.js +++ b/source/Ox.UI/js/Core/Ox.Theme.js @@ -119,9 +119,7 @@ Ox.Theme = (function() { }); }); } - if (localStorage) { - localStorage.OxTheme = theme; - } + localStorage['Ox.theme'] = theme; return that; } diff --git a/source/Ox.UI/js/Form/Ox.Input.js b/source/Ox.UI/js/Form/Ox.Input.js index 8d825eed..e2c75219 100644 --- a/source/Ox.UI/js/Form/Ox.Input.js +++ b/source/Ox.UI/js/Form/Ox.Input.js @@ -869,7 +869,7 @@ Ox.Input = function(options, self) { that.clearInput = function() { clear(); return that; - } + }; /*@ focusInput Focus input element @@ -1654,6 +1654,7 @@ Ox.InputElement_ = function(options, self) { self.options.autosuggestSubmit && submit(); } + // FIXME: make this public! function cursor(start, end) { /* cursor() returns [start, end] diff --git a/source/Ox.UI/js/Form/Ox.Spreadsheet.js b/source/Ox.UI/js/Form/Ox.Spreadsheet.js index 7e97615b..55dfeffd 100644 --- a/source/Ox.UI/js/Form/Ox.Spreadsheet.js +++ b/source/Ox.UI/js/Form/Ox.Spreadsheet.js @@ -39,8 +39,6 @@ Ox.Spreadsheet = function(options, self) { }); } - Ox.print('Ss ----', self.options) - renderSpreadsheet(); function addColumn(index) { diff --git a/source/Ox/js/Core.js b/source/Ox/js/Core.js index 38b7307d..3e364dfc 100644 --- a/source/Ox/js/Core.js +++ b/source/Ox/js/Core.js @@ -125,13 +125,48 @@ Ox.load = function() { }); }; +/*@ +Ox.localStorage localStorage wrapper +@*/ +Ox.localStorage = function(namespace) { + if (Ox.isUndefined(localStorage)) { + localStorage = {}; + } + function storage(key, val) { + var ret, value; + if (Ox.isUndefined(key)) { + ret = {}; + Ox.forEach(localStorage, function(val, key) { + if (Ox.startsWith(key, namespace + '.')) { + ret[key.substr(namespace.length + 1)] = JSON.parse(val); + } + }); + } else if (Ox.isUndefined(val)) { + value = localStorage[namespace + '.' + key]; + ret = Ox.isUndefined(value) ? void 0 : JSON.parse(value); + } else { + localStorage[namespace + '.' + key] = JSON.stringify(val); + ret = this; + } + return ret; + }; + storage.delete = function(key) { + var keys = Ox.isUndefined(key) + ? Object.keys(storage()) + : [key]; + keys.forEach(function(key) { + delete localStorage[namespace + '.' + key]; + }); + }; + return storage; +}; + /*@ Ox.Log Logging module @*/ Ox.Log = (function() { - var log = localStorage && localStorage.OxLog - ? JSON.parse(localStorage.OxLog) - : {filter: [], filterEnabled: true}, + var storage = Ox.localStorage('Ox'), + log = storage('Log') || {filter: [], filterEnabled: true}; that = function() { var ret; if (arguments.length == 0) { @@ -141,16 +176,11 @@ Ox.Log = (function() { } return ret; }; - function save() { - if (localStorage) { - localStorage.OxLog = JSON.stringify(log); - } - } that.filter = function(val) { if (!Ox.isUndefined(val)) { that.filter.enable(); log.filter = Ox.toArray(val); - save(); + storage('Log', log); } return log.filter; }; @@ -159,11 +189,11 @@ Ox.Log = (function() { }; that.filter.disable = function() { log.filterEnabled = false; - save(); + storage('Log', log); }; that.filter.enable = function() { log.filterEnabled = true; - save(); + storage('Log', log); }; that.filter.remove = function(val) { val = Ox.toArray(val); @@ -176,9 +206,7 @@ Ox.Log = (function() { if (!log.filterEnabled || log.filter.indexOf(args[0]) > -1) { date = new Date(); args.unshift( - Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3)/*, - (arguments.callee.caller && arguments.callee.caller.name) - || '(anonymous)'*/ + Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3) ); window.console && window.console.log.apply(window.console, args); ret = args.join(' ');