-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add dialog to save config #2100
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
Changes from 5 commits
f4531b6
a8dd50f
f64094f
f1275c6
f8a3369
7e832b7
27cd97e
e1a1f03
762efc5
eec8775
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| import "@polymer/paper-button/paper-button"; | ||
| import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; | ||
| import { fireEvent } from "../../../common/dom/fire_event"; | ||
| import { | ||
| showEditCardDialog, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is perfect, we should do this for all dialogs. |
||
| registerEditCardDialog, | ||
| } from "../editor/hui-dialog-edit-card"; | ||
| import { HomeAssistant } from "../../../types"; | ||
| import { LovelaceCardConfig } from "../types"; | ||
|
|
||
|
|
@@ -18,11 +22,7 @@ export class HuiCardOptions extends LitElement { | |
| super.connectedCallback(); | ||
| if (!registeredDialog) { | ||
| registeredDialog = true; | ||
| fireEvent(this, "register-dialog", { | ||
| dialogShowEvent: "show-edit-card", | ||
| dialogTag: "hui-dialog-edit-card", | ||
| dialogImport: () => import("../editor/hui-dialog-edit-card"), | ||
| }); | ||
| registerEditCardDialog(this); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -48,11 +48,9 @@ export class HuiCardOptions extends LitElement { | |
| `; | ||
| } | ||
| private _editCard() { | ||
| fireEvent(this, "show-edit-card", { | ||
| hass: this.hass, | ||
| cardConfig: this.cardConfig, | ||
| reloadLovelace: () => fireEvent(this, "config-refresh"), | ||
| }); | ||
| showEditCardDialog(this, this.hass!, this.cardConfig!, () => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this should take 2 arguments;
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you already have this with |
||
| fireEvent(this, "config-refresh") | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,33 @@ import { TemplateResult } from "lit-html"; | |
|
|
||
| import { HomeAssistant } from "../../../types"; | ||
| import { LovelaceCardConfig } from "../types"; | ||
| import { EditDialogParams } from "./types"; | ||
| import { fireEvent } from "../../../common/dom/fire_event"; | ||
| import "./hui-edit-card"; | ||
| import "./hui-migrate-config"; | ||
|
|
||
| const dialogShowEvent = "show-edit-card"; | ||
| const dialogTag = "hui-dialog-edit-config"; | ||
|
|
||
| export const registerEditCardDialog = (element: HTMLElement) => | ||
| fireEvent(element, "register-dialog", { | ||
| dialogShowEvent, | ||
| dialogTag, | ||
| dialogImport: () => import("./hui-dialog-edit-card"), | ||
| }); | ||
|
|
||
| export const showEditCardDialog = ( | ||
| element: HTMLElement, | ||
| hass: HomeAssistant, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to pass |
||
| cardConfig: LovelaceCardConfig, | ||
| reloadLovelace: () => void | ||
| ) => | ||
| fireEvent(element, dialogShowEvent, { | ||
| hass, | ||
| cardConfig, | ||
| reloadLovelace, | ||
| }); | ||
|
|
||
| export class HuiDialogEditCard extends LitElement { | ||
| protected _hass?: HomeAssistant; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| private _cardConfig?: LovelaceCardConfig; | ||
|
|
@@ -18,10 +42,10 @@ export class HuiDialogEditCard extends LitElement { | |
| }; | ||
| } | ||
|
|
||
| public async showDialog({ hass, cardConfig, reloadLovelace }): Promise<void> { | ||
| this._hass = hass; | ||
| this._cardConfig = cardConfig; | ||
| this._reloadLovelace = reloadLovelace; | ||
| public async showDialog(params: EditDialogParams): Promise<void> { | ||
| this._hass = params.hass; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just |
||
| this._cardConfig = params.cardConfig; | ||
| this._reloadLovelace = params.reloadLovelace; | ||
| await this.updateComplete; | ||
| (this.shadowRoot!.children[0] as any).showDialog(); | ||
| } | ||
|
|
@@ -55,4 +79,4 @@ declare global { | |
| } | ||
| } | ||
|
|
||
| customElements.define("hui-dialog-edit-card", HuiDialogEditCard); | ||
| customElements.define(dialogTag, HuiDialogEditCard); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; | ||
| import { TemplateResult } from "lit-html"; | ||
|
|
||
| import "@polymer/paper-spinner/paper-spinner"; | ||
| import "@polymer/paper-dialog/paper-dialog"; | ||
| // This is not a duplicate import, one is for types, one is for element. | ||
| // tslint:disable-next-line | ||
| import { PaperDialogElement } from "@polymer/paper-dialog/paper-dialog"; | ||
| import "@polymer/paper-button/paper-button"; | ||
|
|
||
| import { HomeAssistant } from "../../../types"; | ||
| import { LovelaceConfig } from "../types"; | ||
| import { SaveDialogParams } from "./types"; | ||
|
|
||
| import { saveConfig, migrateConfig } from "../common/data"; | ||
| import { fireEvent } from "../../../common/dom/fire_event"; | ||
| import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; | ||
|
|
||
| const dialogShowEvent = "show-save-config"; | ||
| const dialogTag = "hui-dialog-save-config"; | ||
|
|
||
| export const registerSaveDialog = (element: HTMLElement) => | ||
| fireEvent(element, "register-dialog", { | ||
| dialogShowEvent, | ||
| dialogTag, | ||
| dialogImport: () => import("./hui-dialog-save-config"), | ||
| }); | ||
|
|
||
| export const showSaveDialog = ( | ||
| element: HTMLElement, | ||
| hass: HomeAssistant, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to pass |
||
| config: LovelaceConfig, | ||
| reloadLovelace: () => void | ||
| ) => fireEvent(element, dialogShowEvent, { hass, config, reloadLovelace }); | ||
|
|
||
| export class HuiSaveConfig extends hassLocalizeLitMixin(LitElement) { | ||
| protected _hass?: HomeAssistant; | ||
| private _config?: LovelaceConfig; | ||
| private _reloadLovelace?: () => void; | ||
| private _saving: boolean; | ||
|
|
||
| static get properties(): PropertyDeclarations { | ||
| return { | ||
| _hass: {}, | ||
| _cardConfig: {}, | ||
| _saving: {}, | ||
| }; | ||
| } | ||
|
|
||
| protected constructor() { | ||
| super(); | ||
| this._saving = false; | ||
| } | ||
|
|
||
| public async showDialog(params: SaveDialogParams): Promise<void> { | ||
| this._hass = params.hass; | ||
| this._config = params.config; | ||
| this._reloadLovelace = params.reloadLovelace; | ||
| await this.updateComplete; | ||
| this._dialog.open(); | ||
| } | ||
|
|
||
| private get _dialog(): PaperDialogElement { | ||
| return this.shadowRoot!.querySelector("paper-dialog")!; | ||
| } | ||
|
|
||
| protected render(): TemplateResult { | ||
| return html` | ||
| ${this.renderStyle()} | ||
| <paper-dialog with-backdrop> | ||
| <h2>${this.localize("ui.panel.lovelace.editor.save_config.header")}</h2> | ||
| <paper-dialog-scrollable> | ||
| <p>${this.localize("ui.panel.lovelace.editor.save_config.para")}</p> | ||
| <p> | ||
| ${this.localize("ui.panel.lovelace.editor.save_config.para_sure")} | ||
| </p> | ||
| </paper-dialog-scrollable> | ||
| <div class="paper-dialog-buttons"> | ||
| <paper-button @click="${this._closeDialog}" | ||
| >${ | ||
| this.localize("ui.panel.lovelace.editor.save_config.cancel") | ||
| }</paper-button | ||
| > | ||
| <paper-button | ||
| ?disabled="${this._saving}" | ||
| @click="${this._saveConfig}" | ||
| > | ||
| <paper-spinner | ||
| ?active="${this._saving}" | ||
| alt="Saving" | ||
| ></paper-spinner> | ||
| ${ | ||
| this.localize("ui.panel.lovelace.editor.save_config.save") | ||
| }</paper-button | ||
| > | ||
| </div> | ||
| </paper-dialog> | ||
| `; | ||
| } | ||
|
|
||
| private renderStyle(): TemplateResult { | ||
| return html` | ||
| <style> | ||
| paper-dialog { | ||
| width: 650px; | ||
| } | ||
| paper-spinner { | ||
| display: none; | ||
| } | ||
| paper-spinner[active] { | ||
| display: block; | ||
| } | ||
| paper-button paper-spinner { | ||
| width: 14px; | ||
| height: 14px; | ||
| margin-right: 20px; | ||
| } | ||
| </style> | ||
| `; | ||
| } | ||
|
|
||
| private _closeDialog(): void { | ||
| this._dialog.close(); | ||
| } | ||
|
|
||
| private async _saveConfig(): Promise<void> { | ||
| if (!this._hass || !this._config) { | ||
| return; | ||
| } | ||
| this._saving = true; | ||
| delete this._config._frontendAuto; | ||
| try { | ||
| await saveConfig(this._hass, this._config, "json"); | ||
| await migrateConfig(this._hass); | ||
| this._saving = false; | ||
| this._closeDialog(); | ||
| this._reloadLovelace!(); | ||
|
balloob marked this conversation as resolved.
Outdated
|
||
| } catch (err) { | ||
| alert(`Saving failed: ${err.message}`); | ||
| this._saving = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| "hui-dialog-save-config": HuiSaveConfig; | ||
| } | ||
| } | ||
|
|
||
| customElements.define(dialogTag, HuiSaveConfig); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a future PR, we should move this file to
src/data/lovelace.ts, as that's also what we do for all other components.