-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-form.js
149 lines (135 loc) · 4.55 KB
/
dom-form.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
define(["./_base/lang", "./dom", "./io-query", "./json"], function(lang, dom, ioq, json){
// module:
// dojo/dom-form
function setValue(/*Object*/ obj, /*String*/ name, /*String*/ value){
// summary:
// For the named property in object, set the value. If a value
// already exists and it is a string, convert the value to be an
// array of values.
// Skip it if there is no value
if(value === null){
return;
}
var val = obj[name];
if(typeof val == "string"){ // inline'd type check
obj[name] = [val, value];
}else if(lang.isArray(val)){
val.push(value);
}else{
obj[name] = value;
}
}
var exclude = "file|submit|image|reset|button";
var form = {
// summary:
// This module defines form-processing functions.
fieldToObject: function fieldToObject(/*DOMNode|String*/ inputNode){
// summary:
// Serialize a form field to a JavaScript object.
// description:
// Returns the value encoded in a form field as
// as a string or an array of strings. Disabled form elements
// and unchecked radio and checkboxes are skipped. Multi-select
// elements are returned as an array of string values.
// inputNode: DOMNode|String
// returns: Object
var ret = null;
inputNode = dom.byId(inputNode);
if(inputNode){
var _in = inputNode.name, type = (inputNode.type || "").toLowerCase();
if(_in && type && !inputNode.disabled){
if(type == "radio" || type == "checkbox"){
if(inputNode.checked){
ret = inputNode.value;
}
}else if(inputNode.multiple){
ret = [];
var nodes = [inputNode.firstChild];
while(nodes.length){
for(var node = nodes.pop(); node; node = node.nextSibling){
if(node.nodeType == 1 && node.tagName.toLowerCase() == "option"){
if(node.selected){
ret.push(node.value);
}
}else{
if(node.nextSibling){
nodes.push(node.nextSibling);
}
if(node.firstChild){
nodes.push(node.firstChild);
}
break;
}
}
}
}else{
ret = inputNode.value;
}
}
}
return ret; // Object
},
toObject: function formToObject(/*DOMNode|String*/ formNode){
// summary:
// Serialize a form node to a JavaScript object.
// description:
// Returns the values encoded in an HTML form as
// string properties in an object which it then returns. Disabled form
// elements, buttons, and other non-value form elements are skipped.
// Multi-select elements are returned as an array of string values.
// formNode: DOMNode|String
// example:
// This form:
// | <form id="test_form">
// | <input type="text" name="blah" value="blah">
// | <input type="text" name="no_value" value="blah" disabled>
// | <input type="button" name="no_value2" value="blah">
// | <select type="select" multiple name="multi" size="5">
// | <option value="blah">blah</option>
// | <option value="thud" selected>thud</option>
// | <option value="thonk" selected>thonk</option>
// | </select>
// | </form>
//
// yields this object structure as the result of a call to
// formToObject():
//
// | {
// | blah: "blah",
// | multi: [
// | "thud",
// | "thonk"
// | ]
// | };
var ret = {}, elems = dom.byId(formNode).elements;
for(var i = 0, l = elems.length; i < l; ++i){
var item = elems[i], _in = item.name, type = (item.type || "").toLowerCase();
if(_in && type && exclude.indexOf(type) < 0 && !item.disabled){
setValue(ret, _in, form.fieldToObject(item));
if(type == "image"){
ret[_in + ".x"] = ret[_in + ".y"] = ret[_in].x = ret[_in].y = 0;
}
}
}
return ret; // Object
},
toQuery: function formToQuery(/*DOMNode|String*/ formNode){
// summary:
// Returns a URL-encoded string representing the form passed as either a
// node or string ID identifying the form to serialize
// formNode: DOMNode|String
// returns: String
return ioq.objectToQuery(form.toObject(formNode)); // String
},
toJson: function formToJson(/*DOMNode|String*/ formNode, /*Boolean?*/ prettyPrint){
// summary:
// Create a serialized JSON string from a form node or string
// ID identifying the form to serialize
// formNode: DOMNode|String
// prettyPrint: Boolean?
// returns: String
return json.stringify(form.toObject(formNode), null, prettyPrint ? 4 : 0); // String
}
};
return form;
});