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

48 lines
1.2 KiB
JavaScript

class GestureHandler {
constructor(sidebar) {
this.sidebar = sidebar;
this.startX = 0;
this.startY = 0;
this.tracking = false;
document.addEventListener('touchstart', (e) => this.onTouchStart(e), { passive: true });
document.addEventListener('touchmove', (e) => this.onTouchMove(e), { passive: true });
document.addEventListener('touchend', (e) => this.onTouchEnd(e), { passive: true });
}
onTouchStart(e) {
const touch = e.touches[0];
this.startX = touch.clientX;
this.startY = touch.clientY;
this.tracking = true;
}
onTouchMove(e) {
if (!this.tracking) return;
}
onTouchEnd(e) {
if (!this.tracking) return;
this.tracking = false;
const touch = e.changedTouches[0];
const dx = touch.clientX - this.startX;
const dy = touch.clientY - this.startY;
// Minimum swipe distance
if (Math.abs(dx) < 50 || Math.abs(dy) > Math.abs(dx)) return;
// Swipe right from left edge = open sidebar
if (dx > 0 && this.startX < 50) {
this.sidebar.open();
}
// Swipe left = close sidebar
if (dx < 0 && this.sidebar.isOpen()) {
this.sidebar.close();
}
}
}
window.GestureHandler = GestureHandler;