8 lines
3.2 KiB
JavaScript
8 lines
3.2 KiB
JavaScript
var jaaulde=window.jaaulde||{};jaaulde.utils=jaaulde.utils||{};
|
|
jaaulde.utils.cookies=function(){var f,g,d,e,b={expiresAt:null,path:"/",domain:null,secure:!1};f=function(a){var c,h;"object"!==typeof a||null===a?c=b:(c={expiresAt:b.expiresAt,path:b.path,domain:b.domain,secure:b.secure},"object"===typeof a.expiresAt&&a.expiresAt instanceof Date?c.expiresAt=a.expiresAt:"number"===typeof a.hoursToLive&&0!==a.hoursToLive&&(h=new Date,h.setTime(h.getTime()+36E5*a.hoursToLive),c.expiresAt=h),"string"===typeof a.path&&""!==a.path&&(c.path=a.path),"string"===typeof a.domain&&
|
|
""!==a.domain&&(c.domain=a.domain),!0===a.secure&&(c.secure=a.secure));return c};g=function(a){a=f(a);return("object"===typeof a.expiresAt&&a.expiresAt instanceof Date?"; expires="+a.expiresAt.toGMTString():"")+"; path="+a.path+("string"===typeof a.domain?"; domain="+a.domain:"")+(!0===a.secure?"; secure":"")};d=function(){var a={},c,h,d,b,e=document.cookie.split(";"),f;for(c=0;c<e.length;c+=1){h=e[c].split("=");d=h[0].replace(/^\s*/,"").replace(/\s*$/,"");try{b=decodeURIComponent(h[1])}catch(g){b=
|
|
h[1]}if("object"===typeof JSON&&null!==JSON&&"function"===typeof JSON.parse)try{f=b,b=JSON.parse(b)}catch(g){b=f}a[d]=b}return a};e=function(){};e.prototype.get=function(a){var c,h,b=d();if("string"===typeof a)c="undefined"!==typeof b[a]?b[a]:null;else if("object"===typeof a&&null!==a)for(h in c={},a)c[a[h]]="undefined"!==typeof b[a[h]]?b[a[h]]:null;else c=b;return c};e.prototype.filter=function(a){var c,b={},e=d();"string"===typeof a&&(a=new RegExp(a));for(c in e)c.match(a)&&(b[c]=e[c]);return b};
|
|
e.prototype.set=function(a,c,b){if("object"!==typeof b||null===b)b={};if("undefined"===typeof c||null===c)c="",b.hoursToLive=-8760;else if("string"!==typeof c)if("object"===typeof JSON&&null!==JSON&&"function"===typeof JSON.stringify)c=JSON.stringify(c);else throw Error("cookies.set() received non-string value and could not serialize.");b=g(b);document.cookie=a+"="+encodeURIComponent(c)+b};e.prototype.del=function(a,c){var b={},d;if("object"!==typeof c||null===c)c={};"boolean"===typeof a&&!0===a?
|
|
b=this.get():"string"===typeof a&&(b[a]=!0);for(d in b)"string"===typeof d&&""!==d&&this.set(d,null,c)};e.prototype.test=function(){var a=!1;this.set("cT","data");"data"===this.get("cT")&&(this.del("cT"),a=!0);return a};e.prototype.setOptions=function(a){"object"!==typeof a&&(a=null);b=f(a)};return new e}();
|
|
(function(){window.jQuery&&function(f){f.cookies=jaaulde.utils.cookies;f.each({cookify:function(g){return this.each(function(){var d,e=["name","id"],b,a=f(this),c;for(d in e)if(!isNaN(d)&&(b=a.attr(e[d]),"string"===typeof b&&""!==b)){a.is(":checkbox, :radio")?a.attr("checked")&&(c=a.val()):c=a.is(":input")?a.val():a.html();if("string"!==typeof c||""===c)c=null;f.cookies.set(b,c,g);break}})},cookieFill:function(){return this.each(function(){var g,d,e=["name","id"],b,a=f(this);for(d=function(){g=e.pop();
|
|
return!!g};d();)if(b=a.attr(g),"string"===typeof b&&""!==b){d=f.cookies.get(b);null!==d&&(a.is(":checkbox, :radio")?a.val()===d?a.attr("checked","checked"):a.removeAttr("checked"):a.is(":input")?a.val(d):a.html(d));break}})},cookieBind:function(g){return this.each(function(){var d=f(this);d.cookieFill().change(function(){d.cookify(g)})})}},function(g){f.fn[g]=this})}(window.jQuery)})();
|