2018-03-06 11:29:28 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
let touchStartPos = null;
|
|
|
|
let touchCurPos = null;
|
|
|
|
let touchStartTime = 0;
|
|
|
|
let menuWidth = 0;
|
|
|
|
let menuIsOpen = false;
|
|
|
|
let menuIsMoving = false;
|
2018-06-28 07:45:40 +00:00
|
|
|
let menuIsAbsolute = false;
|
2018-03-06 11:29:28 +00:00
|
|
|
|
|
|
|
class SlideoutMenu {
|
|
|
|
static enable() {
|
2018-07-06 18:15:15 +00:00
|
|
|
this.viewport = document.getElementById("viewport");
|
|
|
|
this.menu = document.getElementById("sidebar");
|
|
|
|
this.sidebarOverlay = document.getElementById("sidebar-overlay");
|
|
|
|
|
2018-03-10 22:23:43 +00:00
|
|
|
document.body.addEventListener("touchstart", onTouchStart, {passive: true});
|
2018-03-06 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static toggle(state) {
|
|
|
|
menuIsOpen = state;
|
2018-07-06 18:15:15 +00:00
|
|
|
this.viewport.classList.toggle("menu-open", state);
|
2018-03-06 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isOpen() {
|
|
|
|
return menuIsOpen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onTouchStart(e) {
|
2018-10-15 11:54:33 +00:00
|
|
|
touchStartPos = touchCurPos = e.touches.item(0);
|
|
|
|
|
2018-03-06 11:29:28 +00:00
|
|
|
if (e.touches.length !== 1) {
|
|
|
|
onTouchEnd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-13 07:41:12 +00:00
|
|
|
const styles = window.getComputedStyle(SlideoutMenu.menu);
|
2018-03-06 11:29:28 +00:00
|
|
|
|
2018-06-28 07:45:40 +00:00
|
|
|
menuWidth = parseFloat(styles.width);
|
|
|
|
menuIsAbsolute = styles.position === "absolute";
|
2018-03-06 11:29:28 +00:00
|
|
|
|
2018-10-15 11:54:33 +00:00
|
|
|
if (!menuIsOpen || touchStartPos.screenX > menuWidth) {
|
2018-03-06 11:29:28 +00:00
|
|
|
touchStartTime = Date.now();
|
|
|
|
|
2018-03-17 09:35:12 +00:00
|
|
|
document.body.addEventListener("touchmove", onTouchMove, {passive: true});
|
2018-03-10 22:23:43 +00:00
|
|
|
document.body.addEventListener("touchend", onTouchEnd, {passive: true});
|
2018-03-06 11:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onTouchMove(e) {
|
|
|
|
const touch = touchCurPos = e.touches.item(0);
|
2018-03-18 05:18:43 +00:00
|
|
|
let distX = touch.screenX - touchStartPos.screenX;
|
|
|
|
const distY = touch.screenY - touchStartPos.screenY;
|
2018-03-06 11:29:28 +00:00
|
|
|
|
2018-03-17 09:35:12 +00:00
|
|
|
if (!menuIsMoving) {
|
2018-03-18 05:18:43 +00:00
|
|
|
// tan(45°) is 1. Gestures in 0°-45° (< 1) are considered horizontal, so
|
|
|
|
// menu must be open; gestures in 45°-90° (>1) are considered vertical, so
|
|
|
|
// chat windows must be scrolled.
|
|
|
|
if (Math.abs(distY / distX) >= 1) {
|
2018-03-17 09:35:12 +00:00
|
|
|
onTouchEnd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-18 05:18:43 +00:00
|
|
|
const devicePixelRatio = window.devicePixelRatio || 2;
|
|
|
|
|
|
|
|
if (Math.abs(distX) > devicePixelRatio) {
|
2018-07-13 07:41:12 +00:00
|
|
|
SlideoutMenu.viewport.classList.toggle("menu-dragging", true);
|
2018-03-17 09:35:12 +00:00
|
|
|
menuIsMoving = true;
|
|
|
|
}
|
2018-03-06 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 07:45:40 +00:00
|
|
|
// Do not animate the menu on desktop view
|
|
|
|
if (!menuIsAbsolute) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-06 11:29:28 +00:00
|
|
|
if (menuIsOpen) {
|
2018-03-18 05:18:43 +00:00
|
|
|
distX += menuWidth;
|
2018-03-06 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 05:18:43 +00:00
|
|
|
if (distX > menuWidth) {
|
|
|
|
distX = menuWidth;
|
|
|
|
} else if (distX < 0) {
|
|
|
|
distX = 0;
|
2018-03-06 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 07:41:12 +00:00
|
|
|
SlideoutMenu.menu.style.transform = "translate3d(" + distX + "px, 0, 0)";
|
|
|
|
SlideoutMenu.sidebarOverlay.style.opacity = distX / menuWidth;
|
2018-03-06 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onTouchEnd() {
|
|
|
|
const diff = touchCurPos.screenX - touchStartPos.screenX;
|
|
|
|
const absDiff = Math.abs(diff);
|
|
|
|
|
|
|
|
if (absDiff > menuWidth / 2 || Date.now() - touchStartTime < 180 && absDiff > 50) {
|
|
|
|
SlideoutMenu.toggle(diff > 0);
|
|
|
|
}
|
|
|
|
|
2018-03-10 22:23:43 +00:00
|
|
|
document.body.removeEventListener("touchmove", onTouchMove);
|
|
|
|
document.body.removeEventListener("touchend", onTouchEnd);
|
2018-07-13 07:41:12 +00:00
|
|
|
SlideoutMenu.viewport.classList.toggle("menu-dragging", false);
|
|
|
|
SlideoutMenu.menu.style.transform = null;
|
|
|
|
SlideoutMenu.sidebarOverlay.style.opacity = null;
|
2018-03-06 11:29:28 +00:00
|
|
|
|
|
|
|
touchStartPos = null;
|
|
|
|
touchCurPos = null;
|
|
|
|
touchStartTime = 0;
|
|
|
|
menuIsMoving = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SlideoutMenu;
|