Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/panels/lovelace/cards/hui-map-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
@property()
private _history?: HassEntity[][];
private _date?: Date;
private _loaded = false;

@property()
private _config?: MapCardConfig;
Expand Down Expand Up @@ -282,6 +283,10 @@ class HuiMapCard extends LitElement implements LovelaceCard {
}

private async loadMap(): Promise<void> {
if (this._loaded) {
return;
}
this._loaded = true;
[this._leafletMap, this.Leaflet] = await setupLeafletMap(
this._mapEl,
this._config !== undefined ? this._config.dark_mode === true : false
Expand Down
41 changes: 26 additions & 15 deletions src/panels/lovelace/editor/card-editor/hui-card-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ import { EntityConfig } from "../../entity-rows/types";
import { getCardElementClass } from "../../create-element/create-card-element";
import { GUIModeChangedEvent } from "../types";

export interface ConfigChangedEvent {
config: LovelaceCardConfig;
error?: string;
guiModeAvailable?: boolean;
}

declare global {
interface HASSDomEvents {
"entities-changed": {
entities: EntityConfig[];
};
"config-changed": {
config: LovelaceCardConfig;
error?: string;
};
"config-changed": ConfigChangedEvent;
"GUImode-changed": GUIModeChangedEvent;
}
}
Expand Down Expand Up @@ -75,6 +78,7 @@ export class HuiCardEditor extends LitElement {
fireEvent(this, "config-changed", {
config: this.value!,
error: this._error,
guiModeAvailable: !(this.hasWarning || this.hasError),
});
}

Expand All @@ -101,7 +105,10 @@ export class HuiCardEditor extends LitElement {

public set GUImode(guiMode: boolean) {
this._GUImode = guiMode;
fireEvent(this as HTMLElement, "GUImode-changed", { guiMode });
fireEvent(this as HTMLElement, "GUImode-changed", {
guiMode,
guiModeAvailable: !(this.hasWarning || this.hasError),
});
}

private get _yamlEditor(): HaCodeEditor {
Expand Down Expand Up @@ -174,6 +181,13 @@ export class HuiCardEditor extends LitElement {
}
fireEvent(this as HTMLElement, "iron-resize");
}

if (this._configElement && changedProperties.has("hass")) {
this._configElement.hass = this.hass;
}
if (this._configElement && changedProperties.has("lovelace")) {
this._configElement.lovelace = this.lovelace;
}
}

private _refreshYamlEditor(focus = false) {
Expand Down Expand Up @@ -232,6 +246,13 @@ export class HuiCardEditor extends LitElement {

this._configElement = configElement;
this._configElType = cardType;

// Perform final setup
this._configElement.hass = this.hass;
this._configElement.lovelace = this.lovelace;
this._configElement.addEventListener("config-changed", (ev) =>
this._handleUIConfigChanged(ev as UIConfigChangedEvent)
);
}

// Setup GUI editor and check that it can handle the current config
Expand All @@ -240,16 +261,6 @@ export class HuiCardEditor extends LitElement {
} catch (err) {
throw Error(`WARNING: ${err.message}`);
}

// Perform final setup
this._configElement!.hass = this.hass;
this._configElement!.lovelace = this.lovelace;
this._configElement!.addEventListener("config-changed", (ev) =>
this._handleUIConfigChanged(ev as UIConfigChangedEvent)
);

this.GUImode = true;
return;
} catch (err) {
if (err.message.startsWith("WARNING:")) {
this._warning = err.message.substr(8);
Expand Down
14 changes: 9 additions & 5 deletions src/panels/lovelace/editor/card-editor/hui-dialog-edit-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from "../../../../data/lovelace";
import "./hui-card-editor";
// tslint:disable-next-line
import { HuiCardEditor } from "./hui-card-editor";
import { HuiCardEditor, ConfigChangedEvent } from "./hui-card-editor";
import "./hui-card-preview";
import "./hui-card-picker";
import { EditCardDialogParams } from "./show-edit-card-dialog";
Expand Down Expand Up @@ -52,12 +52,15 @@ export class HuiDialogEditCard extends LitElement {

@property() private _saving: boolean = false;
@property() private _error?: string;
@property() private _guiModeAvailable? = true;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


@query("hui-card-editor") private _cardEditorEl?: HuiCardEditor;
@property() private _GUImode?: boolean;
@property() private _GUImode = true;

public async showDialog(params: EditCardDialogParams): Promise<void> {
this._params = params;
this._GUImode = true;
this._guiModeAvailable = true;
const [view, card] = params.path;
this._viewConfig = params.lovelaceConfig.views[view];
this._cardConfig =
Expand Down Expand Up @@ -139,8 +142,7 @@ export class HuiDialogEditCard extends LitElement {
? html`
<mwc-button
@click=${this._toggleMode}
?disabled=${this._cardEditorEl?.hasWarning ||
this._cardEditorEl?.hasError}
.disabled=${!this._guiModeAvailable}
class="gui-mode-button"
>
${this.hass!.localize(
Expand Down Expand Up @@ -288,9 +290,10 @@ export class HuiDialogEditCard extends LitElement {
this._error = ev.detail.error;
}

private _handleConfigChanged(ev) {
private _handleConfigChanged(ev: HASSDomEvent<ConfigChangedEvent>) {
this._cardConfig = deepFreeze(ev.detail.config);
this._error = ev.detail.error;
this._guiModeAvailable = ev.detail.guiModeAvailable;
}

private _handleKeyUp(ev: KeyboardEvent) {
Expand All @@ -302,6 +305,7 @@ export class HuiDialogEditCard extends LitElement {
private _handleGUIModeChanged(ev: HASSDomEvent<GUIModeChangedEvent>): void {
ev.stopPropagation();
this._GUImode = ev.detail.guiMode;
this._guiModeAvailable = ev.detail.guiModeAvailable;
}

private _toggleMode(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class HuiAlarmPanelCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class HuiButtonCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ import {
property,
CSSResult,
css,
query,
} from "lit-element";
import "@polymer/paper-tabs";

import { struct } from "../../common/structs/struct";
import { HomeAssistant } from "../../../../types";
import { LovelaceCardEditor } from "../../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 "../../../../components/entity/ha-entity-picker";
import "../../../../components/ha-switch";
import {
HuiCardEditor,
ConfigChangedEvent,
} from "../card-editor/hui-card-editor";
import { GUIModeChangedEvent } from "../types";

const conditionStruct = struct({
entity: "string",
Expand All @@ -36,7 +42,10 @@ export class HuiConditionalCardEditor extends LitElement
@property() public hass?: HomeAssistant;
@property() public lovelace?: LovelaceConfig;
@property() private _config?: StackCardConfig;
@property() private _GUImode = true;
@property() private _guiModeAvailable? = true;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need the optional (undefined) ? (I don't remember what that is called)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event does not have to send it, so it could be undefined, should not happen...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah Gotcha

@property() private _cardTab: boolean = false;
@query("hui-card-editor") private _cardEditorEl?: HuiCardEditor;

public setConfig(config: StackCardConfig): void {
this._config = cardConfigStruct(config);
Expand Down Expand Up @@ -69,6 +78,17 @@ export class HuiConditionalCardEditor extends LitElement
${this._config.card.type
? html`
<div class="card-options">
<mwc-button
@click=${this._toggleMode}
.disabled=${!this._guiModeAvailable}
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._handleReplaceCard}
>${this.hass!.localize(
"ui.panel.lovelace.editor.card.conditional.change_type"
Expand All @@ -80,13 +100,14 @@ export class HuiConditionalCardEditor extends LitElement
.value=${this._config.card}
.lovelace=${this.lovelace}
@config-changed=${this._handleCardChanged}
@GUImode-changed=${this._handleGUIModeChanged}
></hui-card-editor>
`
: html`
<hui-card-picker
.hass=${this.hass}
.lovelace=${this.lovelace}
@config-changed=${this._handleCardChanged}
@config-changed=${this._handleCardPicked}
></hui-card-picker>
`}
</div>
Expand Down Expand Up @@ -162,14 +183,44 @@ export class HuiConditionalCardEditor extends LitElement
this._cardTab = parseInt((ev.target! as any).selected!, 10) === 1;
}

private _handleCardChanged(ev: CustomEvent): void {
private _toggleMode(): void {
this._cardEditorEl?.toggleMode();
}

private _setMode(value: boolean): void {
this._GUImode = value;
if (this._cardEditorEl) {
this._cardEditorEl!.GUImode = value;
}
}

private _handleGUIModeChanged(ev: HASSDomEvent<GUIModeChangedEvent>): void {
ev.stopPropagation();
this._GUImode = ev.detail.guiMode;
this._guiModeAvailable = ev.detail.guiModeAvailable;
}

private _handleCardPicked(ev: CustomEvent): void {
ev.stopPropagation();
if (!this._config) {
return;
}
this._setMode(true);
this._guiModeAvailable = true;
this._config.card = ev.detail.config;
fireEvent(this, "config-changed", { config: this._config });
}

private _handleCardChanged(ev: HASSDomEvent<ConfigChangedEvent>): void {
ev.stopPropagation();
if (!this._config) {
return;
}
this._config.card = ev.detail.config;
this._guiModeAvailable = ev.detail.guiModeAvailable;
fireEvent(this, "config-changed", { config: this._config });
}

private _handleReplaceCard(): void {
if (!this._config) {
return;
Expand Down Expand Up @@ -267,6 +318,9 @@ export class HuiConditionalCardEditor extends LitElement
justify-content: flex-end;
width: 100%;
}
.gui-mode-button {
margin-right: auto;
}
`;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class HuiEntitiesCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class HuiEntityCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class HuiGaugeCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class HuiGlanceCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class HuiHistoryGraphCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class HuiIframeCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class HuiLightCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class HuiMapCardEditor extends LitElement implements LovelaceCardEditor {
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class HuiMarkdownCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class HuiMediaControlCardEditor extends LitElement
}

protected render(): TemplateResult {
if (!this.hass) {
if (!this.hass || !this._config) {
return html``;
}

Expand Down
Loading