-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy path_Templated-widgetsInTemplate1.x.html
469 lines (442 loc) · 16.5 KB
/
_Templated-widgetsInTemplate1.x.html
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
465
466
467
468
<html>
<head>
<title>testing widgetsInTemplate support</title>
<style type="text/css">
@import "../themes/claro/document.css";
@import "../themes/claro/claro.css";
</style>
<script type="text/javascript" src="../../dojo/dojo.js"
djConfig="isDebug: true"></script>
<script type="text/javascript">
dojo.require("doh.runner");
dojo.require("dojo.parser");
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.CheckBox");
dojo.require("dijit.ProgressBar");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit._Container");
dojo.require("dijit._Contained");
dojo.require("dijit.layout._LayoutWidget");
dojo.ready(function(){
var testW;
dojo.declare('Test1Widget', [dijit._Widget, dijit._Templated], {
templateString: dojo.byId('Test1Template').textContent || dojo.byId('Test1Template').innerText,
onClick: function(e){
if(e.target){
alert('onClick widgetId='+e.target.id);
}else{
if(e._counter == undefined){
e._counter = 1;
}else{
e._counter++;
}
}
}
});
dojo.declare('Test2Widget', [dijit._Widget, dijit._Templated], {
templateString: dojo.byId('Test2Template').textContent || dojo.byId('Test2Template').innerText
});
dojo.declare('Test4Widget', [dijit._Widget, dijit._Templated], {
templateString: dojo.byId('Test4Template').textContent || dojo.byId('Test4Template').innerText
});
function validateTest4Widget(t, testW){
var selectedTab = dojo.query(".dijitTabChecked", testW.domNode)[0];
var selectedPane = dojo.query(".dijitVisible > .dijitTabPane", testW.domNode)[0];
var tabBox = selectedTab ? dojo.contentBox(selectedTab) : null;
var paneBox = selectedPane ? dojo.contentBox(selectedPane) : null;
t.t(tabBox && tabBox.w > 0 && tabBox.h > 0, "tabBox && tabBox.w > 0 && tabBox.h > 0");
t.t(paneBox && paneBox.w > 0 && paneBox.h > 0, "paneBox && paneBox.w > 0 && paneBox.h");
// Check that everything got started
t.t(testW._started, "testW._started");
t.t(testW.tabCont._started, "tabCont._started");
t.t(testW.tab1._started, "tab1._started");
t.t(testW.tab2._started, "tab2._started");
}
dojo.declare('TestLayoutWidget', dijit.layout._LayoutWidget, {
startup: function(){
if(this._started){
this._doubleStarted = true;
}
this.inherited(arguments);
},
destroy: function(){
if(this._destroyed){
this._doubleDestroyed = true;
}
this.inherited(arguments);
this._destroyed = true;
}
});
dojo.declare('TestCtnrWidget', [dijit._Widget, dijit._Container], {
startup: function(){
if(this._started){
this._doubleStarted = true;
}
this.inherited(arguments);
},
destroy: function(){
if(this._destroyed){
this._doubleDestroyed = true;
}
this.inherited(arguments);
this._destroyed = true;
}
});
dojo.declare('TestCtndWidget', [dijit._Widget, dijit._Contained], {
startup: function(){
if(this._started){
this._doubleStarted = true;
}
this.inherited(arguments);
},
destroy: function(){
if(this._destroyed){
this._doubleDestroyed = true;
}
this.inherited(arguments);
this._destroyed = true;
}
});
dojo.declare('TestNonCtnrWidget', [dijit._Widget, dijit._Templated], {
templateString: "<div dojoAttachPoint=containerNode></div>",
startup: function(){
if(this._started){
this._doubleStarted = true;
}
this.inherited(arguments);
},
destroy: function(){
if(this._destroyed){
this._doubleDestroyed = true;
}
this.inherited(arguments);
this._destroyed = true;
}
});
dojo.declare('TestStubWidget', dijit._Widget, {
startup: function(){
if(this._started){
this._doubleStarted = true;
}
this.inherited(arguments);
},
destroy: function(){
if(this._destroyed){
this._doubleDestroyed = true;
}
this.inherited(arguments);
this._destroyed = true;
}
});
dojo.declare('Test5Widget', [dijit._Widget, dijit._Templated], {
templateString: dojo.byId('Test5Template').textContent || dojo.byId('Test5Template').innerText,
startup: function(){
if(this._started){
this._doubleStarted = true;
}
this.inherited(arguments);
},
destroy: function(){
if(this._destroyed){
this._doubleDestroyed = true;
}
this.inherited(arguments);
this._destroyed = true;
}
});
function getTestWidgets(testW){
return [
testW,
testW.layout,
testW.layChild1,
testW.layChild2,
testW.container,
testW.contained1,
testW.contained2,
testW.nonContainer,
testW.nonContained1,
testW.nonContained2,
testW.threeLevel,
testW.secondLevel,
testW.bottomLevel,
testW.anotherThree,
testW.anotherSecond,
testW.anotherBottom,
testW.stub1
];
}
function validateTest5Widget(t, testW){
// Check that everything got started, but not double-started
dojo.forEach(getTestWidgets(testW), function(w){
t.t(w._started, "w._started: " + w);
t.is(undefined, w._doubleStarted, "w._doubleStarted: " + w);
});
}
function validateTest5WidgetDestroy(t, testW){
var savedWidgets = getTestWidgets(testW);
testW.destroy();
dojo.forEach(savedWidgets, function(w, idx){
t.t(w._destroyed, "w._destroyed: " + w);
t.is(undefined, w._doubleDestroyed, "w._doubleDestroyed: " + w);
});
}
dojo.declare("Missing", [dijit._Widget, dijit._Templated], {
templateString: '<div>' +
'<div dojoType="dijit.layout.ContentPane">' +
'<div dojoType="dijit.form.Button" id="missingButtonId" ' +
'dojoAttachPoint="missingButton">Missing...</div>' +
'</div>' +
'</div>'
});
doh.register("parse", function(){
dojo.parser.parse();
});
doh.register("_Templated-widgetsInTemplate1.x",
[
{
name: "dojoAttachPoint",
runTest: function(t){
var testW = dijit.byId("test1Widget");
t.t(testW.normalNode, "normalNode");
t.f(isNaN(testW.normalNode.nodeType), "normalNode.nodeType");
t.t(testW.buttonWidget instanceof dijit.form.Button, "buttonWidget is Button");
t.t(testW.checkboxWidget instanceof dijit.form.CheckBox, "checkboxWidget is CheckBox");
t.t(testW.progressBarWidget instanceof dijit.ProgressBar, "progressBarWidget is ProgressBar");
testW = dijit.byId("test2Widget");
t.t(testW.containerNode, "containerNode");
t.f(isNaN(testW.containerNode.nodeType), "containerNode nodeType");
t.is(undefined,testW.buttonWidget, "buttonWidget undefined");
t.t(testW.checkboxWidget instanceof dijit.form.CheckBox, "checkboxWidget is still CheckBox");
}
},
{
name: "dojoAttachEvent",
runTest: function(t){
var testW = dijit.byId("test1Widget");
testW.buttonWidget._counter=0;
testW.buttonWidget.onClick(testW.buttonWidget);
testW.checkboxWidget._counter=0;
testW.checkboxWidget.onClick(testW.checkboxWidget);
testW.progressBarWidget._counter=0;
testW.progressBarWidget.onChange(testW.progressBarWidget);
t.is(1,testW.buttonWidget._counter, "buttonWidget._counter");
t.is(1,testW.checkboxWidget._counter, "checkboxWidget._counter");
t.is(1,testW.progressBarWidget._counter, "progressBarWidget._counter");
}
},
{
// Test that getDescendants ()
// finds direct descendants but skips widgetsInTemplates
// and also nested widgets (if direct==true)
name: "destruction",
runTest: function(t){
var testW = dijit.byId("test3Widget");
/*** performance tests
var start = new Date();
for(var i=0; i<1000; i++)
testW.getChildren();
console.log("*** time for getChildren(): " + (new Date()-start));
var start = new Date();
for(var i=0; i<1000; i++)
testW.getDescendants();
console.log("*** time for getDescendants(false): " + (new Date()-start));
***/
var chil = testW.getChildren();
t.is(5, chil.length, "number of direct descendants");
t.is(chil[0].id, "3.1");
t.is(chil[1].id, "3.2");
t.is(chil[2].id, "3.3");
t.is(chil[3].id, "3.4");
t.is(chil[4].id, "3.5");
var desc = testW.getDescendants();
t.is(7, desc.length, "number of descendants (including nested ones)");
t.is(desc[0].id, "3.1");
t.is(desc[1].id, "3.2");
t.is(desc[2].id, "3.3");
t.is(desc[3].id, "3.nested");
t.is(desc[4].id, "3.nested2");
t.is(desc[5].id, "3.4");
t.is(desc[6].id, "3.5");
}
},
{
// Check that declarative widget with layout widgets in template is correctly created and rendered
name: "declarative widget with layout widgets",
runTest: function(t){
validateTest4Widget(t, dijit.byId("test4Widget"));
}
},
{
// Check that programmatic widget with layout widgets in template is correctly created and rendered
name: "programmatic widget with layout widgets",
runTest: function(t){
test4WidgetProgrammatic = new Test4Widget({}).placeAt("test4Widget", "after");
test4WidgetProgrammatic.startup();
validateTest4Widget(t, test4WidgetProgrammatic);
}
},
{
// Compare programmatic and declaratively created widget with layout widgets in template
name: "programmatic vs declarative with layout widgets",
runTest: function(t){
// Focus unrelated element, so that we don't have different classes on our two widgets
dojo.byId("plainInput").focus();
var declW = dijit.byId("test4Widget");
var progW = test4WidgetProgrammatic;
// Check that generated HTML in DOM is same
var declNeutralHtml = declW.domNode.innerHTML.replace(/_\d+/g, "");
var progNeutralHtml = progW.domNode.innerHTML.replace(/_\d+/g, "");
t.is(declNeutralHtml, progNeutralHtml, "progNeutralHtml");
// Check that dimensions are same
var declBox = dojo.contentBox(declW.domNode);
var progBox = dojo.contentBox(progW.domNode);
t.is(declBox.h, progBox.h, "progBox.h");
t.is(declBox.w, progBox.w, "progBox.w");
}
},
{
// Check that declarative widget with other widgets in template is correctly started
name: "declarative widget with many child widgets",
runTest: function(t){
validateTest5Widget(t, dijit.byId("test5Widget"));
}
},
{
// Check that programmatic widget with other widgets in template is correctly started
name: "programmatic widget with many child widgets",
runTest: function(t){
test5WidgetProgrammatic = new Test5Widget().placeAt("test5Widget", "after");
test5WidgetProgrammatic.startup();
validateTest5Widget(t, test5WidgetProgrammatic);
}
},
{
// Check that destroying our declarative widget works correctly
name: "declarative widget destruction",
runTest: function(t){
validateTest5WidgetDestroy(t, dijit.byId("test5Widget"));
}
},
{
// Check that destroying our programmatic widget works correctly
name: "programmatic widget destruction",
runTest: function(t){
validateTest5WidgetDestroy(t, test5WidgetProgrammatic);
}
},
{
// Test that dojoAttachPoint inside of a ContentPane (inside of a template) works
name: "ContentPane",
runTest: function(){
var testW = dijit.byId("missing");
doh.t(testW, "widget was created");
doh.t(testW.missingButton, "dojoAttachPoint created");
doh.is("dijit.form.Button", testW.missingButton.declaredClass, "and it's to a widget");
doh.t(dijit.byId("missingButtonId"), "nested widget also registered by id");
}
}
]
);
doh.run();
});
</script>
</head>
<body class="claro">
<h1>testing widgetsInTemplate support</h1>
<xmp id="Test1Template" style="display:none;">
<div>
<div dojoAttachPoint="normalNode" >normal node</div>
<button dojoAttachPoint="buttonWidget" dojoAttachEvent="onClick:onClick" dojoType="dijit.form.Button">button #1</button>
<div dojoAttachPoint="checkboxWidget" dojoAttachEvent="onClick:onClick" dojoType="dijit.form.CheckBox"></div> checkbox #1
<div dojoAttachPoint="progressBarWidget" dojoAttachEvent="onChange:onClick" style="width:400px"
maximum="200" progress="20" dojoType="dijit.ProgressBar"></div>
</div>
</xmp>
<div dojoType="Test1Widget" id="test1Widget" ></div>
<!-- TODO: this is a strange test; the containerNode shouldn't have any contents as they will be overwritten -->
<xmp id="Test2Template" style="display:none;">
<div>
<div dojoAttachPoint="containerNode" >
<div dojoAttachPoint="checkboxWidget" dojoType="dijit.form.CheckBox"></div>
checkbox #2
</div>
</div>
</xmp>
<div dojoType="Test2Widget" id="test2Widget" ><button dojoAttachPoint="buttonWidget" dojoType="dijit.form.Button">button #2</button></div>
<xmp id="Test3Template" style="display:none;">
<div>
<div dojoAttachPoint="checkboxWidget" dojoType="dijit.form.CheckBox"></div> checkbox #3
<div dojoAttachPoint="containerNode" ></div>
</div>
</xmp>
<script>
dojo.declare('Test3Widget', [dijit._Widget, dijit._Templated], {
templateString: dojo.byId('Test3Template').textContent || dojo.byId('Test3Template').innerText
});
</script>
<div dojoType="Test3Widget" id="test3Widget" >
<span>hello world</span>
<b style="border: 1px solid blue;">this is my
<button dojoType="dijit.form.Button" id="3.1">first button</button>
</b>
<button dojoType="dijit.form.Button" id="3.2">another button</button>
<i>and some more</i>
<div style="border: 1px solid red;">
<div dojoType="dijit.layout.ContentPane" style="border: 1px solid gray;" id="3.3">
<button dojoType="dijit.form.Button" id="3.nested">a nested button</button>
<button dojoType="dijit.form.Button" id="3.nested2">another nested button</button>
</div>
<button dojoType="dijit.form.Button" id="3.4">yet another button</button>
<button dojoType="dijit.form.Button" id="3.5">yet yet another button</button>
</div>
</div>
<!-- Test templated widget containing layout widgets in template -->
<xmp id="Test4Template" style="display:none;">
<div>
<div dojoType="dijit.layout.TabContainer" dojoAttachPoint="tabCont" style="height: 5em;">
<div dojoType="dijit.layout.ContentPane" dojoAttachPoint="tab1" title="Tab 1">
pane 1
</div>
<div dojoType="dijit.layout.ContentPane" dojoAttachPoint="tab2" title="Tab 2">
pane 2
</div>
</div>
</div>
</xmp>
<div dojoType="Test4Widget" id="test4Widget" ></div>
<!-- Test templated widget containing container and nested widgets in template -->
<xmp id="Test5Template" style="display:none;">
<div>
<div dojoType="TestLayoutWidget" dojoAttachPoint="layout">
<div dojoType="TestCtndWidget" dojoAttachPoint="layChild1"></div>
<div dojoType="TestCtndWidget" dojoAttachPoint="layChild2"></div>
</div>
<div dojoType="TestCtnrWidget" dojoAttachPoint="container">
<div dojoType="TestCtndWidget" dojoAttachPoint="contained1"></div>
<div dojoType="TestCtndWidget" dojoAttachPoint="contained2"></div>
</div>
<div dojoType="TestStubWidget" dojoAttachPoint="stub1"></div>
<div dojoType="TestNonCtnrWidget" dojoAttachPoint="nonContainer">
<div dojoType="TestStubWidget" dojoAttachPoint="nonContained1"></div>
<div dojoType="TestStubWidget" dojoAttachPoint="nonContained2"></div>
</div>
<div dojoType="TestCtnrWidget" dojoAttachPoint="threeLevel">
<div dojoType="TestNonCtnrWidget" dojoAttachPoint="secondLevel">
<div dojoType="TestStubWidget" dojoAttachPoint="bottomLevel"></div>
</div>
</div>
<div dojoType="TestNonCtnrWidget" dojoAttachPoint="anotherThree">
<div dojoType="TestCtnrWidget" dojoAttachPoint="anotherSecond">
<div dojoType="TestCtndWidget" dojoAttachPoint="anotherBottom"></div>
</div>
</div>
</div>
</xmp>
<div dojoType="Test5Widget" id="test5Widget" ></div>
<div dojoType="Missing" id="missing"></div>
<input id="plainInput"/>
</body>
</html>