summaryrefslogtreecommitdiff
path: root/includes/js/dijit/tests/form/Form.html
blob: 312399a68a9250243df26ac979ff0fcc73ca0a3b (plain)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

	<title>Form unit test</title>
	<style type="text/css">
		@import "../../../dojo/resources/dojo.css";
		@import "../css/dijitTests.css";
	</style>
	<script type="text/javascript" src="../../../dojo/dojo.js"
		djConfig="isDebug: true, parseOnLoad: true"></script>
	<script type="text/javascript" src="../_testCommon.js"></script>

	<script type="text/javascript">
		dojo.require("doh.runner");
		dojo.require("dojo.date");
		dojo.require("dijit.form.Form");
		dojo.require("dijit.layout.LayoutContainer");
		dojo.require("dijit.layout.ContentPane");
		dojo.require("dijit.form.ComboBox");
		dojo.require("dijit.form.CheckBox");
		dojo.require("dijit.form.DateTextBox");
		dojo.require("dijit.form.Button");
		dojo.require("dijit.form.MultiSelect");
		dojo.require("dijit.form.Textarea");
		dojo.require("dijit.form.SimpleTextarea");
		dojo.require("dijit.Editor");

		var obj;
		function getValues(){
			obj = dijit.byId('myForm').getValues();
			console.log("Object is: " + dojo.toJson(obj, true));
		}
		
		function setValues(){
			if(!obj){
		 		obj = {testF: 'testi'};
			}
			console.log("Object is: " + dojo.toJson(obj, true));
			dijit.byId('myForm').setValues(obj);
		}

		// make dojo.toJson() print dates correctly (this feels a bit dirty)
		Date.prototype.json = function(){ return dojo.date.stamp.toISOString(this, {selector: 'date'});};

		var d = dojo.date.stamp.fromISOString;

		// These are the values assigned to the widgets in the page's HTML
		var original =	{
							foo: {bar: {baz: {quux: d("2007-12-30")} } },
							available: {from: d("2005-01-02"), to: d("2006-01-02")},
							plop: {combo: "one"},
							cb2: ["2", "3"],
							r2: "2",
							ms1: ["VA", "WA"],
							h1: "hidden",
							t1: "line 1\nline 2",
							st1: "simple line 1\nsimple line 2",
							richtext: "<h1>original</h1><p>This is the default content</p>",
							filename: ""
						};

		// we change the form to these values
		var changed =	{
							foo: {bar: {baz: {quux: d("2005-01-01")} } },
							available: {from: d("2005-11-02"), to: d("2006-11-02")},
							plop: {combo: "three"},
							cb2: ["4"],
							r2: "1",
							ms1: ["FL", "CA"],
							h1: "still hidden",
							t1: "new line 1\nnew line 2",
							st1: "new simple line 1\nnew simple line 2",
							richtext: "<h1>changed</h1><p>This is the changed content set by setValues</p>",
							filename: ""
						};
		// we reset the form to these values
		var reset =	{
							foo: {bar: {baz: {quux: d("2007-12-30")} } },
							available: {from: d("2005-01-02"), to: d("2006-01-02")},
							plop: {combo: "one"},
							cb2: ["2", "3"],
							r2: "2",
							ms1: ["VA", "WA"],
							h1: "hidden",
							t1: "line 1\nline 2",
							st1: "simple line 1\nsimple line 2",
							richtext: "<h1>changed</h1><p>This is the changed content set by setValues</p>", // not a form element, so not reset
							filename: ""
						};

		dojo.addOnLoad(function(){
			doh.register("dijit.form.Form",
				[
					function getValues(){
						var values = dijit.byId("myForm").getValues();
						
						// FF3 sticks in some tabs and newlines that mess up the equality check
						// Need better way to compare two HTML trees but for now do this.
						values.richtext = values.richtext.replace(/[\n\t]/, "", "g");

						doh.is( dojo.toJson(original), dojo.toJson(values) );
					},
					function setValues(){
						dijit.byId("myForm").setValues(changed);
						doh.is( dojo.toJson(changed), dojo.toJson(dijit.byId("myForm").getValues()) );
					},
					function nameAttributeSurvived(){  // ticket:4753
						var radios = dojo.query(".RadioButton", dijit.byId("radio-cells")).forEach(
							function(r) {
								doh.is( r.inputNode.name, "r2" );
							});
						
					},
					function resetTest(){
						dijit.byId("myForm").reset();
						doh.is( dojo.toJson(reset), dojo.toJson(dijit.byId("myForm").getValues()) );
					}
				]
			);
			doh.run();
		});

	</script>
