fix some localStorage bugs

This commit is contained in:
rolux 2011-12-30 21:07:41 +05:30
parent c9d2ac5fcd
commit 64592e7278
4 changed files with 17 additions and 9 deletions

View file

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

View file

@ -127,31 +127,39 @@ Ox.load = function() {
/*@
Ox.localStorage <o> localStorage wrapper
() -> <o> key:value pairs
(key) -> <*> value
(key, val) -> <f> localStorage object
({key, val}) -> <f> localStorage object
@*/
Ox.localStorage = function(namespace) {
if (Ox.isUndefined(localStorage)) {
// Ox.Log calls this before Ox.isUndefined is defined
if (localStorage === void 0) {
localStorage = {};
}
function storage(key, val) {
var ret, value;
if (Ox.isUndefined(key)) {
if (arguments.length == 0) {
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)) {
} else if (arguments.length == 1 && !Ox.isObject(key)) {
value = localStorage[namespace + '.' + key];
ret = Ox.isUndefined(value) ? void 0 : JSON.parse(value);
} else {
localStorage[namespace + '.' + key] = JSON.stringify(val);
args = Ox.makeObject(arguments);
Ox.forEach(args, function(val, key) {
localStorage[namespace + '.' + key] = JSON.stringify(val);
});
ret = this;
}
return ret;
};
storage.delete = function(key) {
var keys = Ox.isUndefined(key)
var keys = arguments.length == 0
? Object.keys(storage())
: [key];
keys.forEach(function(key) {
@ -166,7 +174,7 @@ Ox.Log <f> Logging module
@*/
Ox.Log = (function() {
var storage = Ox.localStorage('Ox'),
log = storage('Log') || {filter: [], filterEnabled: true};
log = storage('Log') || {filter: [], filterEnabled: true},
that = function() {
var ret;
if (arguments.length == 0) {

View file

@ -291,8 +291,7 @@ Ox.isUndefined <f> Tests if a value is undefined
@*/
Ox.isUndefined = function(val) {
// fixme: val === void 0 is also nice
return typeof val == 'undefined';
return val === void 0;
};
/*@

View file

@ -78,6 +78,7 @@ def build_oxjs(geo):
write_link(link_source, link_target)
# Ox.js
# FIXME: Document what exactly the following dependecies are!
filenames = [
['Fallback.js', 'Core.js'],
['Collection.js', 'Math.js']