Add basic UserSettings persistence static utility.

Based on localStorage for now.
This commit is contained in:
Vince 2013-06-17 19:54:43 +02:00
parent 403105aae4
commit 889d5c0d53
3 changed files with 73 additions and 0 deletions

View file

@ -130,6 +130,7 @@
<script src="js/utils/core.js"></script>
<script src="js/utils/PixelUtils.js"></script>
<script src="js/utils/CanvasUtils.js"></script>
<script src="js/utils/UserSettings.js"></script>
<script src="js/lib/jsColor_1_4_0/jscolor.js"></script>
<!-- Application libraries-->

View file

@ -21,6 +21,9 @@
*/
ns.SettingsController.prototype.init = function() {
var show_grid = pskl.UserSettings.get(pskl.UserSettings.SHOW_GRID);
$('#show-grid').prop('checked', show_grid);
// Expand drawer when clicking 'Settings' tab.
$('#settings').click(function(evt) {
$('.right-sticky-section').toggleClass('expanded');
@ -32,6 +35,7 @@
$('#show-grid').change($.proxy(function(evt) {
var checked = this.isShowGridChecked_();
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [checked]);
pskl.UserSettings.set(pskl.UserSettings.SHOW_GRID, checked);
}, this));
// Handle canvas background changes:

68
js/utils/UserSettings.js Normal file
View file

@ -0,0 +1,68 @@
(function () {
var ns = $.namespace("pskl");
ns.UserSettings = {
SHOW_GRID: 'SHOW_GRID',
KEY_TO_DEFAULT_VALUE_MAP_ : {
'SHOW_GRID' : false
},
/**
* @private
*/
cache_: {
},
/**
* Static method to access a user defined settings value ot its default
* value if not defined yet.
*/
get : function (key) {
this.checKeyValidity_(key);
if (key in this.cache_) {
return cache[key];
}
return this.get_(key);
},
set : function (key, value) {
this.checKeyValidity_(key);
this.cache_[key] = value;
this.set_(key, value);
},
/**
* @private
*/
get_ : function(key) {
var value = window.localStorage[key];
if (value === undefined) {
value = this.KEY_TO_DEFAULT_VALUE_MAP_[key];
}
else {
var entry = JSON.parse(value);
value = entry.jsonValue;
}
return value;
},
/**
* @private
*/
set_ : function(key, value) {
var entry = { 'jsonValue': value };
window.localStorage[key] = JSON.stringify(entry);
},
checKeyValidity_ : function(key) {
if(key in this.KEY_TO_DEFAULT_VALUE_MAP_) {
return true;
}
console.log("UserSettings key <"+ key +"> not find in supported keys.")
return false;
}
};
})();