Simplify action handler directive (#5157)

This commit is contained in:
Bram Kragten 2020-03-12 20:11:05 +01:00 committed by GitHub
parent 91edcf9b52
commit 558802c7dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,7 +32,6 @@ class ActionHandler extends HTMLElement implements ActionHandler {
public ripple: Ripple; public ripple: Ripple;
protected timer?: number; protected timer?: number;
protected held = false; protected held = false;
protected touch?: boolean;
private dblClickTimeout?: number; private dblClickTimeout?: number;
constructor() { constructor() {
@ -92,22 +91,6 @@ class ActionHandler extends HTMLElement implements ActionHandler {
return false; return false;
}); });
const touchStart = (ev: TouchEvent) => {
if (this.touch === false) {
return;
}
this.touch = true;
start(ev);
};
const clickStart = (ev: MouseEvent) => {
if (this.touch === true) {
return;
}
this.touch = false;
start(ev);
};
const start = (ev: Event) => { const start = (ev: Event) => {
this.held = false; this.held = false;
let x; let x;
@ -126,29 +109,16 @@ class ActionHandler extends HTMLElement implements ActionHandler {
}, this.holdTime); }, this.holdTime);
}; };
const touchEnd = (ev: TouchEvent) => {
if (this.touch === false) {
return;
}
end(ev);
};
const clickEnd = (ev: MouseEvent) => {
if (this.touch === true) {
return;
}
end(ev);
};
const handleEnter = (ev: KeyboardEvent) => { const handleEnter = (ev: KeyboardEvent) => {
if (this.touch === true || ev.keyCode !== 13) { if (ev.keyCode !== 13) {
return; return;
} }
this.touch = false;
end(ev); end(ev);
}; };
const end = (ev: Event) => { const end = (ev: Event) => {
// Prevent mouse event if touch event
ev.preventDefault();
if ( if (
["touchend", "touchcancel"].includes(ev.type) && ["touchend", "touchcancel"].includes(ev.type) &&
this.timer === undefined this.timer === undefined
@ -177,15 +147,14 @@ class ActionHandler extends HTMLElement implements ActionHandler {
} else { } else {
fireEvent(element, "action", { action: "tap" }); fireEvent(element, "action", { action: "tap" });
} }
window.setTimeout(() => (this.touch = undefined), this.holdTime);
}; };
element.addEventListener("touchstart", touchStart, { passive: true }); element.addEventListener("touchstart", start, { passive: true });
element.addEventListener("touchend", touchEnd); element.addEventListener("touchend", end);
element.addEventListener("touchcancel", touchEnd); element.addEventListener("touchcancel", end);
element.addEventListener("mousedown", clickStart, { passive: true }); element.addEventListener("mousedown", start, { passive: true });
element.addEventListener("click", clickEnd); element.addEventListener("click", end);
element.addEventListener("keyup", handleEnter); element.addEventListener("keyup", handleEnter);
} }