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