add Ox.localStorage

This commit is contained in:
rolux 2011-12-30 20:36:55 +05:30
parent 11cb7b1dc8
commit 45243cb5ff
5 changed files with 48 additions and 20 deletions

View file

@ -18,6 +18,7 @@ Ox.App = function(options) {
var self = { var self = {
options: Ox.extend({ options: Ox.extend({
name: 'App',
timeout: 60000, timeout: 60000,
type: 'POST', type: 'POST',
url: '/api/' url: '/api/'
@ -39,6 +40,8 @@ Ox.App = function(options) {
}); });
}); });
that.localStorage = Ox.localStorage(self.options.name);
function getUserData() { function getUserData() {
return { return {
document: { document: {

View file

@ -119,9 +119,7 @@ Ox.Theme = (function() {
}); });
}); });
} }
if (localStorage) { localStorage['Ox.theme'] = theme;
localStorage.OxTheme = theme;
}
return that; return that;
} }

View file

@ -869,7 +869,7 @@ Ox.Input = function(options, self) {
that.clearInput = function() { that.clearInput = function() {
clear(); clear();
return that; return that;
} };
/*@ /*@
focusInput <f> Focus input element focusInput <f> Focus input element
@ -1654,6 +1654,7 @@ Ox.InputElement_ = function(options, self) {
self.options.autosuggestSubmit && submit(); self.options.autosuggestSubmit && submit();
} }
// FIXME: make this public!
function cursor(start, end) { function cursor(start, end) {
/* /*
cursor() returns [start, end] cursor() returns [start, end]

View file

@ -39,8 +39,6 @@ Ox.Spreadsheet = function(options, self) {
}); });
} }
Ox.print('Ss ----', self.options)
renderSpreadsheet(); renderSpreadsheet();
function addColumn(index) { function addColumn(index) {

View file

@ -125,13 +125,48 @@ Ox.load = function() {
}); });
}; };
/*@
Ox.localStorage <o> 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 <f> Logging module Ox.Log <f> Logging module
@*/ @*/
Ox.Log = (function() { Ox.Log = (function() {
var log = localStorage && localStorage.OxLog var storage = Ox.localStorage('Ox'),
? JSON.parse(localStorage.OxLog) log = storage('Log') || {filter: [], filterEnabled: true};
: {filter: [], filterEnabled: true},
that = function() { that = function() {
var ret; var ret;
if (arguments.length == 0) { if (arguments.length == 0) {
@ -141,16 +176,11 @@ Ox.Log = (function() {
} }
return ret; return ret;
}; };
function save() {
if (localStorage) {
localStorage.OxLog = JSON.stringify(log);
}
}
that.filter = function(val) { that.filter = function(val) {
if (!Ox.isUndefined(val)) { if (!Ox.isUndefined(val)) {
that.filter.enable(); that.filter.enable();
log.filter = Ox.toArray(val); log.filter = Ox.toArray(val);
save(); storage('Log', log);
} }
return log.filter; return log.filter;
}; };
@ -159,11 +189,11 @@ Ox.Log = (function() {
}; };
that.filter.disable = function() { that.filter.disable = function() {
log.filterEnabled = false; log.filterEnabled = false;
save(); storage('Log', log);
}; };
that.filter.enable = function() { that.filter.enable = function() {
log.filterEnabled = true; log.filterEnabled = true;
save(); storage('Log', log);
}; };
that.filter.remove = function(val) { that.filter.remove = function(val) {
val = Ox.toArray(val); val = Ox.toArray(val);
@ -176,9 +206,7 @@ Ox.Log = (function() {
if (!log.filterEnabled || log.filter.indexOf(args[0]) > -1) { if (!log.filterEnabled || log.filter.indexOf(args[0]) > -1) {
date = new Date(); date = new Date();
args.unshift( args.unshift(
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3)/*, Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3)
(arguments.callee.caller && arguments.callee.caller.name)
|| '(anonymous)'*/
); );
window.console && window.console.log.apply(window.console, args); window.console && window.console.log.apply(window.console, args);
ret = args.join(' '); ret = args.join(' ');