| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
| 5 | <title>dijit/place unit test</title> |
| 6 | |
| 7 | <script src="boilerplate.js"></script> |
| 8 | |
| 9 | <style type="text/css"> |
| 10 | html { |
| 11 | overflow: hidden; /* ie6 needs this */ |
| 12 | } |
| 13 | body { |
| 14 | height: 100%; |
| 15 | padding: 0; |
| 16 | margin: 10px; /* negative test for #16148; shouldn't affect placement unless body position relative/absolute */ |
| 17 | border: 0; |
| 18 | } |
| 19 | |
| 20 | .aroundNode { |
| 21 | position: absolute; |
| 22 | width: 20px; |
| 23 | height: 20px; |
| 24 | background: yellow; |
| 25 | } |
| 26 | |
| 27 | #popup { |
| 28 | position: absolute; |
| 29 | width: 75px; |
| 30 | background: blue; |
| 31 | color: white; |
| 32 | } |
| 33 | </style> |
| 34 | |
| 35 | <script type="text/javascript"> |
| 36 | require([ |
| 37 | "doh/runner", |
| 38 | "dojo/dom", "dojo/dom-geometry", "dojo/window", |
| 39 | "dijit/place", "dojo/domReady!" |
| 40 | ], function(doh, dom, domGeometry, winUtils, place){ |
| 41 | |
| 42 | // The around nodes |
| 43 | var aroundTop = dom.byId("aroundTop"), |
| 44 | aroundBottom = dom.byId("aroundBottom"), |
| 45 | aroundLeft = dom.byId("aroundLeft"), |
| 46 | aroundRight = dom.byId("aroundRight"); |
| 47 | |
| 48 | // The popup (aka dropdown) |
| 49 | var popup = dom.byId("popup"); |
| 50 | |
| 51 | doh.register("at", [ |
| 52 | function atTL(t){ |
| 53 | // Place popup at (10,7)... place.at() should choose the top-left corner, because |
| 54 | // any of the other corner would make the popup go partially off the screen |
| 55 | var ret = place.at(popup, {x: 10, y:7}, ["TR", "BR", "BL", "TL"]); |
| 56 | |
| 57 | doh.is("TL", ret.corner, "top left corner chosen"); |
| 58 | doh.is("10px", popup.style.left, "x coord"); |
| 59 | doh.is("7px", popup.style.top, "y coord"); |
| 60 | doh.is(75, ret.w, "it's 75px wide"); |
| 61 | }, |
| 62 | function atTooltip(t){ |
| 63 | // Same as above test except that shape of drop down changes depending on where it's positioned. |
| 64 | // Simulates tooltip placement (tooltip shape changes b/c of the arrow). |
| 65 | |
| 66 | function layoutNode(node, aroundCorner, nodeCorner){ |
| 67 | node.style.width = (nodeCorner == "TL" ? "80px" : "75px"); |
| 68 | } |
| 69 | var ret = place.at( |
| 70 | popup, |
| 71 | {x: 10, y: 7}, // center of aroundNode |
| 72 | ["TR", "BR", "BL", "TL"], |
| 73 | {x: 5, y: 5}, // half of width and height of aroundNode |
| 74 | layoutNode |
| 75 | ); |
| 76 | |
| 77 | doh.is("TL", ret.corner, "popup's corner"); |
| 78 | doh.is("BR", ret.aroundCorner, "around corner"); |
| 79 | doh.is("80px", popup.style.width, "layout node width"); |
| 80 | popup.style.width = "75px"; |
| 81 | }, |
| 82 | function atTR(t){ |
| 83 | // Place popup at top right... place.at() should choose the top-right corner, because |
| 84 | // any of the other corner would make the popup go partially off the screen |
| 85 | var viewport = winUtils.getBox(), |
| 86 | ret = place.at(popup, {x: viewport.w-10, y:7}, ["TL", "BR", "TR", "BL"]), |
| 87 | popupCoords = domGeometry.position(popup); |
| 88 | |
| 89 | doh.is("TR", ret.corner, "top left corner chosen"); |
| 90 | doh.is(viewport.w-10, popupCoords.x + popupCoords.w, "x coord"); |
| 91 | doh.is("7px", popup.style.top, "y coord"); |
| 92 | doh.is(75, ret.w, "it's 75px wide"); |
| 93 | } |
| 94 | ]); |
| 95 | |
| 96 | doh.register("around", [ |
| 97 | function aroundT(t){ |
| 98 | // Dropdown from "aroundTop" node. Should pick the second choice, since the first |
| 99 | // goes off screen. |
| 100 | var ret = place.around(popup, aroundTop, [ |
| 101 | "above", // aroundTop's top-left corner with the popup's bottom-left corner (fails) |
| 102 | "below", // aroundTop's bottom-left corner with the popup's top-left corner (works) |
| 103 | "below-alt" // aroundTop's bottom-right corner with the popup's top-right corner (works) |
| 104 | ], true); |
| 105 | |
| 106 | doh.is("BL", ret.aroundCorner, "around corner"); |
| 107 | doh.is("TL", ret.corner, "popup's corner"); |
| 108 | doh.is("20px", popup.style.top, "underneath around node"); |
| 109 | doh.is(Math.round(domGeometry.position(aroundTop).x), Math.round(popup.style.left.replace("px", "")), |
| 110 | "left sides aligned"); |
| 111 | }, |
| 112 | function aroundTooltip(t){ |
| 113 | // Same as above test except that shape of drop down changes depending on where it's positioned. |
| 114 | // Simulates tooltip placement (tooltip shape changes b/c of the arrow). |
| 115 | // Should pick the third choice this time |
| 116 | |
| 117 | function layoutNode(node, aroundCorner, nodeCorner){ |
| 118 | node.style.width = (nodeCorner == "TL" ? "5000px" : "75px"); |
| 119 | } |
| 120 | var ret = place.around(popup, aroundTop, [ |
| 121 | "above", // aroundTop's top-left corner with the popup's bottom-left corner (fails) |
| 122 | "below", // aroundTop's bottom-left corner with the popup's top-left corner (works) |
| 123 | "below-alt" // aroundTop's bottom-right corner with the popup's top-right corner (works) |
| 124 | ], true, layoutNode); |
| 125 | |
| 126 | doh.is("BR", ret.aroundCorner, "around corner"); |
| 127 | doh.is("TR", ret.corner, "popup's corner"); |
| 128 | doh.is("20px", popup.style.top, "underneath around node"); |
| 129 | doh.is(Math.round(domGeometry.position(aroundTop).x+domGeometry.position(aroundTop).w), |
| 130 | Math.round(domGeometry.position(popup).x+domGeometry.position(popup).w), |
| 131 | "right sides aligned"); |
| 132 | }, |
| 133 | function aroundB(t){ |
| 134 | // Dropdown from "aroundBottom" node. Should go above aroundNode so that |
| 135 | // popup doesn't go offscreen |
| 136 | var ret = place.around(popup, aroundBottom, [ |
| 137 | "below", // aroundBottom's bottom-left corner with the popup's top-left corner (fails) |
| 138 | "above", // aroundBottom's top-left corner with the popup's bottom-left corner (works) |
| 139 | "below-alt" // aroundBottom's bottom-right corner with the popup's top-right corner (fails) |
| 140 | ], true); |
| 141 | |
| 142 | doh.is("TL", ret.aroundCorner, "around corner"); |
| 143 | doh.is("BL", ret.corner, "popup's corner"); |
| 144 | doh.is(Math.round(domGeometry.position(aroundBottom).y), |
| 145 | Math.round(domGeometry.position(popup).y + domGeometry.position(popup).h), "above around node"); |
| 146 | }, |
| 147 | function aroundBM(t){ |
| 148 | // bottom middle popup from "aroundBottom" node |
| 149 | var ret = place.around(popup, aroundBottom, [ |
| 150 | "above-centered", // aroundBottom's top-middle with the popup's bottom-middle (works) |
| 151 | "below-centered" // aroundBottom's bottom-middle with the popup's top-middle (fails) |
| 152 | ], true); |
| 153 | |
| 154 | doh.is("TM", ret.aroundCorner, "around middle"); |
| 155 | doh.is("BM", ret.corner, "popup's middle"); |
| 156 | var popupPos = domGeometry.position(popup); |
| 157 | var aroundPos = domGeometry.position(aroundBottom); |
| 158 | doh.is(Math.round(aroundPos.y), Math.round(popupPos.y + popupPos.h), "above around node"); |
| 159 | doh.t(aroundPos.x > popupPos.x, "starts before around node"); |
| 160 | doh.t(aroundPos.x < (popupPos.x + popupPos.w), "ends after around node"); |
| 161 | }, |
| 162 | function aroundTM(t){ |
| 163 | // top middle popup from "aroundTop" node |
| 164 | var ret = place.around(popup, aroundTop, [ |
| 165 | "above-centered", // aroundTop's top-middle with the popup's bottom-middle (fails) |
| 166 | "below-centered" // aroundTop's bottom-middle with the popup's top-middle (works) |
| 167 | ], true); |
| 168 | |
| 169 | doh.is("BM", ret.aroundCorner, "around middle"); |
| 170 | doh.is("TM", ret.corner, "popup's middle"); |
| 171 | var popupPos = domGeometry.position(popup); |
| 172 | var aroundPos = domGeometry.position(aroundTop); |
| 173 | doh.is(Math.round(aroundPos.y + aroundPos.h), Math.round(popupPos.y), "below around node"); |
| 174 | doh.t(aroundPos.x > popupPos.x, "starts before around node"); |
| 175 | doh.t(aroundPos.x < (popupPos.x + popupPos.w), "ends after around node"); |
| 176 | }, |
| 177 | function aroundML(t){ |
| 178 | // middle left popup from "aroundLeft" node |
| 179 | var ret = place.around(popup, aroundLeft, [ |
| 180 | "after-centered", // aroundLeft's middle-right with the popup's middle-left (works) |
| 181 | "before-centered" // aroundLeft's middle-left with the popup's middle-right (fails) |
| 182 | ], true); |
| 183 | |
| 184 | doh.is("MR", ret.aroundCorner, "around middle"); |
| 185 | doh.is("ML", ret.corner, "popup's middle"); |
| 186 | var popupPos = domGeometry.position(popup); |
| 187 | var aroundPos = domGeometry.position(aroundLeft); |
| 188 | doh.is(Math.round(aroundPos.x + aroundPos.w), Math.round(popupPos.x), "after around node"); |
| 189 | doh.t(aroundPos.y > popupPos.y, "starts before around node"); |
| 190 | doh.t(aroundPos.y < (popupPos.y + popupPos.h), "ends after around node"); |
| 191 | }, |
| 192 | function aroundMR(t){ |
| 193 | // middle left popup from "aroundRight" node |
| 194 | var ret = place.around(popup, aroundRight, [ |
| 195 | "after-centered", // aroundRight's middle-right with the popup's middle-left (fails) |
| 196 | "before-centered" // aroundRight's middle-left with the popup's middle-right (works) |
| 197 | ], true); |
| 198 | |
| 199 | doh.is("ML", ret.aroundCorner, "around middle"); |
| 200 | doh.is("MR", ret.corner, "popup's middle"); |
| 201 | var popupPos = domGeometry.position(popup); |
| 202 | var aroundPos = domGeometry.position(aroundRight); |
| 203 | doh.is(Math.round(popupPos.x + popupPos.w), Math.round(aroundPos.x), "before around node"); |
| 204 | doh.t(aroundPos.y > popupPos.y, "starts before around node"); |
| 205 | doh.t(aroundPos.y < (popupPos.y + popupPos.h), "ends after around node"); |
| 206 | }, |
| 207 | function aroundMLB(t){ |
| 208 | // This will put the drop-down below the "aroundLeft" node, first trying to right-align |
| 209 | // but since that doesn't work then trying to left-align. |
| 210 | var ret = place.around(popup, aroundLeft, ["below-alt"], true); |
| 211 | |
| 212 | doh.is("BL", ret.aroundCorner, "around left"); |
| 213 | doh.is("TL", ret.corner, "popup's left"); |
| 214 | var popupPos = domGeometry.position(popup); |
| 215 | var aroundPos = domGeometry.position(aroundLeft); |
| 216 | doh.is(Math.round(aroundPos.y + aroundPos.h), Math.round(popupPos.y), "below around node"); |
| 217 | doh.is(aroundPos.x, popupPos.x, "left aligned with around node"); |
| 218 | }, |
| 219 | function aroundMRT(t){ |
| 220 | // This will put the drop-down above the "aroundRight" node, first trying to left-align |
| 221 | // but since that doesn't work then trying to right-align. |
| 222 | var ret = place.around(popup, aroundRight, ["above"], true); |
| 223 | |
| 224 | doh.is("TR", ret.aroundCorner, "around right"); |
| 225 | doh.is("BR", ret.corner, "popup's right"); |
| 226 | var popupPos = domGeometry.position(popup); |
| 227 | var aroundPos = domGeometry.position(aroundRight); |
| 228 | doh.is(Math.round(popupPos.x + popupPos.w), Math.round(aroundPos.x + aroundPos.w), |
| 229 | "right aligned with around node"); |
| 230 | doh.is(Math.round(popupPos.y + popupPos.h), Math.round(aroundPos.y), |
| 231 | "above around node"); |
| 232 | } |
| 233 | ]); |
| 234 | |
| 235 | doh.run(); |
| 236 | }); |
| 237 | |
| 238 | </script> |
| 239 | </head> |
| 240 | <body> |
| 241 | <h1>Dijit Place Unit Test</h1> |
| 242 | <div id="aroundTop" class="aroundNode" style="top: 0; left: 50%">T</div> |
| 243 | <div id="aroundLeft" class="aroundNode" style="bottom: 30%; left: 0;">L</div> |
| 244 | <div id="aroundRight" class="aroundNode" style="bottom: 30%; right: 1px;">R</div> |
| 245 | <div id="aroundBottom" class="aroundNode" style="bottom: 5px; left: 50%;">B</div> |
| 246 | |
| 247 | <div id="popup"> |
| 248 | I'm a drop down, wider and taller than the around nodes I'm placed next to. |
| 249 | </div> |
| 250 | </body> |
| 251 | </html> |