From ca93c2cfcdaf0ac0007f474199c51be9617c33da Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 13 Oct 2018 07:18:38 +0200 Subject: [PATCH] Convert glance card to lit (#1760) * Convert glance card to lit * Guard for hass before config * Lint * better click listening * Move config check * Format HTML --- package.json | 1 + src/panels/lovelace/cards/hui-glance-card.js | 113 +++++++++++-------- 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index f0ee071f3e..23598d405e 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "intl-messageformat": "^2.2.0", "js-yaml": "^3.12.0", "leaflet": "^1.3.4", + "lit-html": "^0.12.0", "marked": "^0.5.0", "mdn-polyfills": "^5.12.0", "moment": "^2.22.2", diff --git a/src/panels/lovelace/cards/hui-glance-card.js b/src/panels/lovelace/cards/hui-glance-card.js index 81353615d5..ca56e86ab2 100644 --- a/src/panels/lovelace/cards/hui-glance-card.js +++ b/src/panels/lovelace/cards/hui-glance-card.js @@ -1,5 +1,6 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag.js"; -import { PolymerElement } from "@polymer/polymer/polymer-element.js"; +import { LitElement, html } from "@polymer/lit-element"; +import { classMap } from "lit-html/directives/classMap.js"; +import { repeat } from "lit-html/directives/repeat"; import computeStateDisplay from "../../../common/entity/compute_state_display.js"; import computeStateName from "../../../common/entity/compute_state_name.js"; @@ -18,8 +19,8 @@ import LocalizeMixin from "../../../mixins/localize-mixin.js"; * @appliesMixin EventsMixin * @appliesMixin LocalizeMixin */ -class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) { - static get template() { +class HuiGlanceCard extends LocalizeMixin(EventsMixin(LitElement)) { + renderStyle() { return html` + `; + } - -
- + renderEntity(entityConf) { + const stateObj = this.hass.states[entityConf.entity]; + + return html` +
+ ${ + this._config.show_name !== false + ? html`
${ + "name" in entityConf + ? entityConf.name + : computeStateName(stateObj) + }
` + : "" + } + + ${ + this._config.show_state !== false + ? html`
${computeStateDisplay(this.localize, stateObj)}
` + : "" + } +
+ `; + } + + render() { + if (!this._config) return html``; + const { title } = this._config; + const states = this.hass.states; + const entities = this._configEntities.filter( + (conf) => conf.entity in states + ); + + return html` + ${this.renderStyle()} + +
+ ${repeat( + entities, + (entityConf) => entityConf.entity, + (entityConf) => this.renderEntity(entityConf) + )}
`; @@ -82,9 +118,7 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) { static get properties() { return { - hass: Object, - _config: Object, - _configEntities: Array, + hass: {}, }; } @@ -106,34 +140,13 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) { } this._configEntities = processConfigEntities(config.entities); - } - - _computeClasses(hasHeader) { - return `entities ${hasHeader ? "" : "no-header"}`; - } - - _showEntity(item, states) { - return item.entity in states; - } - - _showInfo(info) { - return info !== false; - } - - _computeName(item, states) { - return "name" in item ? item.name : computeStateName(states[item.entity]); - } - - _computeStateObj(item, states) { - return states[item.entity]; - } - - _computeState(item, states) { - return computeStateDisplay(this.localize, states[item.entity]); + if (this.hass) { + this.requestUpdate(); + } } _handleClick(ev) { - const config = ev.model.item; + const config = ev.currentTarget.entityConf; const entityId = config.entity; switch (config.tap_action) { case "toggle":