|
3 | 3 | // * use export to make it work with ES6 modules. |
4 | 4 | // * the addition of `const` to make it strict mode compatible. |
5 | 5 | // * ignore forms with "ignore-dirty" class, ignore hidden forms (closest('.tw-hidden')) |
6 | | -// * extract the default options to make it available to other functions |
7 | | -// * check if any form is dirty |
| 6 | +// * extract the dirty check logic into a separate function |
8 | 7 |
|
9 | 8 | /*! |
10 | 9 | * jQuery Plugin: Are-You-Sure (Dirty Form Detection) |
|
18 | 17 | * Version: 1.9.0 |
19 | 18 | * Date: 13th August 2014 |
20 | 19 | */ |
21 | | -const defaultOptions = { |
22 | | - 'message': 'You have unsaved changes!', |
23 | | - 'dirtyClass': 'dirty', |
24 | | - 'change': null, |
25 | | - 'silent': false, |
26 | | - 'addRemoveFieldsMarksDirty': false, |
27 | | - 'fieldEvents': 'change keyup propertychange input', |
28 | | - 'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])" |
29 | | -}; |
| 20 | + |
| 21 | +const dataKeyAysSettings = 'ays-settings'; |
30 | 22 |
|
31 | 23 | export function initAreYouSure($) { |
32 | 24 |
|
33 | 25 | $.fn.areYouSure = function(options) { |
34 | 26 |
|
35 | | - var settings = $.extend({}, defaultOptions, options); |
| 27 | + var settings = $.extend( |
| 28 | + { |
| 29 | + 'message' : 'You have unsaved changes!', |
| 30 | + 'dirtyClass' : 'dirty', |
| 31 | + 'change' : null, |
| 32 | + 'silent' : false, |
| 33 | + 'addRemoveFieldsMarksDirty' : false, |
| 34 | + 'fieldEvents' : 'change keyup propertychange input', |
| 35 | + 'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])" |
| 36 | + }, options); |
36 | 37 |
|
37 | 38 | var getValue = function($field) { |
38 | 39 | if ($field.hasClass('ays-ignore') |
@@ -127,6 +128,7 @@ export function initAreYouSure($) { |
127 | 128 | $(fields).unbind(settings.fieldEvents, checkForm); |
128 | 129 | $(fields).bind(settings.fieldEvents, checkForm); |
129 | 130 | $form.data("ays-orig-field-count", $(fields).length); |
| 131 | + $form.data(dataKeyAysSettings, settings); |
130 | 132 | setDirtyStatus($form, false); |
131 | 133 | }; |
132 | 134 |
|
@@ -165,7 +167,7 @@ export function initAreYouSure($) { |
165 | 167 | if (!settings.silent && !window.aysUnloadSet) { |
166 | 168 | window.aysUnloadSet = true; |
167 | 169 | $(window).bind('beforeunload', function() { |
168 | | - if (!triggersAreYouSure(settings)) return; |
| 170 | + if (!shouldTriggerAreYouSure(settings)) return; |
169 | 171 |
|
170 | 172 | // Prevent multiple prompts - seen on Chrome and IE |
171 | 173 | if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) { |
@@ -212,7 +214,14 @@ export function ignoreAreYouSure(selectorOrEl: string|Element|$) { |
212 | 214 | $(selectorOrEl).addClass('ignore-dirty'); |
213 | 215 | } |
214 | 216 |
|
215 | | -export function triggersAreYouSure(opts = {}) { |
216 | | - const $forms = $('form:not(.ignore-dirty)').filter('.' + (opts.dirtyClass ?? defaultOptions.dirtyClass)); |
217 | | - return Array.from($forms).some((form) => form.closest('.tw-hidden') === null); |
| 217 | +export function shouldTriggerAreYouSure(): boolean { |
| 218 | + const forms = document.querySelectorAll('form:not(.ignore-dirty)'); |
| 219 | + for (const form of forms) { |
| 220 | + const settings = $(form).data(dataKeyAysSettings); |
| 221 | + if (!settings) continue; |
| 222 | + if (!form.matches('.' + settings.dirtyClass)) continue; |
| 223 | + if (form.closest('.tw-hidden')) continue; |
| 224 | + return true; |
| 225 | + } |
| 226 | + return false; |
218 | 227 | } |
0 commit comments