forked from snikch/jquery.dirtyforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.dirtyforms.js
338 lines (296 loc) · 8.75 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
// Copyright 2010 Mal Curtis
if (typeof jQuery == 'undefined') throw("jQuery Required");
(function($){
// Public General Plugin methods $.DirtyForms
$.extend({
DirtyForms: {
debug : false,
message : 'You\'ve made changes on this page which aren\'t saved. If you leave you will lose these changes.',
title : 'Are you sure you want to do that?',
dirtyClass : 'dirty',
listeningClass : 'dirtylisten',
ignoreClass : 'ignoredirty',
helpers : [],
dialog : {
refire : function(content, ev){
$.facebox(content);
},
fire : function(message, title){
var content = '<h1>' + title + '</h1><p>' + message + '</p><p><a href="#" class="ignoredirty button medium red continue">Continue</a><a href="#" class="ignoredirty button medium cancel">Stop</a>';
$.facebox(content);
},
bind : function(){
var close = function(decision) {
return function(e) {
e.preventDefault();
$(document).trigger('close.facebox');
decision(e);
};
};
$('#facebox .cancel, #facebox .close, #facebox_overlay').click(close(decidingCancel));
$('#facebox .continue').click(close(decidingContinue));
},
stash : function(){
var fb = $('#facebox');
return ($.trim(fb.html()) == '' || fb.css('display') != 'block') ?
false :
$('#facebox .content').clone(true);
},
selector : '#facebox .content'
},
isDirty : function(){
dirtylog('Core isDirty is starting ');
var isDirty = false;
$(':dirtylistening').each(function(){
if($(this).isDirty()){
isDirty = true;
return true;
}
});
$.each($.DirtyForms.helpers, function(key,obj){
if("isDirty" in obj){
if(obj.isDirty()){
isDirty = true;
return true;
}
}
});
dirtylog('Core isDirty is returning ' + isDirty);
return isDirty;
}
}
});
// Create a custom selector $('form:dirty')
$.extend($.expr[":"], {
dirtylistening : function(a){
return $(a).hasClass($.DirtyForms.listeningClass);
},
dirty : function(a){
return $(a).hasClass($.DirtyForms.dirtyClass);
}
});
// Public Element methods $('form').dirtyForm();
$.fn.dirtyForms = function(){
var core = $.DirtyForms;
var thisForm = this;
dirtylog('Adding forms to watch');
bindExit();
return this.each(function(e){
dirtylog('Adding form ' + $(this).attr('id') + ' to forms to watch');
$(this).addClass(core.listeningClass);
$('input, textarea, select', this).focus(onFocus);
});
}
$.fn.setDirty = function(){
dirtylog('setDirty called');
return this.each(function(e){
$(this).addClass($.DirtyForms.dirtyClass).parents('form').addClass($.DirtyForms.dirtyClass);
});
}
// Returns true if any of the supplied elements are dirty
$.fn.isDirty = function(){
var isDirty = false;
var node = this;
if (focusedIsDirty()) {
isDirty = true;
return true;
}
this.each(function(e){
if($(this).hasClass($.DirtyForms.dirtyClass)){
isDirty = true;
return true;
}
});
$.each($.DirtyForms.helpers, function(key,obj){
if("isNodeDirty" in obj){
if(obj.isNodeDirty(node)){
isDirty = true;
return true;
}
}
});
dirtylog('isDirty returned ' + isDirty);
return isDirty;
}
// Private Properties and Methods
var settings = $.DirtyForms = $.extend({
exitBound : false,
formStash : false,
dialogStash : false,
deciding : false,
decidingEvent : false,
currentForm : false,
hasFirebug : "console" in window && "firebug" in window.console,
hasConsoleLog: "console" in window && "log" in window.console,
focused: {"element": false, "value": false}
}, $.DirtyForms);
onFocus = function() {
element = $(this);
if (focusedIsDirty()) {
element.setDirty();
}
settings.focused['element'] = element;
settings.focused['value'] = element.val();
}
focusedIsDirty = function() {
/** Check, whether the value of focused element has changed */
return settings.focused["element"] &&
(settings.focused["element"].val() !== settings.focused["value"]);
}
dirtylog = function(msg){
if(!$.DirtyForms.debug) return;
msg = "[DirtyForms] " + msg;
settings.hasFirebug ?
console.log(msg) :
settings.hasConsoleLog ?
window.console.log(msg) :
alert(msg);
}
bindExit = function(){
if(settings.exitBound) return;
$('a').live('click',aBindFn);
$('form').live('submit',formBindFn);
$(window).bind('beforeunload', beforeunloadBindFn);
settings.exitBound = true;
}
aBindFn = function(ev){
bindFn(ev);
}
formBindFn = function(ev){
settings.currentForm = this;
bindFn(ev);
}
beforeunloadBindFn = function(ev){
var result = bindFn(ev);
if(result && settings.doubleunloadfix != true){
dirtylog('Before unload will be called, resetting');
settings.deciding = false;
}
settings.doubleunloadfix = true;
setTimeout(function(){settings.doubleunloadfix = false;},200);
if(result === false) return null;
return result;
}
bindFn = function(ev){
dirtylog('Entering: Leaving Event fired, type: ' + ev.type + ', element: ' + ev.target + ', class: ' + $(ev.target).attr('class') + ' and id: ' + ev.target.id);
if(ev.type == 'beforeunload' && settings.doubleunloadfix){
dirtylog('Skip this unload, Firefox bug triggers the unload event multiple times');
settings.doubleunloadfix = false;
return false;
}
if($(ev.target).hasClass(settings.ignoreClass)){
dirtylog('Leaving: Element has ignore class');
if(!ev.isDefaultPrevented()){
clearUnload();
}
return false;
}
if(settings.deciding){
dirtylog('Leaving: Already in the deciding process');
return false;
}
if(ev.isDefaultPrevented()){
dirtylog('Leaving: Event has been stopped elsewhere');
return false;
}
if(!settings.isDirty()){
dirtylog('Leaving: Not dirty');
if(!ev.isDefaultPrevented()){
clearUnload();
}
return false;
}
if(ev.type == 'submit' && $(ev.target).isDirty()){
dirtylog('Leaving: Form submitted is a dirty form');
if(!ev.isDefaultPrevented()){
clearUnload();
}
return true;
}
settings.deciding = true;
settings.decidingEvent = ev;
dirtylog('Setting deciding active');
if(settings.dialog !== false)
{
dirtylog('Saving dialog content');
settings.dialogStash =settings.dialog.stash();
dirtylog(settings.dialogStash);
}
// Callback for page access in current state
$(document).trigger('defer.dirtyforms');
if(ev.type == 'beforeunload'){
//clearUnload();
dirtylog('Returning to beforeunload browser handler with: ' + settings.message);
return settings.message;
}
if(!settings.dialog) return;
ev.preventDefault();
ev.stopImmediatePropagation();
if($(ev.target).is('form') && $(ev.target).parents(settings.dialog.selector).length > 0){
dirtylog('Stashing form');
settings.formStash = $(ev.target).clone(true).hide();
}else{
settings.formStash = false;
}
dirtylog('Deferring to the dialog');
settings.dialog.fire($.DirtyForms.message, $.DirtyForms.title);
settings.dialog.bind();
}
decidingCancel = function(ev){
ev.preventDefault();
$(document).trigger('decidingcancelled.dirtyforms');
if(settings.dialog !== false && settings.dialogStash !== false)
{
dirtylog('Refiring the dialog with stashed content');
settings.dialog.refire(settings.dialogStash.html(), ev);
}
$(document).trigger('decidingcancelledAfter.dirtyforms');
settings.dialogStash = false;
settings.deciding = settings.currentForm = settings.decidingEvent = false;
}
decidingContinue = function(ev){
ev.preventDefault();
settings.dialogStash = false;
$(document).trigger('decidingcontinued.dirtyforms');
refire(settings.decidingEvent);
settings.deciding = settings.currentForm = settings.decidingEvent = false;
}
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', beforeunloadBindFn);
window.onbeforeunload = null;
}
refire = function(e){
$(document).trigger('beforeRefire.dirtyforms');
switch(e.type){
case 'click':
dirtylog("Refiring click event");
var event = new jQuery.Event('click');
$(e.target).trigger(event);
if(!event.isDefaultPrevented()){
var anchor = $(e.target).closest('[href]');
dirtylog('Sending location to ' + anchor.attr('href'));
location.href = anchor.attr('href');
return;
}
break;
default:
dirtylog("Refiring " + e.type + " event on " + e.target);
var target;
if(settings.formStash){
dirtylog('Appending stashed form to body');
target = settings.formStash;
$('body').append(target);
}
else{
target = $(e.target);
if(!target.is('form'))
target = target.closest('form');
}
target.trigger(e.type);
break;
}
}
})(jQuery);