touch_handler.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. class TouchHandler {
  2. constructor(navHandler) {
  3. this.navHandler = navHandler;
  4. this.reset();
  5. }
  6. reset() {
  7. this.touch = {
  8. start: {x: -1, y: -1},
  9. move: {x: -1, y: -1},
  10. element: null
  11. };
  12. }
  13. calculateDistance() {
  14. if (this.touch.start.x >= -1 && this.touch.move.x >= -1) {
  15. let horizontalDistance = Math.abs(this.touch.move.x - this.touch.start.x);
  16. let verticalDistance = Math.abs(this.touch.move.y - this.touch.start.y);
  17. if (horizontalDistance > 30 && verticalDistance < 70) {
  18. return this.touch.move.x - this.touch.start.x;
  19. }
  20. }
  21. return 0;
  22. }
  23. findElement(element) {
  24. if (element.classList.contains("touch-item")) {
  25. return element;
  26. }
  27. return DomHelper.findParent(element, "touch-item");
  28. }
  29. onTouchStart(event) {
  30. if (event.touches === undefined || event.touches.length !== 1) {
  31. return;
  32. }
  33. this.reset();
  34. this.touch.start.x = event.touches[0].clientX;
  35. this.touch.start.y = event.touches[0].clientY;
  36. this.touch.element = this.findElement(event.touches[0].target);
  37. }
  38. onTouchMove(event) {
  39. if (event.touches === undefined || event.touches.length !== 1 || this.element === null) {
  40. return;
  41. }
  42. this.touch.move.x = event.touches[0].clientX;
  43. this.touch.move.y = event.touches[0].clientY;
  44. let distance = this.calculateDistance();
  45. let absDistance = Math.abs(distance);
  46. if (absDistance > 0) {
  47. let opacity = 1 - (absDistance > 75 ? 0.9 : absDistance / 75 * 0.9);
  48. let tx = distance > 75 ? 75 : (distance < -75 ? -75 : distance);
  49. this.touch.element.style.opacity = opacity;
  50. this.touch.element.style.transform = "translateX(" + tx + "px)";
  51. event.preventDefault();
  52. }
  53. }
  54. onTouchEnd(event) {
  55. if (event.touches === undefined) {
  56. return;
  57. }
  58. if (this.touch.element !== null) {
  59. let distance = Math.abs(this.calculateDistance());
  60. if (distance > 75) {
  61. EntryHandler.toggleEntryStatus(this.touch.element);
  62. }
  63. this.touch.element.style.opacity = 1;
  64. this.touch.element.style.transform = "none";
  65. }
  66. this.reset();
  67. }
  68. listen() {
  69. let elements = document.querySelectorAll(".touch-item");
  70. let hasPassiveOption = DomHelper.hasPassiveEventListenerOption();
  71. elements.forEach((element) => {
  72. element.addEventListener("touchstart", (e) => this.onTouchStart(e), hasPassiveOption ? { passive: true } : false);
  73. element.addEventListener("touchmove", (e) => this.onTouchMove(e), hasPassiveOption ? { passive: false } : false);
  74. element.addEventListener("touchend", (e) => this.onTouchEnd(e), hasPassiveOption ? { passive: true } : false);
  75. element.addEventListener("touchcancel", () => this.reset(), hasPassiveOption ? { passive: true } : false);
  76. });
  77. let entryContentElement = document.querySelector(".entry-content");
  78. if (entryContentElement) {
  79. let doubleTapTimers = {
  80. previous: null,
  81. next: null
  82. };
  83. const detectDoubleTap = (doubleTapTimer, event) => {
  84. const timer = doubleTapTimers[doubleTapTimer];
  85. if (timer === null) {
  86. doubleTapTimers[doubleTapTimer] = setTimeout(() => {
  87. doubleTapTimers[doubleTapTimer] = null;
  88. }, 200);
  89. } else {
  90. event.preventDefault();
  91. this.navHandler.goToPage(doubleTapTimer);
  92. }
  93. };
  94. entryContentElement.addEventListener("touchend", (e) => {
  95. if (e.changedTouches[0].clientX >= (entryContentElement.offsetWidth / 2)) {
  96. detectDoubleTap("next", e);
  97. } else {
  98. detectDoubleTap("previous", e);
  99. }
  100. }, hasPassiveOption ? { passive: false } : false);
  101. entryContentElement.addEventListener("touchmove", (e) => {
  102. Object.keys(doubleTapTimers).forEach(timer => doubleTapTimers[timer] = null);
  103. });
  104. }
  105. }
  106. }