forked from snikch/jquery.dirtyforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.dirtyforms.js
613 lines (533 loc) · 22.3 KB
/
jquery.dirtyforms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*!
Dirty Forms jQuery Plugin | v2.0.0 | github.com/snikch/jquery.dirtyforms
(c) 2010-2016 Mal Curtis
License MIT
*/
/*<iife_head>*/
// Support for UMD: https://github.com/umdjs/umd/blob/master/jqueryPluginCommonjs.js
// See: http://blog.npmjs.org/post/112712169830/making-your-jquery-plugin-work-better-with-npm for details.
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'window', 'document'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = factory(require('jquery'), window, document);
} else {
// Browser globals
factory(jQuery, window, document);
}
}(function ($, window, document, undefined) {
/*</iife_head>*/
// Can't use ECMAScript 5's strict mode because several apps
// including ASP.NET trace the stack via arguments.caller.callee
// and Firefox dies if you try to trace through "use strict" call chains.
// See jQuery issue (#13335)
// Support: Firefox 18+
//"use strict";
if (!$.fn.on) {
// Patch jQuery 1.4.2 - 1.7 with an on function (that uses delegate).
$.fn.on = function (events, selector, data, handler) {
return this.delegate(selector, events, data, handler);
};
}
$.fn.dirtyForms = function (method) {
// Method calling logic
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.dirtyForms');
}
};
// Public Element methods ( $('form').dirtyForms('methodName', args) )
var methods = {
init: function (options) {
var data = {};
if (!state.initialized) {
// Override any default options
$.extend(true, $.DirtyForms, options);
$(document).trigger('bind.dirtyforms', [events]);
events.bind(window, document, data);
state.initialized = true;
}
this.filter('form').not(':dirtylistening').each(function () {
var $form = $(this);
dirtylog('Adding form ' + $form.attr('id') + ' to forms to watch');
// Store original values of the fields
$form.find($.DirtyForms.fieldSelector).each(function () {
storeOriginalValue($(this));
});
$form.trigger('scan.dirtyforms');
events.bindForm($form, data);
});
return this;
},
// Returns true if any of the selected elements or their children are dirty
isDirty: function (excludeHelpers) {
var ignoreSelector = getIgnoreSelector(),
dirtyClass = $.DirtyForms.dirtyClass,
isDirty = false;
this.each(function (index) {
var $node = $(this),
ignored = isFieldIgnored($node, ignoreSelector);
if ($node.hasClass(dirtyClass) && !ignored) {
isDirty = true;
// Exit out of the .each() function
return false;
}
// Check any descendant nodes (if this is a container element)
$node.find('.' + dirtyClass).each(function () {
if (!isFieldIgnored($(this), ignoreSelector)) {
isDirty = true;
// Exit out of the .each() function
return false;
}
});
// Exit out of the .each() function
if (isDirty) return false;
if (!ignored && !excludeHelpers) {
// Test helpers for this node.
$.each($.DirtyForms.helpers, function (i, helper) {
if (helper.isDirty && helper.isDirty($node, index)) {
isDirty = true;
// Exit out of the .each() function
return false;
}
});
// Exit out of the .each() function
if (isDirty) return false;
}
});
return isDirty;
},
// Marks the element(s) and any helpers within the element not dirty.
// If all of the fields in a form are marked not dirty, the form itself will be marked not dirty even
// if it is not included in the selector. Also resets original values to the current state -
// essentially "forgetting" the node or its descendants are dirty.
setClean: function (excludeIgnored, excludeHelpers) {
dirtylog('setClean called');
var doSetClean = function () {
var $field = $(this);
// Reset by storing the original value again
storeOriginalValue($field);
// Remove the dirty class
setDirtyStatus($field, false);
};
elementsInRange(this, $.DirtyForms.fieldSelector, excludeIgnored)
.each(doSetClean)
.parents('form').trigger('setclean.dirtyforms', [excludeIgnored]);
if (excludeHelpers) return this;
return fireHelperMethod(this, 'setClean', excludeIgnored, getIgnoreSelector());
},
// Scans the selected elements and descendants for any new fields and stores their original values.
// Ignores any original values that had been set previously. Also resets the dirty status of all fields
// whose ignore status has changed since the last scan.
rescan: function (excludeIgnored, excludeHelpers) {
dirtylog('rescan called');
var doRescan = function () {
var $field = $(this);
// Skip previously added fields
if (!hasOriginalValue($field)) {
// Store the original value
storeOriginalValue($field);
}
// Set the dirty status
setDirtyStatus($field, isFieldDirty($field));
};
elementsInRange(this, $.DirtyForms.fieldSelector, excludeIgnored)
.each(doRescan)
.parents('form').trigger('rescan.dirtyforms', [excludeIgnored]);
if (excludeHelpers) return this;
return fireHelperMethod(this, 'rescan', excludeIgnored, getIgnoreSelector());
}
};
// Custom selectors $('form:dirty')
$.extend($.expr[":"], {
dirty: function (element) {
var $element = $(element);
return $element.hasClass($.DirtyForms.dirtyClass) && !$element.is(':dirtyignored');
},
dirtylistening: function (element) {
return $(element).hasClass($.DirtyForms.listeningClass);
},
dirtyignored: function (element) {
return isFieldIgnored($(element), false);
}
});
// Public General Plugin properties and methods $.DirtyForms
$.DirtyForms = {
message: "You've made changes on this page which aren't saved. If you leave you will lose these changes.",
dirtyClass: 'dirty',
listeningClass: 'dirtylisten',
ignoreClass: 'dirtyignore',
ignoreSelector: '',
// exclude all HTML 4 except checkbox, option, text and password, but include HTML 5 except search
fieldSelector: "input:not([type='button'],[type='image'],[type='submit']," +
"[type='reset'],[type='file'],[type='search']),select,textarea",
/*<log>*/
debug: false,
dirtylog: function (msg) {
dirtylog(msg);
},
/*</log>*/
helpers: [],
dialog: false
};
// Private State Management
var state = {
initialized: false,
formStash: false,
dialogStash: false,
deciding: false,
decidingEvent: false
};
// Dialog Decision Management
var choice;
var bindKeys = function (ev) {
if (ev.data.bindEscKey && ev.which == 27 || ev.data.bindEnterKey && ev.which == 13) {
return doCommit(ev, false);
}
};
var bindDialog = function (choice) {
var staySelector = choice.staySelector,
proceedSelector = choice.proceedSelector;
if (staySelector !== '') {
$(staySelector).unbind('click', doCommit)
.click(doCommit);
}
if (proceedSelector !== '') {
$(proceedSelector).unbind('click', doProceed)
.click(doProceed);
}
if (choice.bindEscKey || choice.bindEnterKey) {
$(document).unbind('keydown', bindKeys)
.keydown(choice, bindKeys);
}
};
var callDialogClose = function (proceeding, unstashing) {
if ($.isFunction($.DirtyForms.dialog.close)) {
dirtylog('Calling dialog close');
$.DirtyForms.dialog.close(proceeding, unstashing);
}
};
var doProceed = function (ev) {
return doCommit(ev, true);
};
var doCommit = function (ev, proceeding) {
if (!state.deciding) return;
ev.preventDefault();
if (proceeding === true) {
var refireEvent = state.decidingEvent;
$(document).trigger('proceed.dirtyforms', [refireEvent]);
events.clearUnload(); // fix for chrome/safari
callDialogClose(proceeding, false);
refire(refireEvent);
} else {
$(document).trigger('stay.dirtyforms');
var isUnstashing = $.DirtyForms.dialog !== false && state.dialogStash !== false && $.isFunction($.DirtyForms.dialog.unstash);
callDialogClose(proceeding, isUnstashing);
if (isUnstashing) {
dirtylog('Refiring the dialog with stashed content');
$.DirtyForms.dialog.unstash(state.dialogStash, ev);
}
$(document).trigger('afterstay.dirtyforms');
}
state.deciding = state.decidingEvent = state.dialogStash = state.formStash = false;
return false;
};
// Event management
var events = {
bind: function (window, document, data) {
$(window).bind('beforeunload', data, events.onBeforeUnload);
$(document).on('click', 'a:not([target="_blank"])', data, events.onAnchorClick)
.on('submit', 'form', data, events.onSubmit);
},
bindForm: function ($form, data) {
var dirtyForms = $.DirtyForms;
// Test whether we are dealing with IE < 10
var isIE8_9 = ('onpropertychange' in document.createElement('input'));
var inputEvents = 'change input' + (isIE8_9 ? ' keyup selectionchange cut paste' : '');
$form.addClass(dirtyForms.listeningClass)
.on('focus keydown', dirtyForms.fieldSelector, data, events.onFocus)
.on(inputEvents, dirtyForms.fieldSelector, data, events.onFieldChange)
.bind('reset', data, events.onReset);
},
// For any fields added after the form was initialized, store the value when focused.
onFocus: function (ev) {
var $field = $(ev.target);
if (!hasOriginalValue($field)) {
storeOriginalValue($field);
}
},
onFieldChange: function (ev) {
var $field = $(ev.target);
if (ev.type !== 'change') {
delay(function () { setFieldStatus($field); }, 100);
} else {
setFieldStatus($field);
}
},
onReset: function (ev) {
var $form = $(ev.target).closest('form');
// Need a delay here because reset is called before the state of the form is reset.
setTimeout(function () { $form.dirtyForms('setClean'); }, 100);
},
onAnchorClick: function (ev) {
bindFn(ev);
},
onSubmit: function (ev) {
bindFn(ev);
},
onBeforeUnload: function (ev) {
var result = bindFn(ev);
if (result && state.doubleunloadfix !== true) {
dirtylog('Before unload will be called, resetting');
state.deciding = false;
}
state.doubleunloadfix = true;
setTimeout(function () { state.doubleunloadfix = false; }, 200);
// Only return the result if it is a string, otherwise don't return anything.
if (typeof result === 'string') {
// For IE and Firefox prior to version 4, set the returnValue.
ev.returnValue = result;
return result;
}
},
onRefireClick: function (ev) {
var event = new $.Event('click');
$(ev.target).trigger(event);
if (!event.isDefaultPrevented()) {
events.onRefireAnchorClick(ev);
}
},
onRefireAnchorClick: function (ev) {
var href = $(ev.target).closest('a[href]').attr('href');
if (href !== undefined) {
dirtylog('Sending location to ' + href);
window.location.href = href;
}
},
clearUnload: function () {
// I'd like to just be able to unbind this but there seems
// to be a bug in jQuery which doesn't unbind onbeforeunload
dirtylog('Clearing the beforeunload event');
$(window).unbind('beforeunload', events.onBeforeUnload);
window.onbeforeunload = null;
$(document).trigger('beforeunload.dirtyforms');
}
};
var elementsInRange = function ($this, selector, excludeIgnored) {
var $elements = $this.filter(selector).add($this.find(selector));
if (excludeIgnored) {
$elements = $elements.not(':dirtyignored');
}
return $elements;
};
var fireHelperMethod = function ($this, method, excludeIgnored, ignoreSelector) {
return $this.each(function (index) {
var $node = $(this);
if (!excludeIgnored || !isFieldIgnored($node, ignoreSelector)) {
$.each($.DirtyForms.helpers, function (i, helper) {
if (helper[method]) { helper[method]($node, index, excludeIgnored); }
});
}
});
};
var getFieldValue = function ($field) {
var value;
if ($field.is('select')) {
value = '';
$field.find('option').each(function () {
var $option = $(this);
if ($option.is(':selected')) {
if (value.length > 0) { value += ','; }
value += $option.val();
}
});
} else if ($field.is(":checkbox,:radio")) {
value = $field.is(':checked');
} else {
value = $field.val();
}
return value;
};
var storeOriginalValue = function ($field) {
dirtylog('Storing original value for ' + $field.attr('name'));
$field.data('df-orig', getFieldValue($field));
var isEmpty = ($field.data('df-orig') === undefined);
$field.data('df-empty', isEmpty);
};
var hasOriginalValue = function ($field) {
return ($field.data('df-orig') !== undefined || $field.data('df-empty') === true);
};
var getIgnoreSelector = function () {
var dirtyForms = $.DirtyForms,
result = dirtyForms.ignoreSelector;
$.each(dirtyForms.helpers, function (key, obj) {
if ('ignoreSelector' in obj) {
if (result.length > 0) { result += ','; }
result += obj.ignoreSelector;
}
});
return result;
};
var isFieldIgnored = function ($field, ignoreSelector) {
if (!ignoreSelector) {
ignoreSelector = getIgnoreSelector();
}
return $field.is(ignoreSelector) || $field.closest('.' + $.DirtyForms.ignoreClass).length > 0;
};
var isFieldDirty = function ($field, ignoreSelector) {
if (!hasOriginalValue($field) || isFieldIgnored($field, ignoreSelector)) return false;
return (getFieldValue($field) != $field.data('df-orig'));
};
var setFieldStatus = function ($field, ignoreSelector) {
if (isFieldIgnored($field, ignoreSelector)) return;
// Option groups are a special case because they change more than the current element.
if ($field.is(':radio[name]')) {
var name = $field.attr('name'),
$form = $field.parents('form');
$form.find(":radio[name='" + name + "']").each(function () {
var $radio = $(this);
setDirtyStatus($radio, isFieldDirty($radio, ignoreSelector));
});
} else {
setDirtyStatus($field, isFieldDirty($field, ignoreSelector));
}
};
var setDirtyStatus = function ($field, isDirty) {
dirtylog('Setting dirty status to ' + isDirty + ' on field ' + $field.attr('id'));
var dirtyClass = $.DirtyForms.dirtyClass,
$form = $field.parents('form');
// Mark the field dirty/clean
$field.toggleClass(dirtyClass, isDirty);
var changed = (isDirty !== ($form.hasClass(dirtyClass) && $form.find(':dirty').length === 0));
if (changed) {
dirtylog('Setting dirty status to ' + isDirty + ' on form ' + $form.attr('id'));
$form.toggleClass(dirtyClass, isDirty);
if (isDirty) $form.trigger('dirty.dirtyforms');
if (!isDirty) $form.trigger('clean.dirtyforms');
}
};
// A delay to keep the key events from slowing down when changing the dirty status on the fly.
var delay = (function () {
var timer = 0;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
var bindFn = function (ev) {
var $element = $(ev.target),
eventType = ev.type,
dirtyForms = $.DirtyForms;
dirtylog('Entering: Leaving Event fired, type: ' + eventType + ', element: ' + ev.target + ', class: ' + $element.attr('class') + ' and id: ' + ev.target.id);
// Important: Do this check before calling events.clearUnload()
if (ev.isDefaultPrevented()) {
dirtylog('Leaving: Event has been stopped elsewhere');
return false;
}
if (eventType == 'beforeunload' && state.doubleunloadfix) {
dirtylog('Skip this unload, Firefox bug triggers the unload event multiple times');
state.doubleunloadfix = false;
return false;
}
if ($element.is(':dirtyignored')) {
dirtylog('Leaving: Element has ignore class or a descendant of an ignored element');
events.clearUnload();
return false;
}
if (state.deciding) {
dirtylog('Leaving: Already in the deciding process');
return false;
}
if (!$('form:dirtylistening').dirtyForms('isDirty')) {
dirtylog('Leaving: Not dirty');
events.clearUnload();
return false;
}
if (eventType == 'submit' && $element.dirtyForms('isDirty')) {
dirtylog('Leaving: Form submitted is a dirty form');
events.clearUnload();
return true;
}
// Callback for page access in current state
$(document).trigger('defer.dirtyforms');
if (eventType == 'beforeunload') {
dirtylog('Returning to beforeunload browser handler with: ' + dirtyForms.message);
return dirtyForms.message;
}
if (!dirtyForms.dialog) return;
// Using the GUI dialog...
ev.preventDefault();
ev.stopImmediatePropagation();
dirtylog('Setting deciding active');
state.deciding = true;
state.decidingEvent = ev;
// Stash the dialog (with a form). This is done so it can be shown again via unstash().
if ($.isFunction(dirtyForms.dialog.stash)) {
dirtylog('Stashing dialog content');
state.dialogStash = dirtyForms.dialog.stash();
dirtylog('Dialog Stash: ' + state.dialogStash);
}
// Stash the form from the dialog. This is done so we can fire events on it if the user makes a proceed choice.
var stashSelector = dirtyForms.dialog.stashSelector;
if (typeof stashSelector === 'string' && $element.is('form') && $element.parents(stashSelector).length > 0) {
dirtylog('Stashing form');
state.formStash = $element.clone(true).hide();
} else {
state.formStash = false;
}
dirtylog('Deferring to the dialog');
// Create a new choice object
choice = {
proceed: false,
commit: function (ev) {
return doCommit(ev, choice.proceed);
},
bindEscKey: true,
bindEnterKey: false,
proceedSelector: '',
staySelector: ''
};
dirtyForms.dialog.open(choice, dirtyForms.message, dirtyForms.ignoreClass);
bindDialog(choice);
};
var refire = function (ev) {
if (ev.type === 'click') {
dirtylog("Refiring click event");
events.onRefireClick(ev);
} else {
dirtylog("Refiring " + ev.type + " event on " + ev.target);
var target;
if (state.formStash) {
dirtylog('Appending stashed form to body');
target = state.formStash;
$('body').append(target);
}
else {
target = $(ev.target).closest('form');
}
target.trigger(ev.type);
}
};
/*<log>*/
var dirtylog = function (msg) {
if (!$.DirtyForms.debug) return;
var hasFirebug = 'console' in window && 'firebug' in window.console,
hasConsoleLog = 'console' in window && 'log' in window.console;
msg = '[DirtyForms] ' + msg;
if (hasFirebug) {
console.log(msg);
} else if (hasConsoleLog) {
window.console.log(msg);
} else {
alert(msg);
}
};
/*</log>*/
/*<iife_foot>*/
}));
/*</iife_foot>*/