Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

touch.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { keys } from '../util/helpers';
  2. var handleGesture = function handleGesture(wrapper) {
  3. var touchstartX = wrapper.touchstartX,
  4. touchendX = wrapper.touchendX,
  5. touchstartY = wrapper.touchstartY,
  6. touchendY = wrapper.touchendY;
  7. var dirRatio = 0.5;
  8. var minDistance = 16;
  9. wrapper.offsetX = touchendX - touchstartX;
  10. wrapper.offsetY = touchendY - touchstartY;
  11. if (Math.abs(wrapper.offsetY) < dirRatio * Math.abs(wrapper.offsetX)) {
  12. wrapper.left && touchendX < touchstartX - minDistance && wrapper.left(wrapper);
  13. wrapper.right && touchendX > touchstartX + minDistance && wrapper.right(wrapper);
  14. }
  15. if (Math.abs(wrapper.offsetX) < dirRatio * Math.abs(wrapper.offsetY)) {
  16. wrapper.up && touchendY < touchstartY - minDistance && wrapper.up(wrapper);
  17. wrapper.down && touchendY > touchstartY + minDistance && wrapper.down(wrapper);
  18. }
  19. };
  20. function _touchstart(event, wrapper) {
  21. var touch = event.changedTouches[0];
  22. wrapper.touchstartX = touch.clientX;
  23. wrapper.touchstartY = touch.clientY;
  24. wrapper.start && wrapper.start(Object.assign(event, wrapper));
  25. }
  26. function _touchend(event, wrapper) {
  27. var touch = event.changedTouches[0];
  28. wrapper.touchendX = touch.clientX;
  29. wrapper.touchendY = touch.clientY;
  30. wrapper.end && wrapper.end(Object.assign(event, wrapper));
  31. handleGesture(wrapper);
  32. }
  33. function _touchmove(event, wrapper) {
  34. var touch = event.changedTouches[0];
  35. wrapper.touchmoveX = touch.clientX;
  36. wrapper.touchmoveY = touch.clientY;
  37. wrapper.move && wrapper.move(Object.assign(event, wrapper));
  38. }
  39. function createHandlers(value) {
  40. var wrapper = {
  41. touchstartX: 0,
  42. touchstartY: 0,
  43. touchendX: 0,
  44. touchendY: 0,
  45. touchmoveX: 0,
  46. touchmoveY: 0,
  47. offsetX: 0,
  48. offsetY: 0,
  49. left: value.left,
  50. right: value.right,
  51. up: value.up,
  52. down: value.down,
  53. start: value.start,
  54. move: value.move,
  55. end: value.end
  56. };
  57. return {
  58. touchstart: function touchstart(e) {
  59. return _touchstart(e, wrapper);
  60. },
  61. touchend: function touchend(e) {
  62. return _touchend(e, wrapper);
  63. },
  64. touchmove: function touchmove(e) {
  65. return _touchmove(e, wrapper);
  66. }
  67. };
  68. }
  69. function inserted(el, binding, vnode) {
  70. var value = binding.value;
  71. var target = value.parent ? el.parentElement : el;
  72. var options = value.options || { passive: true };
  73. // Needed to pass unit tests
  74. if (!target) return;
  75. var handlers = createHandlers(binding.value);
  76. target._touchHandlers = Object(target._touchHandlers);
  77. target._touchHandlers[vnode.context._uid] = handlers;
  78. keys(handlers).forEach(function (eventName) {
  79. target.addEventListener(eventName, handlers[eventName], options);
  80. });
  81. }
  82. function unbind(el, binding, vnode) {
  83. var target = binding.value.parent ? el.parentElement : el;
  84. if (!target || !target._touchHandlers) return;
  85. var handlers = target._touchHandlers[vnode.context._uid];
  86. keys(handlers).forEach(function (eventName) {
  87. target.removeEventListener(eventName, handlers[eventName]);
  88. });
  89. delete target._touchHandlers[vnode.context._uid];
  90. }
  91. export default {
  92. inserted: inserted,
  93. unbind: unbind
  94. };
  95. //# sourceMappingURL=touch.js.map