forked from simsalabim/sisyphus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sisyphus.min.js
10 lines (10 loc) · 6.34 KB
/
sisyphus.min.js
1
2
3
4
5
6
7
8
9
10
/**
* Plugin developed to save html forms data to LocalStorage to restore them after browser crashes, tabs closings
* and other disasters.
*
* https://github.com/simsalabim/sisyphus
*
* @author Alexander Kaupanin <[email protected]>
* @license MIT - see https://github.com/simsalabim/sisyphus/blob/master/MIT-LICENSE
*/
!function(c){function d(t){return"[id="+t.attr("id")+"][name="+t.attr("name")+"]"}c.fn.sisyphus=function(t){var e=c.map(this,function(t){return d(c(t))}).join(),i=Sisyphus.getInstance(e);return i.protect(this,t),i};var a,f,e={};e.isAvailable=function(){if("object"==typeof c.jStorage)return!0;try{return localStorage.getItem}catch(t){return!1}},e.set=function(t,e){if("object"==typeof c.jStorage)c.jStorage.set(t,e+"");else try{localStorage.setItem(t,e+"")}catch(t){}},e.get=function(t){if("object"!=typeof c.jStorage)return localStorage.getItem(t);var e=c.jStorage.get(t);return e?e.toString():e},e.remove=function(t){"object"==typeof c.jStorage?c.jStorage.deleteKey(t):localStorage.removeItem(t)},Sisyphus=(a={instantiated:[],started:[]},f=window.CKEDITOR,{getInstance:function(t){return a.instantiated[t]||(a.instantiated[t]={setInstanceIdentifier:function(t){this.identifier=t},getInstanceIdentifier:function(){return this.identifier},setInitialOptions:function(t){this.options=this.options||c.extend({excludeFields:[],customKeySuffix:"",locationBased:!1,timeout:0,autoRelease:!0,onBeforeSave:function(){},onSave:function(){},onBeforeRestore:function(){},onRestore:function(){},onRelease:function(){}},t),this.browserStorage=e},setOptions:function(t){this.options=this.options||this.setInitialOptions(t),this.options=c.extend(this.options,t)},protect:function(t,e){this.setOptions(e),t=t||{};var i=this;if(this.targets=this.targets||[],i.options.name?this.href=i.options.name:this.href=location.hostname+location.pathname+location.search+location.hash,this.targets=c.merge(this.targets,t),this.targets=c.unique(this.targets),this.targets=c(this.targets),!this.browserStorage.isAvailable())return!1;var n=i.options.onBeforeRestore.call(i);if((void 0===n||n)&&i.restoreAllData(),this.options.autoRelease&&i.bindReleaseData(),!a.started[this.getInstanceIdentifier()])if(i.isCKEditorPresent())var o=setInterval(function(){f.isLoaded&&(clearInterval(o),i.bindSaveData(),a.started[i.getInstanceIdentifier()]=!0)},100);else i.bindSaveData(),a.started[i.getInstanceIdentifier()]=!0},isCKEditorPresent:function(){return!!this.isCKEditorExists()&&(f.isLoaded=!1,f.on("instanceReady",function(){f.isLoaded=!0}),!0)},isCKEditorExists:function(){return void 0!==f},findFieldsToProtect:function(t){return t.find(":input").not(":submit").not(":reset").not(":button").not(":file").not(":password").not(":disabled").not("[readonly]")},bindSaveData:function(){var n=this;n.options.timeout&&n.saveDataByTimeout(),n.targets.each(function(){var i=d(c(this));n.findFieldsToProtect(c(this)).each(function(){if(-1!==c.inArray(this,n.options.excludeFields))return!0;var t=c(this),e=(n.options.locationBased?n.href:"")+i+d(t)+n.options.customKeySuffix;(t.is(":text")||t.is("textarea"))&&(n.options.timeout||n.bindSaveDataImmediately(t,e)),n.bindSaveDataOnChange(t)})})},saveAllData:function(){var r=this;r.targets.each(function(){var a=d(c(this)),s={};r.findFieldsToProtect(c(this)).each(function(){var t=c(this);if(-1!==c.inArray(this,r.options.excludeFields)||void 0===t.attr("name")&&void 0===t.attr("id"))return!0;var e=(r.options.locationBased?r.href:"")+a+d(t)+r.options.customKeySuffix,i=t.val();if(t.is(":checkbox")){var n=t.attr("name");if(void 0!==n&&-1!==n.indexOf("[")){if(!0===s[n])return;i=[],c("[name='"+n+"']:checked").each(function(){i.push(c(this).val())}),s[n]=!0}else i=t.is(":checked");r.saveToBrowserStorage(e,i,!1)}else if(t.is(":radio"))t.is(":checked")?(i=t.val(),r.saveToBrowserStorage(e,i,!1)):r.browserStorage.remove(e);else if(r.isCKEditorExists()){var o=f.instances[t.attr("name")]||f.instances[t.attr("id")];o?(o.updateElement(),r.saveToBrowserStorage(e,t.val(),!1)):r.saveToBrowserStorage(e,i,!1)}else r.saveToBrowserStorage(e,i,!1)})}),r.options.onSave.call(r)},restoreAllData:function(){var o=this,a=!1;o.targets.each(function(){var t=c(this),n=d(c(this));o.findFieldsToProtect(t).each(function(){if(-1!==c.inArray(this,o.options.excludeFields))return!0;var t=c(this),e=(o.options.locationBased?o.href:"")+n+d(t)+o.options.customKeySuffix,i=o.browserStorage.get(e);null!==i&&(o.restoreFieldsData(t,i),a=!0)})}),a&&o.options.onRestore.call(o)},restoreFieldsData:function(t,e){if(void 0===t.attr("name")&&void 0===t.attr("id"))return!1;var i=t.attr("name");!t.is(":checkbox")||"false"===e||void 0!==i&&-1!==i.indexOf("[")?!t.is(":checkbox")||"false"!==e||void 0!==i&&-1!==i.indexOf("[")?t.is(":radio")?t.val()===e&&t.prop("checked",!0):(void 0===i||-1===i.indexOf("[")||(e=e.split(",")),t.val(e)):t.prop("checked",!1):t.prop("checked",!0)},bindSaveDataImmediately:function(t,e){var i=this;if("onpropertychange"in t?t.get(0).onpropertychange=function(){i.saveToBrowserStorage(e,t.val())}:t.get(0).oninput=function(){i.saveToBrowserStorage(e,t.val())},this.isCKEditorExists()){var n=f.instances[t.attr("name")]||f.instances[t.attr("id")];n&&n.document.on("keyup",function(){n.updateElement(),i.saveToBrowserStorage(e,t.val())})}},saveToBrowserStorage:function(t,e,i){var n=this.options.onBeforeSave.call(this);void 0!==n&&!1===n||(i=void 0===i||i,this.browserStorage.set(t,e),i&&""!==e&&this.options.onSave.call(this))},bindSaveDataOnChange:function(t){var e=this;t.change(function(){e.saveAllData()})},saveDataByTimeout:function(){var e=this;e.targets,setTimeout(function t(){e.saveAllData(),setTimeout(t,1e3*e.options.timeout)},1e3*e.options.timeout)},bindReleaseData:function(){var i=this;i.targets.each(function(){var t=c(this),e=d(t);c(this).bind("submit reset",function(){i.releaseData(e,i.findFieldsToProtect(t))})})},manuallyReleaseData:function(){var i=this;i.targets.each(function(){var t=c(this),e=d(t);i.releaseData(e,i.findFieldsToProtect(t))})},releaseData:function(i,t){var n=!1,o=this;a.started[o.getInstanceIdentifier()]=!1,t.each(function(){if(-1!==c.inArray(this,o.options.excludeFields))return!0;var t=c(this),e=(o.options.locationBased?o.href:"")+i+d(t)+o.options.customKeySuffix;o.browserStorage.remove(e),n=!0}),n&&o.options.onRelease.call(o)}},a.instantiated[t].setInstanceIdentifier(t),a.instantiated[t].setInitialOptions()),a.instantiated[t]},free:function(){return a={instantiated:[],started:[]},null},version:"1.1.3"})}(jQuery);