Debian Codegraph @

dojox/geo/charting/TouchInteractionSupport.js

raw symbols SCIP
1 define([
2 "dojo/_base/lang",
3 "dojo/_base/declare",
4 "dojo/_base/event",
5 "dojo/_base/connect",
6 "dojo/_base/window"
7 ], function(lang,declare,event,connect,win){
8
9 return declare("dojox.geo.charting.TouchInteractionSupport",null, {
10 // summary:
11 // A class to handle touch interactions on a dojox/geo/charting/Map component.
12 // tags:
13 // private
14
15 _map: null,
16 _centerTouchLocation: null,
17 _touchMoveListener: null,
18 _touchEndListener: null,
19 _touchEndTapListener: null,
20 _touchStartListener: null,
21 _initialFingerSpacing: null,
22 _initialScale: null,
23 _tapCount: null,
24 _tapThreshold: null,
25 _lastTap: null,
26 _doubleTapPerformed:false,
27 _oneFingerTouch:false,
28 _tapCancel:false,
29
30 constructor: function(map){
31 // summary:
32 // Constructs a new _TouchInteractionSupport instance
33 // map: dojox.geo.charting.Map
34 // the Map widget this class provides touch navigation for.
35 this._map = map;
36 this._centerTouchLocation = {x: 0,y: 0};
37
38 this._tapCount = 0;
39 this._lastTap = {x: 0,y: 0};
40 this._tapThreshold = 100; // square distance in pixels
41 },
42
43 connect: function(){
44 // summary:
45 // install touch listeners
46 this._touchStartListener = this._map.surface.connect("touchstart", this, this._touchStartHandler);
47 },
48
49 disconnect: function(){
50 // summary:
51 // disconnects any installed listeners. Must be called only when disposing of this instance
52 if(this._touchStartListener){
53 connect.disconnect(this._touchStartListener);
54 this._touchStartListener = null;
55 }
56 },
57
58 _getTouchBarycenter: function(touchEvent){
59 // summary:
60 // returns the midpoint of the two first fingers (or the first finger location if only one)
61 // touchEvent: TouchEvent
62 // a touch event
63 // returns:
64 // the midpoint
65 // tags:
66 // private
67 var touches = touchEvent.touches;
68 var firstTouch = touches[0];
69 var secondTouch = null;
70 if(touches.length > 1){
71 secondTouch = touches[1];
72 }else{
73 secondTouch = touches[0];
74 }
75 var containerBounds = this._map._getContainerBounds();
76 var middleX = (firstTouch.pageX + secondTouch.pageX) / 2.0 - containerBounds.x;
77 var middleY = (firstTouch.pageY + secondTouch.pageY) / 2.0 - containerBounds.y;
78 return {x: middleX,y: middleY}; // dojox/gfx/Point
79 },
80
81 _getFingerSpacing: function(touchEvent){
82 // summary:
83 // computes the distance between the first two fingers
84 // touchEvent: a touch event
85 // returns:
86 // a distance. -1 if less than 2 fingers
87 // tags:
88 // private
89 var touches = touchEvent.touches;
90 var spacing = -1;
91 if(touches.length >= 2){
92 var dx = (touches[1].pageX - touches[0].pageX);
93 var dy = (touches[1].pageY - touches[0].pageY);
94 spacing = Math.sqrt(dx*dx + dy*dy);
95 }
96 return spacing;
97 },
98
99 _isDoubleTap: function(touchEvent){
100 // summary:
101 // checks whether the specified touchStart event is a double tap
102 // (i.e. follows closely a previous touchStart at approximately the same location)
103 // touchEvent: TouchEvent
104 // a touch event
105 // returns:
106 // true if this event is considered a double tap
107 // tags:
108 // private
109 var isDoubleTap = false;
110 var touches = touchEvent.touches;
111 if((this._tapCount > 0) && touches.length == 1){
112 // test distance from last tap
113 var dx = (touches[0].pageX - this._lastTap.x);
114 var dy = (touches[0].pageY - this._lastTap.y);
115 var distance = dx*dx + dy*dy;
116 if(distance < this._tapThreshold){
117 isDoubleTap = true;
118 }else{
119 this._tapCount = 0;
120 }
121 }
122 this._tapCount++;
123 this._lastTap.x = touches[0].pageX;
124 this._lastTap.y = touches[0].pageY;
125 setTimeout(lang.hitch(this,function(){
126 this._tapCount = 0;}),300);
127 return isDoubleTap;
128 },
129
130 _doubleTapHandler: function(touchEvent){
131 // summary:
132 // action performed on the map when a double tap was triggered
133 // touchEvent: TouchEvent
134 // a touch event
135 // tags:
136 // private
137 var feature = this._getFeatureFromTouchEvent(touchEvent);
138 if(feature){
139 this._map.fitToMapArea(feature._bbox, 15, true);
140 }else{
141 // perform a basic 2x zoom on touch
142 var touches = touchEvent.touches;
143 var containerBounds = this._map._getContainerBounds();
144 var offX = touches[0].pageX - containerBounds.x;
145 var offY = touches[0].pageY - containerBounds.y;
146 // clicked map point before zooming
147 var mapPoint = this._map.screenCoordsToMapCoords(offX,offY);
148 // zoom increment power
149 this._map.setMapCenterAndScale(mapPoint.x, mapPoint.y,this._map.getMapScale()*2,true);
150 }
151 },
152
153 _getFeatureFromTouchEvent: function(touchEvent){
154 // summary:
155 // utility function to return the feature located at this touch event location
156 // touchEvent: TouchEvent
157 // a touch event
158 // returns:
159 // the feature found if any, null otherwise.
160 // tags:
161 // private
162 var feature = null;
163 if(touchEvent.gfxTarget && touchEvent.gfxTarget.getParent){
164 feature = this._map.mapObj.features[touchEvent.gfxTarget.getParent().id];
165 }
166 return feature; // dojox/geo/charting/Feature
167 },
168
169 _touchStartHandler: function(touchEvent){
170 // summary:
171 // action performed on the map when a touch start was triggered
172 // touchEvent: TouchEvent
173 // a touch event
174 // tags:
175 // private
176 event.stop(touchEvent);
177 this._oneFingerTouch = (touchEvent.touches.length == 1);
178 this._tapCancel = !this._oneFingerTouch;
179 // test double tap
180 this._doubleTapPerformed = false;
181 if(this._isDoubleTap(touchEvent)){
182 //console.log("double tap recognized");
183 this._doubleTapHandler(touchEvent);
184 this._doubleTapPerformed = true;
185 return;
186 }
187 // compute map midpoint between fingers
188 var middlePoint = this._getTouchBarycenter(touchEvent);
189 var mapPoint = this._map.screenCoordsToMapCoords(middlePoint.x,middlePoint.y);
190 this._centerTouchLocation.x = mapPoint.x;
191 this._centerTouchLocation.y = mapPoint.y;
192 // store initial finger spacing to compute zoom later
193 this._initialFingerSpacing = this._getFingerSpacing(touchEvent);
194 // store initial map scale
195 this._initialScale = this._map.getMapScale();
196 // install touch move and up listeners (if not done by other fingers before)
197 if(!this._touchMoveListener){
198 this._touchMoveListener = connect.connect(win.global,"touchmove",this,this._touchMoveHandler);
199 }
200 if(!this._touchEndTapListener){
201 this._touchEndTapListener = this._map.surface.connect("touchend", this, this._touchEndTapHandler);
202 }
203 if(!this._touchEndListener){
204 this._touchEndListener = connect.connect(win.global,"touchend",this, this._touchEndHandler);
205 }
206 },
207
208 _touchEndTapHandler: function(touchEvent){
209 // summary:
210 // action performed on the map when a tap was triggered
211 // touchEvent: TouchEvent
212 // a touch event
213 // tags:
214 // private
215 var touches = touchEvent.touches;
216 if(touches.length == 0){
217
218 // test potential tap ?
219 if(this._oneFingerTouch && !this._tapCancel){
220 this._oneFingerTouch = false;
221 setTimeout(lang.hitch(this,function(){
222 // wait to check if double tap
223 // perform test for single tap
224 //console.log("double tap was performed ? " + this._doubleTapPerformed);
225 if(!this._doubleTapPerformed){
226 // test distance from last tap
227 var dx = (touchEvent.changedTouches[0].pageX - this._lastTap.x);
228 var dy = (touchEvent.changedTouches[0].pageY - this._lastTap.y);
229 var distance = dx*dx + dy*dy;
230 if(distance < this._tapThreshold){
231 // single tap ok
232 this._singleTapHandler(touchEvent);
233 }
234 }
235 }), 350);
236 }
237 this._tapCancel = false;
238 }
239 },
240
241 _touchEndHandler: function(touchEvent){
242 // summary:
243 // action performed on the map when a touch end was triggered
244 // touchEvent: TouchEvent
245 // a touch event
246 // tags:
247 // private
248 event.stop(touchEvent);
249 var touches = touchEvent.touches;
250 if(touches.length == 0){
251 // disconnect listeners only when all fingers are up
252 if(this._touchMoveListener){
253 connect.disconnect(this._touchMoveListener);
254 this._touchMoveListener = null;
255 }
256 if(this._touchEndListener){
257 connect.disconnect(this._touchEndListener);
258 this._touchEndListener = null;
259 }
260 }else{
261 // recompute touch center
262 var middlePoint = this._getTouchBarycenter(touchEvent);
263 var mapPoint = this._map.screenCoordsToMapCoords(middlePoint.x,middlePoint.y);
264 this._centerTouchLocation.x = mapPoint.x;
265 this._centerTouchLocation.y = mapPoint.y;
266 }
267 },
268
269 _singleTapHandler: function(touchEvent){
270 // summary:
271 // action performed on the map when a single tap was triggered
272 // touchEvent: TouchEvent
273 // a touch event
274 // tags:
275 // private
276 var feature = this._getFeatureFromTouchEvent(touchEvent);
277 if(feature){
278 // call feature handler
279 feature._onclickHandler(touchEvent);
280 }else{
281 // unselect all
282 for(var name in this._map.mapObj.features){
283 this._map.mapObj.features[name].select(false);
284 }
285 this._map.onFeatureClick(null);
286 }
287 },
288
289 _touchMoveHandler: function(touchEvent){
290 // summary:
291 // action performed on the map when a touch move was triggered
292 // touchEvent: TouchEvent
293 // a touch event
294 // tags:
295 // private
296
297 // prevent browser interaction
298 event.stop(touchEvent);
299
300 // cancel tap if moved too far from first touch location
301 if(!this._tapCancel){
302 var dx = (touchEvent.touches[0].pageX - this._lastTap.x),
303 dy = (touchEvent.touches[0].pageY - this._lastTap.y);
304 var distance = dx*dx + dy*dy;
305 if(distance > this._tapThreshold){
306 this._tapCancel = true;
307 }
308 }
309 var middlePoint = this._getTouchBarycenter(touchEvent);
310 // compute map offset
311 var mapPoint = this._map.screenCoordsToMapCoords(middlePoint.x,middlePoint.y),
312 mapOffsetX = mapPoint.x - this._centerTouchLocation.x,
313 mapOffsetY = mapPoint.y - this._centerTouchLocation.y;
314 // compute scale factor
315 var scaleFactor = 1;
316 var touches = touchEvent.touches;
317 if(touches.length >= 2){
318 var fingerSpacing = this._getFingerSpacing(touchEvent);
319 scaleFactor = fingerSpacing / this._initialFingerSpacing;
320 // scale map
321 this._map.setMapScale(this._initialScale*scaleFactor);
322 }
323 // adjust map center on barycentre
324 var currentMapCenter = this._map.getMapCenter();
325 this._map.setMapCenter(currentMapCenter.x - mapOffsetX, currentMapCenter.y - mapOffsetY);
326 }
327 });
328 });