-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.js
464 lines (432 loc) · 12.6 KB
/
forms.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
/**
* <title>PointedEars' DOM Library: Forms</title>
* @version $Id$
* @requires dom.js
*
* @section Copyright & Disclaimer
*
* @author
* (C) 2004-2011, 2013 Thomas Lahn <js@PointedEars.de>
*
* @partof PointedEars' JavaScript Extensions (JSX)
*
* JSX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JSX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JSX. If not, see <http://www.gnu.org/licenses/>.
*/
if (typeof jsx == "undefined")
{
/**
* @namespace
*/
var jsx = {};
}
if (typeof jsx.dom == "undefined")
{
jsx.dom = {};
}
/**
* Sets the <code>value</code> property of an <code>HTMLInputElement</code>
* object, and its <code>title</code> property accordingly if specified.
*
* @author
* (C) 2004 Thomas Lahn <dhtml.js@PointedEars.de>
* @param {HTMLInputElement} oInput
* Required. Reference to an HTMLInputElement object.
* @param {string} sValue
* New value of the <code>value</code> property of the
* HTMLInputElement object.
* @param {boolean} bSetTitle = false
* Specifies if the <code>title</code> property should be set to
* the same value as the <code>value</code> property. The default
* is <code>false</code>.
* @return {boolean}
* If <var>bSetTitle</var> evaluates to <code>false</code>
* or omitted:
*
* <code>true</code> if the <code>value</code> property could be set
* properly, <code>false</code> otherwise.
*
* If <var>bSetTitle</var> is <code>true</code>:
*
* <code>true</code> if <em>both</em> the <code>value</code> and
* <code>title</code> properties could be set properly,
* <code>false</code> otherwise.
*/
jsx.dom.setValue = function(oInput, sValue, bSetTitle) {
if (oInput && typeof oInput.value != "undefined")
{
oInput.value = sValue;
if (bSetTitle && typeof oInput.title != "undefined")
{
oInput.title = sValue;
return (oInput.value == sValue && oInput.title == sValue);
}
return (oInput.value == sValue);
}
return false;
};
/**
* Retrieves the checked radio button of a radio button group.
*
* @author
* Copyright (C) 2004, 2007 Thomas Lahn <dhtml.js@PointedEars.de>
* @param {HTMLFormElement} oForm
* Required. Reference to an HTMLFormElement object to contain
* the radio button group.
* @param {string} sGroup
* Name of the radio button group from which the
* checked radio button should be retrieved.
* @return {Object|boolean|HTMLInputElement}
* <code>null</code> if <var>oForm</var> is invalid or there is no such
* <var>sGroup</var>;
* <code>false</code> if no radio button of <var>sGroup</var> is checked;
* a reference to the checked radio button otherwise
*/
jsx.dom.getCheckedRadio = function(oForm, sGroup) {
var result = null, e, ig;
if (oForm && (e = oForm.elements) && (ig = e[sGroup]))
{
result = false;
for (var i = ig.length; i--;)
{
if (ig[i].checked)
{
result = ig[i];
break;
}
}
}
return result;
};
/**
* Removes all options from a HTMLSelectElement object.
*
* @author
* (C) 2003 Thomas Lahn <dhtml.js@PointedEars.de>
* @param {HTMLSelectElement} oSelect
* Reference to a HTMLSelectElement object.
* @param {boolean} bAllowReload
* If <code>true</code>, reloads the document.
* @return {boolean}
* <code>true</code> if successful, <code>false</code>
* otherwise.
*/
jsx.dom.removeOptions = function(oSelect, bAllowReload) {
if (oSelect
&& oSelect.tagName
&& oSelect.tagName.toLowerCase() == "select")
{
var o = oSelect.options;
if (o && o.length > 0)
{
/* shortcut if "length" property is not read-only */
o.length = 0;
while (o.length > 0)
{
if (jsx.object.isMethod(o, "remove"))
{
o.remove(o.length - 1);
}
else
{
o[o.length - 1] = null;
if (bAllowReload)
{
history.go(0);
}
}
}
return true;
}
}
return false;
};
/**
* Adds an option to an HTMLSelectElement object.
*
* @author
* (C) 2003, 2012 Thomas Lahn <dhtml.js@PointedEars.de>
* @param {HTMLSelectElement} oSelect
* Required reference to an HTMLSelectElement object.
* @param {string} sText
* Required text of the new HTMLOptionElement object.
* @param {string} sValue (optional)
* Optional value of the new HTMLOptionElement object.
* @return {Option|HTMLOptionElement}
* A reference to the new option if successful,
* <code>null</code> otherwise.
*/
jsx.dom.addOption = function (oSelect, sText, sValue) {
if (!(oSelect
&& oSelect.tagName
&& typeof oSelect.tagName == "string"
&& oSelect.tagName.toLowerCase
&& oSelect.tagName.toLowerCase() == "select"
&& oSelect.options))
{
return null;
}
var oNew = new Option(sText, (arguments.length < 3) ? "" : sValue);
var o = oSelect.options;
o[o.length] = oNew;
return oNew;
};
/**
* Select a radio button depending on its value and, optionally,
* its name.
*
* @author
* (C) 2003 Thomas Lahn <dhtml.js@PointedEars.de>
* @param {HTMLFormElement} oForm
* Reference to the <code>HTMLFormElement</code> object
* which contains the <code>HTMLInputElement</code> object.
* @param aName
* Name of the radio button, i.e. the value of the
* <code>name</code> attribute of the respective
* <code>input</code> (X)HTML element or the value
* of the <code>name</code> property of the respective
* <code>HTMLInputElement</code> object. Use an expression
* that is evaluated to <code>false</code> for the argument
* to be ignored.
* @param {string} sValue
* Value of the radio button, i.e. the value of the
* <code>value</code> attribute of the respective
* <code>input</code> (X)HTML element or the value
* of the <code>value</code> property of the respective
* <code>HTMLInputElement</code> object.
*/
jsx.dom.selectRadioBtn = function(oForm, aName, sValue) {
for (var i = 0; i < oForm.elements.length; i++)
{
var curEl = oForm.elements[i];
if (!curEl.disabled
&& curEl.type == "radio"
&& (!sName || curEl.name == sName)
&& curEl.value == sValue)
{
curEl.checked = true;
}
}
};
/**
* Disables a form element or a collection of form elements.
*
* @author
* (C) 2003, 2010 Thomas Lahn <dhtml.js@PointedEars.de>
* @param {HTMLElement|HTML(Options)Collection} oElementGroup
* Reference to the <code>HTMLElement</code> or to
* the <code>HTML(Options)Collection</code> object.
* @param {Number|String} index (optional)
* Optional number or string to specify
* one element within the collection.
*/
jsx.dom.disableElementGroup = function(oElementGroup, index) {
if (oElementGroup)
{
if (oElementGroup[index]
&& typeof oElementGroup[index].disabled != "undefined")
{
oElementGroup[index].disabled = true;
}
else if (typeof oElementGroup.disabled != "undefined")
{
oElementGroup.disabled = true;
}
}
};
/**
* Disables or enables form controls by name/ID.
*
* @param {HTMLFormElement} oForm
* Reference to the <code>form</code> element object.
* @params String|HTMLElement
* Names/IDs of the elements or references
* to the element objects to disable/enable.
* @param {boolean} bDisable (optional)
* If <code>false</code>, elements will be
* enabled, otherwise disabled.
*/
jsx.dom.disableElements = function(oForm) {
if (oForm)
{
var len = arguments.length - 1, bDisable = arguments[len];
if (bDisable && typeof bDisable == "boolean")
{
len = arguments.length - 2;
}
for (var i = 1; i < len; i++)
{
var a = arguments[i], o;
if (typeof a != "object")
{
o = oForm.elements[a];
}
else
{
o = a;
}
var len2;
if (typeof o.disabled != "undefined")
{
o.disabled = bDisable;
}
else if (typeof (len2 = o.length) != "undefined")
{
for (var j = len2; j--; 0)
{
var o2;
if (typeof (o2 = o[j]).disabled != "undefined")
{
o2.disabled = bDisable;
}
}
}
}
}
};
/**
* @function
*/
jsx.dom.serializeForm = (function() {
var _getFeature = jsx.object.getFeature;
var
rxSubmit = /(^|\s)(submit|image)(\s|$)/i,
rxSelect = /(^|\s)(select(-one)?|undefined)(\s|$)/i,
rxFileReset = /^\s*(file|reset)\s*$/i,
rxObject = /^\s*object\s*$/i;
/**
* @param {HTMLFormElement} form
* @param {HTMLInputElement} submitButton (optional)
* Reference to the submit button that should be successful.
* By default, only the first submit button is successful.
* @return {string}
* The serialization of this form
*/
return function(form, submitButton) {
/**
* @param {HTMLSelectElement|HTMLInputElement|HTMLTextAreaElement|HTMLButtonElement} control
*/
function serializeControl(control)
{
/* HTML 4.01: Controls that are disabled cannot be successful. */
if (control.disabled)
{
return;
}
/*
* If a form contains more than one submit button,
* only the activated submit button is successful.
* (here: the first one or the specified one)
*/
var isSubmit = rxSubmit.test(control.type);
if ((!gotSubmit && !submitButton || control != submitButton) || !isSubmit)
{
if (isSubmit)
{
gotSubmit = true;
}
/*
* For menus, the control name is provided by a SELECT element
* and values are provided by OPTION elements. Only selected
* options may be successful. When no options are selected,
* the control is not successful and neither the name nor any
* values are submitted to the server when the form is submitted.
*/
var m = rxSelect.exec(control.type);
if (m)
{
/* select-one */
if (m[3])
{
if (control.selectedIndex > -1)
{
/*
* MSHTML 6 is buggy with <option>foo</option>;
* always provide a `value' attribute!
*/
items.add(control.name, control.options[control.selectedIndex].value);
}
}
/* select */
else if (m[2])
{
for (var i = 0, opts = control.options, len = opts && opts.length; i < len; i++)
{
var opt = opts[i];
if (opt.selected)
{
/*
* MSHTML 6 is buggy with <option>foo</option>;
* always provide a `value' attribute!
*/
items.add(control.name, opt.value);
}
}
}
}
/*
* All "on" checkboxes may be successful.
* For radio buttons that share the same value of the
* name attribute, only the "on" radio button may be successful.
*/
else if (!rxFileReset.test(control.type)
&& !(rxObject.test(control.tagName) && control.declare)
&& !/^\s*(checkbox|radio)\s*$/i.test(control.type)
|| control.checked)
{
items.add(control.name, control.value);
}
}
}
var es = _getFeature(form, "elements");
if (!es)
{
return "";
}
var items = [];
items.add = function(sName, sValue) {
var s = encodeURIComponent(sName) + "=" + encodeURIComponent(sValue);
this.push(s);
};
if (!jsx.object.isMethod(items, "push"))
{
items.push = function() {
for (var i = 0, len = arguments.length; i < len; i++)
{
this[this.length] = arguments[i];
}
};
}
var gotSubmit = false;
for (var i = 0, len = es.length; i < len; i++)
{
var e = es[i];
/*
* Elements with the same name create a NodeList object,
* however options of select objects are also indexable in Gecko.
*/
if (typeof e[0] != "undefined" && typeof e.options == "undefined")
{
for (var j = 0, len2 = e.length; j < len2; j++)
{
serializeControl(e[j]);
}
}
else
{
serializeControl(e);
}
}
return items.join("&");
};
})();