|
| 1 | +// Fork of the upstream module. The only changes are the addition of `const` on |
| 2 | +// lines 93 and 161 to make it strict mode compatible. |
| 3 | + |
| 4 | +/*! |
| 5 | + * jQuery Plugin: Are-You-Sure (Dirty Form Detection) |
| 6 | + * https://github.com/codedance/jquery.AreYouSure/ |
| 7 | + * |
| 8 | + * Copyright (c) 2012-2014, Chris Dance and PaperCut Software http://www.papercut.com/ |
| 9 | + * Dual licensed under the MIT or GPL Version 2 licenses. |
| 10 | + * http://jquery.org/license |
| 11 | + * |
| 12 | + |
| 13 | + * Version: 1.9.0 |
| 14 | + * Date: 13th August 2014 |
| 15 | + */ |
| 16 | +(function($) { |
| 17 | + |
| 18 | + $.fn.areYouSure = function(options) { |
| 19 | + |
| 20 | + var settings = $.extend( |
| 21 | + { |
| 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 | + }, options); |
| 30 | + |
| 31 | + var getValue = function($field) { |
| 32 | + if ($field.hasClass('ays-ignore') |
| 33 | + || $field.hasClass('aysIgnore') |
| 34 | + || $field.attr('data-ays-ignore') |
| 35 | + || $field.attr('name') === undefined) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + if ($field.is(':disabled')) { |
| 40 | + return 'ays-disabled'; |
| 41 | + } |
| 42 | + |
| 43 | + var val; |
| 44 | + var type = $field.attr('type'); |
| 45 | + if ($field.is('select')) { |
| 46 | + type = 'select'; |
| 47 | + } |
| 48 | + |
| 49 | + switch (type) { |
| 50 | + case 'checkbox': |
| 51 | + case 'radio': |
| 52 | + val = $field.is(':checked'); |
| 53 | + break; |
| 54 | + case 'select': |
| 55 | + val = ''; |
| 56 | + $field.find('option').each(function(o) { |
| 57 | + var $option = $(this); |
| 58 | + if ($option.is(':selected')) { |
| 59 | + val += $option.val(); |
| 60 | + } |
| 61 | + }); |
| 62 | + break; |
| 63 | + default: |
| 64 | + val = $field.val(); |
| 65 | + } |
| 66 | + |
| 67 | + return val; |
| 68 | + }; |
| 69 | + |
| 70 | + var storeOrigValue = function($field) { |
| 71 | + $field.data('ays-orig', getValue($field)); |
| 72 | + }; |
| 73 | + |
| 74 | + var checkForm = function(evt) { |
| 75 | + |
| 76 | + var isFieldDirty = function($field) { |
| 77 | + var origValue = $field.data('ays-orig'); |
| 78 | + if (undefined === origValue) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + return (getValue($field) != origValue); |
| 82 | + }; |
| 83 | + |
| 84 | + var $form = ($(this).is('form')) |
| 85 | + ? $(this) |
| 86 | + : $(this).parents('form'); |
| 87 | + |
| 88 | + // Test on the target first as it's the most likely to be dirty |
| 89 | + if (isFieldDirty($(evt.target))) { |
| 90 | + setDirtyStatus($form, true); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const $fields = $form.find(settings.fieldSelector); |
| 95 | + |
| 96 | + if (settings.addRemoveFieldsMarksDirty) { |
| 97 | + // Check if field count has changed |
| 98 | + var origCount = $form.data("ays-orig-field-count"); |
| 99 | + if (origCount != $fields.length) { |
| 100 | + setDirtyStatus($form, true); |
| 101 | + return; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Brute force - check each field |
| 106 | + var isDirty = false; |
| 107 | + $fields.each(function() { |
| 108 | + var $field = $(this); |
| 109 | + if (isFieldDirty($field)) { |
| 110 | + isDirty = true; |
| 111 | + return false; // break |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + setDirtyStatus($form, isDirty); |
| 116 | + }; |
| 117 | + |
| 118 | + var initForm = function($form) { |
| 119 | + var fields = $form.find(settings.fieldSelector); |
| 120 | + $(fields).each(function() { storeOrigValue($(this)); }); |
| 121 | + $(fields).unbind(settings.fieldEvents, checkForm); |
| 122 | + $(fields).bind(settings.fieldEvents, checkForm); |
| 123 | + $form.data("ays-orig-field-count", $(fields).length); |
| 124 | + setDirtyStatus($form, false); |
| 125 | + }; |
| 126 | + |
| 127 | + var setDirtyStatus = function($form, isDirty) { |
| 128 | + var changed = isDirty != $form.hasClass(settings.dirtyClass); |
| 129 | + $form.toggleClass(settings.dirtyClass, isDirty); |
| 130 | + |
| 131 | + // Fire change event if required |
| 132 | + if (changed) { |
| 133 | + if (settings.change) settings.change.call($form, $form); |
| 134 | + |
| 135 | + if (isDirty) $form.trigger('dirty.areYouSure', [$form]); |
| 136 | + if (!isDirty) $form.trigger('clean.areYouSure', [$form]); |
| 137 | + $form.trigger('change.areYouSure', [$form]); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + var rescan = function() { |
| 142 | + var $form = $(this); |
| 143 | + var fields = $form.find(settings.fieldSelector); |
| 144 | + $(fields).each(function() { |
| 145 | + var $field = $(this); |
| 146 | + if (!$field.data('ays-orig')) { |
| 147 | + storeOrigValue($field); |
| 148 | + $field.bind(settings.fieldEvents, checkForm); |
| 149 | + } |
| 150 | + }); |
| 151 | + // Check for changes while we're here |
| 152 | + $form.trigger('checkform.areYouSure'); |
| 153 | + }; |
| 154 | + |
| 155 | + var reinitialize = function() { |
| 156 | + initForm($(this)); |
| 157 | + } |
| 158 | + |
| 159 | + if (!settings.silent && !window.aysUnloadSet) { |
| 160 | + window.aysUnloadSet = true; |
| 161 | + $(window).bind('beforeunload', function() { |
| 162 | + const $dirtyForms = $("form").filter('.' + settings.dirtyClass); |
| 163 | + if ($dirtyForms.length == 0) { |
| 164 | + return; |
| 165 | + } |
| 166 | + // Prevent multiple prompts - seen on Chrome and IE |
| 167 | + if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) { |
| 168 | + if (window.aysHasPrompted) { |
| 169 | + return; |
| 170 | + } |
| 171 | + window.aysHasPrompted = true; |
| 172 | + window.setTimeout(function() {window.aysHasPrompted = false;}, 900); |
| 173 | + } |
| 174 | + return settings.message; |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + return this.each(function(elem) { |
| 179 | + if (!$(this).is('form')) { |
| 180 | + return; |
| 181 | + } |
| 182 | + var $form = $(this); |
| 183 | + |
| 184 | + $form.submit(function() { |
| 185 | + $form.removeClass(settings.dirtyClass); |
| 186 | + }); |
| 187 | + $form.bind('reset', function() { setDirtyStatus($form, false); }); |
| 188 | + // Add a custom events |
| 189 | + $form.bind('rescan.areYouSure', rescan); |
| 190 | + $form.bind('reinitialize.areYouSure', reinitialize); |
| 191 | + $form.bind('checkform.areYouSure', checkForm); |
| 192 | + initForm($form); |
| 193 | + }); |
| 194 | + }; |
| 195 | +})(jQuery); |
0 commit comments