| 1 |
<html> |
| 2 |
<head> |
| 3 |
<title>testing widgetsInTemplate support</title> |
| 4 |
<style type="text/css"> |
| 5 |
@import "../themes/claro/document.css"; |
| 6 |
@import "../themes/claro/claro.css"; |
| 7 |
</style> |
| 8 |
<script type="text/javascript" src="../../dojo/dojo.js" |
| 9 |
djConfig="isDebug: true"></script> |
| 10 |
<script type="text/javascript"> |
| 11 |
dojo.require("doh.runner"); |
| 12 |
|
| 13 |
dojo.require("dojo.parser"); |
| 14 |
|
| 15 |
dojo.require("dijit._Widget"); |
| 16 |
dojo.require("dijit._Templated"); |
| 17 |
dojo.require("dijit.form.Button"); |
| 18 |
dojo.require("dijit.form.CheckBox"); |
| 19 |
dojo.require("dijit.ProgressBar"); |
| 20 |
dojo.require("dijit.layout.ContentPane"); |
| 21 |
dojo.require("dijit.layout.TabContainer"); |
| 22 |
dojo.require("dijit._Container"); |
| 23 |
dojo.require("dijit._Contained"); |
| 24 |
dojo.require("dijit.layout._LayoutWidget"); |
| 25 |
|
| 26 |
dojo.ready(function(){ |
| 27 |
var testW; |
| 28 |
|
| 29 |
dojo.declare('Test1Widget', [dijit._Widget, dijit._Templated], { |
| 30 |
templateString: dojo.byId('Test1Template').textContent || dojo.byId('Test1Template').innerText, |
| 31 |
onClick: function(e){ |
| 32 |
if(e.target){ |
| 33 |
alert('onClick widgetId='+e.target.id); |
| 34 |
}else{ |
| 35 |
if(e._counter == undefined){ |
| 36 |
e._counter = 1; |
| 37 |
}else{ |
| 38 |
e._counter++; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
}); |
| 43 |
|
| 44 |
dojo.declare('Test2Widget', [dijit._Widget, dijit._Templated], { |
| 45 |
templateString: dojo.byId('Test2Template').textContent || dojo.byId('Test2Template').innerText |
| 46 |
}); |
| 47 |
|
| 48 |
dojo.declare('Test4Widget', [dijit._Widget, dijit._Templated], { |
| 49 |
templateString: dojo.byId('Test4Template').textContent || dojo.byId('Test4Template').innerText |
| 50 |
}); |
| 51 |
|
| 52 |
function validateTest4Widget(t, testW){ |
| 53 |
var selectedTab = dojo.query(".dijitTabChecked", testW.domNode)[0]; |
| 54 |
var selectedPane = dojo.query(".dijitVisible > .dijitTabPane", testW.domNode)[0]; |
| 55 |
var tabBox = selectedTab ? dojo.contentBox(selectedTab) : null; |
| 56 |
var paneBox = selectedPane ? dojo.contentBox(selectedPane) : null; |
| 57 |
t.t(tabBox && tabBox.w > 0 && tabBox.h > 0, "tabBox && tabBox.w > 0 && tabBox.h > 0"); |
| 58 |
t.t(paneBox && paneBox.w > 0 && paneBox.h > 0, "paneBox && paneBox.w > 0 && paneBox.h"); |
| 59 |
// Check that everything got started |
| 60 |
t.t(testW._started, "testW._started"); |
| 61 |
t.t(testW.tabCont._started, "tabCont._started"); |
| 62 |
t.t(testW.tab1._started, "tab1._started"); |
| 63 |
t.t(testW.tab2._started, "tab2._started"); |
| 64 |
} |
| 65 |
|
| 66 |
dojo.declare('TestLayoutWidget', dijit.layout._LayoutWidget, { |
| 67 |
startup: function(){ |
| 68 |
if(this._started){ |
| 69 |
this._doubleStarted = true; |
| 70 |
} |
| 71 |
this.inherited(arguments); |
| 72 |
}, |
| 73 |
destroy: function(){ |
| 74 |
if(this._destroyed){ |
| 75 |
this._doubleDestroyed = true; |
| 76 |
} |
| 77 |
this.inherited(arguments); |
| 78 |
this._destroyed = true; |
| 79 |
} |
| 80 |
}); |
| 81 |
dojo.declare('TestCtnrWidget', [dijit._Widget, dijit._Container], { |
| 82 |
startup: function(){ |
| 83 |
if(this._started){ |
| 84 |
this._doubleStarted = true; |
| 85 |
} |
| 86 |
this.inherited(arguments); |
| 87 |
}, |
| 88 |
destroy: function(){ |
| 89 |
if(this._destroyed){ |
| 90 |
this._doubleDestroyed = true; |
| 91 |
} |
| 92 |
this.inherited(arguments); |
| 93 |
this._destroyed = true; |
| 94 |
} |
| 95 |
}); |
| 96 |
dojo.declare('TestCtndWidget', [dijit._Widget, dijit._Contained], { |
| 97 |
startup: function(){ |
| 98 |
if(this._started){ |
| 99 |
this._doubleStarted = true; |
| 100 |
} |
| 101 |
this.inherited(arguments); |
| 102 |
}, |
| 103 |
destroy: function(){ |
| 104 |
if(this._destroyed){ |
| 105 |
this._doubleDestroyed = true; |
| 106 |
} |
| 107 |
this.inherited(arguments); |
| 108 |
this._destroyed = true; |
| 109 |
} |
| 110 |
}); |
| 111 |
dojo.declare('TestNonCtnrWidget', [dijit._Widget, dijit._Templated], { |
| 112 |
templateString: "<div dojoAttachPoint=containerNode></div>", |
| 113 |
startup: function(){ |
| 114 |
if(this._started){ |
| 115 |
this._doubleStarted = true; |
| 116 |
} |
| 117 |
this.inherited(arguments); |
| 118 |
}, |
| 119 |
destroy: function(){ |
| 120 |
if(this._destroyed){ |
| 121 |
this._doubleDestroyed = true; |
| 122 |
} |
| 123 |
this.inherited(arguments); |
| 124 |
this._destroyed = true; |
| 125 |
} |
| 126 |
}); |
| 127 |
dojo.declare('TestStubWidget', dijit._Widget, { |
| 128 |
startup: function(){ |
| 129 |
if(this._started){ |
| 130 |
this._doubleStarted = true; |
| 131 |
} |
| 132 |
this.inherited(arguments); |
| 133 |
}, |
| 134 |
destroy: function(){ |
| 135 |
if(this._destroyed){ |
| 136 |
this._doubleDestroyed = true; |
| 137 |
} |
| 138 |
this.inherited(arguments); |
| 139 |
this._destroyed = true; |
| 140 |
} |
| 141 |
}); |
| 142 |
|
| 143 |
dojo.declare('Test5Widget', [dijit._Widget, dijit._Templated], { |
| 144 |
templateString: dojo.byId('Test5Template').textContent || dojo.byId('Test5Template').innerText, |
| 145 |
startup: function(){ |
| 146 |
if(this._started){ |
| 147 |
this._doubleStarted = true; |
| 148 |
} |
| 149 |
this.inherited(arguments); |
| 150 |
}, |
| 151 |
destroy: function(){ |
| 152 |
if(this._destroyed){ |
| 153 |
this._doubleDestroyed = true; |
| 154 |
} |
| 155 |
this.inherited(arguments); |
| 156 |
this._destroyed = true; |
| 157 |
} |
| 158 |
}); |
| 159 |
|
| 160 |
function getTestWidgets(testW){ |
| 161 |
return [ |
| 162 |
testW, |
| 163 |
testW.layout, |
| 164 |
testW.layChild1, |
| 165 |
testW.layChild2, |
| 166 |
testW.container, |
| 167 |
testW.contained1, |
| 168 |
testW.contained2, |
| 169 |
testW.nonContainer, |
| 170 |
testW.nonContained1, |
| 171 |
testW.nonContained2, |
| 172 |
testW.threeLevel, |
| 173 |
testW.secondLevel, |
| 174 |
testW.bottomLevel, |
| 175 |
testW.anotherThree, |
| 176 |
testW.anotherSecond, |
| 177 |
testW.anotherBottom, |
| 178 |
testW.stub1 |
| 179 |
]; |
| 180 |
} |
| 181 |
|
| 182 |
function validateTest5Widget(t, testW){ |
| 183 |
// Check that everything got started, but not double-started |
| 184 |
dojo.forEach(getTestWidgets(testW), function(w){ |
| 185 |
t.t(w._started, "w._started: " + w); |
| 186 |
t.is(undefined, w._doubleStarted, "w._doubleStarted: " + w); |
| 187 |
}); |
| 188 |
} |
| 189 |
|
| 190 |
function validateTest5WidgetDestroy(t, testW){ |
| 191 |
var savedWidgets = getTestWidgets(testW); |
| 192 |
testW.destroy(); |
| 193 |
dojo.forEach(savedWidgets, function(w, idx){ |
| 194 |
t.t(w._destroyed, "w._destroyed: " + w); |
| 195 |
t.is(undefined, w._doubleDestroyed, "w._doubleDestroyed: " + w); |
| 196 |
}); |
| 197 |
} |
| 198 |
|
| 199 |
dojo.declare("Missing", [dijit._Widget, dijit._Templated], { |
| 200 |
templateString: '<div>' + |
| 201 |
'<div dojoType="dijit.layout.ContentPane">' + |
| 202 |
'<div dojoType="dijit.form.Button" id="missingButtonId" ' + |
| 203 |
'dojoAttachPoint="missingButton">Missing...</div>' + |
| 204 |
'</div>' + |
| 205 |
'</div>' |
| 206 |
}); |
| 207 |
|
| 208 |
doh.register("parse", function(){ |
| 209 |
dojo.parser.parse(); |
| 210 |
}); |
| 211 |
|
| 212 |
doh.register("_Templated-widgetsInTemplate1.x", |
| 213 |
[ |
| 214 |
{ |
| 215 |
name: "dojoAttachPoint", |
| 216 |
runTest: function(t){ |
| 217 |
var testW = dijit.byId("test1Widget"); |
| 218 |
t.t(testW.normalNode, "normalNode"); |
| 219 |
t.f(isNaN(testW.normalNode.nodeType), "normalNode.nodeType"); |
| 220 |
t.t(testW.buttonWidget instanceof dijit.form.Button, "buttonWidget is Button"); |
| 221 |
t.t(testW.checkboxWidget instanceof dijit.form.CheckBox, "checkboxWidget is CheckBox"); |
| 222 |
t.t(testW.progressBarWidget instanceof dijit.ProgressBar, "progressBarWidget is ProgressBar"); |
| 223 |
testW = dijit.byId("test2Widget"); |
| 224 |
t.t(testW.containerNode, "containerNode"); |
| 225 |
t.f(isNaN(testW.containerNode.nodeType), "containerNode nodeType"); |
| 226 |
t.is(undefined,testW.buttonWidget, "buttonWidget undefined"); |
| 227 |
t.t(testW.checkboxWidget instanceof dijit.form.CheckBox, "checkboxWidget is still CheckBox"); |
| 228 |
} |
| 229 |
}, |
| 230 |
{ |
| 231 |
name: "dojoAttachEvent", |
| 232 |
runTest: function(t){ |
| 233 |
var testW = dijit.byId("test1Widget"); |
| 234 |
testW.buttonWidget._counter=0; |
| 235 |
testW.buttonWidget.onClick(testW.buttonWidget); |
| 236 |
testW.checkboxWidget._counter=0; |
| 237 |
testW.checkboxWidget.onClick(testW.checkboxWidget); |
| 238 |
testW.progressBarWidget._counter=0; |
| 239 |
testW.progressBarWidget.onChange(testW.progressBarWidget); |
| 240 |
t.is(1,testW.buttonWidget._counter, "buttonWidget._counter"); |
| 241 |
t.is(1,testW.checkboxWidget._counter, "checkboxWidget._counter"); |
| 242 |
t.is(1,testW.progressBarWidget._counter, "progressBarWidget._counter"); |
| 243 |
} |
| 244 |
}, |
| 245 |
{ |
| 246 |
// Test that getDescendants () |
| 247 |
// finds direct descendants but skips widgetsInTemplates |
| 248 |
// and also nested widgets (if direct==true) |
| 249 |
name: "destruction", |
| 250 |
runTest: function(t){ |
| 251 |
var testW = dijit.byId("test3Widget"); |
| 252 |
|
| 253 |
|
| 254 |
/*** performance tests |
| 255 |
var start = new Date(); |
| 256 |
for(var i=0; i<1000; i++) |
| 257 |
testW.getChildren(); |
| 258 |
console.log("*** time for getChildren(): " + (new Date()-start)); |
| 259 |
var start = new Date(); |
| 260 |
for(var i=0; i<1000; i++) |
| 261 |
testW.getDescendants(); |
| 262 |
console.log("*** time for getDescendants(false): " + (new Date()-start)); |
| 263 |
***/ |
| 264 |
var chil = testW.getChildren(); |
| 265 |
t.is(5, chil.length, "number of direct descendants"); |
| 266 |
t.is(chil[0].id, "3.1"); |
| 267 |
t.is(chil[1].id, "3.2"); |
| 268 |
t.is(chil[2].id, "3.3"); |
| 269 |
t.is(chil[3].id, "3.4"); |
| 270 |
t.is(chil[4].id, "3.5"); |
| 271 |
|
| 272 |
var desc = testW.getDescendants(); |
| 273 |
t.is(7, desc.length, "number of descendants (including nested ones)"); |
| 274 |
t.is(desc[0].id, "3.1"); |
| 275 |
t.is(desc[1].id, "3.2"); |
| 276 |
t.is(desc[2].id, "3.3"); |
| 277 |
t.is(desc[3].id, "3.nested"); |
| 278 |
t.is(desc[4].id, "3.nested2"); |
| 279 |
t.is(desc[5].id, "3.4"); |
| 280 |
t.is(desc[6].id, "3.5"); |
| 281 |
} |
| 282 |
}, |
| 283 |
{ |
| 284 |
// Check that declarative widget with layout widgets in template is correctly created and rendered |
| 285 |
name: "declarative widget with layout widgets", |
| 286 |
runTest: function(t){ |
| 287 |
validateTest4Widget(t, dijit.byId("test4Widget")); |
| 288 |
} |
| 289 |
}, |
| 290 |
{ |
| 291 |
// Check that programmatic widget with layout widgets in template is correctly created and rendered |
| 292 |
name: "programmatic widget with layout widgets", |
| 293 |
runTest: function(t){ |
| 294 |
test4WidgetProgrammatic = new Test4Widget({}).placeAt("test4Widget", "after"); |
| 295 |
test4WidgetProgrammatic.startup(); |
| 296 |
validateTest4Widget(t, test4WidgetProgrammatic); |
| 297 |
} |
| 298 |
}, |
| 299 |
{ |
| 300 |
// Compare programmatic and declaratively created widget with layout widgets in template |
| 301 |
name: "programmatic vs declarative with layout widgets", |
| 302 |
runTest: function(t){ |
| 303 |
// Focus unrelated element, so that we don't have different classes on our two widgets |
| 304 |
dojo.byId("plainInput").focus(); |
| 305 |
var declW = dijit.byId("test4Widget"); |
| 306 |
var progW = test4WidgetProgrammatic; |
| 307 |
|
| 308 |
// Check that generated HTML in DOM is same |
| 309 |
var declNeutralHtml = declW.domNode.innerHTML.replace(/_\d+/g, ""); |
| 310 |
var progNeutralHtml = progW.domNode.innerHTML.replace(/_\d+/g, ""); |
| 311 |
t.is(declNeutralHtml, progNeutralHtml, "progNeutralHtml"); |
| 312 |
|
| 313 |
// Check that dimensions are same |
| 314 |
var declBox = dojo.contentBox(declW.domNode); |
| 315 |
var progBox = dojo.contentBox(progW.domNode); |
| 316 |
t.is(declBox.h, progBox.h, "progBox.h"); |
| 317 |
t.is(declBox.w, progBox.w, "progBox.w"); |
| 318 |
} |
| 319 |
}, |
| 320 |
{ |
| 321 |
// Check that declarative widget with other widgets in template is correctly started |
| 322 |
name: "declarative widget with many child widgets", |
| 323 |
runTest: function(t){ |
| 324 |
validateTest5Widget(t, dijit.byId("test5Widget")); |
| 325 |
} |
| 326 |
}, |
| 327 |
{ |
| 328 |
// Check that programmatic widget with other widgets in template is correctly started |
| 329 |
name: "programmatic widget with many child widgets", |
| 330 |
runTest: function(t){ |
| 331 |
test5WidgetProgrammatic = new Test5Widget().placeAt("test5Widget", "after"); |
| 332 |
test5WidgetProgrammatic.startup(); |
| 333 |
validateTest5Widget(t, test5WidgetProgrammatic); |
| 334 |
} |
| 335 |
}, |
| 336 |
{ |
| 337 |
// Check that destroying our declarative widget works correctly |
| 338 |
name: "declarative widget destruction", |
| 339 |
runTest: function(t){ |
| 340 |
validateTest5WidgetDestroy(t, dijit.byId("test5Widget")); |
| 341 |
} |
| 342 |
}, |
| 343 |
{ |
| 344 |
// Check that destroying our programmatic widget works correctly |
| 345 |
name: "programmatic widget destruction", |
| 346 |
runTest: function(t){ |
| 347 |
validateTest5WidgetDestroy(t, test5WidgetProgrammatic); |
| 348 |
} |
| 349 |
}, |
| 350 |
{ |
| 351 |
// Test that dojoAttachPoint inside of a ContentPane (inside of a template) works |
| 352 |
name: "ContentPane", |
| 353 |
runTest: function(){ |
| 354 |
var testW = dijit.byId("missing"); |
| 355 |
doh.t(testW, "widget was created"); |
| 356 |
doh.t(testW.missingButton, "dojoAttachPoint created"); |
| 357 |
doh.is("dijit.form.Button", testW.missingButton.declaredClass, "and it's to a widget"); |
| 358 |
doh.t(dijit.byId("missingButtonId"), "nested widget also registered by id"); |
| 359 |
} |
| 360 |
} |
| 361 |
] |
| 362 |
); |
| 363 |
doh.run(); |
| 364 |
}); |
| 365 |
</script> |
| 366 |
</head> |
| 367 |
<body class="claro"> |
| 368 |
<h1>testing widgetsInTemplate support</h1> |
| 369 |
<xmp id="Test1Template" style="display:none;"> |
| 370 |
<div> |
| 371 |
<div dojoAttachPoint="normalNode" >normal node</div> |
| 372 |
<button dojoAttachPoint="buttonWidget" dojoAttachEvent="onClick:onClick" dojoType="dijit.form.Button">button #1</button> |
| 373 |
<div dojoAttachPoint="checkboxWidget" dojoAttachEvent="onClick:onClick" dojoType="dijit.form.CheckBox"></div> checkbox #1 |
| 374 |
<div dojoAttachPoint="progressBarWidget" dojoAttachEvent="onChange:onClick" style="width:400px" |
| 375 |
maximum="200" progress="20" dojoType="dijit.ProgressBar"></div> |
| 376 |
</div> |
| 377 |
</xmp> |
| 378 |
<div dojoType="Test1Widget" id="test1Widget" ></div> |
| 379 |
|
| 380 |
|
| 381 |
<xmp id="Test2Template" style="display:none;"> |
| 382 |
<div> |
| 383 |
<div dojoAttachPoint="containerNode" > |
| 384 |
<div dojoAttachPoint="checkboxWidget" dojoType="dijit.form.CheckBox"></div> |
| 385 |
checkbox #2 |
| 386 |
</div> |
| 387 |
</div> |
| 388 |
</xmp> |
| 389 |
<div dojoType="Test2Widget" id="test2Widget" ><button dojoAttachPoint="buttonWidget" dojoType="dijit.form.Button">button #2</button></div> |
| 390 |
|
| 391 |
|
| 392 |
<xmp id="Test3Template" style="display:none;"> |
| 393 |
<div> |
| 394 |
<div dojoAttachPoint="checkboxWidget" dojoType="dijit.form.CheckBox"></div> checkbox #3 |
| 395 |
<div dojoAttachPoint="containerNode" ></div> |
| 396 |
</div> |
| 397 |
</xmp> |
| 398 |
<script> |
| 399 |
dojo.declare('Test3Widget', [dijit._Widget, dijit._Templated], { |
| 400 |
templateString: dojo.byId('Test3Template').textContent || dojo.byId('Test3Template').innerText |
| 401 |
}); |
| 402 |
</script> |
| 403 |
<div dojoType="Test3Widget" id="test3Widget" > |
| 404 |
<span>hello world</span> |
| 405 |
<b style="border: 1px solid blue;">this is my |
| 406 |
<button dojoType="dijit.form.Button" id="3.1">first button</button> |
| 407 |
</b> |
| 408 |
<button dojoType="dijit.form.Button" id="3.2">another button</button> |
| 409 |
<i>and some more</i> |
| 410 |
<div style="border: 1px solid red;"> |
| 411 |
<div dojoType="dijit.layout.ContentPane" style="border: 1px solid gray;" id="3.3"> |
| 412 |
<button dojoType="dijit.form.Button" id="3.nested">a nested button</button> |
| 413 |
<button dojoType="dijit.form.Button" id="3.nested2">another nested button</button> |
| 414 |
</div> |
| 415 |
<button dojoType="dijit.form.Button" id="3.4">yet another button</button> |
| 416 |
<button dojoType="dijit.form.Button" id="3.5">yet yet another button</button> |
| 417 |
</div> |
| 418 |
</div> |
| 419 |
|
| 420 |
|
| 421 |
<xmp id="Test4Template" style="display:none;"> |
| 422 |
<div> |
| 423 |
<div dojoType="dijit.layout.TabContainer" dojoAttachPoint="tabCont" style="height: 5em;"> |
| 424 |
<div dojoType="dijit.layout.ContentPane" dojoAttachPoint="tab1" title="Tab 1"> |
| 425 |
pane 1 |
| 426 |
</div> |
| 427 |
<div dojoType="dijit.layout.ContentPane" dojoAttachPoint="tab2" title="Tab 2"> |
| 428 |
pane 2 |
| 429 |
</div> |
| 430 |
</div> |
| 431 |
</div> |
| 432 |
</xmp> |
| 433 |
<div dojoType="Test4Widget" id="test4Widget" ></div> |
| 434 |
|
| 435 |
|
| 436 |
<xmp id="Test5Template" style="display:none;"> |
| 437 |
<div> |
| 438 |
<div dojoType="TestLayoutWidget" dojoAttachPoint="layout"> |
| 439 |
<div dojoType="TestCtndWidget" dojoAttachPoint="layChild1"></div> |
| 440 |
<div dojoType="TestCtndWidget" dojoAttachPoint="layChild2"></div> |
| 441 |
</div> |
| 442 |
<div dojoType="TestCtnrWidget" dojoAttachPoint="container"> |
| 443 |
<div dojoType="TestCtndWidget" dojoAttachPoint="contained1"></div> |
| 444 |
<div dojoType="TestCtndWidget" dojoAttachPoint="contained2"></div> |
| 445 |
</div> |
| 446 |
<div dojoType="TestStubWidget" dojoAttachPoint="stub1"></div> |
| 447 |
<div dojoType="TestNonCtnrWidget" dojoAttachPoint="nonContainer"> |
| 448 |
<div dojoType="TestStubWidget" dojoAttachPoint="nonContained1"></div> |
| 449 |
<div dojoType="TestStubWidget" dojoAttachPoint="nonContained2"></div> |
| 450 |
</div> |
| 451 |
<div dojoType="TestCtnrWidget" dojoAttachPoint="threeLevel"> |
| 452 |
<div dojoType="TestNonCtnrWidget" dojoAttachPoint="secondLevel"> |
| 453 |
<div dojoType="TestStubWidget" dojoAttachPoint="bottomLevel"></div> |
| 454 |
</div> |
| 455 |
</div> |
| 456 |
<div dojoType="TestNonCtnrWidget" dojoAttachPoint="anotherThree"> |
| 457 |
<div dojoType="TestCtnrWidget" dojoAttachPoint="anotherSecond"> |
| 458 |
<div dojoType="TestCtndWidget" dojoAttachPoint="anotherBottom"></div> |
| 459 |
</div> |
| 460 |
</div> |
| 461 |
</div> |
| 462 |
</xmp> |
| 463 |
<div dojoType="Test5Widget" id="test5Widget" ></div> |
| 464 |
|
| 465 |
<div dojoType="Missing" id="missing"></div> |
| 466 |
<input id="plainInput"/> |
| 467 |
</body> |
| 468 |
</html> |
| 469 |
|