Fix hold_action not working on chrome for android (#2011)

This commit is contained in:
Thomas Lovén 2018-11-11 21:27:36 +01:00 committed by Paulus Schoutsen
parent 2076949289
commit d974d5dc52

View File

@ -53,6 +53,7 @@ class LongPress extends HTMLElement implements LongPress {
() => { () => {
clearTimeout(this.timer); clearTimeout(this.timer);
this.stopAnimation(); this.stopAnimation();
this.timer = undefined;
}, },
{ passive: true } { passive: true }
); );
@ -64,9 +65,21 @@ class LongPress extends HTMLElement implements LongPress {
return; return;
} }
element.longPress = true; element.longPress = true;
element.addEventListener(
isTouch ? "touchstart" : "mousedown", element.addEventListener("contextmenu", (ev: Event) => {
(ev: Event) => { const e = ev || window.event;
if (e.preventDefault) {
e.preventDefault();
}
if (e.stopPropagation) {
e.stopPropagation();
}
e.cancelBubble = true;
e.returnValue = false;
return false;
});
const clickStart = (ev: Event) => {
this.held = false; this.held = false;
let x; let x;
let y; let y;
@ -81,17 +94,30 @@ class LongPress extends HTMLElement implements LongPress {
this.startAnimation(x, y); this.startAnimation(x, y);
this.held = true; this.held = true;
}, this.holdTime); }, this.holdTime);
}, };
{ passive: true }
); const clickEnd = () => {
element.addEventListener("click", () => { clearTimeout(this.timer);
this.stopAnimation(); this.stopAnimation();
if (isTouch && this.timer === undefined) {
return;
}
this.timer = undefined;
if (this.held) { if (this.held) {
element.dispatchEvent(new Event("ha-hold")); element.dispatchEvent(new Event("ha-hold"));
} else { } else {
element.dispatchEvent(new Event("ha-click")); element.dispatchEvent(new Event("ha-click"));
} }
}); };
if (isTouch) {
element.addEventListener("touchstart", clickStart, { passive: true });
element.addEventListener("touchend", clickEnd);
element.addEventListener("touchcancel", clickEnd);
} else {
element.addEventListener("mousedown", clickStart, { passive: true });
element.addEventListener("click", clickEnd);
}
} }
private startAnimation(x: number, y: number) { private startAnimation(x: number, y: number) {