1 | <!DOCTYPE html> |
---|
2 | <html> |
---|
3 | <head> |
---|
4 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
---|
5 | <title>_WidgetBase on() test</title> |
---|
6 | <style type="text/css"> |
---|
7 | @import "../themes/claro/document.css"; |
---|
8 | @import "../themes/claro/claro.css"; |
---|
9 | @import "css/dijitTests.css"; |
---|
10 | </style> |
---|
11 | |
---|
12 | <script type="text/javascript" src="../../dojo/dojo.js" |
---|
13 | data-dojo-config="isDebug: true"></script> |
---|
14 | <script> |
---|
15 | require([ |
---|
16 | "doh/runner", |
---|
17 | "dojo/_base/declare", "dojo/has", "dojo/_base/lang", "dojo/on", "dojo/parser", "dojo/touch", |
---|
18 | "dijit/registry", "dijit/_WidgetBase", "dojo/domReady!" |
---|
19 | ], function(doh, declare, has, lang, on, parser, touch, registry, _WidgetBase){ |
---|
20 | |
---|
21 | var mousedOver, clicked; |
---|
22 | |
---|
23 | doh.register("on", [ |
---|
24 | function setup(){ |
---|
25 | declare("MyWidget", _WidgetBase, { |
---|
26 | postCreate: function(){ |
---|
27 | on(this.domNode, "click", lang.hitch(this, "onFooBar")); |
---|
28 | }, |
---|
29 | onFooBar: function(){ |
---|
30 | // This is called whenever the widget is clicked |
---|
31 | }, |
---|
32 | foobar: function(){ |
---|
33 | // A widget.on("foobar") should go to onFooBar() (above), not here |
---|
34 | } |
---|
35 | }); |
---|
36 | |
---|
37 | parser.parse(); |
---|
38 | }, |
---|
39 | |
---|
40 | function connect(){ |
---|
41 | // This should work despite the fact that the function onMouseOver has |
---|
42 | // multiple capital letters |
---|
43 | registry.byId("myWidget").on("mouseover", function(){ |
---|
44 | mousedOver = true; |
---|
45 | console.log("mouseover event"); |
---|
46 | }); |
---|
47 | |
---|
48 | // Likewise, this should work despite the fact that the function onFooBar has |
---|
49 | // multiple capital letters |
---|
50 | registry.byId("myWidget").on("foobar", function(){ |
---|
51 | clicked = true; |
---|
52 | console.log("click event"); |
---|
53 | }); |
---|
54 | }, |
---|
55 | |
---|
56 | function test(){ |
---|
57 | var myWidget = registry.byId("myWidget"); |
---|
58 | |
---|
59 | // Test that _WidgetBase.on() catches click event |
---|
60 | doh.f(clicked, "clicked"); |
---|
61 | on.emit(myWidget.domNode, "click", { |
---|
62 | bubbles: true, |
---|
63 | cancelable: true, |
---|
64 | which: 1 |
---|
65 | }); |
---|
66 | doh.t(clicked, "clicked"); |
---|
67 | |
---|
68 | // Test that _WidgetBase.on() catches mouseover event |
---|
69 | doh.f(mousedOver, "mousedOver"); |
---|
70 | on.emit(myWidget.domNode, "mouseover", { |
---|
71 | bubbles: true, |
---|
72 | cancelable: true, |
---|
73 | which: 1 |
---|
74 | }); |
---|
75 | doh.t(mousedOver, "mousedOver"); |
---|
76 | }, |
---|
77 | |
---|
78 | function synthetic(){ |
---|
79 | // Test that on() works for synthetic events |
---|
80 | |
---|
81 | var myWidget = registry.byId("myWidget"), |
---|
82 | touched; |
---|
83 | |
---|
84 | myWidget.on(dojo.touch.press, function(){ |
---|
85 | touched = true; |
---|
86 | }); |
---|
87 | |
---|
88 | on.emit(myWidget.domNode, navigator.pointerEnabled ? "pointerdown" : navigator.msPointerEnabled ? "MSPointerDown" : has("touch") ? "touchstart" : "mousedown", { |
---|
89 | bubbles: true, |
---|
90 | cancelable: true, |
---|
91 | which: 1 |
---|
92 | }); |
---|
93 | |
---|
94 | doh.t(touched, "touched"); |
---|
95 | }, |
---|
96 | |
---|
97 | function syntheticNoCallbackArgs(){ |
---|
98 | var evt = null; |
---|
99 | var MyWidget = declare(_WidgetBase, { |
---|
100 | show: function(){ |
---|
101 | return this.emit('show'); |
---|
102 | }, |
---|
103 | onshow: function(e){ |
---|
104 | evt = e; |
---|
105 | } |
---|
106 | }); |
---|
107 | new MyWidget({}).show(); |
---|
108 | doh.isNot(null, evt, "onshow was called with event object"); |
---|
109 | } |
---|
110 | ]); |
---|
111 | |
---|
112 | doh.run(); |
---|
113 | }); |
---|
114 | |
---|
115 | </script> |
---|
116 | </head> |
---|
117 | <body class="claro"> |
---|
118 | <div id="myWidget" data-dojo-type="MyWidget"> |
---|
119 | mouseover and click events to console |
---|
120 | </div> |
---|
121 | </body> |
---|
122 | </html> |
---|