Icontem

File: jquery.handleStorage.min.js

Recommend this page to a friend!
  Classes of Jason Gerfen  >  jQuery.handleStorage  >  jquery.handleStorage.min.js  >  Download  
File: jquery.handleStorage.min.js
Role: Class source
Content type: text/plain
Description: Class source
Class: jQuery.handleStorage
Save forms in cookies, local or session storage
Author: By
Last change: Fri Apr 20 17:26:27 MDT 2012 - Added support for quota checks prior to all saves. Uses 5mb for HTML5 storage mechanisms and 4k for depreciated cookie support
Fri Apr 6 04:53:39 MDT 2012 - Migrated changes to minified version
Sat Feb 18 01:24:31 MST 2012 - Replicated new key handling to minified version
Thu Feb 16 20:16:28 MST 2012 - Added pre, post and error callback methods
Date: 7 years ago
Size: 4,882 bytes
 

Contents

Class file image Download
/* https://www.github.com/jas-/jQuery.handleStorage */
(function(a){a.fn.handleStorage=function(e){var m={appID:"jQuery.handleStorage",storage:"localStorage",interval:5000,aes:false,uuid:"",form:"",data:{},callback:function(){},preCallback:function(){},errCallback:function(){}};var y={init:function(A){var z=a.extend({},m,A);z.form=a(this).attr("id");if(o(z)){z.data[z.appID]=(f(z))?f(z):{};var B=d(z);if((typeof B==="object")&&(j(B)>0)){v(z,B)}((z.preCallback)&&(a.isFunction(z.preCallback)))?z.preCallback(a(this)):false;a("#"+z.form).delegate("input, input:radio:selected, input:checkbox:checked, textarea","change keyup blur submit",function(){h(z)});setInterval(function(){h(z)},z.interval);return true}else{return false}}};var j=function(z){var A=0;a.each(z,function(C,B){if(z.hasOwnProperty(C)){A++}});return A};var b=function(z){var A=(z=="localStorage"||z=="sessionStorage")?1024*1025*5:1024*4;if(A-unescape(encodeURIComponent(JSON.stringify(z))).length<0){console.log("It seems the maximum quota has been met using "+z);return false}return true};var s=function(C,B,A){var z=false;C=(c(C))?C:"cookie";if(b(C)){switch(C){case"localStorage":z=q(B,A);break;case"sessionStorage":z=k(B,A);break;case"cookie":z=w(B,A);break;default:z=q(B,A);break}}return z};var p=function(B,A){var z=false;B=(c(B))?B:"cookie";switch(B){case"localStorage":z=l(A);break;case"sessionStorage":z=i(A);break;case"cookie":z=u(A);break;default:z=l(A);break}return z};var q=function(A,z){return(localStorage.setItem(A,z))?false:true};var k=function(A,z){return(sessionStorage.setItem(A,z))?false:true};var w=function(A,z){if(typeof a.cookie==="function"){return(a.cookie(A,z,{expires:7}))?true:false}else{return false}};var l=function(z){return(localStorage.getItem(z))?localStorage.getItem(z):false};var i=function(z){return(sessionStorage.getItem(z))?sessionStorage.getItem(z):false};var u=function(z){if(typeof a.cookie==="function"){return(a.cookie(z))?a.cookie(z):false}else{return false}};var f=function(z){return(p(z.storage,z.appID))?JSON.parse(p(z.storage,z.appID)):false};var d=function(B){var A={},z;if(typeof B.data[B.appID][B.form]==="object"){a.each(a("#"+B.form+" > :input"),function(D,C){if((r(C.name)!==false)&&(r(B.data[B.appID][B.form][C.name])!==false)){A[C.name]=((B.aes)&&(B.data[B.appID][B.form]["uuid"])&&(z!==false))?GibberishAES.dec(B.data[B.appID][B.form][C.name],g(B.data[B.appID][B.form]["uuid"])):B.data[B.appID][B.form][C.name]}})}return A};var v=function(A,z){if(j(z)>0){a.each(z,function(C,B){if((a("#"+A.form+" > input[name="+C+"]").attr("name")===C)||(a("#"+A.form+" > select[name="+C+"]").attr("name")===C)||(a("#"+A.form+" > textarea[name="+C+"]").attr("name")===C)&&(r(B)!==false)){a("#"+A.form+" > input[name="+C+"], #"+A.form+" > select[name="+C+"], #"+A.form+" > textarea[name="+C+"]").val(B)}})}};var h=function(A){var z={};z[A.form]={};z[A.form]["uuid"]=((A.aes)&&(!A.uuid))?x(A):A.uuid;a.each(a("#"+A.form+" > :input"),function(C,B){if((r(B.value)!==false)&&(r(B.name)!==false)){z[A.form][B.name]=((A.aes)&&(z[A.form]["uuid"]))?GibberishAES.enc(B.value,g(z[A.form]["uuid"])):B.value}});A.data[A.appID]=(j(A.data[A.appID])>0)?a.extend({},A.data[A.appID],z):z;if(s(A.storage,A.appID,JSON.stringify(A.data[A.appID]))){((A.callback)&&(a.isFunction(A.callback)))?A.callback.call(a(this)):false}else{((A.errCallback)&&(a.isFunction(A.errCallback)))?A.errCallback.call(a(this)):false}};var r=function(z){if(z){return((z===false)||(z.length===0)||(!z)||(z===null)||(z==="")||(typeof z==="undefined"))?false:true}else{return false}};var c=function(z){try{return((z in window)&&(window[z]))?true:false}catch(A){return false}};var o=function(A){var z=true;if(A.aes){if(!a.isFunction(GibberishAES.enc)){console.log("AES use specified but required libraries not available.Please include the Gibberish-AES libs...");z=false}}if(A.storage==="cookie"){if(!a.isFunction(a.cookie)){console.log("Cookie use specified but required libraries not available.Please include the jQuery cookie plugin...");z=false}}return z};var t=function(z){var E="0123456789abcdef".split("");var C=[],B=Math.random,D;C[8]=C[13]=C[18]=C[23]="-";C[14]="4";for(var A=0;A<36;A++){if(!C[A]){D=0|B()*16;C[A]=E[(A==19)?(D&3)|8:D&15]}}return(z!==null)?C.join("").replace(/-/g,"").split("",z).join(""):C.join("")};var x=function(A){if(A.aes){var z=(f(A))?f(A):{};z[A.form]=(z[A.form])?z[A.form]:{};var B=(r(z[A.form]["uuid"]))?z[A.form]["uuid"]:t(null);return B}return false};var g=function(z){return(z)?encodeURI(z.replace(/-/gi,"").substring(16,Math.ceil(16*z.length)%z.length)):false};var n=function(z){a.each(z,function(A,B){if(typeof B==="object"){n(B)}else{console.log(A+" => "+B)}})};if(y[e]){return y[e].apply(this,Array.prototype.slice.call(arguments,1))}else{if((typeof e==="object")||(!e)){return y.init.apply(this,arguments)}else{a.error("Method "+e+" does not exist on "+opts.name)}}}})(jQuery);