2150 |
mathias |
1 |
if(!dojo._hasResource["dijit.form.Form"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dijit.form.Form"] = true;
|
|
|
3 |
dojo.provide("dijit.form.Form");
|
|
|
4 |
|
|
|
5 |
dojo.require("dijit._Widget");
|
|
|
6 |
dojo.require("dijit._Templated");
|
|
|
7 |
|
|
|
8 |
dojo.declare("dijit.form._FormMixin", null,
|
|
|
9 |
{
|
|
|
10 |
/*
|
|
|
11 |
summary:
|
|
|
12 |
Widget corresponding to <form> tag, for validation and serialization
|
|
|
13 |
|
|
|
14 |
usage:
|
|
|
15 |
<form dojoType="dijit.form.Form" id="myForm">
|
|
|
16 |
Name: <input type="text" name="name" />
|
|
|
17 |
</form>
|
|
|
18 |
myObj={name: "John Doe"};
|
|
|
19 |
dijit.byId('myForm').setValues(myObj);
|
|
|
20 |
|
|
|
21 |
myObj=dijit.byId('myForm').getValues();
|
|
|
22 |
TODO:
|
|
|
23 |
* Repeater
|
|
|
24 |
* better handling for arrays. Often form elements have names with [] like
|
|
|
25 |
* people[3].sex (for a list of people [{name: Bill, sex: M}, ...])
|
|
|
26 |
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
// HTML <FORM> attributes
|
|
|
30 |
|
|
|
31 |
action: "",
|
|
|
32 |
method: "",
|
|
|
33 |
enctype: "",
|
|
|
34 |
name: "",
|
|
|
35 |
"accept-charset": "",
|
|
|
36 |
accept: "",
|
|
|
37 |
target: "",
|
|
|
38 |
|
|
|
39 |
attributeMap: dojo.mixin(dojo.clone(dijit._Widget.prototype.attributeMap),
|
|
|
40 |
{action: "", method: "", enctype: "", "accept-charset": "", accept: "", target: ""}),
|
|
|
41 |
|
|
|
42 |
// execute: Function
|
|
|
43 |
// User defined function to do stuff when the user hits the submit button
|
|
|
44 |
execute: function(/*Object*/ formContents){},
|
|
|
45 |
|
|
|
46 |
// onCancel: Function
|
|
|
47 |
// Callback when user has canceled dialog, to notify container
|
|
|
48 |
// (user shouldn't override)
|
|
|
49 |
onCancel: function(){},
|
|
|
50 |
|
|
|
51 |
// onExecute: Function
|
|
|
52 |
// Callback when user is about to execute dialog, to notify container
|
|
|
53 |
// (user shouldn't override)
|
|
|
54 |
onExecute: function(){},
|
|
|
55 |
|
|
|
56 |
templateString: "<form dojoAttachPoint='containerNode' dojoAttachEvent='onsubmit:_onSubmit' name='${name}' enctype='multipart/form-data'></form>",
|
|
|
57 |
|
|
|
58 |
_onSubmit: function(/*event*/e) {
|
|
|
59 |
// summary: callback when user hits submit button
|
|
|
60 |
dojo.stopEvent(e);
|
|
|
61 |
this.onExecute(); // notify container that we are about to execute
|
|
|
62 |
this.execute(this.getValues());
|
|
|
63 |
},
|
|
|
64 |
|
|
|
65 |
submit: function() {
|
|
|
66 |
// summary: programatically submit form
|
|
|
67 |
this.containerNode.submit();
|
|
|
68 |
},
|
|
|
69 |
|
|
|
70 |
setValues: function(/*object*/obj) {
|
|
|
71 |
// summary: fill in form values from a JSON structure
|
|
|
72 |
|
|
|
73 |
// generate map from name --> [list of widgets with that name]
|
|
|
74 |
var map = {};
|
|
|
75 |
dojo.forEach(this.getDescendants(), function(widget){
|
|
|
76 |
if(!widget.name){ return; }
|
|
|
77 |
var entry = map[widget.name] || (map[widget.name] = [] );
|
|
|
78 |
entry.push(widget);
|
|
|
79 |
});
|
|
|
80 |
|
|
|
81 |
// call setValue() or setChecked() for each widget, according to obj
|
|
|
82 |
for(var name in map){
|
|
|
83 |
var widgets = map[name], // array of widgets w/this name
|
|
|
84 |
values = dojo.getObject(name, false, obj); // list of values for those widgets
|
|
|
85 |
if(!dojo.isArray(values)){
|
|
|
86 |
values = [ values ];
|
|
|
87 |
}
|
|
|
88 |
if(widgets[0].setChecked){
|
|
|
89 |
// for checkbox/radio, values is a list of which widgets should be checked
|
|
|
90 |
dojo.forEach(widgets, function(w, i){
|
|
|
91 |
w.setChecked(dojo.indexOf(values, w.value) != -1);
|
|
|
92 |
});
|
|
|
93 |
}else{
|
|
|
94 |
// otherwise, values is a list of values to be assigned sequentially to each widget
|
|
|
95 |
dojo.forEach(widgets, function(w, i){
|
|
|
96 |
w.setValue(values[i]);
|
|
|
97 |
});
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/***
|
|
|
102 |
* TODO: code for plain input boxes (this shouldn't run for inputs that are part of widgets
|
|
|
103 |
|
|
|
104 |
dojo.forEach(this.containerNode.elements, function(element){
|
|
|
105 |
if (element.name == ''){return}; // like "continue"
|
|
|
106 |
var namePath = element.name.split(".");
|
|
|
107 |
var myObj=obj;
|
|
|
108 |
var name=namePath[namePath.length-1];
|
|
|
109 |
for(var j=1,len2=namePath.length;j<len2;++j) {
|
|
|
110 |
var p=namePath[j - 1];
|
|
|
111 |
// repeater support block
|
|
|
112 |
var nameA=p.split("[");
|
|
|
113 |
if (nameA.length > 1) {
|
|
|
114 |
if(typeof(myObj[nameA[0]]) == "undefined") {
|
|
|
115 |
myObj[nameA[0]]=[ ];
|
|
|
116 |
} // if
|
|
|
117 |
|
|
|
118 |
nameIndex=parseInt(nameA[1]);
|
|
|
119 |
if(typeof(myObj[nameA[0]][nameIndex]) == "undefined") {
|
|
|
120 |
myObj[nameA[0]][nameIndex]={};
|
|
|
121 |
}
|
|
|
122 |
myObj=myObj[nameA[0]][nameIndex];
|
|
|
123 |
continue;
|
|
|
124 |
} // repeater support ends
|
|
|
125 |
|
|
|
126 |
if(typeof(myObj[p]) == "undefined") {
|
|
|
127 |
myObj=undefined;
|
|
|
128 |
break;
|
|
|
129 |
};
|
|
|
130 |
myObj=myObj[p];
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
if (typeof(myObj) == "undefined") {
|
|
|
134 |
return; // like "continue"
|
|
|
135 |
}
|
|
|
136 |
if (typeof(myObj[name]) == "undefined" && this.ignoreNullValues) {
|
|
|
137 |
return; // like "continue"
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
// TODO: widget values (just call setValue() on the widget)
|
|
|
141 |
|
|
|
142 |
switch(element.type) {
|
|
|
143 |
case "checkbox":
|
|
|
144 |
element.checked = (name in myObj) &&
|
|
|
145 |
dojo.some(myObj[name], function(val){ return val==element.value; });
|
|
|
146 |
break;
|
|
|
147 |
case "radio":
|
|
|
148 |
element.checked = (name in myObj) && myObj[name]==element.value;
|
|
|
149 |
break;
|
|
|
150 |
case "select-multiple":
|
|
|
151 |
element.selectedIndex=-1;
|
|
|
152 |
dojo.forEach(element.options, function(option){
|
|
|
153 |
option.selected = dojo.some(myObj[name], function(val){ return option.value == val; });
|
|
|
154 |
});
|
|
|
155 |
break;
|
|
|
156 |
case "select-one":
|
|
|
157 |
element.selectedIndex="0";
|
|
|
158 |
dojo.forEach(element.options, function(option){
|
|
|
159 |
option.selected = option.value == myObj[name];
|
|
|
160 |
});
|
|
|
161 |
break;
|
|
|
162 |
case "hidden":
|
|
|
163 |
case "text":
|
|
|
164 |
case "textarea":
|
|
|
165 |
case "password":
|
|
|
166 |
element.value = myObj[name] || "";
|
|
|
167 |
break;
|
|
|
168 |
}
|
|
|
169 |
});
|
|
|
170 |
*/
|
|
|
171 |
},
|
|
|
172 |
|
|
|
173 |
getValues: function() {
|
|
|
174 |
// summary: generate JSON structure from form values
|
|
|
175 |
|
|
|
176 |
// get widget values
|
|
|
177 |
var obj = {};
|
|
|
178 |
dojo.forEach(this.getDescendants(), function(widget){
|
|
|
179 |
var value = widget.getValue ? widget.getValue() : widget.value;
|
|
|
180 |
var name = widget.name;
|
|
|
181 |
if(!name){ return; }
|
|
|
182 |
|
|
|
183 |
// Store widget's value(s) as a scalar, except for checkboxes which are automatically arrays
|
|
|
184 |
if(widget.setChecked){
|
|
|
185 |
if(/Radio/.test(widget.declaredClass)){
|
|
|
186 |
// radio button
|
|
|
187 |
if(widget.checked){
|
|
|
188 |
dojo.setObject(name, value, obj);
|
|
|
189 |
}
|
|
|
190 |
}else{
|
|
|
191 |
// checkbox/toggle button
|
|
|
192 |
var ary=dojo.getObject(name, false, obj);
|
|
|
193 |
if(!ary){
|
|
|
194 |
ary=[];
|
|
|
195 |
dojo.setObject(name, ary, obj);
|
|
|
196 |
}
|
|
|
197 |
if(widget.checked){
|
|
|
198 |
ary.push(value);
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
}else{
|
|
|
202 |
// plain input
|
|
|
203 |
dojo.setObject(name, value, obj);
|
|
|
204 |
}
|
|
|
205 |
});
|
|
|
206 |
|
|
|
207 |
/***
|
|
|
208 |
* code for plain input boxes (see also dojo.formToObject, can we use that instead of this code?
|
|
|
209 |
* but it doesn't understand [] notation, presumably)
|
|
|
210 |
var obj = { };
|
|
|
211 |
dojo.forEach(this.containerNode.elements, function(elm){
|
|
|
212 |
if (!elm.name) {
|
|
|
213 |
return; // like "continue"
|
|
|
214 |
}
|
|
|
215 |
var namePath = elm.name.split(".");
|
|
|
216 |
var myObj=obj;
|
|
|
217 |
var name=namePath[namePath.length-1];
|
|
|
218 |
for(var j=1,len2=namePath.length;j<len2;++j) {
|
|
|
219 |
var nameIndex = null;
|
|
|
220 |
var p=namePath[j - 1];
|
|
|
221 |
var nameA=p.split("[");
|
|
|
222 |
if (nameA.length > 1) {
|
|
|
223 |
if(typeof(myObj[nameA[0]]) == "undefined") {
|
|
|
224 |
myObj[nameA[0]]=[ ];
|
|
|
225 |
} // if
|
|
|
226 |
nameIndex=parseInt(nameA[1]);
|
|
|
227 |
if(typeof(myObj[nameA[0]][nameIndex]) == "undefined") {
|
|
|
228 |
myObj[nameA[0]][nameIndex]={};
|
|
|
229 |
}
|
|
|
230 |
} else if(typeof(myObj[nameA[0]]) == "undefined") {
|
|
|
231 |
myObj[nameA[0]]={}
|
|
|
232 |
} // if
|
|
|
233 |
|
|
|
234 |
if (nameA.length == 1) {
|
|
|
235 |
myObj=myObj[nameA[0]];
|
|
|
236 |
} else {
|
|
|
237 |
myObj=myObj[nameA[0]][nameIndex];
|
|
|
238 |
} // if
|
|
|
239 |
} // for
|
|
|
240 |
|
|
|
241 |
if ((elm.type != "select-multiple" && elm.type != "checkbox" && elm.type != "radio") || (elm.type=="radio" && elm.checked)) {
|
|
|
242 |
if(name == name.split("[")[0]) {
|
|
|
243 |
myObj[name]=elm.value;
|
|
|
244 |
} else {
|
|
|
245 |
// can not set value when there is no name
|
|
|
246 |
}
|
|
|
247 |
} else if (elm.type == "checkbox" && elm.checked) {
|
|
|
248 |
if(typeof(myObj[name]) == 'undefined') {
|
|
|
249 |
myObj[name]=[ ];
|
|
|
250 |
}
|
|
|
251 |
myObj[name].push(elm.value);
|
|
|
252 |
} else if (elm.type == "select-multiple") {
|
|
|
253 |
if(typeof(myObj[name]) == 'undefined') {
|
|
|
254 |
myObj[name]=[ ];
|
|
|
255 |
}
|
|
|
256 |
for (var jdx=0,len3=elm.options.length; jdx<len3; ++jdx) {
|
|
|
257 |
if (elm.options[jdx].selected) {
|
|
|
258 |
myObj[name].push(elm.options[jdx].value);
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
} // if
|
|
|
262 |
name=undefined;
|
|
|
263 |
}); // forEach
|
|
|
264 |
***/
|
|
|
265 |
return obj;
|
|
|
266 |
},
|
|
|
267 |
|
|
|
268 |
isValid: function() {
|
|
|
269 |
// TODO: ComboBox might need time to process a recently input value. This should be async?
|
|
|
270 |
// make sure that every widget that has a validator function returns true
|
|
|
271 |
return dojo.every(this.getDescendants(), function(widget){
|
|
|
272 |
return !widget.isValid || widget.isValid();
|
|
|
273 |
});
|
|
|
274 |
}
|
|
|
275 |
});
|
|
|
276 |
|
|
|
277 |
dojo.declare(
|
|
|
278 |
"dijit.form.Form",
|
|
|
279 |
[dijit._Widget, dijit._Templated, dijit.form._FormMixin],
|
|
|
280 |
null
|
|
|
281 |
);
|
|
|
282 |
|
|
|
283 |
}
|