/**
* Storage Utility
*
* Used for File Management
*/
var Storage = function() {};
Storage.prototype = {
/**
* Initialise
*
* Run when the Utility is created
*/
init: function UtilStorageInit() {
if (typeof localStorage !== 'undefined') {
this._localStorageAvailable = true;
} else {
this._localStorageAvailable = false;
}
this.configureStorage();
},
/**
* Configure Storage
*
* If Local Storage is available then use Cookies
*
* @returns {Storage}
*/
configureStorage: function Storage() {
if (this._localStorageAvailable) {
// as we have local storage create a storage object with required functions
this.save = function StorageSave(id, data) {
localStorage[id] = JSON.stringify(data);
};
this.load = function StorageLoad(id) {
var data = localStorage[id];
if (data === null || !data)
return null;
return JSON.parse(data);
};
this.clear = function StorageClear(id) {
try {
localStorage[id] = null;
} catch(e) {
alert(e);
}
};
} else {
// no local storage so use the old cookie approach
this.log('localStorage NOT supported', 'warning');
this.save = function StorageSave(id, data) {
document.cookie = (id) + "=" + encodeURIComponent(JSON.stringify(data));
};
this.load = function StorageLoad(id) {
var s = "; " + document.cookie + ";", p = s.indexOf("; " + id + "=");
if (p < 0)
return "";
p = p + id.length + 3;
var p2 = s.indexOf(";", p + 1);
return JSON.parse(decodeURIComponent(s.substring(p, p2)));
};
this.clear = function StorageClear(id) {
// TODO: This
};
}
try {
delete this._localStorageAvailable;
delete this.init;
delete this.configureStorage;
delete this.__proto__.init;
delete this.__proto__.configureStorage;
} catch(e) {};
}
};