forked from guillaumepotier/Garlic.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarlic.min.js
2 lines (2 loc) · 3.35 KB
/
garlic.min.js
1
2
/* version 0.0.10 http://garlicjs.org */
!function(a){"use strict";var b=function(){this.defined="undefined"!=typeof localStorage};b.prototype={constructor:b,get:function(a,b){return localStorage.getItem(a)?localStorage.getItem(a):b!==void 0?b:null},has:function(a){return localStorage.getItem(a)?!0:!1},set:function(a,b,c){return"string"==typeof b&&""!==b&&localStorage.setItem(a,b),"function"==typeof c?c():!0},destroy:function(a,b){return localStorage.removeItem(a),"function"==typeof b?b():!0},clean:function(a){for(var b=localStorage.length-1;b>=0;b--)-1!==localStorage.key(b).indexOf("garlic:")&&localStorage.removeItem(localStorage.key(b));return"function"==typeof a?a():!0},clear:function(a){return localStorage.clear(),"function"==typeof a?a():!0}};var c=function(a,b,c){this.init("garlic",a,b,c)};c.prototype={constructor:c,init:function(b,c,d,e){this.type=b,this.$element=a(c),this.options=this.getOptions(e),this.storage=d,this.path=this.getPath(),this.retrieve(),this.$element.on(this.options.events.join("."+this.type+" "),!1,a.proxy(this.persist,this)),this.options.destroy&&this.$element.closest("form").on("submit reset",!1,a.proxy(this.destroy,this)),this.$element.addClass("garlic-auto-save")},getOptions:function(b){return b=a.extend({},a.fn[this.type].defaults,b,this.$element.data())},persist:function(){return this.$element.is("input[type=checkbox]")&&this.storage.has(this.path)?(this.destroy(),void 0):(this.storage.set(this.path,this.$element.val()),void 0)},retrieve:function(){if(this.storage.has(this.path)){if(this.$element.is("input[type=radio], input[type=checkbox]"))return this.storage.get(this.path)===this.$element.val()&&this.$element.attr("checked","checked"),void 0;this.$element.val(this.storage.get(this.path))}},destroy:function(){this.$element.is("input[type=radio], input[type=checkbox]")&&this.$element.attr("checked",!1),this.storage.destroy(this.path)},remove:function(){this.remove(),this.$element.val("")},getPath:function(){if(this.$element.length!=1)return!1;var b="",c=this.$element.is("input[type=checkbox]"),d=this.$element;while(d.length){var e=d[0],f=e.localName;if(!f)break;f=f.toLowerCase();var g=d.parent(),h=g.children(f);if(a(e).is("form, input, select, textarea")||c){if(f+=a(e).attr("name")!==void 0?"."+a(e).attr("name"):"",h.length>1&&!a(e).is("input[type=radio]")&&(f+=":eq("+h.index(e)+")"),b=f+(b?">"+b:""),"form"==e.localName)break;d=g}else d=g}return"garlic:"+document.domain+(this.options.domain?"*":window.location.pathname)+">"+b},getStorage:function(){return this.storage}},a.fn.garlic=function(d){function h(b){var g=a(b),h=g.data("garlic"),i=a.extend(e,g.data());if(i.storage===void 0||i.storage)return h||g.data("garlic",h=new c(b,f,i)),"string"==typeof d&&"function"==typeof h[d]?h[d]():void 0}var e=a.extend({},a.fn.garlic.defaults,d,this.data()),f=new b,g=!1;return f.defined?(e.debug&&"undefined"==typeof garlicStorage&&(window.garlicStorage=f),this.each(function(){a(this).is("form")?a(this).find(e.inputs).each(function(){g=h(a(this))}):a(this).is(e.inputs)&&(g=h(a(this)))}),g):!1},a.fn.garlic.Constructor=c,a.fn.garlic.defaults={debug:!0,inputs:"input[type=text], input[type=radio], input[type=checkbox], textarea, select",events:["DOMAttrModified","textInput","input","change","keypress","paste","focus"],destroy:!0,domain:!1},a(window).on("load",function(){a('[data-persist="garlic"]').each(function(){a(this).garlic()})})}(window.jQuery||Zepto)