]>
Commit | Line | Data |
---|---|---|
f0cfe83e AD |
1 | define("dojo/touch", ["./_base/kernel", "./aspect", "./dom", "./on", "./has", "./mouse", "./ready", "./_base/window"], |
2 | function(dojo, aspect, dom, on, has, mouse, ready, win){ | |
3 | ||
4 | // module: | |
5 | // dojo/touch | |
6 | ||
7 | var hasTouch = has("touch"); | |
8 | ||
9 | // TODO: get iOS version from dojo/sniff after #15827 is fixed | |
10 | var ios4 = false; | |
11 | if(has("ios")){ | |
12 | var ua = navigator.userAgent; | |
13 | var v = ua.match(/OS ([\d_]+)/) ? RegExp.$1 : "1"; | |
14 | var os = parseFloat(v.replace(/_/, '.').replace(/_/g, '')); | |
15 | ios4 = os < 5; | |
16 | } | |
17 | ||
18 | var touchmove, hoveredNode; | |
19 | ||
20 | if(hasTouch){ | |
21 | ready(function(){ | |
22 | // Keep track of currently hovered node | |
23 | hoveredNode = win.body(); // currently hovered node | |
24 | ||
25 | win.doc.addEventListener("touchstart", function(evt){ | |
26 | // Precede touchstart event with touch.over event. DnD depends on this. | |
27 | // Use addEventListener(cb, true) to run cb before any touchstart handlers on node run, | |
28 | // and to ensure this code runs even if the listener on the node does event.stop(). | |
29 | var oldNode = hoveredNode; | |
30 | hoveredNode = evt.target; | |
31 | on.emit(oldNode, "dojotouchout", { | |
32 | target: oldNode, | |
33 | relatedTarget: hoveredNode, | |
34 | bubbles: true | |
35 | }); | |
36 | on.emit(hoveredNode, "dojotouchover", { | |
37 | target: hoveredNode, | |
38 | relatedTarget: oldNode, | |
39 | bubbles: true | |
40 | }); | |
41 | }, true); | |
42 | ||
43 | // Fire synthetic touchover and touchout events on nodes since the browser won't do it natively. | |
44 | on(win.doc, "touchmove", function(evt){ | |
45 | var newNode = win.doc.elementFromPoint( | |
46 | evt.pageX - (ios4 ? 0 : win.global.pageXOffset), // iOS 4 expects page coords | |
47 | evt.pageY - (ios4 ? 0 : win.global.pageYOffset) | |
48 | ); | |
49 | if(newNode && hoveredNode !== newNode){ | |
50 | // touch out on the old node | |
51 | on.emit(hoveredNode, "dojotouchout", { | |
52 | target: hoveredNode, | |
53 | relatedTarget: newNode, | |
54 | bubbles: true | |
55 | }); | |
56 | ||
57 | // touchover on the new node | |
58 | on.emit(newNode, "dojotouchover", { | |
59 | target: newNode, | |
60 | relatedTarget: hoveredNode, | |
61 | bubbles: true | |
62 | }); | |
63 | ||
64 | hoveredNode = newNode; | |
65 | } | |
66 | }); | |
67 | }); | |
68 | ||
69 | // Define synthetic touch.move event that unlike the native touchmove, fires for the node the finger is | |
70 | // currently dragging over rather than the node where the touch started. | |
71 | touchmove = function(node, listener){ | |
72 | return on(win.doc, "touchmove", function(evt){ | |
73 | if(node === win.doc || dom.isDescendant(hoveredNode, node)){ | |
74 | evt.target = hoveredNode; | |
75 | listener.call(this, evt); | |
76 | } | |
77 | }); | |
78 | }; | |
79 | } | |
80 | ||
81 | ||
82 | function _handle(type){ | |
83 | // type: String | |
84 | // press | move | release | cancel | |
85 | ||
86 | return function(node, listener){//called by on(), see dojo.on | |
87 | return on(node, type, listener); | |
88 | }; | |
89 | } | |
90 | ||
91 | //device neutral events - touch.press|move|release|cancel/over/out | |
92 | var touch = { | |
93 | press: _handle(hasTouch ? "touchstart": "mousedown"), | |
94 | move: hasTouch ? touchmove :_handle("mousemove"), | |
95 | release: _handle(hasTouch ? "touchend": "mouseup"), | |
96 | cancel: hasTouch ? _handle("touchcancel") : mouse.leave, | |
97 | over: _handle(hasTouch ? "dojotouchover": "mouseover"), | |
98 | out: _handle(hasTouch ? "dojotouchout": "mouseout"), | |
99 | enter: mouse._eventHandler(hasTouch ? "dojotouchover" : "mouseover"), | |
100 | leave: mouse._eventHandler(hasTouch ? "dojotouchout" : "mouseout") | |
101 | }; | |
102 | /*===== | |
103 | touch = { | |
104 | // summary: | |
105 | // This module provides unified touch event handlers by exporting | |
106 | // press, move, release and cancel which can also run well on desktop. | |
107 | // Based on http://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html | |
108 | // | |
109 | // example: | |
110 | // Used with dojo.on | |
111 | // | define(["dojo/on", "dojo/touch"], function(on, touch){ | |
112 | // | on(node, touch.press, function(e){}); | |
113 | // | on(node, touch.move, function(e){}); | |
114 | // | on(node, touch.release, function(e){}); | |
115 | // | on(node, touch.cancel, function(e){}); | |
116 | // example: | |
117 | // Used with touch.* directly | |
118 | // | touch.press(node, function(e){}); | |
119 | // | touch.move(node, function(e){}); | |
120 | // | touch.release(node, function(e){}); | |
121 | // | touch.cancel(node, function(e){}); | |
122 | ||
123 | press: function(node, listener){ | |
124 | // summary: | |
125 | // Register a listener to 'touchstart'|'mousedown' for the given node | |
126 | // node: Dom | |
127 | // Target node to listen to | |
128 | // listener: Function | |
129 | // Callback function | |
130 | // returns: | |
131 | // A handle which will be used to remove the listener by handle.remove() | |
132 | }, | |
133 | move: function(node, listener){ | |
134 | // summary: | |
135 | // Register a listener to 'touchmove'|'mousemove' for the given node | |
136 | // node: Dom | |
137 | // Target node to listen to | |
138 | // listener: Function | |
139 | // Callback function | |
140 | // returns: | |
141 | // A handle which will be used to remove the listener by handle.remove() | |
142 | }, | |
143 | release: function(node, listener){ | |
144 | // summary: | |
145 | // Register a listener to 'touchend'|'mouseup' for the given node | |
146 | // node: Dom | |
147 | // Target node to listen to | |
148 | // listener: Function | |
149 | // Callback function | |
150 | // returns: | |
151 | // A handle which will be used to remove the listener by handle.remove() | |
152 | }, | |
153 | cancel: function(node, listener){ | |
154 | // summary: | |
155 | // Register a listener to 'touchcancel'|'mouseleave' for the given node | |
156 | // node: Dom | |
157 | // Target node to listen to | |
158 | // listener: Function | |
159 | // Callback function | |
160 | // returns: | |
161 | // A handle which will be used to remove the listener by handle.remove() | |
162 | }, | |
163 | over: function(node, listener){ | |
164 | // summary: | |
165 | // Register a listener to 'mouseover' or touch equivalent for the given node | |
166 | // node: Dom | |
167 | // Target node to listen to | |
168 | // listener: Function | |
169 | // Callback function | |
170 | // returns: | |
171 | // A handle which will be used to remove the listener by handle.remove() | |
172 | }, | |
173 | out: function(node, listener){ | |
174 | // summary: | |
175 | // Register a listener to 'mouseout' or touch equivalent for the given node | |
176 | // node: Dom | |
177 | // Target node to listen to | |
178 | // listener: Function | |
179 | // Callback function | |
180 | // returns: | |
181 | // A handle which will be used to remove the listener by handle.remove() | |
182 | }, | |
183 | enter: function(node, listener){ | |
184 | // summary: | |
185 | // Register a listener to mouse.enter or touch equivalent for the given node | |
186 | // node: Dom | |
187 | // Target node to listen to | |
188 | // listener: Function | |
189 | // Callback function | |
190 | // returns: | |
191 | // A handle which will be used to remove the listener by handle.remove() | |
192 | }, | |
193 | leave: function(node, listener){ | |
194 | // summary: | |
195 | // Register a listener to mouse.leave or touch equivalent for the given node | |
196 | // node: Dom | |
197 | // Target node to listen to | |
198 | // listener: Function | |
199 | // Callback function | |
200 | // returns: | |
201 | // A handle which will be used to remove the listener by handle.remove() | |
202 | } | |
203 | }; | |
204 | =====*/ | |
205 | ||
206 | 1 && (dojo.touch = touch); | |
207 | ||
208 | return touch; | |
209 | }); |