Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.widget.Form");
14
dojo.require("dojo.widget.*");
15
dojo.require("dojo.widget.HtmlWidget");
16
dojo.widget.defineWidget("dojo.widget.Form", dojo.widget.HtmlWidget, {isContainer:true, templateString:"<form dojoAttachPoint='containerNode' dojoAttachEvent='onSubmit:onSubmit'></form>", formElements:[], ignoreNullValues:false, postCreate:function (args, frag) {
17
	for (var key in args) {
18
		if (key == "dojotype") {
19
			continue;
20
		}
21
		var attr = document.createAttribute(key);
22
		attr.nodeValue = args[key];
23
		this.containerNode.setAttributeNode(attr);
24
	}
25
}, _createRepeaters:function (obj, widget) {
26
	for (var i = 0; i < widget.children.length; ++i) {
27
		if (widget.children[i].widgetType == "RepeaterContainer") {
28
			var rIndex = widget.children[i].index;
29
			var rIndexPos = rIndex.indexOf("%{index}");
30
			rIndex = rIndex.substr(0, rIndexPos - 1);
31
			var myObj = this._getObject(obj, rIndex);
32
			if (typeof (myObj) == "object" && myObj.length == 0) {
33
				myObj = new Array();
34
			}
35
			var rowCount = widget.children[i].getRowCount();
36
			for (var j = 0, len = rowCount; j < len; ++j) {
37
				widget.children[i].deleteRow(0);
38
			}
39
			for (var j = 0; j < myObj.length; j++) {
40
				widget.children[i].addRow(false);
41
			}
42
		}
43
		if (widget.children[i].isContainer) {
44
			this._createRepeaters(obj, widget.children[i]);
45
		}
46
	}
47
}, _createFormElements:function () {
48
	if (dojo.render.html.safari) {
49
		this.formElements = [];
50
		var elems = ["INPUT", "SELECT", "TEXTAREA"];
51
		for (var k = 0; k < elems.length; k++) {
52
			var list = this.containerNode.getElementsByTagName(elems[k]);
53
			for (var j = 0, len2 = list.length; j < len2; j++) {
54
				this.formElements.push(list[j]);
55
			}
56
		}
57
	} else {
58
		this.formElements = this.containerNode.elements;
59
	}
60
}, onSubmit:function (e) {
61
	e.preventDefault();
62
}, submit:function () {
63
	this.containerNode.submit();
64
}, _getFormElement:function (name) {
65
	if (dojo.render.html.ie) {
66
		for (var i = 0, len = this.formElements.length; i < len; i++) {
67
			var element = this.formElements[i];
68
			if (element.name == name) {
69
				return element;
70
			}
71
		}
72
	} else {
73
		var elem = this.formElements[name];
74
		if (typeof (elem) != "undefined") {
75
			return elem;
76
		}
77
	}
78
	return null;
79
}, _getObject:function (obj, searchString) {
80
	var namePath = [];
81
	namePath = searchString.split(".");
82
	var myObj = obj;
83
	var name = namePath[namePath.length - 1];
84
	for (var j = 0, len = namePath.length; j < len; ++j) {
85
		var p = namePath[j];
86
		if (typeof (myObj[p]) == "undefined") {
87
			myObj[p] = {};
88
		}
89
		myObj = myObj[p];
90
	}
91
	return myObj;
92
}, _setToContainers:function (obj, widget) {
93
	for (var i = 0, len = widget.children.length; i < len; ++i) {
94
		var currentWidget = widget.children[i];
95
		if (currentWidget.widgetType == "Repeater") {
96
			for (var j = 0, len = currentWidget.getRowCount(); j < len; ++j) {
97
				currentWidget._initRow(j);
98
			}
99
		}
100
		if (currentWidget.isContainer) {
101
			this._setToContainers(obj, currentWidget);
102
			continue;
103
		}
104
		switch (currentWidget.widgetType) {
105
		  case "Checkbox":
106
			currentWidget.setValue(currentWidget.inputNode.checked);
107
			break;
108
		  case "DropdownDatePicker":
109
			currentWidget.setValue(currentWidget.getValue());
110
			break;
111
		  case "Select":
112
			continue;
113
			break;
114
		  case "ComboBox":
115
			continue;
116
			break;
117
		  default:
118
			break;
119
		}
120
	}
121
}, setValues:function (obj) {
122
	this._createFormElements();
123
	this._createRepeaters(obj, this);
124
	for (var i = 0, len = this.formElements.length; i < len; i++) {
125
		var element = this.formElements[i];
126
		if (element.name == "") {
127
			continue;
128
		}
129
		var namePath = new Array();
130
		namePath = element.name.split(".");
131
		var myObj = obj;
132
		var name = namePath[namePath.length - 1];
133
		for (var j = 1, len2 = namePath.length; j < len2; ++j) {
134
			var p = namePath[j - 1];
135
			if (typeof (myObj[p]) == "undefined") {
136
				myObj = undefined;
137
				break;
138
			}
139
			myObj = myObj[p];
140
		}
141
		if (typeof (myObj) == "undefined") {
142
			continue;
143
		}
144
		if (typeof (myObj[name]) == "undefined" && this.ignoreNullValues) {
145
			continue;
146
		}
147
		var type = element.type;
148
		if (type == "hidden" || type == "text" || type == "textarea" || type == "password") {
149
			type = "text";
150
		}
151
		switch (type) {
152
		  case "checkbox":
153
			element.checked = false;
154
			if (typeof (myObj[name]) == "undefined") {
155
				continue;
156
			}
157
			for (var j = 0, len2 = myObj[name].length; j < len2; ++j) {
158
				if (element.value == myObj[name][j]) {
159
					element.checked = true;
160
				}
161
			}
162
			break;
163
		  case "radio":
164
			element.checked = false;
165
			if (typeof (myObj[name]) == "undefined") {
166
				continue;
167
			}
168
			if (myObj[name] == element.value) {
169
				element.checked = true;
170
			}
171
			break;
172
		  case "select-multiple":
173
			element.selectedIndex = -1;
174
			for (var j = 0, len2 = element.options.length; j < len2; ++j) {
175
				for (var k = 0, len3 = myObj[name].length; k < len3; ++k) {
176
					if (element.options[j].value == myObj[name][k]) {
177
						element.options[j].selected = true;
178
					}
179
				}
180
			}
181
			break;
182
		  case "select-one":
183
			element.selectedIndex = "0";
184
			for (var j = 0, len2 = element.options.length; j < len2; ++j) {
185
				if (element.options[j].value == myObj[name]) {
186
					element.options[j].selected = true;
187
				} else {
188
				}
189
			}
190
			break;
191
		  case "text":
192
			var value = "";
193
			if (typeof (myObj[name]) != "undefined") {
194
				value = myObj[name];
195
			}
196
			element.value = value;
197
			break;
198
		  default:
199
			dojo.debug("Not supported type (" + type + ")");
200
			break;
201
		}
202
	}
203
	this._setToContainers(obj, this);
204
}, getValues:function () {
205
	this._createFormElements();
206
	var obj = {};
207
	for (var i = 0, len = this.formElements.length; i < len; i++) {
208
		var elm = this.formElements[i];
209
		var namePath = [];
210
		if (elm.name == "") {
211
			continue;
212
		}
213
		namePath = elm.name.split(".");
214
		var myObj = obj;
215
		var name = namePath[namePath.length - 1];
216
		for (var j = 1, len2 = namePath.length; j < len2; ++j) {
217
			var nameIndex = null;
218
			var p = namePath[j - 1];
219
			var nameA = p.split("[");
220
			if (nameA.length > 1) {
221
				if (typeof (myObj[nameA[0]]) == "undefined") {
222
					myObj[nameA[0]] = [];
223
				}
224
				nameIndex = parseInt(nameA[1]);
225
				if (typeof (myObj[nameA[0]][nameIndex]) == "undefined") {
226
					myObj[nameA[0]][nameIndex] = {};
227
				}
228
			} else {
229
				if (typeof (myObj[nameA[0]]) == "undefined") {
230
					myObj[nameA[0]] = {};
231
				}
232
			}
233
			if (nameA.length == 1) {
234
				myObj = myObj[nameA[0]];
235
			} else {
236
				myObj = myObj[nameA[0]][nameIndex];
237
			}
238
		}
239
		if ((elm.type != "select-multiple" && elm.type != "checkbox" && elm.type != "radio") || (elm.type == "radio" && elm.checked)) {
240
			if (name == name.split("[")[0]) {
241
				myObj[name] = elm.value;
242
			} else {
243
			}
244
		} else {
245
			if (elm.type == "checkbox" && elm.checked) {
246
				if (typeof (myObj[name]) == "undefined") {
247
					myObj[name] = [];
248
				}
249
				myObj[name].push(elm.value);
250
			} else {
251
				if (elm.type == "select-multiple") {
252
					if (typeof (myObj[name]) == "undefined") {
253
						myObj[name] = [];
254
					}
255
					for (var jdx = 0, len3 = elm.options.length; jdx < len3; ++jdx) {
256
						if (elm.options[jdx].selected) {
257
							myObj[name].push(elm.options[jdx].value);
258
						}
259
					}
260
				}
261
			}
262
		}
263
		name = undefined;
264
	}
265
	return obj;
266
}});
267