| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | (function (factory) { |
| | if(typeof define==='function' && define.amd){ |
| | |
| | define(['jquery'],factory); |
| | }else if(typeof exports==='object') { |
| | |
| | factory(require('jquery')); |
| | }else { |
| | |
| | factory(jQuery); |
| | } |
| | }(function($){ |
| | |
| | var cookie_local_prefix="ls_"; |
| | var cookie_session_prefix="ss_"; |
| |
|
| | |
| | function _get(storage){ |
| | var l=arguments.length,s=window[storage],a=arguments,a1=a[1],vi,ret,tmp; |
| | if(l<2) throw new Error('Minimum 2 arguments must be given'); |
| | else if($.isArray(a1)){ |
| | |
| | ret={}; |
| | for(var i in a1){ |
| | vi=a1[i]; |
| | try{ |
| | ret[vi]=JSON.parse(s.getItem(vi)); |
| | }catch(e){ |
| | ret[vi]=s.getItem(vi); |
| | } |
| | } |
| | return ret; |
| | }else if(l==2){ |
| | |
| | try{ |
| | return JSON.parse(s.getItem(a1)); |
| | }catch(e){ |
| | return s.getItem(a1); |
| | } |
| | }else{ |
| | |
| | |
| | try{ |
| | ret=JSON.parse(s.getItem(a1)); |
| | }catch(e){ |
| | throw new ReferenceError(a1+' is not defined in this storage'); |
| | } |
| | |
| | for(var i=2;i<l-1;i++){ |
| | ret=ret[a[i]]; |
| | if(ret===undefined) throw new ReferenceError([].slice.call(a,1,i+1).join('.')+' is not defined in this storage'); |
| | } |
| | |
| | |
| | if($.isArray(a[i])){ |
| | tmp=ret; |
| | ret={}; |
| | for(var j in a[i]){ |
| | ret[a[i][j]]=tmp[a[i][j]]; |
| | } |
| | return ret; |
| | }else{ |
| | return ret[a[i]]; |
| | } |
| | } |
| | } |
| |
|
| | |
| | function _set(storage){ |
| | var l=arguments.length,s=window[storage],a=arguments,a1=a[1],a2=a[2],vi,to_store={},tmp; |
| | if(l<2 || !$.isPlainObject(a1) && l<3) throw new Error('Minimum 3 arguments must be given or second parameter must be an object'); |
| | else if($.isPlainObject(a1)){ |
| | |
| | for(var i in a1){ |
| | vi=a1[i]; |
| | if(!$.isPlainObject(vi)) s.setItem(i,vi); |
| | else s.setItem(i,JSON.stringify(vi)); |
| | } |
| | return a1; |
| | }else if(l==3){ |
| | |
| | if(typeof a2==='object') s.setItem(a1,JSON.stringify(a2)); |
| | else s.setItem(a1,a2); |
| | return a2; |
| | }else{ |
| | |
| | |
| | try{ |
| | tmp=s.getItem(a1); |
| | if(tmp!=null) { |
| | to_store=JSON.parse(tmp); |
| | } |
| | }catch(e){ |
| | } |
| | tmp=to_store; |
| | |
| | for(var i=2;i<l-2;i++){ |
| | vi=a[i]; |
| | if(!tmp[vi] || !$.isPlainObject(tmp[vi])) tmp[vi]={}; |
| | tmp=tmp[vi]; |
| | } |
| | tmp[a[i]]=a[i+1]; |
| | s.setItem(a1,JSON.stringify(to_store)); |
| | return to_store; |
| | } |
| | } |
| |
|
| | |
| | function _remove(storage){ |
| | var l=arguments.length,s=window[storage],a=arguments,a1=a[1],to_store,tmp; |
| | if(l<2) throw new Error('Minimum 2 arguments must be given'); |
| | else if($.isArray(a1)){ |
| | |
| | for(var i in a1){ |
| | s.removeItem(a1[i]); |
| | } |
| | return true; |
| | }else if(l==2){ |
| | |
| | s.removeItem(a1); |
| | return true; |
| | }else{ |
| | |
| | |
| | try{ |
| | to_store=tmp=JSON.parse(s.getItem(a1)); |
| | }catch(e){ |
| | throw new ReferenceError(a1+' is not defined in this storage'); |
| | } |
| | |
| | for(var i=2;i<l-1;i++){ |
| | tmp=tmp[a[i]]; |
| | if(tmp===undefined) throw new ReferenceError([].slice.call(a,1,i).join('.')+' is not defined in this storage'); |
| | } |
| | |
| | |
| | if($.isArray(a[i])){ |
| | for(var j in a[i]){ |
| | delete tmp[a[i][j]]; |
| | } |
| | }else{ |
| | delete tmp[a[i]]; |
| | } |
| | s.setItem(a1,JSON.stringify(to_store)); |
| | return true; |
| | } |
| | } |
| |
|
| | |
| | function _removeAll(storage, reinit_ns){ |
| | var keys=_keys(storage); |
| | for(var i in keys){ |
| | _remove(storage,keys[i]); |
| | } |
| | |
| | if(reinit_ns){ |
| | for(var i in $.namespaceStorages){ |
| | _createNamespace(i); |
| | } |
| | } |
| | } |
| |
|
| | |
| | function _isEmpty(storage){ |
| | var l=arguments.length,a=arguments,s=window[storage],a1=a[1]; |
| | if(l==1){ |
| | |
| | return (_keys(storage).length==0); |
| | }else if($.isArray(a1)){ |
| | |
| | for(var i=0; i<a1.length;i++){ |
| | if(!_isEmpty(storage,a1[i])) return false; |
| | } |
| | return true; |
| | }else{ |
| | |
| | try{ |
| | var v=_get.apply(this, arguments); |
| | |
| | if(!$.isArray(a[l-1])) v={'totest':v}; |
| | for(var i in v){ |
| | if(!( |
| | ($.isPlainObject(v[i]) && $.isEmptyObject(v[i])) || |
| | ($.isArray(v[i]) && !v[i].length) || |
| | (!v[i]) |
| | )) return false; |
| | } |
| | return true; |
| | }catch(e){ |
| | return true; |
| | } |
| | } |
| | } |
| |
|
| | |
| | function _isSet(storage){ |
| | var l=arguments.length,a=arguments,s=window[storage],a1=a[1]; |
| | if(l<2) throw new Error('Minimum 2 arguments must be given'); |
| | if($.isArray(a1)){ |
| | |
| | for(var i=0; i<a1.length;i++){ |
| | if(!_isSet(storage,a1[i])) return false; |
| | } |
| | return true; |
| | }else{ |
| | |
| | try{ |
| | var v=_get.apply(this, arguments); |
| | |
| | if(!$.isArray(a[l-1])) v={'totest':v}; |
| | for(var i in v){ |
| | if(!(v[i]!==undefined && v[i]!==null)) return false; |
| | } |
| | return true; |
| | }catch(e){ |
| | return false; |
| | } |
| | } |
| | } |
| |
|
| | |
| | function _keys(storage){ |
| | var l=arguments.length,s=window[storage],a=arguments,a1=a[1],keys=[],o={}; |
| | |
| | |
| | if(l>1){ |
| | o=_get.apply(this,a); |
| | }else{ |
| | o=s; |
| | } |
| | if(o._cookie){ |
| | |
| | for(var key in $.cookie()){ |
| | if(key!='') { |
| | keys.push(key.replace(o._prefix,'')); |
| | } |
| | } |
| | }else{ |
| | for(var i in o){ |
| | keys.push(i); |
| | } |
| | } |
| | return keys; |
| | } |
| |
|
| | |
| | function _createNamespace(name){ |
| | if(!name || typeof name!="string") throw new Error('First parameter must be a string'); |
| | if(storage_available){ |
| | if(!window.localStorage.getItem(name)) window.localStorage.setItem(name,'{}'); |
| | if(!window.sessionStorage.getItem(name)) window.sessionStorage.setItem(name,'{}'); |
| | }else{ |
| | if(!window.localCookieStorage.getItem(name)) window.localCookieStorage.setItem(name,'{}'); |
| | if(!window.sessionCookieStorage.getItem(name)) window.sessionCookieStorage.setItem(name,'{}'); |
| | } |
| | var ns={ |
| | localStorage:$.extend({},$.localStorage,{_ns:name}), |
| | sessionStorage:$.extend({},$.sessionStorage,{_ns:name}) |
| | }; |
| | if($.cookie){ |
| | if(!window.cookieStorage.getItem(name)) window.cookieStorage.setItem(name,'{}'); |
| | ns.cookieStorage=$.extend({},$.cookieStorage,{_ns:name}); |
| | } |
| | $.namespaceStorages[name]=ns; |
| | return ns; |
| | } |
| |
|
| | |
| | function _testStorage(name){ |
| | if(!window[name]) return false; |
| | var foo='jsapi'; |
| | try{ |
| | window[name].setItem(foo,foo); |
| | window[name].removeItem(foo); |
| | return true; |
| | }catch(e){ |
| | return false; |
| | } |
| | } |
| | |
| | |
| | var storage_available=_testStorage('localStorage'); |
| | |
| | |
| | var storage={ |
| | _type:'', |
| | _ns:'', |
| | _callMethod:function(f,a){ |
| | var p=[this._type],a=Array.prototype.slice.call(a),a0=a[0]; |
| | if(this._ns) p.push(this._ns); |
| | if(typeof a0==='string' && a0.indexOf('.')!==-1){ |
| | a.shift(); |
| | [].unshift.apply(a,a0.split('.')); |
| | } |
| | [].push.apply(p,a); |
| | return f.apply(this,p); |
| | }, |
| | |
| | get:function(){ |
| | return this._callMethod(_get,arguments); |
| | }, |
| | |
| | set:function(){ |
| | var l=arguments.length,a=arguments,a0=a[0]; |
| | if(l<1 || !$.isPlainObject(a0) && l<2) throw new Error('Minimum 2 arguments must be given or first parameter must be an object'); |
| | |
| | if($.isPlainObject(a0) && this._ns){ |
| | for(var i in a0){ |
| | _set(this._type,this._ns,i,a0[i]); |
| | } |
| | return a0; |
| | }else{ |
| | var r=this._callMethod(_set,a); |
| | if(this._ns) return r[a0.split('.')[0]]; |
| | else return r; |
| | } |
| | }, |
| | |
| | remove:function(){ |
| | if(arguments.length<1) throw new Error('Minimum 1 argument must be given'); |
| | return this._callMethod(_remove,arguments); |
| | }, |
| | |
| | removeAll:function(reinit_ns){ |
| | if(this._ns){ |
| | _set(this._type,this._ns,{}); |
| | return true; |
| | }else{ |
| | return _removeAll(this._type, reinit_ns); |
| | } |
| | }, |
| | |
| | isEmpty:function(){ |
| | return this._callMethod(_isEmpty,arguments); |
| | }, |
| | |
| | isSet:function(){ |
| | if(arguments.length<1) throw new Error('Minimum 1 argument must be given'); |
| | return this._callMethod(_isSet,arguments); |
| | }, |
| | |
| | keys:function(){ |
| | return this._callMethod(_keys,arguments); |
| | } |
| | }; |
| |
|
| | |
| | if($.cookie){ |
| | |
| | if(!window.name) window.name=Math.floor(Math.random()*100000000); |
| | var cookie_storage={ |
| | _cookie:true, |
| | _prefix:'', |
| | _expires:null, |
| | _path:null, |
| | _domain:null, |
| | setItem:function(n,v){ |
| | $.cookie(this._prefix+n,v,{expires:this._expires,path:this._path,domain:this._domain}); |
| | }, |
| | getItem:function(n){ |
| | return $.cookie(this._prefix+n); |
| | }, |
| | removeItem:function(n){ |
| | return $.removeCookie(this._prefix+n); |
| | }, |
| | clear:function(){ |
| | for(var key in $.cookie()){ |
| | if(key!=''){ |
| | if(!this._prefix && key.indexOf(cookie_local_prefix)===-1 && key.indexOf(cookie_session_prefix)===-1 || this._prefix && key.indexOf(this._prefix)===0) { |
| | $.removeCookie(key); |
| | } |
| | } |
| | } |
| | }, |
| | setExpires:function(e){ |
| | this._expires=e; |
| | return this; |
| | }, |
| | setPath:function(p){ |
| | this._path=p; |
| | return this; |
| | }, |
| | setDomain:function(d){ |
| | this._domain=d; |
| | return this; |
| | }, |
| | setConf:function(c){ |
| | if(c.path) this._path=c.path; |
| | if(c.domain) this._domain=c.domain; |
| | if(c.expires) this._expires=c.expires; |
| | return this; |
| | }, |
| | setDefaultConf:function(){ |
| | this._path=this._domain=this._expires=null; |
| | } |
| | }; |
| | if(!storage_available){ |
| | window.localCookieStorage=$.extend({},cookie_storage,{_prefix:cookie_local_prefix,_expires:365*10}); |
| | window.sessionCookieStorage=$.extend({},cookie_storage,{_prefix:cookie_session_prefix+window.name+'_'}); |
| | } |
| | window.cookieStorage=$.extend({},cookie_storage); |
| | |
| | $.cookieStorage=$.extend({},storage,{ |
| | _type:'cookieStorage', |
| | setExpires:function(e){window.cookieStorage.setExpires(e); return this;}, |
| | setPath:function(p){window.cookieStorage.setPath(p); return this;}, |
| | setDomain:function(d){window.cookieStorage.setDomain(d); return this;}, |
| | setConf:function(c){window.cookieStorage.setConf(c); return this;}, |
| | setDefaultConf:function(){window.cookieStorage.setDefaultConf(); return this;} |
| | }); |
| | } |
| |
|
| | |
| | $.initNamespaceStorage=function(ns){ return _createNamespace(ns); }; |
| | if(storage_available) { |
| | |
| | $.localStorage=$.extend({},storage,{_type:'localStorage'}); |
| | |
| | $.sessionStorage=$.extend({},storage,{_type:'sessionStorage'}); |
| | }else{ |
| | |
| | $.localStorage=$.extend({},storage,{_type:'localCookieStorage'}); |
| | |
| | $.sessionStorage=$.extend({},storage,{_type:'sessionCookieStorage'}); |
| | } |
| | |
| | $.namespaceStorages={}; |
| | |
| | $.removeAllStorages=function(reinit_ns){ |
| | $.localStorage.removeAll(reinit_ns); |
| | $.sessionStorage.removeAll(reinit_ns); |
| | if($.cookieStorage) $.cookieStorage.removeAll(reinit_ns); |
| | if(!reinit_ns){ |
| | $.namespaceStorages={}; |
| | } |
| | } |
| | })); |
| |
|