-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f4531b6
Add dialog to save config
bramkragten a8dd50f
Change types
bramkragten f64094f
Helper funcs for register dialog
bramkragten f1275c6
Clean up
bramkragten f8a3369
Migrate config after save
bramkragten 7e832b7
Clean up
bramkragten 27cd97e
Unused imports
bramkragten e1a1f03
Comments
bramkragten 762efc5
Missed half...
bramkragten eec8775
cardConfig cant be undefined
bramkragten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,9 +48,8 @@ export class HuiCardOptions extends LitElement { | |
| `; | ||
| } | ||
| private _editCard() { | ||
| fireEvent(this, "show-edit-card", { | ||
| hass: this.hass, | ||
| cardConfig: this.cardConfig, | ||
| showEditCardDialog(this, { | ||
| cardConfig: this.cardConfig!, | ||
| reloadLovelace: () => fireEvent(this, "config-refresh"), | ||
| }); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,48 +3,66 @@ import { TemplateResult } from "lit-html"; | |
|
|
||
| import { HomeAssistant } from "../../../types"; | ||
| import { LovelaceCardConfig } 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 interface EditCardDialogParams { | ||
| cardConfig: LovelaceCardConfig; | ||
| reloadLovelace: () => void; | ||
| } | ||
|
|
||
| export const registerEditCardDialog = (element: HTMLElement) => | ||
| fireEvent(element, "register-dialog", { | ||
| dialogShowEvent, | ||
| dialogTag, | ||
| dialogImport: () => import("./hui-dialog-edit-card"), | ||
| }); | ||
|
|
||
| export const showEditCardDialog = ( | ||
| element: HTMLElement, | ||
| editCardDialogParams: EditCardDialogParams | ||
| ) => fireEvent(element, dialogShowEvent, editCardDialogParams); | ||
|
|
||
| export class HuiDialogEditCard extends LitElement { | ||
| protected _hass?: HomeAssistant; | ||
| private _cardConfig?: LovelaceCardConfig; | ||
| private _reloadLovelace?: () => void; | ||
| protected hass?: HomeAssistant; | ||
| private _params?: EditCardDialogParams; | ||
|
|
||
| static get properties(): PropertyDeclarations { | ||
| return { | ||
| _hass: {}, | ||
| hass: {}, | ||
| _cardConfig: {}, | ||
| }; | ||
| } | ||
|
|
||
| public async showDialog({ hass, cardConfig, reloadLovelace }): Promise<void> { | ||
| this._hass = hass; | ||
| this._cardConfig = cardConfig; | ||
| this._reloadLovelace = reloadLovelace; | ||
| public async showDialog(params: EditCardDialogParams): Promise<void> { | ||
| this._params = params; | ||
| await this.updateComplete; | ||
| (this.shadowRoot!.children[0] as any).showDialog(); | ||
| } | ||
|
|
||
| protected render(): TemplateResult { | ||
| if (!this._params) { | ||
| return html``; | ||
| } | ||
| if (!this._params.cardConfig.id) { | ||
| return html` | ||
|
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. Since the whole tag is just 1 turnery statement, I would split it up: if (!this.params.cardConfig!.id) {
return html`
<hui-migrate-config…
`
} |
||
| <hui-migrate-config | ||
| .hass="${this.hass}" | ||
| @reload-lovelace="${this._params.reloadLovelace}" | ||
| ></hui-migrate-config> | ||
| `; | ||
| } | ||
| return html` | ||
| ${ | ||
| this._cardConfig!.id | ||
| ? html` | ||
| <hui-edit-card | ||
| .cardConfig="${this._cardConfig}" | ||
| .hass="${this._hass}" | ||
| @reload-lovelace="${this._reloadLovelace}" | ||
| > | ||
| </hui-edit-card> | ||
| ` | ||
| : html` | ||
| <hui-migrate-config | ||
| .hass="${this._hass}" | ||
| @reload-lovelace="${this._reloadLovelace}" | ||
| ></hui-migrate-config> | ||
| ` | ||
| } | ||
| <hui-edit-card | ||
| .cardConfig="${this._params.cardConfig}" | ||
| .hass="${this.hass}" | ||
| @reload-lovelace="${this._params.reloadLovelace}" | ||
| > | ||
| </hui-edit-card> | ||
| `; | ||
| } | ||
| } | ||
|
|
@@ -55,4 +73,4 @@ declare global { | |
| } | ||
| } | ||
|
|
||
| customElements.define("hui-dialog-edit-card", HuiDialogEditCard); | ||
| customElements.define(dialogTag, HuiDialogEditCard); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| 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 { 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 interface SaveDialogParams { | ||
| config: LovelaceConfig; | ||
| reloadLovelace: () => void; | ||
| } | ||
|
|
||
| export const registerSaveDialog = (element: HTMLElement) => | ||
| fireEvent(element, "register-dialog", { | ||
| dialogShowEvent, | ||
| dialogTag, | ||
| dialogImport: () => import("./hui-dialog-save-config"), | ||
| }); | ||
|
|
||
| export const showSaveDialog = ( | ||
| element: HTMLElement, | ||
| saveDialogParams: SaveDialogParams | ||
| ) => fireEvent(element, dialogShowEvent, saveDialogParams); | ||
|
|
||
| export class HuiSaveConfig extends hassLocalizeLitMixin(LitElement) { | ||
| protected hass?: HomeAssistant; | ||
| private _params?: SaveDialogParams; | ||
| private _saving: boolean; | ||
|
|
||
| static get properties(): PropertyDeclarations { | ||
| return { | ||
| hass: {}, | ||
| _params: {}, | ||
| _saving: {}, | ||
| }; | ||
| } | ||
|
|
||
| protected constructor() { | ||
| super(); | ||
| this._saving = false; | ||
| } | ||
|
|
||
| public async showDialog(params: SaveDialogParams): Promise<void> { | ||
| this._params = params; | ||
| 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._params) { | ||
| return; | ||
| } | ||
| this._saving = true; | ||
| delete this._params.config._frontendAuto; | ||
| try { | ||
| await saveConfig(this.hass, this._params.config, "json"); | ||
| await migrateConfig(this.hass); | ||
| this._saving = false; | ||
| this._closeDialog(); | ||
| this._params.reloadLovelace!(); | ||
| } catch (err) { | ||
| alert(`Saving failed: ${err.message}`); | ||
| this._saving = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| "hui-dialog-save-config": HuiSaveConfig; | ||
| } | ||
| } | ||
|
|
||
| customElements.define(dialogTag, HuiSaveConfig); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.