forked from kugaevsky/jquery-phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.phoenix.min.js
1 lines (1 loc) · 3.24 KB
/
jquery.phoenix.min.js
1
!function(t,e){"use strict";var n,i,r,o,s;r="phoenix",i={namespace:"phoenixStorage",maxItems:100,saveInterval:1e3,clearOnSubmit:!1,saveOnChange:!1,keyAttributes:["tagName","id","name"]},o=[],n=function(){function n(n,o){var s;this.element=n,this._defaults=i,this._name=r,this.$element=t(this.element),this.options=t.extend({},i,"object"==typeof o?o:void 0),"string"==typeof o&&(this.action=o),this.uri=e.location.host+e.location.pathname,this.storageKey=([this.options.namespace,this.uri]+[function(){var t,e,n,i;for(n=this.options.keyAttributes,i=[],t=0,e=n.length;e>t;t++)s=n[t],i.push(this.element[s]);return i}.call(this)]).join("."),this.storageIndexKey=[this.options.namespace,"index",e.location.host].join("."),this.init()}return n.prototype.getStorageKey=function(){},n.prototype.indexedItems=function(){return JSON.parse(localStorage[this.storageIndexKey])},n.prototype.remove=function(){var e,n;this.stop(),localStorage.removeItem(this.storageKey),e=t.Event("phnx.removed"),this.$element.trigger(e),n=this.indexedItems(),n.slice(t.inArray(this.storageKey,n),1),localStorage[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&&(localStorage.removeItem(e[0]),e.shift()),localStorage[this.storageIndexKey]=JSON.stringify(e))},n.prototype.load=function(){var e,n;return n=localStorage[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 localStorage[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=setInterval(function(t){return function(){return t.save()}}(this),this.options.saveInterval),o.push(n),e=t.Event("phnx.started"),this.$element.trigger(e)},n.prototype.stop=function(){var e;return o.forEach(function(t){return clearInterval(t)}),e=t.Event("phnx.stopped"),this.$element.trigger(e)},n.prototype.init=function(){switch(void 0===localStorage[this.storageIndexKey]&&(localStorage[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}(),s=function(){try{return"localStorage"in e&&null!==e.localStorage}catch(t){return!1}},t.fn[r]=function(e){var i;return i="plugin_"+r,this.each(function(){return!t.data(this,i)||s()?t.data(this,i,new n(this,e)):void 0})}}(jQuery,window);