PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/ajax/libs/jquery-storage-api/1.7.0/jquery.storageapi.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 412 lines | 331 code | 11 blank | 70 comment | 93 complexity | 8e0b38d1103eec8aed8042223e8e4750 MD5 | raw file
  1. /*
  2. * jQuery Storage API Plugin
  3. *
  4. * Copyright (c) 2013 Julien Maurel
  5. *
  6. * Licensed under the MIT license:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. *
  9. * Project home:
  10. * https://github.com/julien-maurel/jQuery-Storage-API
  11. *
  12. * Version: 1.7.0
  13. *
  14. */
  15. (function($){
  16. // Prefix to use with cookie fallback
  17. var cookie_local_prefix="ls_";
  18. var cookie_session_prefix="ss_";
  19. // Get items from a storage
  20. function _get(storage){
  21. var l=arguments.length,s=window[storage],a=arguments,a1=a[1],vi,ret,tmp;
  22. if(l<2) throw new Error('Minimum 2 arguments must be given');
  23. else if($.isArray(a1)){
  24. // If second argument is an array, return an object with value of storage for each item in this array
  25. ret={};
  26. for(var i in a1){
  27. vi=a1[i];
  28. try{
  29. ret[vi]=JSON.parse(s.getItem(vi));
  30. }catch(e){
  31. ret[vi]=s.getItem(vi);
  32. }
  33. }
  34. return ret;
  35. }else if(l==2){
  36. // If only 2 arguments, return value directly
  37. try{
  38. return JSON.parse(s.getItem(a1));
  39. }catch(e){
  40. return s.getItem(a1);
  41. }
  42. }else{
  43. // If more than 2 arguments, parse storage to retrieve final value to return it
  44. // Get first level
  45. try{
  46. ret=JSON.parse(s.getItem(a1));
  47. }catch(e){
  48. throw new ReferenceError(a1+' is not defined in this storage');
  49. }
  50. // Parse next levels
  51. for(var i=2;i<l-1;i++){
  52. ret=ret[a[i]];
  53. if(ret===undefined) throw new ReferenceError([].slice.call(a,1,i+1).join('.')+' is not defined in this storage');
  54. }
  55. // If last argument is an array, return an object with value for each item in this array
  56. // Else return value normally
  57. if($.isArray(a[i])){
  58. tmp=ret;
  59. ret={};
  60. for(var j in a[i]){
  61. ret[a[i][j]]=tmp[a[i][j]];
  62. }
  63. return ret;
  64. }else{
  65. return ret[a[i]];
  66. }
  67. }
  68. }
  69. // Set items of a storage
  70. function _set(storage){
  71. var l=arguments.length,s=window[storage],a=arguments,a1=a[1],a2=a[2],vi,to_store={},tmp;
  72. if(l<2 || !$.isPlainObject(a1) && l<3) throw new Error('Minimum 3 arguments must be given or second parameter must be an object');
  73. else if($.isPlainObject(a1)){
  74. // If first argument is an object, set values of storage for each property of this object
  75. for(var i in a1){
  76. vi=a1[i];
  77. if(!$.isPlainObject(vi)) s.setItem(i,vi);
  78. else s.setItem(i,JSON.stringify(vi));
  79. }
  80. return a1;
  81. }else if(l==3){
  82. // If only 3 arguments, set value of storage directly
  83. if(typeof a2==='object') s.setItem(a1,JSON.stringify(a2));
  84. else s.setItem(a1,a2);
  85. return a2;
  86. }else{
  87. // If more than 3 arguments, parse storage to retrieve final node and set value
  88. // Get first level
  89. try{
  90. tmp=s.getItem(a1);
  91. if(tmp!=null) {
  92. to_store=JSON.parse(tmp);
  93. }
  94. }catch(e){
  95. }
  96. tmp=to_store;
  97. // Parse next levels and set value
  98. for(var i=2;i<l-2;i++){
  99. vi=a[i];
  100. if(!tmp[vi] || !$.isPlainObject(tmp[vi])) tmp[vi]={};
  101. tmp=tmp[vi];
  102. }
  103. tmp[a[i]]=a[i+1];
  104. s.setItem(a1,JSON.stringify(to_store));
  105. return to_store;
  106. }
  107. }
  108. // Remove items from a storage
  109. function _remove(storage){
  110. var l=arguments.length,s=window[storage],a=arguments,a1=a[1],to_store,tmp;
  111. if(l<2) throw new Error('Minimum 2 arguments must be given');
  112. else if($.isArray(a1)){
  113. // If first argument is an array, remove values from storage for each item of this array
  114. for(var i in a1){
  115. s.removeItem(a1[i]);
  116. }
  117. return true;
  118. }else if(l==2){
  119. // If only 2 arguments, remove value from storage directly
  120. s.removeItem(a1);
  121. return true;
  122. }else{
  123. // If more than 2 arguments, parse storage to retrieve final node and remove value
  124. // Get first level
  125. try{
  126. to_store=tmp=JSON.parse(s.getItem(a1));
  127. }catch(e){
  128. throw new ReferenceError(a1+' is not defined in this storage');
  129. }
  130. // Parse next levels and remove value
  131. for(var i=2;i<l-1;i++){
  132. tmp=tmp[a[i]];
  133. if(tmp===undefined) throw new ReferenceError([].slice.call(a,1,i).join('.')+' is not defined in this storage');
  134. }
  135. // If last argument is an array,remove value for each item in this array
  136. // Else remove value normally
  137. if($.isArray(a[i])){
  138. for(var j in a[i]){
  139. delete tmp[a[i][j]];
  140. }
  141. }else{
  142. delete tmp[a[i]];
  143. }
  144. s.setItem(a1,JSON.stringify(to_store));
  145. return true;
  146. }
  147. }
  148. // Remove all items from a storage
  149. function _removeAll(storage, reinit_ns){
  150. var keys=_keys(storage);
  151. for(var i in keys){
  152. _remove(storage,keys[i]);
  153. }
  154. // Reinitialize all namespace storages
  155. if(reinit_ns){
  156. for(var i in $.namespaceStorages){
  157. _createNamespace(i);
  158. }
  159. }
  160. }
  161. // Check if items of a storage are empty
  162. function _isEmpty(storage){
  163. var l=arguments.length,a=arguments,s=window[storage],a1=a[1];
  164. if(l==1){
  165. // If only one argument, test if storage is empty
  166. return (_keys(storage).length==0);
  167. }else if($.isArray(a1)){
  168. // If first argument is an array, test each item of this array and return true only if all items are empty
  169. for(var i=0; i<a1.length;i++){
  170. if(!_isEmpty(storage,a1[i])) return false;
  171. }
  172. return true;
  173. }else{
  174. // If more than 1 argument, try to get value and test it
  175. try{
  176. var v=_get.apply(this, arguments);
  177. // Convert result to an object (if last argument is an array, _get return already an object) and test each item
  178. if(!$.isArray(a[l-1])) v={'totest':v};
  179. for(var i in v){
  180. if(!(
  181. ($.isPlainObject(v[i]) && $.isEmptyObject(v[i])) ||
  182. ($.isArray(v[i]) && !v[i].length) ||
  183. (!v[i])
  184. )) return false;
  185. }
  186. return true;
  187. }catch(e){
  188. return true;
  189. }
  190. }
  191. }
  192. // Check if items of a storage exist
  193. function _isSet(storage){
  194. var l=arguments.length,a=arguments,s=window[storage],a1=a[1];
  195. if(l<2) throw new Error('Minimum 2 arguments must be given');
  196. if($.isArray(a1)){
  197. // If first argument is an array, test each item of this array and return true only if all items exist
  198. for(var i=0; i<a1.length;i++){
  199. if(!_isSet(storage,a1[i])) return false;
  200. }
  201. return true;
  202. }else{
  203. // For other case, try to get value and test it
  204. try{
  205. var v=_get.apply(this, arguments);
  206. // Convert result to an object (if last argument is an array, _get return already an object) and test each item
  207. if(!$.isArray(a[l-1])) v={'totest':v};
  208. for(var i in v){
  209. if(!(v[i]!==undefined && v[i]!==null)) return false;
  210. }
  211. return true;
  212. }catch(e){
  213. return false;
  214. }
  215. }
  216. }
  217. // Get keys of a storage or of an item of the storage
  218. function _keys(storage){
  219. var l=arguments.length,s=window[storage],a=arguments,a1=a[1],keys=[],o={};
  220. // If more than 1 argument, get value from storage to retrieve keys
  221. // Else, use storage to retrieve keys
  222. if(l>1){
  223. o=_get.apply(this,a);
  224. }else{
  225. o=s;
  226. }
  227. if(o._cookie){
  228. // If storage is a cookie, use $.cookie to retrieve keys
  229. for(var key in $.cookie()){
  230. if(key!='') {
  231. keys.push(key.replace(o._prefix,''));
  232. }
  233. }
  234. }else{
  235. for(var i in o){
  236. keys.push(i);
  237. }
  238. }
  239. return keys;
  240. }
  241. // Create new namespace storage
  242. function _createNamespace(name){
  243. if(!name || typeof name!="string") throw new Error('First parameter must be a string');
  244. if(!window.localStorage.getItem(name)) window.localStorage.setItem(name,'{}');
  245. if(!window.sessionStorage.getItem(name)) window.sessionStorage.setItem(name,'{}');
  246. var ns={
  247. localStorage:$.extend({},$.localStorage,{_ns:name}),
  248. sessionStorage:$.extend({},$.sessionStorage,{_ns:name})
  249. };
  250. if($.cookie){
  251. if(!window.cookieStorage.getItem(name)) window.cookieStorage.setItem(name,'{}');
  252. ns.cookieStorage=$.extend({},$.cookieStorage,{_ns:name});
  253. }
  254. $.namespaceStorages[name]=ns;
  255. return ns;
  256. }
  257. // Namespace object
  258. var storage={
  259. _type:'',
  260. _ns:'',
  261. _callMethod:function(f,a){
  262. var p=[this._type],a=Array.prototype.slice.call(a),a0=a[0];
  263. if(this._ns) p.push(this._ns);
  264. if(typeof a0==='string' && a0.indexOf('.')!==-1){
  265. a.shift();
  266. [].unshift.apply(a,a0.split('.'));
  267. }
  268. [].push.apply(p,a);
  269. return f.apply(this,p);
  270. },
  271. // Get items. If no parameters and storage have a namespace, return all namespace
  272. get:function(){
  273. return this._callMethod(_get,arguments);
  274. },
  275. // Set items
  276. set:function(){
  277. var l=arguments.length,a=arguments,a0=a[0];
  278. if(l<1 || !$.isPlainObject(a0) && l<2) throw new Error('Minimum 2 arguments must be given or first parameter must be an object');
  279. // If first argument is an object and storage is a namespace storage, set values individually
  280. if($.isPlainObject(a0) && this._ns){
  281. for(var i in a0){
  282. _set(this._type,this._ns,i,a0[i]);
  283. }
  284. return a0;
  285. }else{
  286. r=this._callMethod(_set,a);
  287. if(this._ns) return r[a0.split('.')[0]];
  288. else return r;
  289. }
  290. },
  291. // Delete items
  292. remove:function(){
  293. if(arguments.length<1) throw new Error('Minimum 1 argument must be given');
  294. return this._callMethod(_remove,arguments);
  295. },
  296. // Delete all items
  297. removeAll:function(reinit_ns){
  298. if(this._ns){
  299. _set(this._type,this._ns,{});
  300. return true;
  301. }else{
  302. return _removeAll(this._type, reinit_ns);
  303. }
  304. },
  305. // Items empty
  306. isEmpty:function(){
  307. return this._callMethod(_isEmpty,arguments);
  308. },
  309. // Items exists
  310. isSet:function(){
  311. if(arguments.length<1) throw new Error('Minimum 1 argument must be given');
  312. return this._callMethod(_isSet,arguments);
  313. },
  314. // Get keys of items
  315. keys:function(){
  316. return this._callMethod(_keys,arguments);
  317. }
  318. };
  319. // Use jquery.cookie for compatibility with old browsers and give access to cookieStorage
  320. if($.cookie){
  321. // sessionStorage is valid for one window/tab. To simulate that with cookie, we set a name for the window and use it for the name of the cookie
  322. if(!window.name) window.name=Math.floor(Math.random()*100000000);
  323. var cookie_storage={
  324. _cookie:true,
  325. _prefix:'',
  326. _expires:null,
  327. _path:null,
  328. _domain:null,
  329. setItem:function(n,v){
  330. $.cookie(this._prefix+n,v,{expires:this._expires,path:this._path,domain:this._domain});
  331. },
  332. getItem:function(n){
  333. return $.cookie(this._prefix+n);
  334. },
  335. removeItem:function(n){
  336. return $.removeCookie(this._prefix+n);
  337. },
  338. clear:function(){
  339. for(var key in $.cookie()){
  340. if(key!=''){
  341. if(!this._prefix && key.indexOf(cookie_local_prefix)===-1 && key.indexOf(cookie_session_prefix)===-1 || this._prefix && key.indexOf(this._prefix)===0) {
  342. $.removeCookie(key);
  343. }
  344. }
  345. }
  346. },
  347. setExpires:function(e){
  348. this._expires=e;
  349. return this;
  350. },
  351. setPath:function(p){
  352. this._path=p;
  353. return this;
  354. },
  355. setDomain:function(d){
  356. this._domain=d;
  357. return this;
  358. },
  359. setConf:function(c){
  360. if(c.path) this._path=c.path;
  361. if(c.domain) this._domain=c.domain;
  362. if(c.expires) this._expires=c.expires;
  363. return this;
  364. },
  365. setDefaultConf:function(){
  366. this._path=this._domain=this._expires=null;
  367. }
  368. };
  369. if(!window.localStorage){
  370. window.localStorage=$.extend({},cookie_storage,{_prefix:cookie_local_prefix,_expires:365*10});
  371. window.sessionStorage=$.extend({},cookie_storage,{_prefix:cookie_session_prefix+window.name+'_'});
  372. }
  373. window.cookieStorage=$.extend({},cookie_storage);
  374. // cookieStorage API
  375. $.cookieStorage=$.extend({},storage,{
  376. _type:'cookieStorage',
  377. setExpires:function(e){window.cookieStorage.setExpires(e); return this;},
  378. setPath:function(p){window.cookieStorage.setPath(p); return this;},
  379. setDomain:function(d){window.cookieStorage.setDomain(d); return this;},
  380. setConf:function(c){window.cookieStorage.setConf(c); return this;},
  381. setDefaultConf:function(){window.cookieStorage.setDefaultConf(); return this;}
  382. });
  383. }
  384. // Get a new API on a namespace
  385. $.initNamespaceStorage=function(ns){ return _createNamespace(ns); };
  386. // localStorage API
  387. $.localStorage=$.extend({},storage,{_type:'localStorage'});
  388. // sessionStorage API
  389. $.sessionStorage=$.extend({},storage,{_type:'sessionStorage'});
  390. // List of all namespace storage
  391. $.namespaceStorages={};
  392. // Remove all items in all storages
  393. $.removeAllStorages=function(reinit_ns){
  394. $.localStorage.removeAll(reinit_ns);
  395. $.sessionStorage.removeAll(reinit_ns);
  396. if($.cookieStorage) $.cookieStorage.removeAll(reinit_ns);
  397. if(!reinit_ns){
  398. $.namespaceStorages={};
  399. }
  400. }
  401. })(jQuery);