From 3f124bbb714200f652988a49664c574fda5d4988 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Wed, 19 Feb 2020 21:26:14 -0600 Subject: [PATCH] Use packaged create functions to allow creation of lazy elements --- package.json | 2 +- src/const.ts | 2 +- src/restriction-card.ts | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index cc40481..05844e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "restriction-card", - "version": "1.1.7", + "version": "1.1.8", "description": "Lovelace restriction-card", "keywords": [ "home-assistant", diff --git a/src/const.ts b/src/const.ts index 4c9062b..f666abe 100644 --- a/src/const.ts +++ b/src/const.ts @@ -1 +1 @@ -export const CARD_VERSION = '1.1.7'; +export const CARD_VERSION = '1.1.8'; diff --git a/src/restriction-card.ts b/src/restriction-card.ts index e7fdef0..6a90111 100644 --- a/src/restriction-card.ts +++ b/src/restriction-card.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { TemplateResult, customElement, LitElement, property, html, CSSResult, css, PropertyValues } from 'lit-element'; import { classMap } from 'lit-html/directives/class-map'; @@ -24,6 +25,7 @@ console.info( class RestrictionCard extends LitElement implements LovelaceCard { @property() protected _config?: RestrictionCardConfig; @property() protected _hass?: HomeAssistant; + @property() private _helpers?: any; set hass(hass: HomeAssistant) { this._hass = hass; @@ -57,6 +59,8 @@ class RestrictionCard extends LitElement implements LovelaceCard { } this._config = { duration: 5, action: 'tap', ...config }; + + this.loadCardHelpers(); } protected shouldUpdate(changedProps: PropertyValues): boolean { @@ -117,7 +121,14 @@ class RestrictionCard extends LitElement implements LovelaceCard { private renderCard(config: LovelaceCardConfig): TemplateResult { if (this._hass && this._config) { - const element = createThing(config, this._config.row); + let element; + + if (this._config.row) { + element = this._helpers ? this._helpers.createEntityRow(config) : createThing(config, this._config.row); + } else { + element = this._helpers ? this._helpers.createCardElement(config) : createThing(config, this._config.row); + } + element.hass = this._hass; return html` @@ -140,6 +151,10 @@ class RestrictionCard extends LitElement implements LovelaceCard { ); } + private async loadCardHelpers(): Promise { + this._helpers = await (window as any).loadCardHelpers(); + } + private _handleAction(ev): void { if (this._config && this._config.action === ev.detail.action) { this._handleRestriction();