Skip to content

Commit

Permalink
Process code review
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbede committed Dec 6, 2024
1 parent 1e8ce01 commit 5a12e11
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
13 changes: 13 additions & 0 deletions src/components/ha-sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { LitElement, css, html, nothing } from "lit";
import { customElement, eventOptions, property, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map";
import memoizeOne from "memoize-one";
import type { PaperListboxElement } from "@polymer/paper-listbox";
import { storage } from "../common/decorators/storage";
import { fireEvent } from "../common/dom/fire_event";
import { toggleAttribute } from "../common/dom/toggle_attribute";
Expand Down Expand Up @@ -285,6 +286,13 @@ class HaSidebar extends SubscribeMixin(LitElement) {
protected firstUpdated(changedProps: PropertyValues) {
super.firstUpdated(changedProps);
this._subscribePersistentNotifications();

window.addEventListener("hass-reset-sidebar", (ev) => {
const sidebar = this.shadowRoot?.getElementById(
"sidebar"
) as PaperListboxElement;
sidebar.selected = ev.detail;
});
}

private _subscribePersistentNotifications(): void {
Expand Down Expand Up @@ -395,6 +403,7 @@ class HaSidebar extends SubscribeMixin(LitElement) {
// prettier-ignore
return html`
<paper-listbox
id="sidebar"
attr-for-selected="data-panel"
class="ha-scrollbar"
.selected=${selectedPanel}
Expand Down Expand Up @@ -1122,4 +1131,8 @@ declare global {
interface HTMLElementTagNameMap {
"ha-sidebar": HaSidebar;
}

interface HASSDomEvents {
"hass-reset-sidebar": string;
}
}
57 changes: 35 additions & 22 deletions src/mixins/prevent-unsaved-mixin.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
import type { LitElement } from "lit";
import type { LitElement, PropertyValues } from "lit";
import type { Constructor } from "../types";
import { isNavigationClick } from "../common/dom/is-navigation-click";
import { navigate } from "../common/navigate";
import { fireEvent } from "../common/dom/fire_event";

export const PreventUnsavedMixin = <T extends Constructor<LitElement>>(
superClass: T
) =>
class extends superClass {
private _handleClick = async (e: MouseEvent) => {
const href = isNavigationClick(e);

if (!href) {
// get the right target, otherwise the composedPath would return <home-assistant> in the new event
const target = e
.composedPath()
.find(
(n) => (n as HTMLElement).tagName === "HA-SVG-ICON"
) as HTMLAnchorElement;
if (!isNavigationClick(e)) {
return;
}

e.preventDefault();
const result = await this.promptDiscardChanges();
if (result) {
navigate(href);
this._removeListeners();
if (target) {
const newEvent = new MouseEvent(e.type, e);
target.dispatchEvent(newEvent);
}
} else {
fireEvent(this, "hass-reset-sidebar", this.getPanel());
}
};

private _handleUnload = (e: BeforeUnloadEvent) => {
if (this.isDirty()) {
e.preventDefault();
}
};
private _handleUnload = (e: BeforeUnloadEvent) => e.preventDefault();

private _removeListeners() {
window.removeEventListener("click", this._handleClick, true);
window.removeEventListener("beforeunload", this._handleUnload);
}

public connectedCallback(): void {
super.connectedCallback();
public willUpdate(changedProperties: PropertyValues): void {
super.willUpdate(changedProperties);

document.body.addEventListener("mousedown", this._handleClick, {
capture: true,
});
window.addEventListener("beforeunload", this._handleUnload);
if (this.isDirty()) {
window.addEventListener("click", this._handleClick, true);
window.addEventListener("beforeunload", this._handleUnload);
} else {
this._removeListeners();
}
}

public disconnectedCallback(): void {
super.disconnectedCallback();

document.body.removeEventListener("click", this._handleClick, {
capture: true,
});
window.removeEventListener("beforeunload", this._handleUnload);
this._removeListeners();
}

protected isDirty(): boolean {
Expand All @@ -52,4 +61,8 @@ export const PreventUnsavedMixin = <T extends Constructor<LitElement>>(
protected async promptDiscardChanges(): Promise<boolean> {
return true;
}

protected getPanel(): string {
return "config";
}
};

0 comments on commit 5a12e11

Please sign in to comment.