add Ox.localStorage
This commit is contained in:
parent
11cb7b1dc8
commit
45243cb5ff
5 changed files with 48 additions and 20 deletions
|
|
@ -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(' ');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue