| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 6 | |
| 7 | <title>_Widget.placeAt tests</title> |
| 8 | |
| 9 | <script src="boilerplate.js"></script> |
| 10 | |
| 11 | <script type="text/javascript"> |
| 12 | require([ |
| 13 | "doh/runner", "dojo/aspect", "dojo/_base/declare", "dojo/dom", |
| 14 | "dijit/_WidgetBase", "dijit/form/Button", |
| 15 | "dijit/layout/ContentPane", "dijit/layout/TabContainer", "dijit/layout/BorderContainer", |
| 16 | "dojo/domReady!" |
| 17 | ], function(doh, aspect, declare, dom, _WidgetBase, Button, ContentPane, TabContainer, BorderContainer){ |
| 18 | |
| 19 | var SimpleWidget = declare(_WidgetBase, { |
| 20 | buildRendering: function(){ |
| 21 | this.domNode = document.createElement("div"); |
| 22 | this.containerNode = document.createElement("div"); |
| 23 | this.domNode.appendChild(this.containerNode); |
| 24 | } |
| 25 | }); |
| 26 | |
| 27 | function getFragment() { |
| 28 | var frag = document.createDocumentFragment(); |
| 29 | frag.appendChild(document.createElement('div')); |
| 30 | frag.appendChild(document.createElement('div')); |
| 31 | frag.appendChild(document.createElement('div')); |
| 32 | return frag; |
| 33 | } |
| 34 | |
| 35 | doh.register("placeAt document fragment", [ |
| 36 | function placeAsLastNode(){ |
| 37 | var frag = getFragment(); |
| 38 | simple = (new SimpleWidget({id: "simple"})).placeAt(frag); |
| 39 | doh.assertEqual(simple.domNode, frag.lastChild); |
| 40 | simple.destroy(); |
| 41 | }, |
| 42 | |
| 43 | function placeAsFirstNode(){ |
| 44 | var frag = getFragment(); |
| 45 | simple = (new SimpleWidget({id: "simple"})).placeAt(frag, "first"); |
| 46 | doh.assertEqual(simple.domNode, frag.firstChild); |
| 47 | simple.destroy(); |
| 48 | }, |
| 49 | |
| 50 | function placeAsOnlyNode() { |
| 51 | var frag = getFragment(); |
| 52 | simple = (new SimpleWidget({id: "simple"})).placeAt(frag, "only"); |
| 53 | doh.assertEqual(frag.childNodes.length, 1); |
| 54 | doh.assertEqual(simple.domNode, frag.firstChild); |
| 55 | simple.destroy(); |
| 56 | }, |
| 57 | |
| 58 | function placeAtPosition() { |
| 59 | var frag = getFragment(); |
| 60 | simple = (new SimpleWidget({id: "simple"})).placeAt(frag, 2); |
| 61 | doh.assertEqual(simple.domNode, frag.childNodes[2]); |
| 62 | simple.destroy(); |
| 63 | } |
| 64 | ]); |
| 65 | |
| 66 | var simple, pane1, pane2, pane3, pane4, pane5; |
| 67 | doh.register("parent without addChild", [ |
| 68 | function placeAsDOMNodeChild(){ |
| 69 | // create a SimpleWidget |
| 70 | simple = (new SimpleWidget({id: "simple"})).placeAt("container"); |
| 71 | doh.is(dom.byId("container"), simple.domNode.parentNode, "simple is child of container"); |
| 72 | }, |
| 73 | |
| 74 | function placeAsWidgetChild(){ |
| 75 | // add the child to the SimpleWidget now |
| 76 | pane1 = (new ContentPane({ title: "pane1" })).placeAt(simple); |
| 77 | doh.is(pane1, simple.getChildren()[0], "pane1 is child of SimpleWidget"); |
| 78 | doh.is(simple.containerNode, pane1.domNode.parentNode, "pane1 added to simple.containerNode not simple.domNode") |
| 79 | }, |
| 80 | |
| 81 | function placeAsWidgetChildOrdered(){ |
| 82 | // add this child (created second) as the new first child |
| 83 | pane2 = (new ContentPane({ title: "pane2" })).placeAt("simple", 0); |
| 84 | doh.is(simple.containerNode, pane2.domNode.parentNode, "pane2 added to simple.containerNode not simple.domNode") |
| 85 | doh.is(pane2, simple.getChildren()[0], "pane2 is new first child of SimpleWidget"); |
| 86 | doh.is(pane1, simple.getChildren()[1], "pane1 is now second child of SimpleWidget"); |
| 87 | }, |
| 88 | |
| 89 | function placeBeforeDomNode(){ |
| 90 | var button = (new Button({})).placeAt(dom.byId("container"), "before"); |
| 91 | doh.is(dom.byId("container"), button.domNode.nextSibling, "button is before tab container"); |
| 92 | }, |
| 93 | |
| 94 | function placeBeforeDomNodeId(){ |
| 95 | var button = (new Button({})).placeAt("container", "before"); |
| 96 | doh.is(dom.byId("container"), button.domNode.nextSibling, "button is before tab container"); |
| 97 | }, |
| 98 | |
| 99 | function placeFirstWidget(){ |
| 100 | simple.startup(); |
| 101 | pane4 = (new ContentPane({ title: "pane4" })).placeAt("simple", "first"); |
| 102 | doh.is(simple.containerNode, pane4.domNode.parentNode, "pane4 added to simple.containerNode not simple.domNode") |
| 103 | doh.is(pane4, simple.getChildren()[0], "pane4 is new first child of SimpleWidget"); |
| 104 | doh.t(pane4._started, "pane4 was automatically started because simple was already started") |
| 105 | }, |
| 106 | function placeLastWidget(){ |
| 107 | pane5 = (new ContentPane({ title: "pane5" })).placeAt(simple.containerNode, "last"); |
| 108 | doh.is(pane5, simple.getChildren()[simple.getChildren().length-1], "pane5 is new last child of SimpleWidget"); |
| 109 | doh.t(pane5._started, "pane5 was automatically started because simple was already started") |
| 110 | } |
| 111 | ]); |
| 112 | |
| 113 | var tc; |
| 114 | doh.register("TabContainer parent", [ |
| 115 | function placeAsDOMNodeChild(){ |
| 116 | // create a TabContainer |
| 117 | tc = (new TabContainer({ |
| 118 | id: "tc", |
| 119 | style: "height:200px; width:200px" |
| 120 | }, "tabContainerThinger")).placeAt("container"); |
| 121 | |
| 122 | doh.is(dom.byId("container"), tc.domNode.parentNode, "TabContainer is child of container"); |
| 123 | }, |
| 124 | |
| 125 | function placeAsWidgetChild(){ |
| 126 | // add the child to the TabContainer now: |
| 127 | pane1 = (new ContentPane({ title: "pane1" })).placeAt(tc); |
| 128 | |
| 129 | doh.is(pane1, tc.getChildren()[0], "pane1 is child of TabContainer"); |
| 130 | }, |
| 131 | |
| 132 | function placeAsWidgetChildOrdered(){ |
| 133 | // add this child (created second) as the first tab: |
| 134 | pane2 = (new ContentPane({ title: "pane2" })).placeAt(tc, 0); |
| 135 | |
| 136 | doh.is(pane2, tc.getChildren()[0], "pane2 is new first child of TabContainer"); |
| 137 | doh.is(pane1, tc.getChildren()[1], "pane1 is now second child of TabContainer"); |
| 138 | }, |
| 139 | |
| 140 | function placeAsWidgetIdChild(){ |
| 141 | // add the child to the TabContainer now: |
| 142 | pane3 = (new ContentPane({ title: "pane1" })).placeAt("tc"); |
| 143 | |
| 144 | doh.is(pane3, tc.getChildren()[2], "pane3 is child of TabContainer"); |
| 145 | }, |
| 146 | |
| 147 | function startup(){ |
| 148 | // just starting the TabContainer so we can do some more tests |
| 149 | tc.startup(); |
| 150 | tc.selectChild(pane2); |
| 151 | }, |
| 152 | |
| 153 | function placeAsFirst(){ |
| 154 | pane2.set("content","button should appear BEFORE this text"); |
| 155 | |
| 156 | // create a button, and add it to pane2 before the above text |
| 157 | var button = (new Button({ |
| 158 | label:"alert", |
| 159 | onClick: function(){ |
| 160 | alert('woot'); |
| 161 | } |
| 162 | })).placeAt(pane2.containerNode, "first"); |
| 163 | |
| 164 | doh.is(button.domNode, pane2.containerNode.firstChild, "button is first child"); |
| 165 | doh.is(3, button.domNode.nextSibling.nodeType, "button went before other content"); |
| 166 | }, |
| 167 | |
| 168 | function placeBefore(){ |
| 169 | // And a button, this time we'll place it before the TabContainer's dom. |
| 170 | // placeAt(refWidget, "before"/"after"/"replace") isn't supported in general, |
| 171 | // especially not when the grandparent has addChild()/removeChild() methods, but testing |
| 172 | // here for regressions in what does work |
| 173 | var otherButton = new Button({ |
| 174 | label:"destroy TabContainer", |
| 175 | onClick:function(){ |
| 176 | tc.destroyRecursive(); |
| 177 | } |
| 178 | }); |
| 179 | otherButton.placeAt("tc", "before"); |
| 180 | |
| 181 | // make sure it went before tc.domNode, not before tc.containerNode |
| 182 | doh.is(tc.domNode, otherButton.domNode.nextSibling, "otherButton is before tab container"); |
| 183 | |
| 184 | // since it doesn't have a widget parent it should have been started |
| 185 | doh.f(otherButton._started, "button wasn't started"); |
| 186 | } |
| 187 | ]); |
| 188 | |
| 189 | doh.register("startup tests", [ |
| 190 | function startup(){ |
| 191 | var bc = new BorderContainer({ |
| 192 | id: "bc", |
| 193 | style: "width:600px; height:400px" |
| 194 | }); |
| 195 | bc.placeAt(document.body); |
| 196 | doh.is(document.body, bc.domNode.parentNode, "BorderContainer parentNode == document.body"); |
| 197 | |
| 198 | // add a center pane before BC startup |
| 199 | var centerStarted; |
| 200 | var center = new ContentPane({ |
| 201 | region: "center", |
| 202 | content: "<p>center</p>" |
| 203 | }); |
| 204 | aspect.after(center, "startup", function(){ centerStarted = true; }, true); |
| 205 | center.placeAt(bc); |
| 206 | doh.f(centerStarted, "center not started"); // shouldn't be started since BC itself isn't started |
| 207 | doh.is(bc, center.getParent(), "center ContentPane parent == BorderContainer"); |
| 208 | |
| 209 | // start BorderContainer |
| 210 | bc.startup(); |
| 211 | doh.t(centerStarted, "center started"); // should be started along with BC |
| 212 | |
| 213 | // add a left pane after startup |
| 214 | var leftStarted; |
| 215 | var left = new ContentPane({ |
| 216 | region: "left", |
| 217 | content: "<p>wowzers</p>", |
| 218 | style: "width:100px" |
| 219 | }); |
| 220 | aspect.after(left, "startup", function(){ leftStarted = true; }, true); |
| 221 | left.placeAt("bc"); |
| 222 | doh.is(bc, left.getParent(), "left ContentPane parent == BorderContainer"); |
| 223 | doh.t(leftStarted, "left ContentPane automatically started since BorderContainer was already started"); |
| 224 | |
| 225 | // add a top pane, and add content |
| 226 | var topStarted; |
| 227 | var top = new ContentPane({ |
| 228 | region: "top", |
| 229 | style: "height:100px" |
| 230 | }); |
| 231 | aspect.after(top, "startup", function(){ topStarted = true; }, true); |
| 232 | top.placeAt(bc).set("content","<div>some HTML text</div>"); |
| 233 | doh.is(bc, top.getParent(), "top ContentPane parent == BorderContainer"); |
| 234 | doh.t(topStarted, "top ContentPane automatically started since BorderContainer was already started"); |
| 235 | } |
| 236 | ]); |
| 237 | |
| 238 | doh.run(); |
| 239 | }); |
| 240 | </script> |
| 241 | </head> |
| 242 | <body> |
| 243 | <h1 class="testTitle">_Widget.placeAt tests</h1> |
| 244 | |
| 245 | <div id="container"> |
| 246 | </div> |
| 247 | </body> |
| 248 | </html> |