Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
99 changes: 99 additions & 0 deletions src/panels/lovelace/editor/card-editor/hui-badge-preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import "@polymer/paper-input/paper-textarea";

import { HomeAssistant } from "../../../../types";
import { LovelaceBadgeConfig } from "../../../../data/lovelace";
import { ConfigError } from "../types";
import { computeRTL } from "../../../../common/util/compute_rtl";
import { LovelaceBadge } from "../../types";
import { createBadgeElement } from "../../create-element/create-badge-element";
import { createErrorBadgeConfig } from "../../badges/hui-error-badge";

export class HuiBadgePreview extends HTMLElement {
private _hass?: HomeAssistant;
private _element?: LovelaceBadge;
private _config?: LovelaceBadgeConfig;

private get _error() {
return this._element?.tagName === "HUI-ERROR-BADGE";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure how to determine if its an error. May need to create a public error on the state label badge.

OR

do we need to do anything special for errors on badges? Maybe just checking if the entity exists?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be a hui-error-card as that is handled in create-element-base

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like we do have an error-badge but it is currently unused.

}

constructor() {
super();
this.addEventListener("ll-rebuild", () => {
this._cleanup();
if (this._config) {
this.config = this._config;
}
});
}

set hass(hass: HomeAssistant) {
if (!this._hass || this._hass.language !== hass.language) {
this.style.direction = computeRTL(hass) ? "rtl" : "ltr";
}

this._hass = hass;
if (this._element) {
this._element.hass = hass;
}
}

set error(error: ConfigError) {
this._createBadge(
createErrorBadgeConfig(`${error.type}: ${error.message}`)
);
}

set config(configValue: LovelaceBadgeConfig) {
const curConfig = this._config;
this._config = configValue;

if (!configValue) {
this._cleanup();
return;
}

if (!this._element) {
this._createBadge(configValue);
return;
}

// in case the element was an error element we always want to recreate it
if (!this._error && curConfig && configValue.type === curConfig.type) {
try {
this._element.setConfig(configValue);
} catch (err) {
this._createBadge(createErrorBadgeConfig(err.message));
}
} else {
this._createBadge(configValue);
}
}

private _createBadge(configValue: LovelaceBadgeConfig): void {
this._cleanup();
this._element = createBadgeElement(configValue);

if (this._hass) {
this._element!.hass = this._hass;
}

this.appendChild(this._element!);
}

private _cleanup() {
if (!this._element) {
return;
}
this.removeChild(this._element);
this._element = undefined;
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-badge-preview": HuiBadgePreview;
}
}

customElements.define("hui-badge-preview", HuiBadgePreview);
25 changes: 23 additions & 2 deletions src/panels/lovelace/editor/view-editor/hui-edit-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { haStyleDialog } from "../../../../resources/styles";
import "../../components/hui-entity-editor";
import "./hui-view-editor";
import "./hui-view-visibility-editor";
import "../card-editor/hui-badge-preview";
import { HomeAssistant } from "../../../../types";
import {
LovelaceViewConfig,
Expand Down Expand Up @@ -123,6 +124,20 @@ export class HuiEditView extends LitElement {
break;
case "tab-badges":
content = html`
${this._badges?.length
? html`
<div class="preview-badges">
${this._badges.map((badgeConfig) => {
return html`
<hui-badge-preview
.hass=${this.hass}
.config=${badgeConfig}
></hui-card-preview>
`;
})}
</div>
`
: ""}
<hui-entity-editor
.hass=${this.hass}
.entities="${this._badges}"
Expand Down Expand Up @@ -310,6 +325,7 @@ export class HuiEditView extends LitElement {
return;
}
this._badges = processEditorEntities(ev.detail.entities);
this._resizeDialog();
}

private _isConfigChanged(): boolean {
Expand Down Expand Up @@ -372,8 +388,13 @@ export class HuiEditView extends LitElement {
color: var(--error-color);
border-bottom: 1px solid var(--error-color);
}
</style>
`,
.preview-badges {
display: flex;
justify-content: center;
margin: 8px 16px;
flex-wrap: wrap;
}
`,
];
}
}
Expand Down