import "@material/mwc-button/mwc-button"; import { mdiAlertOutline } from "@mdi/js"; import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit"; import { customElement, property, state } from "lit/decorators"; import { fireEvent } from "../../common/dom/fire_event"; import "../../components/ha-dialog"; import "../../components/ha-svg-icon"; import "../../components/ha-switch"; import "../../components/ha-textfield"; import { haStyleDialog } from "../../resources/styles"; import { HomeAssistant } from "../../types"; import { DialogBoxParams } from "./show-dialog-box"; @customElement("dialog-box") class DialogBox extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @state() private _params?: DialogBoxParams; @state() private _value?: string; public async showDialog(params: DialogBoxParams): Promise { this._params = params; if (params.prompt) { this._value = params.defaultValue; } } public closeDialog(): boolean { if (this._params?.confirmation || this._params?.prompt) { return false; } if (this._params) { this._dismiss(); return true; } return true; } protected render(): TemplateResult { if (!this._params) { return html``; } const confirmPrompt = this._params.confirmation || this._params.prompt; return html` ` : ""}${this._params.title ? this._params.title : this._params.confirmation && this.hass.localize( "ui.dialogs.generic.default_confirmation_title" )}`} >
${this._params.text ? html`

${this._params.text}

` : ""} ${this._params.prompt ? html` ` : ""}
${confirmPrompt && html` ${this._params.dismissText ? this._params.dismissText : this.hass.localize("ui.dialogs.generic.cancel")} `} ${this._params.confirmText ? this._params.confirmText : this.hass.localize("ui.dialogs.generic.ok")}
`; } private _valueChanged(ev) { this._value = ev.target.value; } private _dismiss(): void { if (this._params?.cancel) { this._params.cancel(); } this._close(); } private _handleKeyUp(ev: KeyboardEvent) { if (ev.keyCode === 13) { this._confirm(); } } private _confirm(): void { if (this._params!.confirm) { this._params!.confirm(this._value); } this._close(); } private _dialogClosed(ev) { if (ev.detail.action === "ignore") { return; } this._dismiss(); } private _close(): void { if (!this._params) { return; } this._params = undefined; fireEvent(this, "dialog-closed", { dialog: this.localName }); } static get styles(): CSSResultGroup { return [ haStyleDialog, css` :host([inert]) { pointer-events: initial !important; cursor: initial !important; } a { color: var(--primary-color); } p { margin: 0; padding-top: 6px; padding-bottom: 24px; color: var(--primary-text-color); } .no-bottom-padding { padding-bottom: 0; } .secondary { color: var(--secondary-text-color); } ha-dialog { /* Place above other dialogs */ --dialog-z-index: 104; } `, ]; } } declare global { interface HTMLElementTagNameMap { "dialog-box": DialogBox; } }