2013-06-17 21:54:43 +04:00
|
|
|
(function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl");
|
2013-06-17 21:54:43 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.UserSettings = {
|
2013-06-17 21:54:43 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
SHOW_GRID : 'SHOW_GRID',
|
|
|
|
CANVAS_BACKGROUND : 'CANVAS_BACKGROUND',
|
2013-06-17 21:54:43 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
KEY_TO_DEFAULT_VALUE_MAP_ : {
|
|
|
|
'SHOW_GRID' : false,
|
2013-09-29 01:52:51 +04:00
|
|
|
'CANVAS_BACKGROUND' : 'medium-canvas-background'
|
2013-08-10 14:11:16 +04:00
|
|
|
},
|
2013-06-17 21:54:43 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
cache_ : {},
|
2013-06-17 21:54:43 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
2013-09-29 01:52:51 +04:00
|
|
|
* Static method to access a user defined settings value ot its default
|
2013-08-10 14:11:16 +04:00
|
|
|
* value if not defined yet.
|
|
|
|
*/
|
|
|
|
get : function (key) {
|
|
|
|
this.checkKeyValidity_(key);
|
|
|
|
if (!(key in this.cache_)) {
|
|
|
|
this.cache_[key] =
|
|
|
|
this.readFromLocalStorage_(key) || this.readFromDefaults_(key);
|
|
|
|
}
|
|
|
|
return this.cache_[key];
|
|
|
|
},
|
2013-06-17 21:54:43 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
set : function (key, value) {
|
|
|
|
this.checkKeyValidity_(key);
|
|
|
|
this.cache_[key] = value;
|
|
|
|
this.writeToLocalStorage_(key, value);
|
2013-06-17 22:24:27 +04:00
|
|
|
|
2013-09-29 01:52:51 +04:00
|
|
|
$.publish(Events.USER_SETTINGS_CHANGED, [key, value]);
|
2013-08-10 14:11:16 +04:00
|
|
|
},
|
2013-06-17 21:54:43 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
readFromLocalStorage_ : function(key) {
|
|
|
|
var value = window.localStorage[key];
|
|
|
|
if (typeof value != "undefined") {
|
|
|
|
value = JSON.parse(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
},
|
2013-06-17 21:54:43 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
writeToLocalStorage_ : function(key, value) {
|
|
|
|
// TODO(grosbouddha): Catch storage exception here.
|
|
|
|
window.localStorage[key] = JSON.stringify(value);
|
|
|
|
},
|
2013-06-19 03:51:53 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
readFromDefaults_ : function (key) {
|
|
|
|
return this.KEY_TO_DEFAULT_VALUE_MAP_[key];
|
|
|
|
},
|
2013-06-17 21:54:43 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
checkKeyValidity_ : function(key) {
|
|
|
|
if(!(key in this.KEY_TO_DEFAULT_VALUE_MAP_)) {
|
|
|
|
// TODO(grosbouddha): Define error catching strategy and throw exception from here.
|
|
|
|
console.log("UserSettings key <"+ key +"> not find in supported keys.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-06-17 21:54:43 +04:00
|
|
|
})();
|