Debian Codegraph @

dijit/tests/popup.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3 <html lang="en">
4 <head>
5 <title>popup and BackgroundIFrame unit test</title>
6
7 <script src="boilerplate.js"></script>
8
9 <style type="text/css">
10 body {
11 height: 100%;
12 padding: 0;
13 margin: 0;
14 }
15
16 div {
17 background: white;
18 border: solid 1px gray;
19 }
20
21 /* the menu type test widgets */
22 .choice div {
23 width: 200px;
24 cursor: pointer;
25 }
26 .choice div:hover {
27 background: #ccc;
28 }
29 </style>
30
31 <script type="text/javascript">
32 function log(str){
33 console.log(str);
34 }
35
36 require([
37 "doh/runner",
38 "dojo/_base/declare",
39 "dojo/dom", "dojo/dom-construct", "dojo/dom-geometry", "dojo/dom-style",
40 "dojo/query", "dojo/sniff", "dojo/_base/window", "dojo/window",
41 "dijit/popup", "dijit/registry", "dijit/_WidgetBase", "dijit/_TemplatedMixin",
42 "dijit/tests/helpers", "dojo/domReady!"
43 ], function(doh, declare, dom, domConstruct, domGeometry, domStyle, query, has, win, winUtils,
44 popupUtil, registry, _WidgetBase, _TemplatedMixin){
45
46
47 declare("SimpleDropDownButton", [_WidgetBase, _TemplatedMixin], {
48 // summary:
49 // A button that shows a popup.
50 // Supply popup as parameter when instantiating this widget.
51
52 label: "show popup",
53 orient: ["below"],
54
55 templateString: "<button data-dojo-attach-event='onclick: openPopup'>${label}</button>",
56
57 openPopup: function(){
58 var self = this;
59
60 popupUtil.open({
61 popup: this.popup,
62 parent: this,
63 around: this.domNode,
64 orient: this.orient,
65 onCancel: function(){
66 log(self.id + ": cancel of child");
67 },
68 onExecute: function(){
69 log(self.id + ": execute of child");
70 popupUtil.close(self.popup);
71 self.open = false;
72 }
73 });
74
75 this.open = true;
76 },
77
78 closePopup: function(){
79 if(this.open){
80 log(this.id + ": close popup due to blur");
81 popupUtil.close(this.popup);
82 this.open = false;
83 }
84 },
85
86 _onBlur: function(){
87 // summary:
88 // This is called from focus manager and when we get the signal we
89 // need to close the drop down
90 this.closePopup();
91 }
92 });
93
94 declare("SimplePopup", [_WidgetBase, _TemplatedMixin], {
95 // summary:
96 // A trivial popup widget
97
98 templateString: "<span>i'm a popup</span>"
99 });
100
101 declare("Tooltip", [_WidgetBase, _TemplatedMixin], {
102 // summary:
103 // A popup with an orient() method to adjust arrows
104
105 templateString: "<span>i'm a tooltip</span>",
106 constructor: function(){
107 this.orientCalls = [];
108 },
109 orient: function(node, aroundCorner, corner, spaceAvailable, aroundNodeCoords){
110 this.orientCalls.push({
111 node: node,
112 aroundCorner: aroundCorner,
113 corner: corner,
114 spaceAvailable: spaceAvailable,
115 aroundNodeCoords: aroundNodeCoords
116 });
117 },
118 onOpen: function(pos){
119 // should be called with pos.corner and pos.aroundCorner set
120 this.onOpenArg = pos;
121 }
122 });
123
124 declare("SimpleChoiceWidget", [_WidgetBase, _TemplatedMixin], {
125 // summary:
126 // A list of values; select a value by pressing an entry in the list.
127
128 choice1: "1",
129 choice2: "2",
130 choice3: "3",
131
132 templateString:
133 "<div class='choice'>" +
134 "<div data-dojo-attach-event='onclick: onClick'>${choice1}</div>" +
135 "<div data-dojo-attach-event='onclick: onClick'>${choice2}</div>" +
136 "<div data-dojo-attach-event='onclick: onClick'>${choice3}</div>" +
137 "</div>",
138
139 onClick: function(e){
140 this.onChange(e.target.innerHTML);
141 },
142
143 onChange: function(val){
144 // summary:
145 // When this widget is used as a popup, popup monitors calls
146 // to onChange and then closes the popup
147 log(this.id + ": selected " + val);
148 }
149 });
150
151 declare("LotsOfChoicesWidget", [_WidgetBase], {
152 // summary:
153 // A list of values; select a value by pressing an entry in the list.
154
155 length: 100,
156
157 postCreate: function(){
158 for(var i=0; i< this.length; i++){
159 domConstruct.place("<div>choice #" + i + "</div>", this.domNode);
160 }
161 }
162 });
163
164 // Create a button that displays a simple drop down
165 choiceDropDown = new SimpleChoiceWidget();
166 (choiceDropDownButton = new SimpleDropDownButton({
167 id: "choiceDropDownButton",
168 label: "show choice drop down",
169 popup: choiceDropDown
170 })).placeAt(dom.byId("widgets"));
171
172 // Create a tall drop down, should get scroll bar
173 tallChoiceDropDown = new LotsOfChoicesWidget({length: 100});
174 (tallChoiceDropDownButton = new SimpleDropDownButton({
175 id: "tallChoiceDropDownButton",
176 label: "show tall drop down",
177 popup: tallChoiceDropDown
178 })).placeAt(dom.byId("widgets"));
179
180 declare("DialogWithPopupWidget", [_WidgetBase, _TemplatedMixin], {
181 // summary:
182 // This is a dialog that contains a button that spawns a drop down.
183 // Supply popup as an argument to this widget.
184
185 title: "I'm a dialog",
186 label: "click me",
187
188 templateString:
189 "<div style='width: 300px'>" +
190 "<div>${title}</div>" +
191 "<input><br>" +
192 "<button data-dojo-attach-point='button'>${label}</button><br>" +
193 "<button data-dojo-attach-event='onclick: onExecute'>OK</button>" +
194 "</div>",
195
196 postCreate: function(){
197 // Convert the plain button into a SimpleDropDownButton widget.
198 // Having it be a widget is important because that's how the popup
199 // code knows where a stack of nested popups (typically menus) ends.
200 // (In this case closing a stack of menus shouldn't close the dialog.)
201
202 new SimpleDropDownButton({
203 id: this.id + "PopupButton",
204 label: this.label,
205 popup: this.popup,
206 orient: ["after"] // so popup doesn't cover OK button
207 }, this.button);
208 },
209
210 onExecute: function(){
211 // summary:
212 // Called when OK button is pressed.
213 // If this is used as a popup this signals to the parent that
214 // Dialog can be closed.
215 console.log(this.id + ": executed");
216 }
217 });
218
219 // Create a button that displays a dialog that displays a choice widget
220 dialogDropDownButton = new SimpleDropDownButton({
221 id: "showSimpleDialogButton",
222 label: "show dialog",
223 popup: new DialogWithPopupWidget({
224 id: "simpleDialog",
225 label: 'show simple choice drop down',
226 popup: new SimpleChoiceWidget({
227 id: "choiceFromDialog"
228 })
229 })
230 }).placeAt(dom.byId("widgets"));
231
232 declare("NestedPopupOpener", [_WidgetBase, _TemplatedMixin], {
233 // summary:
234 // Clicking a value in this list will open a nested popup.
235 // Specify popup1 and popup2 as parameters to this widget.
236
237 templateString:
238 "<div class='choice'>" +
239 "<div data-dojo-attach-event='onclick: onClick'>popup1</div>" +
240 "<div data-dojo-attach-event='onclick: onClick'>popup2</div>" +
241 "</div>",
242
243 onClick: function(e){
244 var id = this.id,
245 popup = this[e.target.innerHTML];
246 log(id + ": opening popup " + popup.id);
247 this.openPopup(popup);
248 },
249
250 openPopup: function(popup){
251 popupUtil.open({
252 popup: popup,
253 parent: this,
254 around: this.domNode,
255 orient: ["after", "before"],
256 onCancel: function(){
257 log(id + ": cancel of child " + popup.id);
258 },
259 onExecute: function(){
260 log(id + ": execute of child " + popup.id);
261 popupUtil.close(popup);
262 }
263 })
264 },
265
266 closePopup: function(popup){
267 popupUtil.close(popup);
268 }
269 });
270
271 // Create a button that displays a nested drop down.
272 nestedOpener = new NestedPopupOpener({
273 id: 'nestedPopupOpener',
274 popup1: (nestedChoice1 = new SimpleChoiceWidget({
275 id: "nestedChoice1"
276 })),
277 popup2: (nestedChoice2 = new SimpleChoiceWidget({
278 id: "nestedChoice2",
279 choice1: "4",
280 choice2: "5",
281 choice3: "6"
282 }))
283 });
284 nestedDropDownButton = new SimpleDropDownButton({
285 id: "showNestedMenuButton",
286 label: "show nested drop down",
287 popup: nestedOpener
288 }).placeAt(dom.byId("widgets"));
289
290 // Create a button that displays a dialog that displays a nested drop down
291 dialogNestedChoice1 = new SimpleChoiceWidget({
292 id: "dialogNestedChoice1"
293 });
294 dialogNestedChoice2 = new SimpleChoiceWidget({
295 id: "dialogNestedChoice2",
296 choice1: "4",
297 choice2: "5",
298 choice3: "6"
299 });
300 dialogNestedPopupOpener = new NestedPopupOpener({
301 id: "nestedPopupOpenerFromDialog",
302 popup1: dialogNestedChoice1,
303 popup2: dialogNestedChoice2
304 });
305 dialogWithNestedPopup = new DialogWithPopupWidget({
306 id: "buttonInComplexDialog",
307 label: 'show nested menu',
308 popup: dialogNestedPopupOpener
309 });
310 dialogDropDownButton = new SimpleDropDownButton({
311 id: "showComplexDialogButton",
312 label: "show dialog w/nested menu",
313 popup: dialogWithNestedPopup
314 }).placeAt(dom.byId("widgets"));
315
316 // For testing, create an unattached widget and unattached DOMNode
317 new SimplePopup({id: "spw"}).placeAt(win.body());
318 domConstruct.place("<span id='sdn'>simple dom node</span>", win.body());
319
320 doh.register("API", [
321 function repeatMoveOffScreen(){
322 // Previously, calling moveOffScreen twice would cause an exception
323 popupUtil.moveOffScreen(registry.byId("spw"));
324 popupUtil.moveOffScreen(registry.byId("spw"));
325 }
326 ]);
327
328 doh.register("simple open and close", [
329 function initialConditions(t){
330 // If the popup code has cached any iframes, make sure they are hidden
331 query("iframe").forEach(function(node){
332 console.log("found iframe", node);
333 doh.is("none", node.style.display, "background iframe is hidden");
334 });
335 },
336
337 // Create popup on the edge of another widget
338 function openAround(){
339 var d = new doh.Deferred();
340
341 var around = choiceDropDownButton,
342 popup = choiceDropDown;
343
344 around.openPopup();
345
346 setTimeout(d.getTestCallback(function(){
347 doh.t(isVisible(popup.domNode), "popup is visible");
348
349 if(has("config-bgIframe")){
350 // Test the BackgroundIFrame
351 var iframes = query("iframe");
352 doh.is(1, iframes.length, "one background iframe");
353
354 var popupCoords = domGeometry.position(popup.domNode),
355 iframeCoords = domGeometry.position(iframes[0]);
356 doh.is(popupCoords.x, iframeCoords.x, "x is same: " + popupCoords.x);
357 doh.is(popupCoords.y, iframeCoords.y, "y is same: " + popupCoords.y);
358 doh.is(popupCoords.w, iframeCoords.w, "w is same: " + popupCoords.w);
359 doh.is(popupCoords.h, iframeCoords.h, "h is same: " + popupCoords.h);
360 doh.is("", iframes[0].style.display, "not display:none");
361 }
362 // TODO: test stack
363 }), 500);
364
365 return d;
366 },
367
368 function closeAround(){
369 // Close the popup
370 var around = choiceDropDownButton,
371 popup = choiceDropDown;
372
373 around.closePopup();
374
375 // Make sure the popup is hidden
376 doh.is("none", popup.domNode.parentNode.style.display, "popup is hidden");
377 }
378 ]);
379
380 doh.register("nested open and close", [
381 // Open first level
382 function openAround(){
383 var d = new doh.Deferred();
384
385 var around = nestedDropDownButton,
386 popup = nestedOpener,
387 nestedPopup = nestedChoice1;
388
389 around.openPopup();
390
391 setTimeout(d.getTestCallback(function(){
392 doh.t(isVisible(popup.domNode), "popup is visible");
393
394 if(has("ie") <= 6){
395 // Test the BackgroundIFrame
396 var iframes = query("iframe", popup.domNode.parentNode);
397 doh.is(1, iframes.length, "one background iframe on IE6");
398
399 var popupCoords = domGeometry.position(popup.domNode),
400 iframeCoords = domGeometry.position(iframes[0]);
401 doh.is(popupCoords.x, iframeCoords.x, "x is same: " + popupCoords.x);
402 doh.is(popupCoords.y, iframeCoords.y, "y is same: " + popupCoords.y);
403 doh.is(popupCoords.w, iframeCoords.w, "w is same: " + popupCoords.w);
404 doh.is(popupCoords.h, iframeCoords.h, "h is same: " + popupCoords.h);
405 doh.is("", iframes[0].style.display, "not display:none");
406 }
407 // TODO: test stack
408 }), 500);
409
410 return d;
411 },
412
413 function openNested(){
414 var d = new doh.Deferred();
415
416 var around = nestedDropDownButton,
417 popup = nestedOpener,
418 nestedPopup = nestedChoice1;
419
420 popup.openPopup(nestedPopup);
421
422 setTimeout(d.getTestCallback(function(){
423 doh.t(isVisible(nestedPopup.domNode), "nested popup is visible");
424
425 if(has("ie") <= 6){
426 // Test the BackgroundIFrame
427 var iframes = query("iframe", popup.domNode.parentNode);
428 doh.is(1, iframes.length, "one background iframe for popup");
429
430 iframes = query("iframe", nestedPopup.domNode.parentNode);
431 doh.is(1, iframes.length, "another background iframe for nested popup");
432
433 var popupCoords = domGeometry.position(nestedPopup.domNode),
434 iframeCoords = domGeometry.position(iframes[0]);
435 doh.is(popupCoords.x, iframeCoords.x, "x is same: " + popupCoords.x);
436 doh.is(popupCoords.y, iframeCoords.y, "y is same: " + popupCoords.y);
437 doh.is(popupCoords.w, iframeCoords.w, "w is same: " + popupCoords.w);
438 doh.is(popupCoords.h, iframeCoords.h, "h is same: " + popupCoords.h);
439 doh.is("", iframes[0].style.display, "not display:none");
440 }
441 // TODO: test stack
442 }), 500);
443
444 return d;
445 },
446
447 function closeAround(){
448 // Close the layer of popups
449 var around = nestedDropDownButton,
450 popup = nestedOpener,
451 nestedPopup = nestedChoice1;
452
453 around.closePopup();
454
455 // Make sure the popups are both hidden
456 // (in a future release this might change to display:none etc but currently it's visibility:hidden)
457 doh.is("none", popup.domNode.parentNode.style.display, "popup is hidden");
458 doh.is("none", nestedPopup.domNode.parentNode.style.display, "nested popup is hidden");
459 }
460 ]);
461
462 // Make sure there are no hidden tab stops when popups are closed.
463 // This test doesn't work on safari (due to safari bugs) although there aren't any hidden tab stops.
464 doh.register("no hidden tab stops", function hiddenTabStops(){
465 var tabbable = tabOrder();
466 doh.is("inputAtStart", tabbable[0].id, "first tabbable node");
467 doh.is("inputAtEnd", tabbable[tabbable.length - 1].id, "last tabbable node");
468 });
469
470 doh.register("x/y placement", function xyPlacement(){
471 // Trivial test of x/y placement. Could also test other orientations.
472 var popup = new SimpleChoiceWidget();
473
474 popupUtil.open({
475 popup: popup,
476 orient: "R",
477 x: 10,
478 y: 15
479 });
480
481 var popupCoords = domGeometry.position(popup.domNode);
482 doh.is(10, popupCoords.x, "popup x coord");
483 doh.is(15, popupCoords.y, "popup y coord");
484
485 popupUtil.close(popup);
486 });
487
488 doh.register("orientCallback", [
489 function setup(){
490 tooltip = new Tooltip({});
491 (tooltipDropDownButton = new SimpleDropDownButton({
492 id: "tooltipDropDownButton",
493 label: "show tooltip",
494 popup: tooltip
495 })).placeAt(dom.byId("widgets"));
496 },
497 function orientCallbackAround(){
498 // Test whether orient() is called correctly for popup around a node
499 tooltipDropDownButton.openPopup();
500 tooltipDropDownButton.closePopup();
501
502 // The final call to orient(), as well as the call to onOpen(), should have been for the final
503 // position of the node, where corner == TL and aroundCorner == BL
504 doh.t(tooltip.orientCalls.length, "orient was called");
505 var final = tooltip.orientCalls.pop();
506 doh.is("TL", final.corner, "popup corner");
507 doh.is("BL", final.aroundCorner, "aroundNode corner");
508
509 doh.t(tooltip.onOpenArg, "onOpen called");
510 doh.is("TL", tooltip.onOpenArg.corner, "popup corner");
511 doh.is("BL", tooltip.onOpenArg.aroundCorner, "aroundNode corner");
512 },
513 function orientCallbackAt(){
514 // Test whether orient() is called correctly for popup around a point
515 tooltip.orientCalls = [];
516 delete tooltip.onOpenArg;
517
518 popupUtil.open({
519 popup: tooltip,
520 orient: "R",
521 x: 10,
522 y: 15
523 });
524
525 // The final call to orient(), as well as the call to onOpen(), should have been for the final
526 // position of the node, where corner == TL and aroundCorner == BR (they are caddy-corner).
527 doh.t(tooltip.orientCalls.length, "orient was called");
528 var final = tooltip.orientCalls.pop();
529 doh.is("TL", final.corner, "popup corner");
530 doh.is("BR", final.aroundCorner, "aroundNode corner");
531
532 doh.t(tooltip.onOpenArg, "onOpen called");
533 doh.is("TL", tooltip.onOpenArg.corner, "popup corner");
534 doh.is("BR", tooltip.onOpenArg.aroundCorner, "aroundNode corner");
535 }
536 ]);
537
538 // Test that onCancel goes back one level.
539 // Call nestedChoice2.onCancel() and that should trigger nestedChoice2 to close
540 // but nestedOpener stays open.
541 // (TODO)
542
543 // Test that onChange/onExecute goes back to top of popup chain.
544 // Call dialogNestedChoice2.onChange() and that should trigger dialogNestedChoice2
545 // and dialogNestedPopupOpener to close, but not dialogWithNestedPopup.
546 // (TODO)
547
548 // ---------------------
549 // a11y tests
550
551 // Test that ESC key closes one level of a popup chain.
552 // Focus nestedChoice2 and type ESC, and that should trigger nestedChoice2 to close
553 // but nestedOpener stays open.
554 // (TODO)
555
556 // Test that TAB key cancels back to top of popup chain.
557 // Focus nestedChoice2 and type TAB, and that should trigger dialogNestedChoice2
558 // and dialogNestedPopupOpener to close, but not dialogWithNestedPopup.
559 // (TODO)
560
561 doh.register("a11y tests", function ariaOnWrapper(){
562 var popup = new SimpleChoiceWidget({id: "scwPopup"});
563
564 popupUtil.open({
565 popup: popup,
566 orient: "R",
567 x: 10,
568 y: 15
569 });
570
571 var w = popup.domNode;
572 doh.is("region", w.parentNode.getAttribute("role"), "popup's wrapper node needs role=region");
573 doh.is("scwPopup", w.parentNode.getAttribute("aria-label"), "popup's wrapper node needs aria-label");
574 });
575
576 doh.register("scrollbar tests", [
577 function scrollbarAt(){
578 var popup = new LotsOfChoicesWidget({id: "tallPopup1"});
579
580 popupUtil.open({
581 popup: popup,
582 orient: "R",
583 x: 10,
584 y: 15
585 });
586
587 var w = popup.domNode;
588 doh.t(domStyle.get(w, "height") > winUtils.getBox().h, "popup is taller than the viewport");
589 doh.t(domStyle.get(w.parentNode, "height") < winUtils.getBox().h, "but wrapper " +
590 domStyle.get(w.parentNode, "height") + " is shorter than viewport " + winUtils.getBox().h);
591 },
592 function scrollbarAround(){
593 tallChoiceDropDownButton.openPopup();
594
595 var w = tallChoiceDropDown.domNode;
596 doh.t(domStyle.get(w, "height") > winUtils.getBox().h, "popup is taller than the viewport");
597 doh.t(domStyle.get(w.parentNode, "height") < winUtils.getBox().h, "but wrapper is shorter than viewport");
598 }
599 ]);
600
601 doh.run();
602 });
603 </script>
604 </head>
605 <body>
606 <h1>popup and BackgroundIFrame Unit Test</h1>
607 <label for="inputAtStart">label:</label><input id="inputAtStart">
608 <div style="height: 150px; overflow: auto; margin: 10px 0px;">
609 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus odio eu nisl ultrices dictum. Sed tincidunt metus et magna placerat eget vehicula dolor faucibus. Nunc nec augue ac mi rutrum congue. Donec at augue felis. Proin et lectus at mauris adipiscing pulvinar tempor vitae lacus. Aliquam erat volutpat. Sed eget sem eu turpis ultrices ullamcorper sed ut augue. Nunc in augue lectus. Curabitur ac posuere libero. Duis luctus dignissim nisl suscipit vehicula. Cras ut augue odio. Integer blandit ligula congue erat pellentesque nec egestas mi hendrerit</p>
610 <span id="widgets"></span>
611 <p>Nunc sollicitudin nisl sed est porta vitae viverra nulla rutrum. Praesent erat tortor, scelerisque sit amet gravida a, sodales a libero. Pellentesque nec arcu nulla, id laoreet orci. Vivamus sit amet quam eu ante pulvinar rhoncus sit amet non ipsum. Sed mattis felis sed risus tincidunt in sagittis justo rhoncus. Praesent ut justo feugiat neque gravida convallis eget mattis felis. Vestibulum vitae velit ante, sed convallis sem. Curabitur gravida volutpat odio eget tincidunt. Mauris pellentesque placerat massa ut venenatis. Mauris ultrices hendrerit dui vel fermentum.</p>
612 </div>
613 <label for="inputAtEnd">label:</label><input id="inputAtEnd">
614 </body>
615 </html>