1(function ($, window, document, undefined) { 2 const pluginName = 'jaliosDirtyFormControlPlugin', 3 dataKey = 'plugin_' + pluginName, 4 defaults = { 5 config: {} 6 }; 7 8 function Plugin(element) { 9 this.$element = $(element); 10 this._defaults = defaults; 11 this._name = pluginName; 12 this.init(); 13 } 14 15 Plugin.prototype = { 16 // initialize options 17 init: function () { 18 this.registerFormChange(); 19 }, 20 21 /** 22 * listen to any input change in a form 23 */ 24 registerFormChange: function () { 25 let that = this; 26 if (!this.$element.is('form')) { 27 $.console.log('Jalios dirty form control only works on FORM element'); 28 } 29 30 this.$element.find(':input').on('jalios.date.change change input', function () { 31 that.$element.data('is-form-dirty', true); 32 }); 33 }, 34 35 /** 36 * Check if a form is dirty 37 * Also handle any wysiwyg changes 38 */ 39 isFormDirty: function () { 40 let isDirty = this.$element.data('is-form-dirty'); 41 if (isDirty) { 42 return true; 43 } 44 45 // Special case for wysiwyg 46 if (typeof tinyMCE !== 'undefined') { 47 var isAnyWysiwygDirty = false; 48 $.each(tinyMCE.editors, function (idx, editor) { 49 if ($(editor.formElement).hasClass('jalios-dirty-form-control')) { 50 isAnyWysiwygDirty |= editor.isDirty(); 51 } 52 }); 53 if (isAnyWysiwygDirty) { 54 return true; 55 } 56 } 57 58 return false; 59 } 60 }; 61 62 /* 63 * Plugin wrapper, preventing against multiple instantiations and return plugin instance. 64 */ 65 $.fn[pluginName] = function (options) { 66 var args = arguments; 67 68 // Is the first parameter an object (options), or was omitted, 69 // instantiate a new instance of the plugin. 70 if (options === undefined || typeof options === 'object') { 71 return this.each(function () { 72 // Only allow the plugin to be instantiated once, 73 // so we check that the element has no plugin instantiation yet 74 if (!$.data(this, dataKey)) { 75 var objD = $(this).data(); 76 // if it has no instance, create a new one, pass options to our plugin constructor, 77 // and store the plugin instance in the elements jQuery data object. 78 $.data(this, dataKey, new Plugin(this, options, objD)); 79 } 80 }); 81 82 // If the first parameter is a string and it doesn't start 83 // with an underscore or "contains" the `init`-function, 84 // treat this as a call to a public method. 85 } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') { 86 // Cache the method call 87 // to make it possible 88 // to return a value 89 var returns; 90 91 this.each(function () { 92 var instance = $.data(this, dataKey); 93 94 // Tests that there's already a plugin-instance 95 // and checks that the requested public method exists 96 if (instance instanceof Plugin && typeof instance[options] === 'function') { 97 // Call the method of our plugin instance, 98 // and pass it the supplied arguments. 99 returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1)); 100 } 101 102 // Allow instances to be destroyed via the 'destroy' method 103 if (options === 'destroy') { 104 $.data(this, dataKey, null); 105 } 106 }); 107 108 // If the earlier cached method 109 // gives a value back return the value, 110 // otherwise return this to preserve chainability. 111 return returns !== undefined ? returns : this; 112 } 113 }; 114 115 /** 116 * Init form with css class jalios-dirty-form-control 117 */ 118 const initDirtyFormListener = function () { 119 if ($('.jalios-dirty-form-control').length > 0) { 120 window.onbeforeunload = validateDirtyFormBeforeUnload; 121 $('.jalios-dirty-form-control').each(function (index, element) { 122 if (!$(element).data('jalios-dirty-form-control-initialized')) { 123 $(element).data('jalios-dirty-form-control-initialized', true); 124 $(element).jaliosDirtyFormControlPlugin(); 125 } 126 }); 127 } 128 }; 129 130 /** 131 * If any form got the css class jalios-dirty-form-control 132 * Check if the form is not dirty 133 * @param {*} event 134 */ 135 const validateDirtyFormBeforeUnload = function (event) { 136 // If this flag is enabled, the message will not be displayed 137 if (window.disableOnBeforeUnloadMessage) { 138 return; 139 } 140 141 let isAnyFormDirtry = false; 142 $('.jalios-dirty-form-control').each(function (index, element) { 143 if ($(element).data(dataKey).isFormDirty()) { 144 isAnyFormDirtry = true; 145 } 146 });
147 148 if (isAnyFormDirtry) { 149 return I18N.glp('warn.edit.contentlost'); 150 } 151 return; 152 }; 153 $(document).ready(function () { 154 initDirtyFormListener(); 155 156 $(document).on('jalios:refresh', function (event) { 157 let refresh = $.jalios.Event.match(event, 'refresh', 'after'); 158 if (refresh) { 159 initDirtyFormListener(); 160 } 161 }); 162 }); 163})(jQuery, window, document);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.