diff --git a/src/panels/lovelace/editor/config-elements/hui-view-editor.ts b/src/panels/lovelace/editor/config-elements/hui-view-editor.ts new file mode 100644 index 0000000000..872c776663 --- /dev/null +++ b/src/panels/lovelace/editor/config-elements/hui-view-editor.ts @@ -0,0 +1,128 @@ +import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; +import { TemplateResult } from "lit-html"; +import "@polymer/paper-input/paper-input"; + +import { EditorTarget } from "../types"; +import { hassLocalizeLitMixin } from "../../../../mixins/lit-localize-mixin"; +import { HomeAssistant } from "../../../../types"; +import { fireEvent } from "../../../../common/dom/fire_event"; +import { configElementStyle } from "./config-elements-style"; + +import "../../components/hui-theme-select-editor"; +import { LovelaceViewConfig } from "../../../../data/lovelace"; + +declare global { + interface HASSDomEvents { + "view-config-changed": { + config: LovelaceViewConfig; + }; + } +} + +export class HuiViewEditor extends hassLocalizeLitMixin(LitElement) { + static get properties(): PropertyDeclarations { + return { hass: {}, _config: {} }; + } + + get _id(): string { + if (!this._config) { + return ""; + } + return this._config.id || ""; + } + + get _title(): string { + if (!this._config) { + return ""; + } + return this._config.title || ""; + } + + get _icon(): string { + if (!this._config) { + return ""; + } + return this._config.icon || ""; + } + + get _theme(): string { + if (!this._config) { + return ""; + } + return this._config.theme || "Backend-selected"; + } + + public hass?: HomeAssistant; + private _config?: LovelaceViewConfig; + + set config(config: LovelaceViewConfig) { + this._config = config; + } + + protected render(): TemplateResult { + if (!this.hass) { + return html``; + } + + return html` + ${configElementStyle} +
+ + + + +
+ `; + } + + private _valueChanged(ev: Event): void { + if (!this._config || !this.hass) { + return; + } + + const target = ev.currentTarget! as EditorTarget; + + if (this[`_${target.configValue}`] === target.value) { + return; + } + + let newConfig; + + if (target.configValue) { + newConfig = { + ...this._config, + [target.configValue]: target.value, + }; + } + + fireEvent(this, "view-config-changed", { config: newConfig }); + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-view-editor": HuiViewEditor; + } +} + +customElements.define("hui-view-editor", HuiViewEditor); diff --git a/src/panels/lovelace/editor/hui-edit-view.ts b/src/panels/lovelace/editor/hui-edit-view.ts index cf9a75a40b..85502fe22c 100644 --- a/src/panels/lovelace/editor/hui-edit-view.ts +++ b/src/panels/lovelace/editor/hui-edit-view.ts @@ -7,13 +7,16 @@ import { import { TemplateResult } from "lit-html"; import "@polymer/paper-spinner/paper-spinner"; +import "@polymer/paper-tabs/paper-tab"; +import "@polymer/paper-tabs/paper-tabs"; 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 "@polymer/paper-dialog-scrollable/paper-dialog-scrollable"; -import "../components/hui-theme-select-editor"; +import "../components/hui-entity-editor"; +import "./config-elements/hui-view-editor"; import { HomeAssistant } from "../../../types"; import { addView, @@ -22,7 +25,9 @@ import { } from "../../../data/lovelace"; import { fireEvent } from "../../../common/dom/fire_event"; import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; -import { EditorTarget } from "./types"; +import { EntitiesEditorEvent, ViewEditEvent } from "./types"; +import { processEditorEntities } from "./process-editor-entities"; +import { EntityConfig } from "../entity-rows/types"; export class HuiEditView extends hassLocalizeLitMixin(LitElement) { static get properties(): PropertyDeclarations { @@ -31,7 +36,9 @@ export class HuiEditView extends hassLocalizeLitMixin(LitElement) { viewConfig: {}, add: {}, _config: {}, + _badges: {}, _saving: {}, + _curTab: {}, }; } @@ -40,11 +47,15 @@ export class HuiEditView extends hassLocalizeLitMixin(LitElement) { public reloadLovelace?: () => {}; protected hass?: HomeAssistant; private _config?: LovelaceViewConfig; + private _badges?: EntityConfig[]; private _saving: boolean; + private _curTabIndex: number; + private _curTab?: string; protected constructor() { super(); this._saving = false; + this._curTabIndex = 0; } public async showDialog(): Promise { @@ -66,9 +77,12 @@ export class HuiEditView extends hassLocalizeLitMixin(LitElement) { this.viewConfig.id !== (changedProperties.get("viewConfig") as LovelaceViewConfig).id) ) { - this._config = this.viewConfig; + const { cards, badges, ...viewConfig } = this.viewConfig; + this._badges = processEditorEntities(badges); + this._config = viewConfig; } else if (changedProperties.has("add")) { - this._config = { cards: [] }; + this._config = {}; + this._badges = []; } this._resizeDialog(); } @@ -77,67 +91,47 @@ export class HuiEditView extends hassLocalizeLitMixin(LitElement) { return this.shadowRoot!.querySelector("paper-dialog")!; } - get _id(): string { - if (!this._config) { - return ""; - } - return this._config.id || ""; - } - - get _title(): string { - if (!this._config) { - return ""; - } - return this._config.title || ""; - } - - get _icon(): string { - if (!this._config) { - return ""; - } - return this._config.icon || ""; - } - - get _theme(): string { - if (!this._config) { - return ""; - } - return this._config.theme || "Backend-selected"; - } - protected render(): TemplateResult { + let content; + switch (this._curTab) { + case "tab-settings": + content = html` + + `; + break; + case "tab-badges": + content = html` + + `; + break; + case "tab-cards": + content = html` + Cards + `; + break; + } return html` ${this.renderStyle()}

${this.localize("ui.panel.lovelace.editor.edit_view.header")}

- -
- - - - -
-
+ + Settings + Badges + + ${content}
${this.localize("ui.common.cancel")} { + if (!this._config) { + return; + } if (!this._isConfigChanged()) { this._closeDialog(); this._saving = false; return; } + if (this._badges) { + this._config.badges = this._badges.map((entityConf) => { + return entityConf.entity; + }); + } + try { if (this.add) { - await addView(this.hass!, this._config!, "json"); + await addView(this.hass!, this._config, "json"); } else { await updateViewConfig( this.hass!, this.viewConfig!.id!, - this._config!, + this._config, "json" ); } @@ -228,23 +245,17 @@ export class HuiEditView extends hassLocalizeLitMixin(LitElement) { } } - private _valueChanged(ev: Event): void { - if (!this._config || !this.hass) { + private _viewConfigChanged(ev: ViewEditEvent): void { + if (ev.detail && ev.detail.config) { + this._config = ev.detail.config; + } + } + + private _badgesChanged(ev: EntitiesEditorEvent): void { + if (!this._badges || !this.hass || !ev.detail || !ev.detail.entities) { return; } - - const target = ev.currentTarget! as EditorTarget; - - if (this[`_${target.configValue}`] === target.value) { - return; - } - - if (target.configValue) { - this._config = { - ...this._config, - [target.configValue]: target.value, - }; - } + this._badges = ev.detail.entities; } private _isConfigChanged(): boolean { diff --git a/src/panels/lovelace/editor/types.ts b/src/panels/lovelace/editor/types.ts index c814cc8ded..fe76d20ece 100644 --- a/src/panels/lovelace/editor/types.ts +++ b/src/panels/lovelace/editor/types.ts @@ -1,4 +1,4 @@ -import { LovelaceCardConfig } from "../../../data/lovelace"; +import { LovelaceCardConfig, LovelaceViewConfig } from "../../../data/lovelace"; import { EntityConfig } from "../entity-rows/types"; export interface YamlChangedEvent extends Event { @@ -13,6 +13,12 @@ export interface CardPickedEvent extends Event { }; } +export interface ViewEditEvent extends Event { + detail: { + config: LovelaceViewConfig; + }; +} + export interface ConfigValue { format: "json" | "yaml"; value?: string | LovelaceCardConfig; diff --git a/src/panels/lovelace/hui-root.js b/src/panels/lovelace/hui-root.js index a20864fb27..33ffb3177b 100644 --- a/src/panels/lovelace/hui-root.js +++ b/src/panels/lovelace/hui-root.js @@ -327,9 +327,8 @@ class HUIRoot extends NavigateMixin( } _editView() { - const { cards, badges, ...viewConfig } = this.config.views[this._curView]; showEditViewDialog(this, { - viewConfig, + viewConfig: this.config.views[this._curView], add: false, reloadLovelace: () => { this.fire("config-refresh"); diff --git a/src/panels/lovelace/hui-view.js b/src/panels/lovelace/hui-view.js index d78dca3bcf..0f75b0d75c 100644 --- a/src/panels/lovelace/hui-view.js +++ b/src/panels/lovelace/hui-view.js @@ -22,6 +22,7 @@ class HUIView extends localizeMixin(EventsMixin(PolymerElement)) { padding: 4px 4px 0; transform: translateZ(0); position: relative; + min-height: calc(100vh - 155px); } #badges {