Card Editor: Add Toggle to Dialog (#5245)

* Add Toggle to dialog

* Fix for Stack Cards

* reviews

* Reviews
This commit is contained in:
Zack Arnett 2020-04-01 13:17:16 -04:00 committed by GitHub
parent e1b48dd2a2
commit 4767fcbdfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 36 deletions

View File

@ -23,6 +23,7 @@ import { HaCodeEditor } from "../../../../components/ha-code-editor";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import { EntityConfig } from "../../entity-rows/types"; import { EntityConfig } from "../../entity-rows/types";
import { getCardElementClass } from "../../create-element/create-card-element"; import { getCardElementClass } from "../../create-element/create-card-element";
import { GUIModeChangedEvent } from "../types";
declare global { declare global {
interface HASSDomEvents { interface HASSDomEvents {
@ -33,6 +34,7 @@ declare global {
config: LovelaceCardConfig; config: LovelaceCardConfig;
error?: string; error?: string;
}; };
"GUImode-changed": GUIModeChangedEvent;
} }
} }
@ -85,16 +87,29 @@ export class HuiCardEditor extends LitElement {
} }
} }
public get hasWarning(): boolean {
return this._warning !== undefined;
}
public get hasError(): boolean { public get hasError(): boolean {
return this._error !== undefined; return this._error !== undefined;
} }
public get GUImode(): boolean {
return this._GUImode;
}
public set GUImode(guiMode: boolean) {
this._GUImode = guiMode;
fireEvent(this as HTMLElement, "GUImode-changed", { guiMode });
}
private get _yamlEditor(): HaCodeEditor { private get _yamlEditor(): HaCodeEditor {
return this.shadowRoot!.querySelector("ha-code-editor")! as HaCodeEditor; return this.shadowRoot!.querySelector("ha-code-editor")! as HaCodeEditor;
} }
public toggleMode() { public toggleMode() {
this._GUImode = !this._GUImode; this.GUImode = !this.GUImode;
} }
public connectedCallback() { public connectedCallback() {
@ -105,7 +120,7 @@ export class HuiCardEditor extends LitElement {
protected render(): TemplateResult { protected render(): TemplateResult {
return html` return html`
<div class="wrapper"> <div class="wrapper">
${this._GUImode ${this.GUImode
? html` ? html`
<div class="gui-editor"> <div class="gui-editor">
${this._loading ${this._loading
@ -145,18 +160,6 @@ export class HuiCardEditor extends LitElement {
</div> </div>
` `
: ""} : ""}
<div class="buttons">
<mwc-button
@click=${this.toggleMode}
?disabled=${this._warning || this._error}
>
${this.hass!.localize(
this._GUImode
? "ui.panel.lovelace.editor.edit_card.show_code_editor"
: "ui.panel.lovelace.editor.edit_card.show_visual_editor"
)}
</mwc-button>
</div>
</div> </div>
`; `;
} }
@ -165,7 +168,7 @@ export class HuiCardEditor extends LitElement {
super.updated(changedProperties); super.updated(changedProperties);
if (changedProperties.has("_GUImode")) { if (changedProperties.has("_GUImode")) {
if (this._GUImode === false) { if (this.GUImode === false) {
// Refresh code editor when switching to yaml mode // Refresh code editor when switching to yaml mode
this._refreshYamlEditor(true); this._refreshYamlEditor(true);
} }
@ -245,6 +248,7 @@ export class HuiCardEditor extends LitElement {
this._handleUIConfigChanged(ev as UIConfigChangedEvent) this._handleUIConfigChanged(ev as UIConfigChangedEvent)
); );
this.GUImode = true;
return; return;
} catch (err) { } catch (err) {
if (err.message.startsWith("WARNING:")) { if (err.message.startsWith("WARNING:")) {
@ -252,7 +256,7 @@ export class HuiCardEditor extends LitElement {
} else { } else {
this._error = err; this._error = err;
} }
this._GUImode = false; this.GUImode = false;
} finally { } finally {
this._loading = false; this._loading = false;
fireEvent(this, "iron-resize"); fireEvent(this, "iron-resize");
@ -277,10 +281,6 @@ export class HuiCardEditor extends LitElement {
.warning { .warning {
color: #ffa726; color: #ffa726;
} }
.buttons {
text-align: right;
padding: 8px 0px;
}
paper-spinner { paper-spinner {
display: block; display: block;
margin: auto; margin: auto;

View File

@ -6,6 +6,7 @@ import {
CSSResultArray, CSSResultArray,
customElement, customElement,
property, property,
query,
} from "lit-element"; } from "lit-element";
import deepFreeze from "deep-freeze"; import deepFreeze from "deep-freeze";
@ -27,6 +28,7 @@ import { addCard, replaceCard } from "../config-util";
import "../../../../components/dialog/ha-paper-dialog"; import "../../../../components/dialog/ha-paper-dialog";
import { haStyleDialog } from "../../../../resources/styles"; import { haStyleDialog } from "../../../../resources/styles";
import { showSaveSuccessToast } from "../../../../util/toast-saved-success"; import { showSaveSuccessToast } from "../../../../util/toast-saved-success";
import { GUIModeChangedEvent } from "../types";
declare global { declare global {
// for fire event // for fire event
@ -51,6 +53,9 @@ export class HuiDialogEditCard extends LitElement {
@property() private _saving: boolean = false; @property() private _saving: boolean = false;
@property() private _error?: string; @property() private _error?: string;
@query("hui-card-editor") private _cardEditorEl?: HuiCardEditor;
@property() private _GUImode?: boolean;
public async showDialog(params: EditCardDialogParams): Promise<void> { public async showDialog(params: EditCardDialogParams): Promise<void> {
this._params = params; this._params = params;
const [view, card] = params.path; const [view, card] = params.path;
@ -62,10 +67,6 @@ export class HuiDialogEditCard extends LitElement {
} }
} }
private get _cardEditorEl(): HuiCardEditor | null {
return this.shadowRoot!.querySelector("hui-card-editor");
}
protected render(): TemplateResult { protected render(): TemplateResult {
if (!this._params) { if (!this._params) {
return html``; return html``;
@ -99,9 +100,9 @@ export class HuiDialogEditCard extends LitElement {
${this._cardConfig === undefined ${this._cardConfig === undefined
? html` ? html`
<hui-card-picker <hui-card-picker
.lovelace="${this._params.lovelaceConfig}" .lovelace=${this._params.lovelaceConfig}
.hass=${this.hass} .hass=${this.hass}
@config-changed="${this._handleCardPicked}" @config-changed=${this._handleCardPicked}
></hui-card-picker> ></hui-card-picker>
` `
: html` : html`
@ -109,15 +110,16 @@ export class HuiDialogEditCard extends LitElement {
<div class="element-editor"> <div class="element-editor">
<hui-card-editor <hui-card-editor
.hass=${this.hass} .hass=${this.hass}
.lovelace="${this._params.lovelaceConfig}" .lovelace=${this._params.lovelaceConfig}
.value="${this._cardConfig}" .value=${this._cardConfig}
@config-changed="${this._handleConfigChanged}" @config-changed=${this._handleConfigChanged}
@GUImode-changed=${this._handleGUIModeChanged}
></hui-card-editor> ></hui-card-editor>
</div> </div>
<div class="element-preview"> <div class="element-preview">
<hui-card-preview <hui-card-preview
.hass=${this.hass} .hass=${this.hass}
.config="${this._cardConfig}" .config=${this._cardConfig}
class=${this._error ? "blur" : ""} class=${this._error ? "blur" : ""}
></hui-card-preview> ></hui-card-preview>
${this._error ${this._error
@ -133,14 +135,30 @@ export class HuiDialogEditCard extends LitElement {
`} `}
</paper-dialog-scrollable> </paper-dialog-scrollable>
<div class="paper-dialog-buttons"> <div class="paper-dialog-buttons">
<mwc-button @click="${this._close}"> ${this._cardConfig !== undefined
? html`
<mwc-button
@click=${this._toggleMode}
?disabled=${this._cardEditorEl?.hasWarning ||
this._cardEditorEl?.hasError}
class="gui-mode-button"
>
${this.hass!.localize(
!this._cardEditorEl || this._GUImode
? "ui.panel.lovelace.editor.edit_card.show_code_editor"
: "ui.panel.lovelace.editor.edit_card.show_visual_editor"
)}
</mwc-button>
`
: ""}
<mwc-button @click=${this._close}>
${this.hass!.localize("ui.common.cancel")} ${this.hass!.localize("ui.common.cancel")}
</mwc-button> </mwc-button>
${this._cardConfig !== undefined ${this._cardConfig !== undefined
? html` ? html`
<mwc-button <mwc-button
?disabled="${!this._canSave || this._saving}" ?disabled=${!this._canSave || this._saving}
@click="${this._save}" @click=${this._save}
> >
${this._saving ${this._saving
? html` ? html`
@ -250,6 +268,9 @@ export class HuiDialogEditCard extends LitElement {
display: block; display: block;
width: 100%; width: 100%;
} }
.gui-mode-button {
margin-right: auto;
}
`, `,
]; ];
} }
@ -278,6 +299,15 @@ export class HuiDialogEditCard extends LitElement {
} }
} }
private _handleGUIModeChanged(ev: HASSDomEvent<GUIModeChangedEvent>): void {
ev.stopPropagation();
this._GUImode = ev.detail.guiMode;
}
private _toggleMode(): void {
this._cardEditorEl?.toggleMode();
}
private _close(): void { private _close(): void {
this._params = undefined; this._params = undefined;
this._cardConfig = undefined; this._cardConfig = undefined;

View File

@ -6,6 +6,7 @@ import {
property, property,
CSSResult, CSSResult,
css, css,
query,
} from "lit-element"; } from "lit-element";
import "@polymer/paper-tabs"; import "@polymer/paper-tabs";
@ -13,8 +14,10 @@ import { struct } from "../../common/structs/struct";
import { HomeAssistant } from "../../../../types"; import { HomeAssistant } from "../../../../types";
import { LovelaceCardEditor } from "../../types"; import { LovelaceCardEditor } from "../../types";
import { StackCardConfig } from "../../cards/types"; import { StackCardConfig } from "../../cards/types";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent, HASSDomEvent } from "../../../../common/dom/fire_event";
import { LovelaceConfig } from "../../../../data/lovelace"; import { LovelaceConfig } from "../../../../data/lovelace";
import { HuiCardEditor } from "../card-editor/hui-card-editor";
import { GUIModeChangedEvent } from "../types";
const cardConfigStruct = struct({ const cardConfigStruct = struct({
type: "string", type: "string",
@ -29,6 +32,8 @@ export class HuiStackCardEditor extends LitElement
@property() public lovelace?: LovelaceConfig; @property() public lovelace?: LovelaceConfig;
@property() private _config?: StackCardConfig; @property() private _config?: StackCardConfig;
@property() private _selectedCard: number = 0; @property() private _selectedCard: number = 0;
@property() private _GUImode?: boolean;
@query("hui-card-editor") private _cardEditorEl?: HuiCardEditor;
public setConfig(config: StackCardConfig): void { public setConfig(config: StackCardConfig): void {
this._config = cardConfigStruct(config); this._config = cardConfigStruct(config);
@ -73,6 +78,18 @@ export class HuiStackCardEditor extends LitElement
selected < numcards selected < numcards
? html` ? html`
<div id="card-options"> <div id="card-options">
<mwc-button
@click=${this._toggleMode}
?disabled=${this._cardEditorEl?.hasWarning ||
this._cardEditorEl?.hasError}
class="gui-mode-button"
>
${this.hass!.localize(
!this._cardEditorEl || this._GUImode
? "ui.panel.lovelace.editor.edit_card.show_code_editor"
: "ui.panel.lovelace.editor.edit_card.show_visual_editor"
)}
</mwc-button>
<paper-icon-button <paper-icon-button
id="move-before" id="move-before"
title="Move card before" title="Move card before"
@ -97,9 +114,10 @@ export class HuiStackCardEditor extends LitElement
<hui-card-editor <hui-card-editor
.hass=${this.hass} .hass=${this.hass}
.value="${this._config.cards[selected]}" .value=${this._config.cards[selected]}
.lovelace=${this.lovelace} .lovelace=${this.lovelace}
@config-changed="${this._handleConfigChanged}" @config-changed=${this._handleConfigChanged}
@GUImode-changed=${this._handleGUIModeChanged}
></hui-card-editor> ></hui-card-editor>
` `
: html` : html`
@ -162,6 +180,15 @@ export class HuiStackCardEditor extends LitElement
fireEvent(this, "config-changed", { config: this._config }); fireEvent(this, "config-changed", { config: this._config });
} }
private _handleGUIModeChanged(ev: HASSDomEvent<GUIModeChangedEvent>): void {
ev.stopPropagation();
this._GUImode = ev.detail.guiMode;
}
private _toggleMode(): void {
this._cardEditorEl?.toggleMode();
}
static get styles(): CSSResult { static get styles(): CSSResult {
return css` return css`
.toolbar { .toolbar {
@ -194,6 +221,10 @@ export class HuiStackCardEditor extends LitElement
margin: 0 -12px; margin: 0 -12px;
} }
} }
.gui-mode-button {
margin-right: auto;
}
`; `;
} }
} }

View File

@ -14,6 +14,10 @@ export interface YamlChangedEvent extends Event {
}; };
} }
export interface GUIModeChangedEvent {
guiMode: boolean;
}
export interface ViewEditEvent extends Event { export interface ViewEditEvent extends Event {
detail: { detail: {
config: LovelaceViewConfig; config: LovelaceViewConfig;