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 = {
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: {

View file

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

View file

@ -869,7 +869,7 @@ Ox.Input = function(options, self) {
that.clearInput = function() {
clear();
return that;
}
};
/*@
focusInput <f> 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]

View file

@ -39,8 +39,6 @@ Ox.Spreadsheet = function(options, self) {
});
}
Ox.print('Ss ----', self.options)
renderSpreadsheet();
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 = (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(' ');