TouchInteractionSupport.js 9.9 KB

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