mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-16 13:56:35 +00:00
Process code review
This commit is contained in:
parent
91777d45b0
commit
96b9d25bc5
@ -27,6 +27,7 @@ import { LitElement, css, html, nothing } from "lit";
|
|||||||
import { customElement, eventOptions, property, state } from "lit/decorators";
|
import { customElement, eventOptions, property, state } from "lit/decorators";
|
||||||
import { classMap } from "lit/directives/class-map";
|
import { classMap } from "lit/directives/class-map";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
|
import type { PaperListboxElement } from "@polymer/paper-listbox";
|
||||||
import { storage } from "../common/decorators/storage";
|
import { storage } from "../common/decorators/storage";
|
||||||
import { fireEvent } from "../common/dom/fire_event";
|
import { fireEvent } from "../common/dom/fire_event";
|
||||||
import { toggleAttribute } from "../common/dom/toggle_attribute";
|
import { toggleAttribute } from "../common/dom/toggle_attribute";
|
||||||
@ -287,6 +288,13 @@ class HaSidebar extends SubscribeMixin(LitElement) {
|
|||||||
protected firstUpdated(changedProps: PropertyValues) {
|
protected firstUpdated(changedProps: PropertyValues) {
|
||||||
super.firstUpdated(changedProps);
|
super.firstUpdated(changedProps);
|
||||||
this._subscribePersistentNotifications();
|
this._subscribePersistentNotifications();
|
||||||
|
|
||||||
|
window.addEventListener("hass-reset-sidebar", (ev) => {
|
||||||
|
const sidebar = this.shadowRoot?.getElementById(
|
||||||
|
"sidebar"
|
||||||
|
) as PaperListboxElement;
|
||||||
|
sidebar.selected = ev.detail;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _subscribePersistentNotifications(): void {
|
private _subscribePersistentNotifications(): void {
|
||||||
@ -397,6 +405,7 @@ class HaSidebar extends SubscribeMixin(LitElement) {
|
|||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
return html`
|
return html`
|
||||||
<paper-listbox
|
<paper-listbox
|
||||||
|
id="sidebar"
|
||||||
attr-for-selected="data-panel"
|
attr-for-selected="data-panel"
|
||||||
class="ha-scrollbar"
|
class="ha-scrollbar"
|
||||||
.selected=${selectedPanel}
|
.selected=${selectedPanel}
|
||||||
@ -1124,4 +1133,8 @@ declare global {
|
|||||||
interface HTMLElementTagNameMap {
|
interface HTMLElementTagNameMap {
|
||||||
"ha-sidebar": HaSidebar;
|
"ha-sidebar": HaSidebar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface HASSDomEvents {
|
||||||
|
"hass-reset-sidebar": string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,48 +1,57 @@
|
|||||||
import type { LitElement } from "lit";
|
import type { LitElement, PropertyValues } from "lit";
|
||||||
import type { Constructor } from "../types";
|
import type { Constructor } from "../types";
|
||||||
import { isNavigationClick } from "../common/dom/is-navigation-click";
|
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>>(
|
export const PreventUnsavedMixin = <T extends Constructor<LitElement>>(
|
||||||
superClass: T
|
superClass: T
|
||||||
) =>
|
) =>
|
||||||
class extends superClass {
|
class extends superClass {
|
||||||
private _handleClick = async (e: MouseEvent) => {
|
private _handleClick = async (e: MouseEvent) => {
|
||||||
const href = isNavigationClick(e);
|
// get the right target, otherwise the composedPath would return <home-assistant> in the new event
|
||||||
|
const target = e
|
||||||
if (!href) {
|
.composedPath()
|
||||||
|
.find(
|
||||||
|
(n) => (n as HTMLElement).tagName === "HA-SVG-ICON"
|
||||||
|
) as HTMLAnchorElement;
|
||||||
|
if (!isNavigationClick(e)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
e.preventDefault();
|
|
||||||
const result = await this.promptDiscardChanges();
|
const result = await this.promptDiscardChanges();
|
||||||
if (result) {
|
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) => {
|
private _handleUnload = (e: BeforeUnloadEvent) => e.preventDefault();
|
||||||
|
|
||||||
|
private _removeListeners() {
|
||||||
|
window.removeEventListener("click", this._handleClick, true);
|
||||||
|
window.removeEventListener("beforeunload", this._handleUnload);
|
||||||
|
}
|
||||||
|
|
||||||
|
public willUpdate(changedProperties: PropertyValues): void {
|
||||||
|
super.willUpdate(changedProperties);
|
||||||
|
|
||||||
if (this.isDirty()) {
|
if (this.isDirty()) {
|
||||||
e.preventDefault();
|
window.addEventListener("click", this._handleClick, true);
|
||||||
|
window.addEventListener("beforeunload", this._handleUnload);
|
||||||
|
} else {
|
||||||
|
this._removeListeners();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
public connectedCallback(): void {
|
|
||||||
super.connectedCallback();
|
|
||||||
|
|
||||||
document.body.addEventListener("mousedown", this._handleClick, {
|
|
||||||
capture: true,
|
|
||||||
});
|
|
||||||
window.addEventListener("beforeunload", this._handleUnload);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public disconnectedCallback(): void {
|
public disconnectedCallback(): void {
|
||||||
super.disconnectedCallback();
|
super.disconnectedCallback();
|
||||||
|
|
||||||
document.body.removeEventListener("click", this._handleClick, {
|
this._removeListeners();
|
||||||
capture: true,
|
|
||||||
});
|
|
||||||
window.removeEventListener("beforeunload", this._handleUnload);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected isDirty(): boolean {
|
protected isDirty(): boolean {
|
||||||
@ -52,4 +61,8 @@ export const PreventUnsavedMixin = <T extends Constructor<LitElement>>(
|
|||||||
protected async promptDiscardChanges(): Promise<boolean> {
|
protected async promptDiscardChanges(): Promise<boolean> {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected getPanel(): string {
|
||||||
|
return "config";
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user