1 | function confirmClose() { |
---|
2 | |
---|
3 | if (arguments.length > 0) { |
---|
4 | for (var i = 0; i < arguments.length; i++) { |
---|
5 | this.forms_id.push(arguments[i]); |
---|
6 | } |
---|
7 | } |
---|
8 | }; |
---|
9 | |
---|
10 | confirmClose.prototype = { |
---|
11 | prompt: 'You have unsaved changes.', |
---|
12 | forms_id: new Array(), |
---|
13 | forms: new Array(), |
---|
14 | formSubmit: false, |
---|
15 | |
---|
16 | getCurrentForms: function() { |
---|
17 | // Store current form's element's values |
---|
18 | |
---|
19 | var formsInPage = this.getForms(); |
---|
20 | var f, e; |
---|
21 | var This = this; |
---|
22 | this.forms = new Array(); |
---|
23 | for (var i = 0; i < formsInPage.length; i++) { |
---|
24 | f = formsInPage[i]; |
---|
25 | var tmpForm = new Array(); |
---|
26 | for (var j = 0; j < f.elements.length; j++) { |
---|
27 | e = this.getFormElementValue(f[j]); |
---|
28 | if (e != undefined) { |
---|
29 | tmpForm.push(e); |
---|
30 | } |
---|
31 | } |
---|
32 | this.forms.push(tmpForm); |
---|
33 | |
---|
34 | chainHandler(f, 'onsubmit', function() { |
---|
35 | This.formSubmit = true; |
---|
36 | }); |
---|
37 | } |
---|
38 | }, |
---|
39 | |
---|
40 | compareForms: function() { |
---|
41 | // Compare current form's element's values to their original values |
---|
42 | // Return false if any difference, else true |
---|
43 | |
---|
44 | if (this.forms.length == 0) { |
---|
45 | return true; |
---|
46 | } |
---|
47 | |
---|
48 | var formsInPage = this.getForms(); |
---|
49 | var f, e; |
---|
50 | for (var i = 0; i < formsInPage.length; i++) { |
---|
51 | f = formsInPage[i]; |
---|
52 | var tmpForm = new Array(); |
---|
53 | for (var j = 0; j < f.elements.length; j++) { |
---|
54 | e = this.getFormElementValue(f[j]); |
---|
55 | if (e != undefined) { |
---|
56 | tmpForm.push(e); |
---|
57 | } |
---|
58 | } |
---|
59 | for (var j = 0; j < this.forms[i].length; j++) { |
---|
60 | if (this.forms[i][j] != tmpForm[j]) { |
---|
61 | return false; |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | return true; |
---|
67 | }, |
---|
68 | |
---|
69 | getForms: function() { |
---|
70 | // Get current list of forms as HTMLCollection(s) |
---|
71 | |
---|
72 | if (!document.getElementsByTagName || !document.getElementById) { |
---|
73 | return new Array(); |
---|
74 | } |
---|
75 | |
---|
76 | if (this.forms_id.length > 0) { |
---|
77 | var res = new Array(); |
---|
78 | var f; |
---|
79 | for (var i = 0; i < this.forms_id.length; i++) { |
---|
80 | f = document.getElementById(this.forms_id[i]); |
---|
81 | if (f != undefined) { |
---|
82 | res.push(f); |
---|
83 | } |
---|
84 | } |
---|
85 | return res; |
---|
86 | } else { |
---|
87 | return document.getElementsByTagName('form'); |
---|
88 | } |
---|
89 | |
---|
90 | return new Array(); |
---|
91 | }, |
---|
92 | |
---|
93 | getFormElementValue: function(e) { |
---|
94 | // Return current value of an form element |
---|
95 | |
---|
96 | if (e == undefined) { |
---|
97 | // Unknown object |
---|
98 | return undefined; |
---|
99 | } |
---|
100 | if (e.type != undefined && e.type == 'button') { |
---|
101 | // Ignore button element |
---|
102 | return undefined; |
---|
103 | } |
---|
104 | if (e.classList.contains('meta-helper') || e.classList.contains('checkbox-helper')) { |
---|
105 | // Ignore some application helper element |
---|
106 | return undefined; |
---|
107 | } |
---|
108 | if (e.type != undefined && e.type == 'radio') { |
---|
109 | // Return actual radio button value if selected, else null |
---|
110 | return this.getFormRadioValue(e); |
---|
111 | } else if (e.type != undefined && e.type == 'checkbox') { |
---|
112 | // Return actual checkbox button value if checked, else null |
---|
113 | return this.getFormCheckValue(e); |
---|
114 | } else if (e.type != undefined && e.type == 'password') { |
---|
115 | // Ignore password element |
---|
116 | return null; |
---|
117 | } else if (e.value != undefined) { |
---|
118 | // Return element value if not undefined |
---|
119 | return e.value; |
---|
120 | } else { |
---|
121 | // Every other case, return null |
---|
122 | return null; |
---|
123 | } |
---|
124 | }, |
---|
125 | |
---|
126 | getFormCheckValue: function(e) { |
---|
127 | if (e.checked) { |
---|
128 | return e.value; |
---|
129 | } |
---|
130 | return null; |
---|
131 | }, |
---|
132 | |
---|
133 | getFormRadioValue: function(e) { |
---|
134 | for (var i = 0; i < e.length; i++) { |
---|
135 | if (e[i].checked) { |
---|
136 | return e[i].value; |
---|
137 | } else { |
---|
138 | return null; |
---|
139 | } |
---|
140 | } |
---|
141 | return null; |
---|
142 | } |
---|
143 | }; |
---|
144 | |
---|
145 | var confirmClosePage = new confirmClose(); |
---|
146 | |
---|
147 | chainHandler(window, 'onload', function() { |
---|
148 | confirmClosePage.getCurrentForms(); |
---|
149 | }); |
---|
150 | |
---|
151 | chainHandler(window, 'onbeforeunload', function(event_) { |
---|
152 | if (event_ == undefined && window.event) { |
---|
153 | event_ = window.event; |
---|
154 | } |
---|
155 | |
---|
156 | if (!confirmClosePage.formSubmit && !confirmClosePage.compareForms()) { |
---|
157 | event_.returnValue = confirmClosePage.prompt; |
---|
158 | return confirmClosePage.prompt; |
---|
159 | } |
---|
160 | return false; |
---|
161 | }); |
---|