Add close function to edit card dialog (#6423)

This commit is contained in:
Bram Kragten 2020-07-17 20:29:08 +02:00 committed by GitHub
parent 3c63c23e5a
commit cddbf460f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 15 deletions

View File

@ -15,7 +15,8 @@ declare global {
} }
} }
interface HassDialog<T = HASSDomEvents[ValidHassDomEvent]> extends HTMLElement { export interface HassDialog<T = HASSDomEvents[ValidHassDomEvent]>
extends HTMLElement {
showDialog(params: T); showDialog(params: T);
closeDialog?: () => boolean | void; closeDialog?: () => boolean | void;
} }

View File

@ -11,7 +11,7 @@ import {
TemplateResult, TemplateResult,
PropertyValues, PropertyValues,
} from "lit-element"; } from "lit-element";
import type { HASSDomEvent } from "../../../../common/dom/fire_event"; import { HASSDomEvent, fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/ha-dialog"; import "../../../../components/ha-dialog";
import type { import type {
LovelaceCardConfig, LovelaceCardConfig,
@ -30,6 +30,8 @@ import "./hui-card-preview";
import type { EditCardDialogParams } from "./show-edit-card-dialog"; import type { EditCardDialogParams } from "./show-edit-card-dialog";
import { getCardDocumentationURL } from "../get-card-documentation-url"; import { getCardDocumentationURL } from "../get-card-documentation-url";
import { mdiHelpCircle } from "@mdi/js"; import { mdiHelpCircle } from "@mdi/js";
import { HassDialog } from "../../../../dialogs/make-dialog-manager";
import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box";
declare global { declare global {
// for fire event // for fire event
@ -43,7 +45,7 @@ declare global {
} }
@customElement("hui-dialog-edit-card") @customElement("hui-dialog-edit-card")
export class HuiDialogEditCard extends LitElement { export class HuiDialogEditCard extends LitElement implements HassDialog {
@property() protected hass!: HomeAssistant; @property() protected hass!: HomeAssistant;
@internalProperty() private _params?: EditCardDialogParams; @internalProperty() private _params?: EditCardDialogParams;
@ -64,6 +66,8 @@ export class HuiDialogEditCard extends LitElement {
@internalProperty() private _documentationURL?: string; @internalProperty() private _documentationURL?: string;
@internalProperty() private _dirty = false;
public async showDialog(params: EditCardDialogParams): Promise<void> { public async showDialog(params: EditCardDialogParams): Promise<void> {
this._params = params; this._params = params;
this._GUImode = true; this._GUImode = true;
@ -77,6 +81,20 @@ export class HuiDialogEditCard extends LitElement {
} }
} }
public closeDialog(): boolean {
if (this._dirty) {
this._confirmCancel();
return false;
}
this._params = undefined;
this._cardConfig = undefined;
this._error = undefined;
this._documentationURL = undefined;
this._dirty = false;
fireEvent(this, "dialog-closed", { dialog: this.localName });
return true;
}
protected updated(changedProps: PropertyValues): void { protected updated(changedProps: PropertyValues): void {
if ( if (
!this._cardConfig || !this._cardConfig ||
@ -122,7 +140,7 @@ export class HuiDialogEditCard extends LitElement {
open open
scrimClickAction scrimClickAction
@keydown=${this._ignoreKeydown} @keydown=${this._ignoreKeydown}
@closed=${this._close} @closed=${this._cancel}
@opened=${this._opened} @opened=${this._opened}
.heading=${html`${heading} .heading=${html`${heading}
${this._documentationURL !== undefined ${this._documentationURL !== undefined
@ -197,7 +215,7 @@ export class HuiDialogEditCard extends LitElement {
` `
: ""} : ""}
<div slot="primaryAction" @click=${this._save}> <div slot="primaryAction" @click=${this._save}>
<mwc-button @click=${this._close}> <mwc-button @click=${this._cancel}>
${this.hass!.localize("ui.common.cancel")} ${this.hass!.localize("ui.common.cancel")}
</mwc-button> </mwc-button>
${this._cardConfig !== undefined ${this._cardConfig !== undefined
@ -214,7 +232,9 @@ export class HuiDialogEditCard extends LitElement {
size="small" size="small"
></ha-circular-progress> ></ha-circular-progress>
` `
: this.hass!.localize("ui.common.save")} : this._dirty
? this.hass!.localize("ui.common.save")
: this.hass!.localize("ui.common.close")}
</mwc-button> </mwc-button>
` `
: ``} : ``}
@ -344,12 +364,14 @@ export class HuiDialogEditCard extends LitElement {
} }
this._cardConfig = deepFreeze(config); this._cardConfig = deepFreeze(config);
this._error = ev.detail.error; this._error = ev.detail.error;
this._dirty = true;
} }
private _handleConfigChanged(ev: HASSDomEvent<ConfigChangedEvent>) { private _handleConfigChanged(ev: HASSDomEvent<ConfigChangedEvent>) {
this._cardConfig = deepFreeze(ev.detail.config); this._cardConfig = deepFreeze(ev.detail.config);
this._error = ev.detail.error; this._error = ev.detail.error;
this._guiModeAvailable = ev.detail.guiModeAvailable; this._guiModeAvailable = ev.detail.guiModeAvailable;
this._dirty = true;
} }
private _handleGUIModeChanged(ev: HASSDomEvent<GUIModeChangedEvent>): void { private _handleGUIModeChanged(ev: HASSDomEvent<GUIModeChangedEvent>): void {
@ -366,13 +388,6 @@ export class HuiDialogEditCard extends LitElement {
this._cardEditorEl?.refreshYamlEditor(); this._cardEditorEl?.refreshYamlEditor();
} }
private _close(): void {
this._params = undefined;
this._cardConfig = undefined;
this._error = undefined;
this._documentationURL = undefined;
}
private get _canSave(): boolean { private get _canSave(): boolean {
if (this._saving) { if (this._saving) {
return false; return false;
@ -386,8 +401,38 @@ export class HuiDialogEditCard extends LitElement {
return true; return true;
} }
private async _confirmCancel() {
// Make sure the open state of this dialog is handled before the open state of confirm dialog
await new Promise((resolve) => setTimeout(resolve, 0));
const confirm = await showConfirmationDialog(this, {
title: this.hass!.localize(
"ui.panel.lovelace.editor.edit_card.unsaved_changes"
),
text: this.hass!.localize(
"ui.panel.lovelace.editor.edit_card.confirm_cancel"
),
dismissText: this.hass!.localize("ui.common.no"),
confirmText: this.hass!.localize("ui.common.yes"),
});
if (confirm) {
this._cancel();
}
}
private _cancel(ev?: Event) {
if (ev) {
ev.stopPropagation();
}
this._dirty = false;
this.closeDialog();
}
private async _save(): Promise<void> { private async _save(): Promise<void> {
if (!this._canSave || this._saving) { if (!this._canSave) {
return;
}
if (!this._dirty) {
this.closeDialog();
return; return;
} }
this._saving = true; this._saving = true;
@ -405,8 +450,9 @@ export class HuiDialogEditCard extends LitElement {
) )
); );
this._saving = false; this._saving = false;
this._dirty = false;
showSaveSuccessToast(this, this.hass); showSaveSuccessToast(this, this.hass);
this._close(); this.closeDialog();
} }
} }

View File

@ -1901,6 +1901,8 @@
"pick_card": "Which card would you like to add?", "pick_card": "Which card would you like to add?",
"pick_card_view_title": "Which card would you like to add to your {name} view?", "pick_card_view_title": "Which card would you like to add to your {name} view?",
"toggle_editor": "Toggle Editor", "toggle_editor": "Toggle Editor",
"unsaved_changes": "You have unsaved changes",
"confirm_cancel": "Are you sure you want to cancel?",
"show_visual_editor": "Show Visual Editor", "show_visual_editor": "Show Visual Editor",
"show_code_editor": "Show Code Editor", "show_code_editor": "Show Code Editor",
"add": "Add Card", "add": "Add Card",