-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.min.js
1 lines (1 loc) · 4.35 KB
/
storage.min.js
1
(function(a){if(typeof XS==="undefined"){XS={}}String.prototype.hashCode=function(){var a=0;if(this.length==0)return a;for(i=0;i<this.length;i++){char=this.charCodeAt(i);a=(a<<5)-a+char;a=a&a}return a};window.domStorage={getItem:function(b){b=a.camelCase(b);return a("body").data(b)!==undefined?a("body").data(b):null},removeItem:function(b){b=a.camelCase(b);if(this.getItem(b)!==null){this.length--;a("body").removeData(b)}return undefined},setItem:function(b,c){b=a.camelCase(b);this.length++;a("body").data(b,c)},clear:function(){a.each(a("body").data(),function(b,c){a("body").removeData(b)});this.length=0;return undefined},length:0,key:function(b){var c=new Array;a.each(a("body").data(),function(a,b){c.push(a)});return c[b]!==undefined?c[b]:null}};XS.AbstractEngines={keyPrefix:null,log:function(a){if(window.console&&window.console.log){console.log(a)}},get:function(a){this._clean(a);var b=this._getEngine().getItem(a);try{b=JSON.parse(b)}catch(c){this.log("Maybe not json?")}return b},set:function(a,b,c){try{this._addTTLRecord(a,c);if(b instanceof Object){b=JSON.stringify(b)}return this._getEngine().setItem(a,b)}catch(d){this.log(d.toString());return false}},remove:function(a){this._removeTTLRecord(a);return this._getEngine().removeItem(a)},isAvailable:function(){return false},flush:function(){var b=this;var c=0;if(b.keyPrefix===null){return false}var d=a.camelCase(b.keyPrefix);var e=new RegExp("^(?:"+d+")");var f=b.getKeys();a.each(f,function(a,d){if(e.test(d)){b.remove(d);c++}});return c},_getEngine:function(){},_addTTLRecord:function(a,b){if(b===undefined){return}var c=this._getTTLRecord();c[a]=b*1e3+(new Date).getTime();this._saveTTL(c)},_removeTTLRecord:function(a){var b=this._getTTLRecord();if(b[a]!==undefined){delete b[a]}this._saveTTL(b)},_clean:function(a){var b=this._getTTLRecord();if(b[a]===undefined){return false}var c=(new Date).getTime();if(b[a]<c){this.remove(a);return true}return false},_saveTTL:function(b){var c=0;a.each(b,function(){c++});if(c>0){this._getEngine().setItem(this.keyPrefix+"ttl",JSON.stringify(b))}else{this._getEngine().removeItem(this.keyPrefix+"ttl")}},_getTTLRecord:function(){var a=this._getEngine().getItem(this.keyPrefix+"ttl")||null;if(a===null){return{}}return JSON.parse(a)},resetEngine:function(){this.keyPrefix=null},getKeys:function(){var a=new Array;for(var b=0;b<this._getEngine().length;b++){a.push(this._getEngine().key(b))}return a}};XS.Engines={localStorage:a.extend({},XS.AbstractEngines,{isAvailable:function(){return typeof window.localStorage!=="undefined"},_getEngine:function(){return window.localStorage}}),sessionStorage:a.extend({},XS.AbstractEngines,{isAvailable:function(){return typeof window.sessionStorage!=="undefined"},_getEngine:function(){return window.sessionStorage}}),domStorage:a.extend({},XS.AbstractEngines,{isAvailable:function(){return a.fn.data!==undefined},_getEngine:function(){return window.domStorage}})};XS.Storage=function(b){this._currentEngine=null;this.options=a.extend({},this.options,b);if(this.options.engines===null||this.options.engines===undefined){throw Error("Specify at least one storage engine")}if(this.options.keyPrefix===null){this.options.keyPrefix=Math.abs(window.location.hostname.hashCode()).toString()}};a.extend(XS.Storage.prototype,{options:{debug:true,keyPrefix:null,engines:"localStorage,sessionStorage,domStorage"},generateKey:function(a){return this.options.keyPrefix+a},get:function(a,b){return this._getStorage(b).get(this.generateKey(a))},set:function(a,b,c,d){return this._getStorage(d).set(this.generateKey(a),b,c)},remove:function(a,b){return this._getStorage(b).remove(this.generateKey(a))},resetStorage:function(){this._currentEngine=null},flush:function(a){return this._getStorage(a).flush()},log:function(a){if(window.console&&window.console.log&&this.options.debug==true){console.log(a)}},_getStorage:function(b){var c=this;var d=c.options.engines.split(",");if(b!==undefined&&XS.Engines[b]!==undefined&&XS.Engines[b].isAvailable()===true){c._currentEngine=a.trim(b);var e=XS.Engines[b];e.resetEngine();e.keyPrefix=c.options.keyPrefix;return e}if(c._currentEngine!==null){return XS.Engines[c._currentEngine]}var e=null;a.each(d,function(b,d){if(XS.Engines[d]!==undefined&&XS.Engines[d].isAvailable()===true){c._currentEngine=a.trim(d);e=XS.Engines[c._currentEngine];e.resetEngine();c.log("Selecting "+c._currentEngine+" engine");return false}});e.keyPrefix=c.options.keyPrefix;return e}})})(jQuery)