-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.dirty.min.js
8 lines (8 loc) · 4.13 KB
/
jquery.dirty.min.js
1
2
3
4
5
6
7
8
/**
* Minified by jsDelivr using Terser v5.3.5.
* Original file: /npm/[email protected]/dist/jquery.dirty.js
*
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
!function(t){var i=[],e="dirty",s="clean",n="dirtyInitialValue",r=function(t){t.form.find("input, select, textarea").on("change.dirty click.dirty keyup.dirty keydown.dirty blur.dirty",(function(i){t.checkValues(i)})),t.form.on("dirty",(function(){t.options.onDirty()})),t.form.on("clean",(function(){t.options.onClean()}))},a=function(e,n){this.form=e,this.isDirty=!1,this.options=n,this.history=[s,s],this.id=t(e).attr("id"),i.push(this)};a.prototype={init:function(){this.saveInitialValues(),this.setEvents()},isRadioOrCheckbox:function(i){return t(i).is(":radio, :checkbox")},isFileInput:function(i){return t(i).is(":file")},saveInitialValues:function(){var i=this;this.form.find("input, select, textarea").each((function(e,s){var r=i.isRadioOrCheckbox(s),a=i.isFileInput(s);if(r){var o=t(s).is(":checked")?"checked":"unchecked";t(s).data(n,o)}else a?t(s).data(n,JSON.stringify(s.files)):t(s).data(n,t(s).val()||"")}))},refreshEvents:function(){!function(t){t.form.find("input, select, textarea").off("change.dirty click.dirty keyup.dirty keydown.dirty blur.dirty"),t.form.off("dirty"),t.form.off("clean")}(this),r(this)},showDirtyFields:function(){return this.form.find("input, select, textarea").filter((function(i,e){return t(e).data("isDirty")}))},setEvents:function(){!function(i){i.form.on("submit",(function(){i.submitting=!0})),i.options.preventLeaving&&t(window).on("beforeunload",(function(t){if(i.isDirty&&!i.submitting)return t.preventDefault(),i.options.leavingMessage}))}(this),r(this)},isFieldDirty:function(t){var i=t.data(n);null==i&&(i="");var e=t.val();null==e&&(e="");var s=/^(true|false)$/i;return s.test(i)&&s.test(e)?!new RegExp("^"+i+"$","i").test(e):e!==i},isFileInputDirty:function(t){var i=t.data(n),e=t[0];return JSON.stringify(e.files)!==i},isCheckboxDirty:function(t){return t.data(n)!==(t.is(":checked")?"checked":"unchecked")},checkValues:function(i){var e=this,s=!1;this.form.find("input, select, textarea").each((function(i,n){var r,a=e.isRadioOrCheckbox(n),o=e.isFileInput(n),c=t(n);r=a?e.isCheckboxDirty(c):o?e.isFileInputDirty(c):e.isFieldDirty(c),c.data("isDirty",r),s|=r})),s?e.setDirty():e.setClean()},setDirty:function(){this.isDirty=!0,this.history[0]=this.history[1],this.history[1]=e,(this.options.fireEventsOnEachChange||this.wasJustClean())&&this.form.trigger("dirty")},setClean:function(){this.isDirty=!1,this.history[0]=this.history[1],this.history[1]=s,(this.options.fireEventsOnEachChange||this.wasJustDirty())&&this.form.trigger("clean")},wasJustDirty:function(){return this.history[0]===e},wasJustClean:function(){return this.history[0]===s},setAsClean:function(){this.saveInitialValues(),this.setClean()},setAsDirty:function(){this.saveInitialValues(),this.setDirty()},resetForm:function(){var i=this;this.form.find("input, select, textarea").each((function(e,s){var r=t(s),a=i.isRadioOrCheckbox(s),o=i.isFileInput(s);if(a){var c="checked"===r.data(n);r.prop("checked",c)}if(o)s.value="",t(s).data(n,JSON.stringify(s.files));else{var h=r.data(n);r.val(h)}})),this.checkValues()}},t.fn.dirty=function(e){if("string"==typeof e&&/^(isDirty|isClean|refreshEvents|resetForm|setAsClean|setAsDirty|showDirtyFields)$/i.test(e)){var s=(n=t(this).attr("id"),i.forEach((function(t){t.id===n&&(r=t)})),r);switch(s||(s=new a(t(this),e)).init(),e.toLowerCase()){case"isclean":return!s.isDirty;case"isdirty":return s.isDirty;case"refreshevents":s.refreshEvents();case"resetform":s.resetForm();case"setasclean":return s.setAsClean();case"setasdirty":return s.setAsDirty();case"showdirtyfields":return s.showDirtyFields()}}else if("object"==typeof e||!e)return this.each((function(i,s){e=t.extend({},t.fn.dirty.defaults,e),new a(t(s),e).init()}));var n,r},t.fn.dirty.defaults={preventLeaving:!1,leavingMessage:"There are unsaved changes on this page which will be discarded if you continue.",onDirty:t.noop,onClean:t.noop,fireEventsOnEachChange:!1}}(jQuery);
//# sourceMappingURL=/sm/f2caef4bd9109b143055d686e98f960aaedac0f1659b4797f7d6dfde842b90db.map