-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
jquery.phoenix.min.js
1 lines (1 loc) · 3.41 KB
/
jquery.phoenix.min.js
1
!function(t,e){"use strict";var n,i,s,r,o;s="phoenix",i={namespace:"phoenixStorage",webStorage:"localStorage",maxItems:100,saveInterval:1e3,clearOnSubmit:!1,saveOnChange:!1,keyAttributes:["tagName","id","name"]},r=[],n=function(){function n(n,r){var o,a;this.element=n,this._defaults=i,this._name=s,this.$element=t(this.element),this.options=t.extend({},i,"object"==typeof r?r:void 0),"string"==typeof r&&(this.action=r),this.uri=e.location.host+e.location.pathname,a=[this.options.namespace,this.uri].concat(function(){var t,e,n,i;for(n=this.options.keyAttributes,i=[],t=0,e=n.length;e>t;t++)o=n[t],i.push(this.element[o]);return i}.call(this)),this.storageKey=a.join("."),this.storageIndexKey=[this.options.namespace,"index",e.location.host].join("."),this.webStorage=e[this.options.webStorage],this.init()}return n.prototype.indexedItems=function(){return JSON.parse(this.webStorage[this.storageIndexKey])},n.prototype.remove=function(){var e,n;this.stop(),this.webStorage.removeItem(this.storageKey),e=t.Event("phnx.removed"),this.$element.trigger(e),n=this.indexedItems(),n.slice(t.inArray(this.storageKey,n),1),this.webStorage[this.storageIndexKey]=JSON.stringify(n)},n.prototype.updateIndex=function(){var e;e=this.indexedItems(),-1===t.inArray(this.storageKey,e)&&(e.push(this.storageKey),e.length>this.options.maxItems&&(this.webStorage.removeItem(e[0]),e.shift()),this.webStorage[this.storageIndexKey]=JSON.stringify(e))},n.prototype.load=function(){var e,n;return n=this.webStorage[this.storageKey],null!=n?(this.$element.is(":checkbox, :radio")?this.element.checked=JSON.parse(n):"SELECT"===this.element.tagName?(this.$element.find("option").prop("selected",!1),t.each(JSON.parse(n),function(t){return function(e,n){return t.$element.find("option[value='"+n+"']").prop("selected",!0)}}(this))):this.element.value=n,e=t.Event("phnx.loaded"),this.$element.trigger(e)):void 0},n.prototype.save=function(){var e,n;return this.webStorage[this.storageKey]=this.$element.is(":checkbox, :radio")?this.element.checked:"SELECT"===this.element.tagName?(n=t.map(this.$element.find("option:selected"),function(t){return t.value}),JSON.stringify(n)):this.element.value,e=t.Event("phnx.saved"),this.$element.trigger(e),this.updateIndex()},n.prototype.start=function(){var e,n;return n=this.options.saveInterval>=0?setInterval(function(t){return function(){return t.save()}}(this),this.options.saveInterval):setTimeout(function(t){return function(){return t.save()}}(this)),r.push(n),e=t.Event("phnx.started"),this.$element.trigger(e)},n.prototype.stop=function(){var e;return r.forEach(function(t){return clearInterval(t)}),e=t.Event("phnx.stopped"),this.$element.trigger(e)},n.prototype.init=function(){switch(void 0===this.webStorage[this.storageIndexKey]&&(this.webStorage[this.storageIndexKey]="[]"),this.action){case"remove":return this.remove();case"start":return this.start();case"stop":return this.stop();case"load":return this.load();case"save":return this.save();default:if(this.load(),this.start(),this.options.clearOnSubmit&&t(this.options.clearOnSubmit).submit(function(t){return function(){return t.remove()}}(this)),this.options.saveOnChange)return t(this.element).change(function(t){return function(){return t.save()}}(this))}},n}(),o=function(t){try{return t in e&&null!==e[t]}catch(n){return!1}},t.fn[s]=function(e){var r;return r="plugin_"+s,this.each(function(){return!t.data(this,r)||o(e.webStorage||i.webStorage)?t.data(this,r,new n(this,e)):void 0})}}(jQuery,window);