| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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; |
| 41 |
}, |
| 42 |
|
| 43 |
connect: function(){ |
| 44 |
|
| 45 |
|
| 46 |
this._touchStartListener = this._map.surface.connect("touchstart", this, this._touchStartHandler); |
| 47 |
}, |
| 48 |
|
| 49 |
disconnect: function(){ |
| 50 |
|
| 51 |
|
| 52 |
if(this._touchStartListener){ |
| 53 |
connect.disconnect(this._touchStartListener); |
| 54 |
this._touchStartListener = null; |
| 55 |
} |
| 56 |
}, |
| 57 |
|
| 58 |
_getTouchBarycenter: function(touchEvent){ |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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}; |
| 79 |
}, |
| 80 |
|
| 81 |
_getFingerSpacing: function(touchEvent){ |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
var isDoubleTap = false; |
| 110 |
var touches = touchEvent.touches; |
| 111 |
if((this._tapCount > 0) && touches.length == 1){ |
| 112 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
var feature = this._getFeatureFromTouchEvent(touchEvent); |
| 138 |
if(feature){ |
| 139 |
this._map.fitToMapArea(feature._bbox, 15, true); |
| 140 |
}else{ |
| 141 |
|
| 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 |
|
| 147 |
var mapPoint = this._map.screenCoordsToMapCoords(offX,offY); |
| 148 |
|
| 149 |
this._map.setMapCenterAndScale(mapPoint.x, mapPoint.y,this._map.getMapScale()*2,true); |
| 150 |
} |
| 151 |
}, |
| 152 |
|
| 153 |
_getFeatureFromTouchEvent: function(touchEvent){ |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 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; |
| 167 |
}, |
| 168 |
|
| 169 |
_touchStartHandler: function(touchEvent){ |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
event.stop(touchEvent); |
| 177 |
this._oneFingerTouch = (touchEvent.touches.length == 1); |
| 178 |
this._tapCancel = !this._oneFingerTouch; |
| 179 |
|
| 180 |
this._doubleTapPerformed = false; |
| 181 |
if(this._isDoubleTap(touchEvent)){ |
| 182 |
|
| 183 |
this._doubleTapHandler(touchEvent); |
| 184 |
this._doubleTapPerformed = true; |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 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 |
|
| 193 |
this._initialFingerSpacing = this._getFingerSpacing(touchEvent); |
| 194 |
|
| 195 |
this._initialScale = this._map.getMapScale(); |
| 196 |
|
| 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 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
var touches = touchEvent.touches; |
| 216 |
if(touches.length == 0){ |
| 217 |
|
| 218 |
|
| 219 |
if(this._oneFingerTouch && !this._tapCancel){ |
| 220 |
this._oneFingerTouch = false; |
| 221 |
setTimeout(lang.hitch(this,function(){ |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if(!this._doubleTapPerformed){ |
| 226 |
|
| 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 |
|
| 232 |
this._singleTapHandler(touchEvent); |
| 233 |
} |
| 234 |
} |
| 235 |
}), 350); |
| 236 |
} |
| 237 |
this._tapCancel = false; |
| 238 |
} |
| 239 |
}, |
| 240 |
|
| 241 |
_touchEndHandler: function(touchEvent){ |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
event.stop(touchEvent); |
| 249 |
var touches = touchEvent.touches; |
| 250 |
if(touches.length == 0){ |
| 251 |
|
| 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 |
|
| 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 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
var feature = this._getFeatureFromTouchEvent(touchEvent); |
| 277 |
if(feature){ |
| 278 |
|
| 279 |
feature._onclickHandler(touchEvent); |
| 280 |
}else{ |
| 281 |
|
| 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 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
event.stop(touchEvent); |
| 299 |
|
| 300 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 321 |
this._map.setMapScale(this._initialScale*scaleFactor); |
| 322 |
} |
| 323 |
|
| 324 |
var currentMapCenter = this._map.getMapCenter(); |
| 325 |
this._map.setMapCenter(currentMapCenter.x - mapOffsetX, currentMapCenter.y - mapOffsetY); |
| 326 |
} |
| 327 |
}); |
| 328 |
}); |