forked from codedance/jquery.AreYouSure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.are-you-sure.js
123 lines (107 loc) · 3.35 KB
/
jquery.are-you-sure.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
/*!
* jQuery Plugin: Are-You-Sure (Dirty Form Detection)
* https://github.com/codedance/jquery.AreYouSure/
*
* Copyright (c) 2012-2013, Chris Dance and PaperCut Software http://www.papercut.com/
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* Author: [email protected]
* Version: 1.2.0
* Date: 1st May 2013
*/
(function($) {
$.fn.areYouSure = function(options) {
var settings = $.extend(
{
'message' : 'You have unsaved changes!',
'dirtyClass' : 'dirty',
'change' : null,
'fieldSelector' : "select,textarea,input[type='text'],input[type='password'],input[type='checkbox'],input[type='radio'],input[type='hidden']"
}, options);
var getValue = function($field) {
if ($field.hasClass('ays-ignore')
|| $field.hasClass('aysIgnore')
|| $field.attr('data-ays-ignore')) {
return null;
}
if ($field.is(':disabled')) {
return 'ays-disabled';
}
var val;
var type = $field.attr('type');
if ($field.is('select')) {
type = 'select';
}
switch (type) {
case 'checkbox':
case 'radio':
val = $field.is(':checked');
break;
case 'select':
val = '';
$field.children('option').each(function(o) {
var $option = $(this);
if ($option.is(':selected')) {
val += $option.val();
}
});
break;
default:
val = $field.val();
}
return val;
};
var storeOrigValue = function() {
var $field = $(this);
$field.data('ays-orig', getValue($field));
};
var checkForm = function(evt) {
var isFieldDirty = function($field) {
return (getValue($field) != $field.data('ays-orig'));
};
var isDirty = false;
var $form = $(this).parents('form');
// Test on the target first as it's the most likely to be dirty.
if (isFieldDirty($(evt.target))) {
isDirty = true;
}
if (!isDirty) {
$form.find(settings.fieldSelector).each(function() {
$field = $(this);
if (isFieldDirty($field)) {
isDirty = true;
return false; // break
}
});
}
markDirty($form, isDirty);
};
var markDirty = function($form, isDirty) {
var changed = isDirty != $form.hasClass(settings.dirtyClass);
$form.toggleClass(settings.dirtyClass, isDirty);
// Fire change event if required
if (changed && settings.change) {
settings.change.call($form, $form);
}
};
$(window).bind('beforeunload', function() {
$dirtyForms = $("form").filter('.' + settings.dirtyClass);
if ($dirtyForms.length > 0) {
// $dirtyForms.removeClass(settings.dirtyClass); // Prevent multiple calls?
return settings.message;
}
});
return this.each(function(elem) {
if (!$(this).is('form')) {
return;
}
$(this).submit(function() {
$(this).removeClass(settings.dirtyClass);
});
$(this).find(settings.fieldSelector).each(storeOrigValue);
$(this).find(settings.fieldSelector).bind('change keyup', checkForm);
$(this).bind('reset', function() { markDirty($(this), false); });
});
};
})(jQuery);