Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix action handler bugs #6811

Merged
merged 1 commit into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/ha-sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import memoizeOne from "memoize-one";
import { LocalStorage } from "../common/decorators/local-storage";
import { fireEvent } from "../common/dom/fire_event";
import { computeDomain } from "../common/entity/compute_domain";
import { navigate } from "../common/navigate";
import { compare } from "../common/string/compare";
import { computeRTL } from "../common/util/compute_rtl";
import { ActionHandlerDetail } from "../data/lovelace";
Expand Down Expand Up @@ -649,6 +650,10 @@ class HaSidebar extends LitElement {
);
}

private _handlePanelTap(ev: Event) {
navigate(this, (ev.currentTarget as HTMLAnchorElement).href);
}

private _renderPanel(
urlPath: string,
title: string | null,
Expand All @@ -661,6 +666,7 @@ class HaSidebar extends LitElement {
href="${`/${urlPath}`}"
data-panel="${urlPath}"
tabindex="-1"
@tap=${this._handlePanelTap}
@mouseenter=${this._itemMouseEnter}
@mouseleave=${this._itemMouseLeave}
>
Expand Down
32 changes: 16 additions & 16 deletions src/panels/lovelace/common/directives/action-handler-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class ActionHandler extends HTMLElement implements ActionHandler {

protected held = false;

private cancelled = false;

private dblClickTimeout?: number;

constructor() {
Expand Down Expand Up @@ -76,9 +78,12 @@ class ActionHandler extends HTMLElement implements ActionHandler {
document.addEventListener(
ev,
() => {
clearTimeout(this.timer);
this.stopAnimation();
this.timer = undefined;
this.cancelled = true;
if (this.timer) {
this.stopAnimation();
clearTimeout(this.timer);
this.timer = undefined;
}
},
{ passive: true }
);
Expand Down Expand Up @@ -124,7 +129,7 @@ class ActionHandler extends HTMLElement implements ActionHandler {
}

element.actionHandler.start = (ev: Event) => {
this.held = false;
this.cancelled = false;
let x;
let y;
if ((ev as TouchEvent).touches) {
Expand All @@ -136,6 +141,7 @@ class ActionHandler extends HTMLElement implements ActionHandler {
}

if (options.hasHold) {
this.held = false;
this.timer = window.setTimeout(() => {
this.startAnimation(x, y);
this.held = true;
Expand All @@ -144,24 +150,20 @@ class ActionHandler extends HTMLElement implements ActionHandler {
};

element.actionHandler.end = (ev: Event) => {
// Don't respond on our own generated click
if (!ev.isTrusted) {
// Don't respond when moved or scrolled while touch
if (["touchend", "touchcancel"].includes(ev.type) && this.cancelled) {
return;
}
// Prevent mouse event if touch event
ev.preventDefault();
if (ev.cancelable) {
ev.preventDefault();
}
if (options.hasHold) {
if (
["touchend", "touchcancel"].includes(ev.type) &&
this.timer === undefined
) {
return;
}
clearTimeout(this.timer);
this.stopAnimation();
this.timer = undefined;
}
if (this.held) {
if (options.hasHold && this.held) {
fireEvent(element, "action", { action: "hold" });
} else if (options.hasDoubleClick) {
if (
Expand All @@ -179,8 +181,6 @@ class ActionHandler extends HTMLElement implements ActionHandler {
}
} else {
fireEvent(element, "action", { action: "tap" });
// Fire the click we prevented the action for
(ev.target as HTMLElement)?.click();
}
};

Expand Down