|
22 | 22 | $.fn.form = function (parameters) {
|
23 | 23 | var
|
24 | 24 | $allModules = $(this),
|
| 25 | + $window = $(window), |
25 | 26 |
|
26 | 27 | time = Date.now(),
|
27 | 28 | performance = [],
|
|
60 | 61 | namespace,
|
61 | 62 | moduleNamespace,
|
62 | 63 | eventNamespace,
|
| 64 | + attachEventsSelector, |
| 65 | + attachEventsAction, |
63 | 66 |
|
64 | 67 | submitting = false,
|
65 | 68 | dirty = false,
|
|
142 | 145 | module[action]();
|
143 | 146 | event.preventDefault();
|
144 | 147 | });
|
| 148 | + |
| 149 | + attachEventsSelector = selector; |
| 150 | + attachEventsAction = action; |
145 | 151 | },
|
146 | 152 |
|
147 | 153 | bindEvents: function () {
|
|
167 | 173 |
|
168 | 174 | // Dirty events
|
169 | 175 | if (settings.preventLeaving) {
|
170 |
| - $(window).on('beforeunload' + eventNamespace, module.event.beforeUnload); |
| 176 | + $window.on('beforeunload' + eventNamespace, module.event.beforeUnload); |
171 | 177 | }
|
172 | 178 |
|
173 | 179 | $field.on('change' + eventNamespace
|
|
185 | 191 | $module.on('clean' + eventNamespace, function (e) {
|
186 | 192 | settings.onClean.call();
|
187 | 193 | });
|
| 194 | + if (attachEventsSelector) { |
| 195 | + module.attachEvents(attachEventsSelector, attachEventsAction); |
| 196 | + } |
188 | 197 | },
|
189 | 198 |
|
190 | 199 | clear: function () {
|
|
233 | 242 | isCheckbox = $field.is(selector.checkbox),
|
234 | 243 | isDropdown = $element.is(selector.uiDropdown) && module.can.useElement('dropdown'),
|
235 | 244 | isCalendar = $calendar.length > 0 && module.can.useElement('calendar'),
|
| 245 | + isFile = $field.is(selector.file), |
236 | 246 | isErrored = $fieldGroup.hasClass(className.error)
|
237 | 247 | ;
|
238 | 248 | if (defaultValue === undefined) {
|
|
253 | 263 | $calendar.calendar('set date', defaultValue);
|
254 | 264 | } else {
|
255 | 265 | module.verbose('Resetting field value', $field, defaultValue);
|
256 |
| - $field.val(defaultValue); |
| 266 | + $field.val(isFile ? '' : defaultValue); |
257 | 267 | }
|
258 | 268 | });
|
259 | 269 | module.remove.states();
|
|
389 | 399 | $module.off(eventNamespace);
|
390 | 400 | $field.off(eventNamespace);
|
391 | 401 | $submit.off(eventNamespace);
|
| 402 | + if (settings.preventLeaving) { |
| 403 | + $window.off(eventNamespace); |
| 404 | + } |
| 405 | + if (attachEventsSelector) { |
| 406 | + $(attachEventsSelector).off(eventNamespace); |
| 407 | + attachEventsSelector = undefined; |
| 408 | + } |
392 | 409 | },
|
393 | 410 |
|
394 | 411 | event: {
|
|
490 | 507 | return rule.type;
|
491 | 508 | },
|
492 | 509 | changeEvent: function (type, $input) {
|
493 |
| - if (type === 'checkbox' || type === 'radio' || type === 'hidden' || $input.is('select')) { |
494 |
| - return 'change'; |
495 |
| - } |
496 |
| - |
497 |
| - return module.get.inputEvent(); |
498 |
| - }, |
499 |
| - inputEvent: function () { |
500 |
| - return document.createElement('input').oninput !== undefined |
501 |
| - ? 'input' |
502 |
| - : (document.createElement('input').onpropertychange !== undefined |
503 |
| - ? 'propertychange' |
504 |
| - : 'keyup'); |
| 510 | + return ['file', 'checkbox', 'radio', 'hidden'].indexOf(type) >= 0 || $input.is('select') ? 'change' : 'input'; |
505 | 511 | },
|
506 | 512 | fieldsFromShorthand: function (fields) {
|
507 | 513 | var
|
|
1106 | 1112 | $field = module.get.field(key),
|
1107 | 1113 | $element = $field.parent(),
|
1108 | 1114 | $calendar = $field.closest(selector.uiCalendar),
|
| 1115 | + isFile = $field.is(selector.file), |
1109 | 1116 | isMultiple = Array.isArray(value),
|
1110 | 1117 | isCheckbox = $element.is(selector.uiCheckbox) && module.can.useElement('checkbox'),
|
1111 | 1118 | isDropdown = $element.is(selector.uiDropdown) && module.can.useElement('dropdown'),
|
|
1148 | 1155 | $calendar.calendar('set date', value);
|
1149 | 1156 | } else {
|
1150 | 1157 | module.verbose('Setting field value', value, $field);
|
1151 |
| - $field.val(value); |
| 1158 | + $field.val(isFile ? '' : value); |
1152 | 1159 | }
|
1153 | 1160 | }
|
1154 | 1161 | });
|
|
1651 | 1658 | selector: {
|
1652 | 1659 | checkbox: 'input[type="checkbox"], input[type="radio"]',
|
1653 | 1660 | clear: '.clear',
|
1654 |
| - field: 'input:not(.search):not([type="file"]):not([type="reset"]):not([type="button"]):not([type="submit"]), textarea, select', |
| 1661 | + field: 'input:not(.search):not([type="reset"]):not([type="button"]):not([type="submit"]), textarea, select', |
| 1662 | + file: 'input[type="file"]', |
1655 | 1663 | group: '.field',
|
1656 |
| - input: 'input:not([type="file"])', |
| 1664 | + input: 'input', |
1657 | 1665 | message: '.error.message',
|
1658 | 1666 | prompt: '.prompt.label',
|
1659 | 1667 | radio: 'input[type="radio"]',
|
|
0 commit comments