</head>
<body>
	<h1>Form Widget Unit Test</h1>
	<p>
		The form widget takes data in a form and serializes/deserializes it, so
		it can be submitted as a JSON string of nested objects.
	</p>
	<div style="color:red">Currently only widgets are supported, not raw elements.</div>
	<!--    to test form submission, you'll need to create an action handler similar to
			http://www.utexas.edu/teamweb/cgi-bin/generic.cgi 
		http://www.tipjar.com/cgi-bin/test -->
	<form dojoType="dijit.form.Form" id="myForm" 
		encType="multipart/form-data" action="" method="">
		<script type="dojo/method" event="onReset">
			return confirm('Press OK to reset widget values');
		</script>
		<script type="dojo/method" event="onSubmit">
			console.debug('Attempting to submit form w/values:\n',
				dojo.toJson(this.getValues(),true)
			);
			if(this.validate()){
				return confirm('Form is valid, press OK to submit');
			}else{
				alert('Form contains invalid data.  Please correct first');
				return false;
			}
			return true;
		</script>
		<script type="dojo/method" event="onReset">
			return confirm('reset Form?');
		</script>
		<p>Just HTML text</p>
		<table style="border: 1px solid #9f9f9f;" cellspacing="10">
			<thead>
				<tr>
					<th>Description</th>
					<th>Name</th>
					<th>Form node/widget</th>
				</tr>
			</thead>
			<tbody>
				<!--
				<tr><td>text</td><td>testF</td><td><input type="text" name="testF" value="bar1" /></td></tr>
				<tr><td>password</td><td>passwordF</td><td><input type="password" name="passwordF" value="bar4" /></td></tr>
				<tr><td>hidden</td><td>hiddenF</td><td><input type="hidden" name="hiddenF" value="bar4" /></td></tr>
				<tr><td>select</td><td>plop.noncombo</td><td>
				<div class="group">
					<select name="plop.noncombo">
					 <option value="1">one</option>
					 <option value="2">two</option>
					 <option value="3">three</option>
					</select>
				</div>

				</td></tr>
				-->

				<tr>
					<td>DateTextBox inside contentpane</td>
					<td>foo.bar.baz.quux</td>
					<td>
						<div dojoType="dijit.layout.ContentPane">
						<input type="text" name="foo.bar.baz.quux" dojoType="dijit.form.DateTextBox" value="2007-12-30" />
						</div>
					</td>
				</tr>
				<tr>
					<td>Layoutcontainer</td>
					<td>
						<div dojoType="dijit.layout.LayoutContainer"></div>
					</td>
				</tr>
				<tr>
					<td>DateTextBox 1</td>
					<td>available.from</td>
					<td>
						<input type="text" name="available.from" dojoType="dijit.form.DateTextBox" value="2005-01-02" />
					</td>
				</tr>
				<tr>
					<td>DateTextBox 2</td>
					<td>available.to</td>
					<td>
						<input type="text" name="available.to" dojoType="dijit.form.DateTextBox" value="2006-01-02" />
					</td>
				</tr>
				<tr>
					<td>ComboBox</td>
					<td>plop.combo</td>
					<td>
						<select name="plop.combo" dojoType="dijit.form.ComboBox">
							<option value="one">one</option>
							<option value="two">two</option>
							<option value="three">three</option>
						</select>
					</td>
				</tr>

				<!--
				<tr>
					<td>textarea</td>
					<td>myTextArea</td>
					<td>
						<textarea name="myTextArea">
							text text text """ \\\/
						</textarea>
					</td>
				</tr>
				-->

				<!--
				<tr>
					<td>CheckBox</td>
					<td>cb1</td>
					<td>
						<input type="checkbox" name="cb1" value="1" /> 1
						<input type="checkbox" name="cb1" value="2" checked="checked" /> 2
						<input type="checkbox" name="cb1" value="3" checked="checked" /> 3
						<input type="checkbox" name="cb1" value="4" /> 4
					</td>
				</tr>
				-->

				<tr>
					<td>CheckBox widget</td>
					<td>cb2</td>
					<td>
						<input dojoType="dijit.form.CheckBox" type="checkbox" name="cb2" value="1" /> 1
						<input dojoType="dijit.form.CheckBox" type="checkbox" name="cb2" value="2" checked="checked" /> 2
						<input dojoType="dijit.form.CheckBox" type="checkbox" name="cb2" value="3" checked="checked" /> 3
						<input dojoType="dijit.form.CheckBox" type="checkbox" name="cb2" value="4" /> 4
					</td>
				</tr>

				<!--
				<tr>
					<td>radio</td>
					<td>r1</td>
					<td>
						<input type="radio" name="r1" value="1" /> 1
						<input type="radio" name="r1" value="2" /> 2
						<input type="radio" name="r1" value="3" /> 3
						<input type="radio" name="r1" value="4" /> 4
					</td>
				</tr>
				-->

				<tr>
				<td>Radio widget</td><td>r2</td>
				<td id="radio-cells">
				<input dojoType="dijit.form.RadioButton" type="radio" name="r2" value="1" /> 1
				<input dojoType="dijit.form.RadioButton" type="radio" name="r2" value="2" checked="checked" /> 2
				<input dojoType="dijit.form.RadioButton" type="radio" name="r2" value="3"/> 3
				<input dojoType="dijit.form.RadioButton" type="radio" name="r2" value="4" /> 4
				</td>
				</tr>

				<tr>
					<td>Multi-select</td><td>ms1</td>
					<td>
						<select id="ms1" multiple="true" name="ms1"
							dojoType="dijit.form.MultiSelect"
							style="height:100px; width:175px; border:5px solid #ededed;">

							<option value="TN">Tennessee</option>
							<option value="VA" selected="true">Virginia</option>
							<option value="WA" selected="true">Washington</option>
							<option value="FL">Florida</option>
							<option value="CA">California</option>

						</select>
					</td>
				</tr>

				<tr>
					<td>Hidden input</td>
					<td>h1</td>
					<td>
						<input id="h1" name="h1" dojoType="dijit.form.TextBox" type="hidden" value="hidden">
					</td>
				</tr>

				<tr>
					<td>Auto-sizing textarea</td>
					<td>t1</td>
					<td>
						<textarea id="t1" name="t1" 
	dojoType="dijit.form.Textarea">line 1
line 2</textarea>
					</td>
				</tr>

				<tr>
					<td>Fixed size textarea</td>
					<td>st1</td>
					<td>
						<textarea id="st1" name="st1" dojoType="dijit.form.SimpleTextarea" rows=5 cols=50>
simple line 1
simple line 2</textarea>
					</td>
				</tr>

				<tr>
					<td>Editor widget</td>
					<td>richtext</td>
					<td>
						<textarea dojoType="dijit.Editor" name="richtext" pluginsConfig="[{items:['bold','italic']}]"><h1>original</h1><p>This is the default content</p></textarea>
					</td>
				</tr>

				<tr>
					<td>File upload</td>
					<td>filename</td>
					<td>
						<input dojoType="dijit.form.TextBox" name="filename" type="file">
					</td>
				</tr>
			</tbody>
		</table>

		<button dojoType=dijit.form.Button onClick="getValues();">Get Values from form!</button>
		<button dojoType=dijit.form.Button onClick="setValues();">Set Values to form!</button>
		<button dojoType=dijit.form.Button type="submit">Submit</button>
		<button dojoType=dijit.form.Button type="reset">Reset</button>
	</form>


</body>
</html>