| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 | |
| 6 | <title>_WidgetBase on() test</title> |
| 7 | |
| 8 | <script src="boilerplate.js"></script> |
| 9 | |
| 10 | <script> |
| 11 | require([ |
| 12 | "doh/runner", |
| 13 | "dojo/_base/declare", "dojo/has", "dojo/_base/lang", "dojo/on", "dojo/parser", "dojo/touch", |
| 14 | "dijit/registry", "dijit/_WidgetBase", "dojo/domReady!" |
| 15 | ], function(doh, declare, has, lang, on, parser, touch, registry, _WidgetBase){ |
| 16 | |
| 17 | var mousedOver, clicked; |
| 18 | |
| 19 | doh.register("on", [ |
| 20 | function setup(){ |
| 21 | declare("MyWidget", _WidgetBase, { |
| 22 | postCreate: function(){ |
| 23 | on(this.domNode, "click", lang.hitch(this, "onFooBar")); |
| 24 | }, |
| 25 | onFooBar: function(){ |
| 26 | // This is called whenever the widget is clicked |
| 27 | }, |
| 28 | foobar: function(){ |
| 29 | // A widget.on("foobar") should go to onFooBar() (above), not here |
| 30 | } |
| 31 | }); |
| 32 | |
| 33 | parser.parse(); |
| 34 | }, |
| 35 | |
| 36 | function connect(){ |
| 37 | // This should work despite the fact that the function onMouseOver has |
| 38 | // multiple capital letters |
| 39 | registry.byId("myWidget").on("mouseover", function(){ |
| 40 | mousedOver = true; |
| 41 | console.log("mouseover event"); |
| 42 | }); |
| 43 | |
| 44 | // Likewise, this should work despite the fact that the function onFooBar has |
| 45 | // multiple capital letters |
| 46 | registry.byId("myWidget").on("foobar", function(){ |
| 47 | clicked = true; |
| 48 | console.log("click event"); |
| 49 | }); |
| 50 | }, |
| 51 | |
| 52 | function test(){ |
| 53 | var myWidget = registry.byId("myWidget"); |
| 54 | |
| 55 | // Test that _WidgetBase.on() catches click event |
| 56 | doh.f(clicked, "clicked"); |
| 57 | on.emit(myWidget.domNode, "click", { |
| 58 | bubbles: true, |
| 59 | cancelable: true, |
| 60 | which: 1 |
| 61 | }); |
| 62 | doh.t(clicked, "clicked"); |
| 63 | |
| 64 | // Test that _WidgetBase.on() catches mouseover event |
| 65 | doh.f(mousedOver, "mousedOver"); |
| 66 | on.emit(myWidget.domNode, "mouseover", { |
| 67 | bubbles: true, |
| 68 | cancelable: true, |
| 69 | which: 1 |
| 70 | }); |
| 71 | doh.t(mousedOver, "mousedOver"); |
| 72 | }, |
| 73 | |
| 74 | function synthetic(){ |
| 75 | // Test that on() works for synthetic events |
| 76 | |
| 77 | var myWidget = registry.byId("myWidget"), |
| 78 | touched; |
| 79 | |
| 80 | myWidget.on(dojo.touch.press, function(){ |
| 81 | touched = true; |
| 82 | }); |
| 83 | |
| 84 | var eventType = has("pointer-events") ? "pointerdown" : has("MSPointer") ? |
| 85 | "MSPointerDown" : has("touch-events") ? "touchstart" : "mousedown", |
| 86 | target = myWidget.domNode; |
| 87 | |
| 88 | if(document.createEvent && target.dispatchEvent){ |
| 89 | // This branch needed or the test fails on iOS in dojo/on, |
| 90 | // because changedTouches[0] isn't defined. |
| 91 | |
| 92 | var touch = { |
| 93 | identifier: (new Date()).getTime(), |
| 94 | target: target, |
| 95 | screenX: 0, |
| 96 | screenY: 0, |
| 97 | clientX: 0, |
| 98 | clientY: 0, |
| 99 | pageX: 0, |
| 100 | pageY: 0 |
| 101 | }; |
| 102 | |
| 103 | var mouseEvent = document.createEvent("MouseEvent"); |
| 104 | mouseEvent.initMouseEvent( |
| 105 | eventType, |
| 106 | true, |
| 107 | true, |
| 108 | target.ownerDocument.defaultView, |
| 109 | 0, |
| 110 | 0, |
| 111 | 0, |
| 112 | 0, |
| 113 | 0, |
| 114 | false, |
| 115 | false, |
| 116 | false, |
| 117 | false, |
| 118 | 0, |
| 119 | document.body // relatedTarget, for mouseout events |
| 120 | ); |
| 121 | |
| 122 | if(/touch/.test(eventType)){ |
| 123 | mouseEvent['touches'] = [touch]; |
| 124 | mouseEvent['targetTouches'] = [touch]; |
| 125 | mouseEvent['changedTouches'] = [touch]; |
| 126 | } |
| 127 | |
| 128 | target.dispatchEvent(mouseEvent); |
| 129 | }else{ |
| 130 | on.emit(myWidget.domNode, eventType, { |
| 131 | bubbles: true, |
| 132 | cancelable: true, |
| 133 | which: 1 |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | doh.t(touched, "touched"); |
| 138 | }, |
| 139 | |
| 140 | function syntheticNoCallbackArgs(){ |
| 141 | var evt = null; |
| 142 | var MyWidget = declare(_WidgetBase, { |
| 143 | show: function(){ |
| 144 | return this.emit('show'); |
| 145 | }, |
| 146 | onshow: function(e){ |
| 147 | evt = e; |
| 148 | } |
| 149 | }); |
| 150 | new MyWidget({}).show(); |
| 151 | doh.isNot(null, evt, "onshow was called with event object"); |
| 152 | } |
| 153 | ]); |
| 154 | |
| 155 | doh.run(); |
| 156 | }); |
| 157 | |
| 158 | </script> |
| 159 | </head> |
| 160 | <body> |
| 161 | <div id="myWidget" data-dojo-type="MyWidget"> |
| 162 | mouseover and click events to console |
| 163 | </div> |
| 164 | </body> |
| 165 | </html> |