foundation.util.touch.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //**************************************************
  2. //**Work inspired by multiple jquery swipe plugins**
  3. //**Done by Yohai Ararat ***************************
  4. //**************************************************
  5. import $ from 'jquery';
  6. var Touch = {};
  7. var startPosX,
  8. startPosY,
  9. startTime,
  10. elapsedTime,
  11. startEvent,
  12. isMoving = false,
  13. didMoved = false;
  14. function onTouchEnd(e) {
  15. this.removeEventListener('touchmove', onTouchMove);
  16. this.removeEventListener('touchend', onTouchEnd);
  17. // If the touch did not move, consider it as a "tap"
  18. if (!didMoved) {
  19. var tapEvent = $.Event('tap', startEvent || e);
  20. $(this).trigger(tapEvent);
  21. }
  22. startEvent = null;
  23. isMoving = false;
  24. didMoved = false;
  25. }
  26. function onTouchMove(e) {
  27. if ($.spotSwipe.preventDefault) { e.preventDefault(); }
  28. if(isMoving) {
  29. var x = e.touches[0].pageX;
  30. var y = e.touches[0].pageY;
  31. var dx = startPosX - x;
  32. var dy = startPosY - y;
  33. var dir;
  34. didMoved = true;
  35. elapsedTime = new Date().getTime() - startTime;
  36. if(Math.abs(dx) >= $.spotSwipe.moveThreshold && elapsedTime <= $.spotSwipe.timeThreshold) {
  37. dir = dx > 0 ? 'left' : 'right';
  38. }
  39. // else if(Math.abs(dy) >= $.spotSwipe.moveThreshold && elapsedTime <= $.spotSwipe.timeThreshold) {
  40. // dir = dy > 0 ? 'down' : 'up';
  41. // }
  42. if(dir) {
  43. e.preventDefault();
  44. onTouchEnd.apply(this, arguments);
  45. $(this)
  46. .trigger($.Event('swipe', e), dir)
  47. .trigger($.Event(`swipe${dir}`, e));
  48. }
  49. }
  50. }
  51. function onTouchStart(e) {
  52. if (e.touches.length == 1) {
  53. startPosX = e.touches[0].pageX;
  54. startPosY = e.touches[0].pageY;
  55. startEvent = e;
  56. isMoving = true;
  57. didMoved = false;
  58. startTime = new Date().getTime();
  59. this.addEventListener('touchmove', onTouchMove, false);
  60. this.addEventListener('touchend', onTouchEnd, false);
  61. }
  62. }
  63. function init() {
  64. this.addEventListener && this.addEventListener('touchstart', onTouchStart, false);
  65. }
  66. function teardown() {
  67. this.removeEventListener('touchstart', onTouchStart);
  68. }
  69. class SpotSwipe {
  70. constructor($) {
  71. this.version = '1.0.0';
  72. this.enabled = 'ontouchstart' in document.documentElement;
  73. this.preventDefault = false;
  74. this.moveThreshold = 75;
  75. this.timeThreshold = 200;
  76. this.$ = $;
  77. this._init();
  78. }
  79. _init() {
  80. var $ = this.$;
  81. $.event.special.swipe = { setup: init };
  82. $.event.special.tap = { setup: init };
  83. $.each(['left', 'up', 'down', 'right'], function () {
  84. $.event.special[`swipe${this}`] = { setup: function(){
  85. $(this).on('swipe', $.noop);
  86. } };
  87. });
  88. }
  89. }
  90. /****************************************************
  91. * As far as I can tell, both setupSpotSwipe and *
  92. * setupTouchHandler should be idempotent, *
  93. * because they directly replace functions & *
  94. * values, and do not add event handlers directly. *
  95. ****************************************************/
  96. Touch.setupSpotSwipe = function($) {
  97. $.spotSwipe = new SpotSwipe($);
  98. };
  99. /****************************************************
  100. * Method for adding pseudo drag events to elements *
  101. ***************************************************/
  102. Touch.setupTouchHandler = function($) {
  103. $.fn.addTouch = function(){
  104. this.each(function(i,el){
  105. $(el).bind('touchstart touchmove touchend touchcancel', function(event) {
  106. //we pass the original event object because the jQuery event
  107. //object is normalized to w3c specs and does not provide the TouchList
  108. handleTouch(event);
  109. });
  110. });
  111. var handleTouch = function(event){
  112. var touches = event.changedTouches,
  113. first = touches[0],
  114. eventTypes = {
  115. touchstart: 'mousedown',
  116. touchmove: 'mousemove',
  117. touchend: 'mouseup'
  118. },
  119. type = eventTypes[event.type],
  120. simulatedEvent
  121. ;
  122. if('MouseEvent' in window && typeof window.MouseEvent === 'function') {
  123. simulatedEvent = new window.MouseEvent(type, {
  124. 'bubbles': true,
  125. 'cancelable': true,
  126. 'screenX': first.screenX,
  127. 'screenY': first.screenY,
  128. 'clientX': first.clientX,
  129. 'clientY': first.clientY
  130. });
  131. } else {
  132. simulatedEvent = document.createEvent('MouseEvent');
  133. simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0/*left*/, null);
  134. }
  135. first.target.dispatchEvent(simulatedEvent);
  136. };
  137. };
  138. };
  139. Touch.init = function ($) {
  140. if(typeof($.spotSwipe) === 'undefined') {
  141. Touch.setupSpotSwipe($);
  142. Touch.setupTouchHandler($);
  143. }
  144. };
  145. export {Touch};