NxTIC/js/lib/local-storage-interface-min.js

6 lines
1.8 KiB
JavaScript

function localStorageInterface(){}
localStorageInterface.prototype={dataset:[],dataset_keys:[],storage:localStorage,setStorage:function(a){if(a!=localStorage&&a!=sessionStorage)return!1;this.storage=a},set:function(a,b,d){b=b.toString();var c=this.dataset.indexOf(a);if(-1==c)return!1;-1==this.dataset_keys[c].indexOf(b)&&(this.dataset_keys[c].push(b),this.storage.setItem(a,JSON.stringify(this.dataset_keys[c])));this.storage.setItem(a+"_"+b,JSON.stringify(d));return!0},get:function(a,b){if(null!=b)return b=b.toString(),-1==this.dataset.indexOf(a)?
!1:JSON.parse(this.storage.getItem(a+"_"+b))},del:function(a,b){b=b.toString();var d=this.dataset.indexOf(a);if(-1==d)return!1;var c=this.dataset_keys[d].indexOf(b);if(-1==c)return!1;this.storage.removeItem(a+"_"+b);this.dataset_keys[d].splice(c,1);this.storage.setItem(a,JSON.stringify(this.dataset_keys[d]));return!0},createDataset:function(a){if("string"!=typeof a)return!1;if(-1<this.dataset.indexOf(a))return!0;var b=this.storage.getItem(a);null!=b?(this.dataset.push(a),this.dataset_keys.push(JSON.parse(b))):
(this.dataset.push(a),this.dataset_keys.push([]),this.storage.setItem(a,JSON.stringify([])));return!0},keys:function(a){a=this.dataset.indexOf(a);return-1==a?!1:this.dataset_keys[a]},"export":function(a){var b=this.dataset.indexOf(a);if(-1==b)return!1;for(var d={},c=0;c<this.dataset_keys[b].length;c++)d[this.dataset_keys[b][c]]=this.get(a,this.dataset_keys[b][c]);return d},"import":function(a,b){-1==this.dataset.indexOf(a)?this.createDataset(a):this.clear(a);for(var d in b)this.set(a,d,b[d]);return!0},
clear:function(a){var b=this.dataset.indexOf(a);if(-1==b)return!1;var d=this["export"](a),c;for(c in d)this.del(a,c);this.dataset_keys[b]=[];this.storage.setItem(a,JSON.stringify(this.dataset_keys[b]));return!0}};var lsi=new localStorageInterface;