2012-09-16 14:59:19 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.service");
|
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
ns.LocalStorageService = function (piskelController) {
|
2012-09-16 14:59:19 +04:00
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
if(piskelController === undefined) {
|
|
|
|
throw "Bad LocalStorageService initialization: <undefined piskelController>";
|
2012-09-16 14:59:19 +04:00
|
|
|
}
|
2013-09-22 23:02:43 +04:00
|
|
|
this.piskelController = piskelController;
|
2012-09-16 14:59:19 +04:00
|
|
|
};
|
|
|
|
|
2014-02-10 04:00:16 +04:00
|
|
|
ns.LocalStorageService.prototype.init = function() {};
|
2013-08-04 20:27:32 +04:00
|
|
|
|
2014-02-10 04:00:16 +04:00
|
|
|
// localStorage.setItem('piskel_bkp', pskl.app.piskelController.serialize())
|
2012-09-16 14:59:19 +04:00
|
|
|
|
2014-02-10 04:00:16 +04:00
|
|
|
ns.LocalStorageService.prototype.save = function(name, description, piskel) {
|
|
|
|
this.removeFromKeys_(name);
|
|
|
|
this.addToKeys_(name, description, Date.now());
|
|
|
|
window.localStorage.setItem('piskel.' + name, piskel);
|
2012-09-16 14:59:19 +04:00
|
|
|
};
|
|
|
|
|
2014-02-10 04:00:16 +04:00
|
|
|
ns.LocalStorageService.prototype.load = function(name) {
|
|
|
|
var piskelString = this.getPiskel(name);
|
|
|
|
var key = this.getKey_(name);
|
2013-12-11 00:25:36 +04:00
|
|
|
|
2014-02-10 04:00:16 +04:00
|
|
|
pskl.utils.serialization.Deserializer.deserialize(JSON.parse(piskelString), function (piskel) {
|
|
|
|
piskel.setDescriptor(new pskl.model.piskel.Descriptor(name, key.description, true));
|
2013-11-14 02:39:43 +04:00
|
|
|
pskl.app.piskelController.setPiskel(piskel);
|
|
|
|
});
|
2012-09-16 14:59:19 +04:00
|
|
|
};
|
|
|
|
|
2014-02-10 04:00:16 +04:00
|
|
|
ns.LocalStorageService.prototype.remove = function(name) {
|
|
|
|
this.removeFromKeys_(name);
|
|
|
|
window.localStorage.removeItem('piskel.' + name);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.LocalStorageService.prototype.saveKeys_ = function(keys) {
|
|
|
|
window.localStorage.setItem('piskel.keys', JSON.stringify(keys));
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.LocalStorageService.prototype.removeFromKeys_ = function(name) {
|
|
|
|
var keys = this.getKeys();
|
|
|
|
var otherKeys = keys.filter(function (key) {
|
|
|
|
return key.name !== name;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.saveKeys_(otherKeys);
|
2012-09-16 14:59:19 +04:00
|
|
|
};
|
|
|
|
|
2014-02-10 04:00:16 +04:00
|
|
|
ns.LocalStorageService.prototype.getKey_ = function(name) {
|
|
|
|
var matches = this.getKeys().filter(function (key) {
|
|
|
|
return key.name === name;
|
|
|
|
});
|
|
|
|
if (matches.length > 0) {
|
|
|
|
return matches[0];
|
|
|
|
} else {
|
|
|
|
return null;
|
2012-09-16 14:59:19 +04:00
|
|
|
}
|
|
|
|
};
|
2014-02-10 04:00:16 +04:00
|
|
|
|
|
|
|
ns.LocalStorageService.prototype.addToKeys_ = function(name, description, date) {
|
|
|
|
var keys = this.getKeys();
|
|
|
|
keys.push({
|
|
|
|
name : name,
|
|
|
|
description : description,
|
|
|
|
date : date
|
|
|
|
});
|
|
|
|
this.saveKeys_(keys);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.LocalStorageService.prototype.getPiskel = function(name) {
|
|
|
|
return window.localStorage.getItem('piskel.' + name);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.LocalStorageService.prototype.getKeys = function(name) {
|
|
|
|
var keysString = window.localStorage.getItem('piskel.keys');
|
|
|
|
return JSON.parse(keysString) || [];
|
|
|
|
};
|
|
|
|
|
2012-09-16 14:59:19 +04:00
|
|
|
})();
|