PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/qooxdoo/framework/source/class/qx/event/type/Touch.js

https://github.com/stephaneerard/qooxdoo
JavaScript | 280 lines | 84 code | 42 blank | 154 comment | 7 complexity | 6d6a8b5bdbbd83790a4b8e62dfe44265 MD5 | raw file
  1. /* ************************************************************************
  2. qooxdoo - the new era of web development
  3. http://qooxdoo.org
  4. Copyright:
  5. 2004-2010 1&1 Internet AG, Germany, http://www.1und1.de
  6. License:
  7. LGPL: http://www.gnu.org/licenses/lgpl.html
  8. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  9. See the LICENSE file in the project's top-level directory for details.
  10. Authors:
  11. * Martin Wittemann (martinwittemann)
  12. * Tino Butz (tbtz)
  13. ************************************************************************ */
  14. /**
  15. * EXPERIMENTAL - NOT READY FOR PRODUCTION
  16. *
  17. * Touch event object.
  18. *
  19. * For more information see:
  20. * http://developer.apple.com/safari/library/documentation/UserExperience/Reference/TouchEventClassReference/TouchEvent/TouchEvent.html
  21. */
  22. qx.Class.define("qx.event.type.Touch",
  23. {
  24. extend : qx.event.type.Dom,
  25. /*
  26. *****************************************************************************
  27. MEMBERS
  28. *****************************************************************************
  29. */
  30. members :
  31. {
  32. // overridden
  33. _cloneNativeEvent : function(nativeEvent, clone)
  34. {
  35. var clone = this.base(arguments, nativeEvent, clone);
  36. clone.pageX = nativeEvent.pageX;
  37. clone.pageY = nativeEvent.pageY;
  38. clone.layerX = nativeEvent.layerX;
  39. clone.layerY = nativeEvent.layerY;
  40. clone.scale = nativeEvent.scale;
  41. clone.rotation = nativeEvent.rotation;
  42. clone.srcElement = nativeEvent.srcElement;
  43. clone.targetTouches = [];
  44. for (var i = 0; i < nativeEvent.targetTouches.length; i++) {
  45. clone.targetTouches[i] = nativeEvent.targetTouches[i];
  46. };
  47. clone.changedTouches = [];
  48. for (var i = 0; i < nativeEvent.changedTouches.length; i++) {
  49. clone.changedTouches[i] = nativeEvent.changedTouches[i];
  50. };
  51. clone.touches = [];
  52. for (var i = 0; i < nativeEvent.touches.length; i++) {
  53. clone.touches[i] = nativeEvent.touches[i];
  54. };
  55. return clone;
  56. },
  57. // overridden
  58. stop : function() {
  59. this.stopPropagation();
  60. },
  61. /**
  62. * Returns an array of native Touch objects representing all current
  63. * touches on the document.
  64. * Returns an empty array for the "touchend" event.
  65. *
  66. * @return {Object[]} Array of touch objects. For more information see:
  67. * http://developer.apple.com/safari/library/documentation/UserExperience/Reference/TouchClassReference/Touch/Touch.html
  68. */
  69. getAllTouches : function() {
  70. return this._native.touches;
  71. },
  72. /**
  73. * Returns an array of native Touch objects representing all touches
  74. * associated with the event target element.
  75. * Returns an empty array for the "touchend" event.
  76. *
  77. * @return {Object[]} Array of touch objects. For more information see:
  78. * http://developer.apple.com/safari/library/documentation/UserExperience/Reference/TouchClassReference/Touch/Touch.html
  79. */
  80. getTargetTouches : function() {
  81. return this._native.targetTouches;
  82. },
  83. /**
  84. * Returns an array of native Touch objects representing all touches of
  85. * the target element that changed in this event.
  86. *
  87. * On the "touchstart" event the array contains all touches that were
  88. * added to the target element.
  89. * On the "touchmove" event the array contains all touches that were
  90. * moved on the target element.
  91. * On the "touchend" event the array contains all touches that used
  92. * to be on the target element.
  93. *
  94. * @return {Object[]} Array of touch objects. For more information see:
  95. * http://developer.apple.com/safari/library/documentation/UserExperience/Reference/TouchClassReference/Touch/Touch.html
  96. */
  97. getChangedTargetTouches : function() {
  98. return this._native.changedTouches;
  99. },
  100. /**
  101. * Checks whether more than one touch is associated with the event target
  102. * element.
  103. *
  104. * @return {Boolean} Is multi-touch
  105. */
  106. isMultiTouch : function() {
  107. return this.__getEventSpecificTouches().length > 1;
  108. },
  109. /**
  110. * iOS only: Returns the distance between two fingers since the start of the event.
  111. * The distance is a multiplier of the initial distance.
  112. * Initial value: 1.0.
  113. * Gestures:
  114. * < 1.0, pinch close / zoom out.
  115. * > 1.0, pinch open / to zoom in.
  116. *
  117. * @return The scale distance between two fingers
  118. */
  119. getScale : function() {
  120. return this._native.scale;
  121. },
  122. /**
  123. * iOS only: Returns the delta of the rotation since the start of the event, in degrees.
  124. * Initial value is 0.0
  125. * Clockwise > 0
  126. * Counter-clockwise < 0.
  127. *
  128. * @return {Float} The rotation delta
  129. */
  130. getRotation : function() {
  131. return this._native.rotation;
  132. },
  133. /**
  134. * Get the horizontal position at which the event occurred relative to the
  135. * left of the document. This property takes into account any scrolling of
  136. * the page.
  137. *
  138. * @param touchIndex {Integer ? 0) The index of the Touch object
  139. * @return {Integer} The horizontal position of the touch in the document.
  140. */
  141. getDocumentLeft : function(touchIndex) {
  142. return this.__getEventSpecificTouch(touchIndex).pageX;
  143. },
  144. /**
  145. * Get the vertical position at which the event occurred relative to the
  146. * top of the document. This property takes into account any scrolling of
  147. * the page.
  148. *
  149. * @param touchIndex {Integer ? 0) The index of the Touch object
  150. * @return {Integer} The vertical position of the touch in the document.
  151. */
  152. getDocumentTop : function(touchIndex) {
  153. return this.__getEventSpecificTouch(touchIndex).pageY;
  154. },
  155. /**
  156. * Get the horizontal coordinate at which the event occurred relative to
  157. * the origin of the screen coordinate system.
  158. *
  159. * @param touchIndex {Integer ? 0) The index of the Touch object
  160. * @return {Integer} The horizontal position of the touch
  161. */
  162. getScreenLeft : function(touchIndex) {
  163. return this.__getEventSpecificTouch(touchIndex).screenX;
  164. },
  165. /**
  166. * Get the vertical coordinate at which the event occurred relative to
  167. * the origin of the screen coordinate system.
  168. *
  169. * @param touchIndex {Integer ? 0) The index of the Touch object
  170. * @return {Integer} The vertical position of the touch
  171. */
  172. getScreenTop: function(touchIndex) {
  173. return this.__getEventSpecificTouch(touchIndex).screenY;
  174. },
  175. /**
  176. * Get the the horizontal coordinate at which the event occurred relative
  177. * to the viewport.
  178. *
  179. * @param touchIndex {Integer ? 0) The index of the Touch object
  180. * @return {Integer} The horizontal position of the touch
  181. */
  182. getViewportLeft : function(touchIndex) {
  183. return this.__getEventSpecificTouch(touchIndex).clientX;
  184. },
  185. /**
  186. * Get the vertical coordinate at which the event occurred relative
  187. * to the viewport.
  188. *
  189. * @param touchIndex {Integer ? 0) The index of the Touch object
  190. * @return {Integer} The vertical position of the touch
  191. */
  192. getViewportTop : function(touchIndex) {
  193. return this.__getEventSpecificTouch(touchIndex).clientY;
  194. },
  195. /**
  196. * Returns the unique identifier for a certain touch object.
  197. *
  198. * @param touchIndex {Integer ? 0) The index of the Touch object
  199. * @return {Integer} Unique identifier of the touch object
  200. */
  201. getIdentifier : function(touchIndex) {
  202. return this.__getEventSpecificTouch(touchIndex).identifier;
  203. },
  204. /**
  205. * Returns an event specific touch on the target element. This function is
  206. * used as the "touchend" event only offers Touch objects in the
  207. * changedTouches array.
  208. *
  209. * @param touchIndex {Integer ? 0) The index of the Touch object to
  210. * retrieve
  211. * @return {Object} A native Touch object
  212. */
  213. __getEventSpecificTouch : function(touchIndex)
  214. {
  215. touchIndex = touchIndex == null ? 0 : touchIndex;
  216. return this.__getEventSpecificTouches()[touchIndex];
  217. },
  218. /**
  219. * Returns the event specific touches on the target element. This function
  220. * is used as the "touchend" event only offers Touch objects in the
  221. * changedTouches array.
  222. *
  223. * @return {Object[]} Array of native Touch objects
  224. */
  225. __getEventSpecificTouches : function()
  226. {
  227. var isTouchEnd = this.getType() == "touchend" || this.getType() == "touchcancel";
  228. var touches = (isTouchEnd ? this.getChangedTargetTouches(): this.getTargetTouches());
  229. return touches;
  230. }
  231. }
  232. });