Debian Codegraph @

dijit/tests/layout/ContentPane.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
5 <title>ContentPane DOH test</title>
6 <style type="text/css">
7 .box {
8 border: 1px solid black;
9 padding: 8px;
10 }
11
12 .dijitTestWidget {
13 border: 1px dashed red;
14 background-color: #C0E209;
15 }
16 </style>
17
18 <script type="text/javascript" src="../boilerplate.js"></script>
19
20 <script type="text/javascript">
21 require([
22 "doh/runner",
23 "dojo/_base/array",
24 "dojo/_base/declare",
25 "dojo/dom",
26 "dojo/data/ItemFileReadStore",
27 "dojo/_base/NodeList",
28 "dojo/parser",
29 "dijit/registry",
30 "dijit/_WidgetBase",
31 "dijit/_TemplatedMixin",
32 "dijit/_WidgetsInTemplateMixin",
33 "dijit/_Container",
34 "dijit/Dialog",
35 "dijit/layout/_LayoutWidget",
36 "dijit/layout/ContentPane",
37 "dijit/layout/StackContainer",
38 "dojo/domReady!"
39 ], function(doh, array, declare, dom, ItemFileReadStore, NodeList, parser,
40 registry, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _Container, Dialog, _LayoutWidget,
41 ContentPane, StackContainer){
42
43 // create a do nothing, only for test widget
44 declare("TestWidget",
45 [_WidgetBase, _TemplatedMixin], {
46 templateString: "<span class='dijitTestWidget'></span>"
47 });
48
49 doh.register("pane1", [
50 {
51 name: "no_autoparse",
52 runTest: function(t){
53 if(registry.byId("pane1")){
54 throw doh._AssertFailure("Page got autoparsed when it shouldn't");
55 }
56 }
57 }
58 ]);
59
60 var pane2, MyWidget;
61
62 doh.registerGroup("pane2", [
63 {
64 name: "clear_content",
65 setUp: function(t){
66 pane2 = new ContentPane({
67 preventCache: true
68 }, dom.byId("pane2"));
69 pane2.set("content", "");// pass undefined on purpose
70 },
71 runTest: function(t){
72 doh.is(0, _WidgetBase.prototype.getChildren.call(pane2).length);
73 doh.is("", pane2.domNode.innerHTML)
74 }
75 },
76 {
77 name: "setContent_String",
78 setUp: function(){
79 pane2.set("content", "");
80 },
81 runTest: function(t){
82 var msg = "<h3>a simple html string</h3>";
83 pane2.set("content", msg);
84 doh.is(msg, pane2.domNode.innerHTML.toLowerCase());
85 }
86 },
87 {
88 name: "setContent_DOMNode",
89 setUp: function(t){
90 var div = document.createElement('div');
91 div.innerHTML = "set('content', [DOMNode] )";
92 div.setAttribute('data-dojo-type', 'TestWidget');
93 pane2.set("content", div);
94 },
95 runTest: function(t){
96 doh.is(1, _WidgetBase.prototype.getChildren.call(pane2).length);
97 },
98 tearDown: function(t){
99 pane2.set("content", ""); // clear content for next test
100 }
101 },
102 {
103 name: "setContent_NodeList",
104 setUp: function(t){
105 var div = document.createElement('div');
106 div.innerHTML = "<div data-dojo-type='TestWidget'>above</div>"
107 +"Testing!<div><p><span><b>Deep nested</b></span></p></div>"
108 +"<div data-dojo-type='TestWidget'>below</div>";
109
110 var list = div.childNodes;
111 pane2.set("content", div.childNodes);
112 },
113 runTest: function(t){
114 doh.is(2, _WidgetBase.prototype.getChildren.call(pane2).length);
115
116 //regular DOM check
117 var children = pane2.domNode.childNodes;
118 doh.is(4, children.length);
119 doh.is("Testing!", children[1].nodeValue);
120 doh.is("div", children[2].nodeName.toLowerCase());
121 doh.is("<p><span><b>deep nested</b></span></p>", children[2].innerHTML.toLowerCase());
122 }
123 },
124 {
125 name: "setContent_dojo_NodeList",
126 setUp: function(t){
127 pane2.set("content", "");
128 },
129 runTest: function(t){
130 var div = document.createElement('div');
131 div.innerHTML = "<div data-dojo-type='TestWidget'>above</div>"
132 +"Testing!<div><p><span><b>Deep nested</b></span></p></div>"
133 +"<div data-dojo-type='TestWidget'>below</div>";
134
135 var list = new NodeList();
136 array.forEach(div.childNodes, function(n){
137 list.push(n.cloneNode(true));
138 });
139
140 pane2.set("content", list);
141 doh.is(4, pane2.domNode.childNodes.length);
142 }
143 },
144 {
145 name: "extractContent",
146 runTest: function(t){
147 var def = pane2.extractContent;
148 doh.f(def);
149
150 // test that it's actually working
151 pane2.extractContent = true;
152 pane2.set("content", '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
153 +'"http://www.w3.org/TR/html4/strict.dtd">'
154 +'<html><head><style>body{font-weight:bold;}</style></head>'
155 +'<body>extractContent test</body></html>');
156
157 doh.is("extractContent test", pane2.domNode.innerHTML);
158
159 // reset back to default
160 pane2.extractContent = def;
161 }
162 },
163 {
164 name: "setContent_widget", // for #12348
165 setUp: function(t){
166 // declare a widget whose template contains a non-templated widget
167 MyWidget = declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
168 templateString: '<div><div '
169 + 'data-dojo-type="dijit/layout/StackContainer"></div></div>'
170 });
171 },
172 runTest: function(t){
173 var w = new MyWidget(), dfd = new doh.Deferred();
174 pane2.set("content", w).then(function(){
175 dfd.callback(true);
176 }, function(e){
177 dfd.errback(e);
178 });
179 return dfd;
180 }
181 }
182 ]);
183
184 // Tests for doLayout parameter.
185 // When this parameter is true, the single ContentPane child is resized
186 // to match the size of the ContentPane.
187 // See also ContentPaneLayout.html.
188 var pane5;
189 doh.registerGroup("doLayout", [
190 {
191 name: "simple",
192 setUp: function(t){
193 pane5 = new ContentPane({
194 content:"<div data-dojo-type='dijit/layout/StackContainer'></div>"
195 }).placeAt(document.body);
196 pane5.startup();
197 },
198 runTest: function(t){
199 // since there's just a single child it should be marked
200 // for layout/resize along w/the ContentPane
201 doh.t(pane5._singleChild);
202 },
203 tearDown: function(t){
204 pane5.destroyRecursive();
205 }
206 },
207 {
208 name: "doLayout=false",
209 setUp: function(t){
210 pane5 = new ContentPane({
211 content:
212 "<div data-dojo-type='dijit/layout/StackContainer'></div>",
213 doLayout: false
214 }).placeAt(document.body);
215 pane5.startup();
216 },
217 runTest: function(t){
218 // since doLayout=false shouldn't try to resize child
219 doh.f(pane5._singleChild);
220 },
221 tearDown: function(t){
222 pane5.destroyRecursive();
223 }
224 },
225 {
226 name: "mixed content",
227 setUp: function(t){
228 pane5 = new ContentPane({
229 content:
230 "<span>hello world</span>" +
231 "<div data-dojo-type='dijit/layout/StackContainer'></div>"
232 }).placeAt(document.body);
233 pane5.startup();
234 },
235 runTest: function(t){
236 // since there's plain HTML along with the widget, ContentPane shouldn't try to adjust
237 // this size of the widget (since that would cover up the other HTML)
238 doh.f(pane5._singleChild);
239 },
240 tearDown: function(t){
241 pane5.destroyRecursive();
242 }
243 },
244 {
245 name: "two widgets",
246 setUp: function(t){
247 pane5 = new ContentPane({
248 content:
249 "<div data-dojo-type='dijit/layout/StackContainer'></div>" +
250 "<div data-dojo-type='dijit/layout/StackContainer'></div>"
251 }).placeAt(document.body);
252 pane5.startup();
253 },
254 runTest: function(t){
255 // since there are multiple children, neither should be marked
256 // for layout/resize along w/the ContentPane
257 doh.f(pane5._singleChild);
258 },
259 tearDown: function(t){
260 pane5.destroyRecursive();
261 }
262 },
263 {
264 name: "dojo.data",
265 setUp: function(t){
266 pane5 = new ContentPane({
267 content:
268 "<div data-dojo-type='dojo/data/ItemFileReadStore' data-dojo-id='dd'></div>" +
269 "<div data-dojo-type='dijit/layout/StackContainer' id='sc'></div>"
270 }).placeAt(document.body);
271 pane5.startup();
272 },
273 runTest: function(t){
274 // there are two children but one is invisible, so the other should be marked
275 // for layout/resize along w/the ContentPane
276 doh.t(dd, "dd exists");
277 doh.t(registry.byId("sc"), "sc exists");
278 doh.is(registry.byId("sc"), pane5._singleChild, "pane5._singleChild");
279 },
280 tearDown: function(t){
281 pane5.destroyRecursive();
282 }
283 },
284 {
285 name: "non-visual tags ignored",
286 setUp: function(t){
287 pane5 = new ContentPane({
288 content:
289 "<scri" + "pt></scri" + "pt>" +
290 "<link rel='stylesheet' href='../../themes/claro/claro.css'>" +
291 "<style>body { background: red; }</style>" +
292 "<div data-dojo-type='dijit/layout/StackContainer' id='sc'></div>"
293 }).placeAt(document.body);
294 pane5.startup();
295 },
296 runTest: function(t){
297 // <script> etc. tags should be ignored, <div> should be detected as single child
298 doh.t(pane5._singleChild, "script,link,style tags ignored, marked as single child");
299 },
300 tearDown: function(t){
301 pane5.destroyRecursive();
302 }
303 }
304 ]);
305
306 declare("TestContained", _LayoutWidget, {
307 startup: function(){
308 this.inherited(arguments);
309 this._started = true;
310 },
311 resize: function(){
312 this.inherited(arguments);
313 this._resized = true;
314 }
315 });
316
317 var container;
318 doh.register("ContentPane as _Container-like widget", [
319 {
320 name: "creation",
321 runTest: function(t){
322 container = new ContentPane();
323 container.placeAt(document.body, "last");
324 container.startup();
325 t.is(0, container.getChildren().length, "number of children before set('content', ...)");
326 container.set('content',
327 '<span>plain non-widget content</span>' +
328 '<div><span>' +
329 '<div id="zero" data-dojo-type="TestContained"></div>' +
330 '<div id="one" data-dojo-type="TestContained"></div>' +
331 '</span></div>' +
332 '<div id="two" data-dojo-type="TestContained"></div>' +
333 '<div id="three" data-dojo-type="dijit/_WidgetBase"></div>'
334 );
335
336 // Since ContentPane is a container it should call startup
337 // on it's children
338 t.t(registry.byId('two')._started, "started");
339
340 // Also, Layout widgets expect resize() to be
341 // called by their parent
342 t.t(registry.byId('two')._resized, "resized");
343 }
344 },
345 {
346 name: "getChildren",
347 runTest: function(t){
348 var children = container.getChildren();
349 t.is(4, children.length, "number of children");
350 t.is("zero", children[0].id);
351 t.is("one", children[1].id);
352 t.is("two", children[2].id);
353 t.is("three", children[3].id);
354 }
355 },
356
357 {
358 name: "deferred resize",
359 runTest: function(t){
360 // This tests that startup isn't called on the child widgets
361 // until the contentpane is made visible
362
363 var hiddenCP = new ContentPane({style: {display: "none"}});
364 hiddenCP.placeAt(document.body, "last");
365 hiddenCP.startup();
366
367 t.is(0, hiddenCP.getChildren().length, "number of children before set('content', ...)");
368 hiddenCP.set('content',
369 '<span>plain non-widget content</span>' +
370 '<div><span>' +
371 '<div id="deferredZero" data-dojo-type="TestContained"></div>' +
372 '<div id="deferredOne" data-dojo-type="TestContained"></div>' +
373 '</span></div>' +
374 '<div id="deferredTwo" data-dojo-type="TestContained"></div>' +
375 '<div id="deferredThree" data-dojo-type="dijit/_WidgetBase"></div>'
376 );
377
378 t.f(registry.byId('deferredTwo')._resized, "not resized yet");
379
380 hiddenCP.set("style", {display: "block"});
381 hiddenCP._onShow();
382
383 t.t(registry.byId('deferredTwo')._resized, "resized");
384 }
385 }
386
387 /***
388 ,
389 {
390 name: "addChild",
391 runTest: function(t){
392 var afterTwo = new TestContained({id: "twoPointFive"});
393 container.addChild(afterTwo, 3);
394
395 // Make sure child was added and is in order
396 var children = container.getChildren();
397 t.is(5, children.length);
398 t.is("zero", children[0].id);
399 t.is("one", children[1].id);
400 t.is("two", children[2].id);
401 t.is("twoPointFive", children[3].id);
402 t.is("three", children[4].id);
403
404 // Since ContentPane is a container it should call startup
405 // on it's children
406 t.t(afterTwo._started, "started");
407
408 // Also, Layout widgets expect resize() to be
409 // called by their parent
410 t.t(afterTwo._resized, "resized");
411 }
412 },
413 {
414 name: "removeChild",
415 runTest: function(t){
416 var children = container.getChildren();
417 t.is(5, children.length);
418 container.removeChild(registry.byId("zero"));
419 container.removeChild(1); // should remove "two" - because zero is already removed
420 children = container.getChildren();
421 t.is(3, children.length);
422 t.is("one", children[0].id);
423 t.is("three", children[2].id);
424 }
425 }
426 ****/
427 ]);
428
429 // Test that popup widgets in a ContentPane are created and deleted correctly
430 doh.register("popup test", [
431 function create(){
432 parser.parse(dom.byId("popupTest"));
433 doh.t(registry.byId("popupPane"), "popup ContentPane created");
434 doh.t(registry.byId("dialog"), "dialog created");
435 doh.is(document.body, registry.byId("dialog").domNode.parentNode, "dialog child of <body>");
436 },
437 function destroy(){
438 registry.byId("popupPane").destroyRecursive();
439 doh.f(registry.byId("popupPane"), "popup ContentPane destroyed");
440 doh.f(registry.byId("dialog"), "dialog widget destroyed");
441 doh.f(dom.byId("dialog"), "dialog DOMNode gone too");
442 }
443 ]);
444
445 // Test that startup() on child widgets and plain JS objects is called at the correct time
446
447 var nwStartupCalls = 0, nwDestroyCalls = 0;
448 declare("NonWidget", null, {
449 // summary:
450 // Doesn't extend _WidgetBase. Used to test that startup() is still called.
451 startup: function(){
452 if(!this._started){
453 nwStartupCalls++;
454 this._started = true;
455 }
456 },
457 destroyRecursive: function(){
458 nwDestroyCalls++;
459 }
460 });
461
462 doh.register("startup", [
463 function startupAfter(){
464 var cp1 = new ContentPane({
465 content: "<div id='startup-c1' data-dojo-type='TestContained'></div>"
466 }).placeAt(document.body);
467
468 var child = registry.byId("startup-c1");
469 doh.t(child, "child widget created");
470 doh.f(child._started, "child widget not started yet");
471
472 cp1.startup();
473
474 doh.t(child._started, "starting ContentPane starts child widget");
475 },
476 function startupBefore(){
477 var cp2 = new ContentPane({}).placeAt(document.body);
478 cp2.startup();
479
480 cp2.set("content", "<div id='startup-c2' data-dojo-type='TestContained'></div>");
481 var child = registry.byId("startup-c2");
482 doh.t(child, "child widget created");
483 doh.t(child._started, "child widget started");
484 }
485 ]);
486
487 // Tests for when ContentPane contains objects not descended from _WidgetBase,
488 // like dojo.Dnd.Source
489 doh.register("non widget", [
490 function startupAndDestroy(){
491 // even non-widgets inside of ContentPane (like dojo.dnd.Source) should have
492 // startup called on them
493 parser.parse(dom.byId("nonWidgetTest"));
494 doh.is(1, nwStartupCalls, "startup() on non-widgets called on parse");
495
496 nwp.set("content", "<div><div data-dojo-type='NonWidget' data-dojo-id='nw2'></div></div>");
497 doh.is(2, nwStartupCalls, "startup() called on non-widgets via set(content, ...)");
498 doh.is(1, nwDestroyCalls, "resetting the ContentPane content destroyed the old non-widget content");
499
500 // And also check that destroying ContentPane destroys the non-widget
501 nwp.destroyRecursive();
502 doh.is(2, nwDestroyCalls, "destroying ContentPane destroyed NonWidget content");
503 }
504 ]);
505
506 doh.run();
507 });
508 </script>
509 </head>
510 <body role="main">
511 <h2>dijit/layout/ContentPane DOH test</h2>
512 <h3>Test designed to run on localhost (minimize impact from network latency)</h3>
513
514 <h4>This should NOT be parsed automatically</h4>
515 <div id="pane1" data-dojo-type="dijit/layout/ContentPane" data-dojo-props='"class":"box"'>
516 <div data-dojo-type='TestWidget'>If this has a different background and a red border, the page parsed when it shouldn't</div>
517 </div>
518 <br/><h3>Testing ContentPane</h3>
519 <div id='pane2' class='box'>
520 Even though the entire page isn't scanned for widgets,
521 any sub widgets of a ContentPane will be created when a ContentPane is created<br/>
522 <span id="2_zero" data-dojo-type='TestWidget'>This should have a backgroundcolor and a border</span>
523 <div id="2_one" data-dojo-type="dijit/_WidgetBase"></div>
524 <div id="2_two" data-dojo-type="dijit/_WidgetBase"></div>
525 <div id="2_three" data-dojo-type="dijit/_WidgetBase"></div>
526 </div>
527 <br/><br/>
528
529 <!-- for container tests -->
530 <div id="container" data-dojo-type="dijit/layout/ContentPane">
531 <div id="zero" data-dojo-type="TestContained"></div>
532 <div id="one" data-dojo-type="TestContained"></div>
533 <div id="two" data-dojo-type="TestContained"></div>
534 <div id="three" data-dojo-type="dijit/_WidgetBase"></div>
535 </div>
536 <div id="outside" data-dojo-type="dijit/_WidgetBase"></div>
537 <div id="outsideCont" data-dojo-type="TestContained"></div>
538
539 <!-- for testing popup widgets inside of a content pane -->
540 <div id="popupTest">
541 <div id="popupPane" data-dojo-type="dijit/layout/ContentPane">
542 <div id="dialog" data-dojo-type="dijit/Dialog">
543 hello world
544 </div>
545 </div>
546 </div>
547
548 <!-- for testing non-widgets inside of a content pane -->
549 <div id="nonWidgetTest">
550 <div id="nonWidgetPane" data-dojo-type="dijit/layout/ContentPane" data-dojo-id="nwp">
551 <div data-dojo-type="NonWidget" data-dojo-id="nw1"></div>
552 </div>
553 </div>
554
555 </body>
556 </html>