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,34 +65,59 @@ 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;
this.held = false; if (e.preventDefault) {
let x; e.preventDefault();
let y; }
if ((ev as TouchEvent).touches) { if (e.stopPropagation) {
x = (ev as TouchEvent).touches[0].pageX; e.stopPropagation();
y = (ev as TouchEvent).touches[0].pageY; }
} else { e.cancelBubble = true;
x = (ev as MouseEvent).pageX; e.returnValue = false;
y = (ev as MouseEvent).pageY; return false;
} });
this.timer = window.setTimeout(() => {
this.startAnimation(x, y); const clickStart = (ev: Event) => {
this.held = true; this.held = false;
}, this.holdTime); let x;
}, let y;
{ passive: true } if ((ev as TouchEvent).touches) {
); x = (ev as TouchEvent).touches[0].pageX;
element.addEventListener("click", () => { y = (ev as TouchEvent).touches[0].pageY;
} else {
x = (ev as MouseEvent).pageX;
y = (ev as MouseEvent).pageY;
}
this.timer = window.setTimeout(() => {
this.startAnimation(x, y);
this.held = true;
}, this.holdTime);
};
const clickEnd = () => {
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) {