forked from simsalabim/sisyphus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sisyphus.min.js
1 lines (1 loc) · 4 KB
/
sisyphus.min.js
1
(function(a){a.sisyphus=function(){return Sisyphus.getInstance()},a.fn.sisyphus=function(a){var b=Sisyphus.getInstance();return b.setOptions(a),b.protect(this),b},Sisyphus=function(){function c(){return{setInitialOptions:function(b){var c={excludeFields:null,customKeyPrefix:"",timeout:0,onSave:function(){},onRestore:function(){},onRelease:function(){}};this.options=this.options||a.extend(c,b)},setOptions:function(b){this.options=this.options||this.setInitialOptions(b),this.options=a.extend(this.options,b)},protect:function(c){c=c||{};var d=this;this.targets=this.targets||[],this.href=location.hostname+location.pathname+location.search,this.targets=a.merge(this.targets,c),this.targets=a.unique(this.targets),this.targets=a(this.targets);if(!this.isLocalStorageAvailable())return!1;d.restoreAllData(),d.bindReleaseData(),b.started||(d.bindSaveData(),b.started=!0)},isLocalStorageAvailable:function(){try{return localStorage.getItem}catch(a){return!1}},bindSaveData:function(){var b=this;b.options.timeout&&b.saveDataByTimeout(),b.targets.each(function(){var c=a(this).attr("id"),d=a(this).find(":input").not(":submit").not(":reset").not(":button");d.each(function(){if(a.inArray(this,b.options.excludeFields)!==-1)return!0;var d=a(this),e=b.href+c+d.attr("name")+b.options.customKeyPrefix;d.is(":text")||d.is("textarea")?b.options.timeout||b.bindSaveDataImmediately(d,e):b.bindSaveDataOnChange(d,e)})})},saveAllData:function(){var b=this;b.targets.each(function(){var c=a(this).attr("id"),d=a(this).find(":input").not(":submit").not(":reset").not(":button");d.each(function(){if(a.inArray(this,b.options.excludeFields)!==-1)return!0;var d=a(this),e=b.href+c+d.attr("name")+b.options.customKeyPrefix,f=d.val();d.is(":checkbox")?(d.attr("name").indexOf("[")!=-1?(f=[],a("[name='"+d.attr("name")+"']:checked").each(function(){f.push(a(this).val())})):f=d.is(":checked"),b.saveToLocalStorage(e,f,!1)):d.is(":radio")?d.is(":checked")&&(f=d.val(),b.saveToLocalStorage(e,f,!1)):b.saveToLocalStorage(e,f,!1)})}),a.isFunction(b.options.onSave)&&b.options.onSave.call()},restoreAllData:function(){var b=this,c=!1;b.targets.each(function(){var d=a(this),e=d.attr("id"),f=d.find(":input").not(":submit").not(":reset").not(":button");f.each(function(){if(a.inArray(this,b.options.excludeFields)!==-1)return!0;var d=a(this),f=b.href+e+d.attr("name")+b.options.customKeyPrefix,g=localStorage.getItem(f);g&&(b.restoreFieldsData(d,g),c=!0)})}),c&&a.isFunction(b.options.onRestore)&&b.options.onRestore.call()},restoreFieldsData:function(a,b){a.is(":checkbox")&&b!=="false"&&a.attr("name").indexOf("[")===-1?a.attr("checked","checked"):a.is(":radio")?a.val()===b&&a.attr("checked","checked"):a.attr("name").indexOf("[")===-1?a.val(b):(b=b.split(","),a.val(b))},bindSaveDataImmediately:function(b,c){var d=this;a.browser.msie==null?b.get(0).oninput=function(){d.saveToLocalStorage(c,b.val())}:b.get(0).onpropertychange=function(){d.saveToLocalStorage(c,b.val())}},saveToLocalStorage:function(b,c,d){d=d==null?!0:d;try{localStorage.setItem(b,c+"")}catch(e){}d&&c!==""&&a.isFunction(this.options.onSave)&&this.options.onSave.call()},bindSaveDataOnChange:function(a,b){var c=this;a.change(function(){c.saveAllData()})},saveDataByTimeout:function(){var a=this,b=a.targets;setTimeout(function(b){function c(){a.saveAllData(),setTimeout(c,a.options.timeout*1e3)}return c}(b),a.options.timeout*1e3)},bindReleaseData:function(){var b=this;b.targets.each(function(c){var d=a(this),e=d.find(":input").not(":submit").not(":reset").not(":button"),f=d.attr("id");a(this).bind("submit reset",function(){b.releaseData(f,e)})})},releaseData:function(b,c){var d=!1,e=this;c.each(function(){if(a.inArray(this,e.options.excludeFields)!==-1)return!0;var c=a(this),f=e.href+b+c.attr("name")+e.options.customKeyPrefix;localStorage.removeItem(f),d=!0}),d&&a.isFunction(e.options.onRelease)&&e.options.onRelease.call()}}}var b={instantiated:null,started:null};return{getInstance:function(){return b.instantiated||(b.instantiated=c(),b.instantiated.setInitialOptions()),b.instantiated},free:function(){return b={},null}}}()})(jQuery)