2018-07-06 07:18:51 +02:00
|
|
|
class TouchHandler {
|
2019-07-18 05:27:39 +02:00
|
|
|
constructor() {
|
2018-07-06 07:18:51 +02:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.touch = {
|
2019-07-18 05:27:39 +02:00
|
|
|
start: { x: -1, y: -1 },
|
|
|
|
move: { x: -1, y: -1 },
|
2022-04-03 09:02:30 +02:00
|
|
|
moved: false,
|
2022-09-30 07:37:57 +02:00
|
|
|
time: 0,
|
2018-07-06 07:18:51 +02:00
|
|
|
element: null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateDistance() {
|
|
|
|
if (this.touch.start.x >= -1 && this.touch.move.x >= -1) {
|
2024-03-20 23:59:37 +01:00
|
|
|
const horizontalDistance = Math.abs(this.touch.move.x - this.touch.start.x);
|
|
|
|
const verticalDistance = Math.abs(this.touch.move.y - this.touch.start.y);
|
2018-07-06 07:18:51 +02:00
|
|
|
|
2022-04-03 09:02:30 +02:00
|
|
|
if (horizontalDistance > 30 && verticalDistance < 70 || this.touch.moved) {
|
2018-07-06 07:18:51 +02:00
|
|
|
return this.touch.move.x - this.touch.start.x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
findElement(element) {
|
2023-01-15 01:49:21 +01:00
|
|
|
if (element.classList.contains("entry-swipe")) {
|
2018-07-06 07:18:51 +02:00
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2024-03-11 04:32:39 +01:00
|
|
|
return element.closest(".entry-swipe");
|
2018-07-06 07:18:51 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 07:37:57 +02:00
|
|
|
onItemTouchStart(event) {
|
2018-07-06 07:18:51 +02:00
|
|
|
if (event.touches === undefined || event.touches.length !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.reset();
|
|
|
|
this.touch.start.x = event.touches[0].clientX;
|
|
|
|
this.touch.start.y = event.touches[0].clientY;
|
|
|
|
this.touch.element = this.findElement(event.touches[0].target);
|
2022-04-03 09:02:30 +02:00
|
|
|
this.touch.element.style.transitionDuration = "0s";
|
2018-07-06 07:18:51 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 07:37:57 +02:00
|
|
|
onItemTouchMove(event) {
|
2018-07-06 07:18:51 +02:00
|
|
|
if (event.touches === undefined || event.touches.length !== 1 || this.element === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.touch.move.x = event.touches[0].clientX;
|
|
|
|
this.touch.move.y = event.touches[0].clientY;
|
|
|
|
|
2024-03-20 23:59:37 +01:00
|
|
|
const distance = this.calculateDistance();
|
|
|
|
const absDistance = Math.abs(distance);
|
2018-07-06 07:18:51 +02:00
|
|
|
|
|
|
|
if (absDistance > 0) {
|
2022-04-03 09:02:30 +02:00
|
|
|
this.touch.moved = true;
|
|
|
|
|
|
|
|
let tx = absDistance > 75 ? Math.pow(absDistance - 75, 0.5) + 75 : absDistance;
|
|
|
|
|
|
|
|
if (distance < 0) {
|
|
|
|
tx = -tx;
|
|
|
|
}
|
2018-07-06 07:18:51 +02:00
|
|
|
|
|
|
|
this.touch.element.style.transform = "translateX(" + tx + "px)";
|
2018-07-18 03:48:56 +02:00
|
|
|
|
|
|
|
event.preventDefault();
|
2018-07-06 07:18:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 07:37:57 +02:00
|
|
|
onItemTouchEnd(event) {
|
2018-07-06 07:18:51 +02:00
|
|
|
if (event.touches === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.touch.element !== null) {
|
2024-03-20 23:59:37 +01:00
|
|
|
const absDistance = Math.abs(this.calculateDistance());
|
2018-07-06 07:18:51 +02:00
|
|
|
|
2022-09-30 07:37:57 +02:00
|
|
|
if (absDistance > 75) {
|
2019-07-18 05:27:39 +02:00
|
|
|
toggleEntryStatus(this.touch.element);
|
2022-04-03 09:02:30 +02:00
|
|
|
}
|
2022-01-28 04:39:35 +01:00
|
|
|
|
2022-04-03 09:02:30 +02:00
|
|
|
if (this.touch.moved) {
|
|
|
|
this.touch.element.style.transitionDuration = "0.15s";
|
2022-01-15 19:47:25 +01:00
|
|
|
this.touch.element.style.transform = "none";
|
2022-01-28 04:39:35 +01:00
|
|
|
}
|
2018-07-06 07:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
2022-09-30 07:37:57 +02:00
|
|
|
onContentTouchStart(event) {
|
|
|
|
if (event.touches === undefined || event.touches.length !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.reset();
|
|
|
|
this.touch.start.x = event.touches[0].clientX;
|
|
|
|
this.touch.start.y = event.touches[0].clientY;
|
|
|
|
this.touch.time = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
onContentTouchMove(event) {
|
|
|
|
if (event.touches === undefined || event.touches.length !== 1 || this.element === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.touch.move.x = event.touches[0].clientX;
|
|
|
|
this.touch.move.y = event.touches[0].clientY;
|
|
|
|
}
|
|
|
|
|
|
|
|
onContentTouchEnd(event) {
|
|
|
|
if (event.touches === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-20 23:59:37 +01:00
|
|
|
const distance = this.calculateDistance();
|
|
|
|
const absDistance = Math.abs(distance);
|
|
|
|
const now = Date.now();
|
2022-09-30 07:37:57 +02:00
|
|
|
|
|
|
|
if (now - this.touch.time <= 1000 && absDistance > 75) {
|
|
|
|
if (distance > 0) {
|
|
|
|
goToPage("previous");
|
|
|
|
} else {
|
|
|
|
goToPage("next");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
onTapEnd(event) {
|
|
|
|
if (event.touches === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-20 23:59:37 +01:00
|
|
|
const now = Date.now();
|
2022-09-30 07:37:57 +02:00
|
|
|
|
|
|
|
if (this.touch.start.x !== -1 && now - this.touch.time <= 200) {
|
2024-03-20 23:59:37 +01:00
|
|
|
const innerWidthHalf = window.innerWidth / 2;
|
2022-09-30 07:37:57 +02:00
|
|
|
|
|
|
|
if (this.touch.start.x >= innerWidthHalf && event.changedTouches[0].clientX >= innerWidthHalf) {
|
|
|
|
goToPage("next");
|
|
|
|
} else if (this.touch.start.x < innerWidthHalf && event.changedTouches[0].clientX < innerWidthHalf) {
|
|
|
|
goToPage("previous");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.reset();
|
|
|
|
} else {
|
|
|
|
this.reset();
|
|
|
|
this.touch.start.x = event.changedTouches[0].clientX;
|
|
|
|
this.touch.time = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 07:18:51 +02:00
|
|
|
listen() {
|
2024-03-20 23:59:37 +01:00
|
|
|
const hasPassiveOption = DomHelper.hasPassiveEventListenerOption();
|
2018-07-06 07:18:51 +02:00
|
|
|
|
2024-03-20 23:59:37 +01:00
|
|
|
document.querySelectorAll(".entry-swipe").forEach((element) => {
|
2022-09-30 07:37:57 +02:00
|
|
|
element.addEventListener("touchstart", (e) => this.onItemTouchStart(e), hasPassiveOption ? { passive: true } : false);
|
2023-12-08 16:26:19 +01:00
|
|
|
element.addEventListener("touchmove", (e) => this.onItemTouchMove(e), hasPassiveOption ? { passive: false } : false);
|
2022-09-30 07:37:57 +02:00
|
|
|
element.addEventListener("touchend", (e) => this.onItemTouchEnd(e), hasPassiveOption ? { passive: true } : false);
|
2018-07-11 05:41:27 +02:00
|
|
|
element.addEventListener("touchcancel", () => this.reset(), hasPassiveOption ? { passive: true } : false);
|
2018-07-06 07:18:51 +02:00
|
|
|
});
|
2019-03-09 14:00:26 +01:00
|
|
|
|
2024-03-20 23:59:37 +01:00
|
|
|
const element = document.querySelector(".entry-content");
|
2022-09-30 07:37:57 +02:00
|
|
|
if (element) {
|
|
|
|
if (element.classList.contains("gesture-nav-tap")) {
|
|
|
|
element.addEventListener("touchend", (e) => this.onTapEnd(e), hasPassiveOption ? { passive: true } : false);
|
|
|
|
element.addEventListener("touchmove", () => this.reset(), hasPassiveOption ? { passive: true } : false);
|
|
|
|
element.addEventListener("touchcancel", () => this.reset(), hasPassiveOption ? { passive: true } : false);
|
|
|
|
} else if (element.classList.contains("gesture-nav-swipe")) {
|
|
|
|
element.addEventListener("touchstart", (e) => this.onContentTouchStart(e), hasPassiveOption ? { passive: true } : false);
|
|
|
|
element.addEventListener("touchmove", (e) => this.onContentTouchMove(e), hasPassiveOption ? { passive: true } : false);
|
|
|
|
element.addEventListener("touchend", (e) => this.onContentTouchEnd(e), hasPassiveOption ? { passive: true } : false);
|
|
|
|
element.addEventListener("touchcancel", () => this.reset(), hasPassiveOption ? { passive: true } : false);
|
|
|
|
}
|
2019-03-09 14:00:26 +01:00
|
|
|
}
|
2018-07-06 07:18:51 +02:00
|
|
|
}
|
|
|
|
}
|