mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-18 14:56:37 +00:00
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
This commit is contained in:
parent
a633e3c553
commit
ca93c2cfcd
@ -76,6 +76,7 @@
|
|||||||
"intl-messageformat": "^2.2.0",
|
"intl-messageformat": "^2.2.0",
|
||||||
"js-yaml": "^3.12.0",
|
"js-yaml": "^3.12.0",
|
||||||
"leaflet": "^1.3.4",
|
"leaflet": "^1.3.4",
|
||||||
|
"lit-html": "^0.12.0",
|
||||||
"marked": "^0.5.0",
|
"marked": "^0.5.0",
|
||||||
"mdn-polyfills": "^5.12.0",
|
"mdn-polyfills": "^5.12.0",
|
||||||
"moment": "^2.22.2",
|
"moment": "^2.22.2",
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { html } from "@polymer/polymer/lib/utils/html-tag.js";
|
import { LitElement, html } from "@polymer/lit-element";
|
||||||
import { PolymerElement } from "@polymer/polymer/polymer-element.js";
|
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 computeStateDisplay from "../../../common/entity/compute_state_display.js";
|
||||||
import computeStateName from "../../../common/entity/compute_state_name.js";
|
import computeStateName from "../../../common/entity/compute_state_name.js";
|
||||||
@ -18,8 +19,8 @@ import LocalizeMixin from "../../../mixins/localize-mixin.js";
|
|||||||
* @appliesMixin EventsMixin
|
* @appliesMixin EventsMixin
|
||||||
* @appliesMixin LocalizeMixin
|
* @appliesMixin LocalizeMixin
|
||||||
*/
|
*/
|
||||||
class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
|
class HuiGlanceCard extends LocalizeMixin(EventsMixin(LitElement)) {
|
||||||
static get template() {
|
renderStyle() {
|
||||||
return html`
|
return html`
|
||||||
<style>
|
<style>
|
||||||
:host(.theme-primary) {
|
:host(.theme-primary) {
|
||||||
@ -55,26 +56,61 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
|
|||||||
.name {
|
.name {
|
||||||
min-height: var(--paper-font-body1_-_line-height, 20px);
|
min-height: var(--paper-font-body1_-_line-height, 20px);
|
||||||
}
|
}
|
||||||
|
state-badge {
|
||||||
|
margin: 8px 0;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
<ha-card header="[[_config.title]]">
|
renderEntity(entityConf) {
|
||||||
<div class$="[[_computeClasses(_config.title)]]">
|
const stateObj = this.hass.states[entityConf.entity];
|
||||||
<template is="dom-repeat" items="[[_configEntities]]">
|
|
||||||
<template is="dom-if" if="[[_showEntity(item, hass.states)]]">
|
return html`
|
||||||
<div class="entity" on-click="_handleClick">
|
<div
|
||||||
<template is="dom-if" if="[[_showInfo(_config.show_name)]]">
|
class="entity"
|
||||||
<div class="name">[[_computeName(item, hass.states)]]</div>
|
.entityConf="${entityConf}"
|
||||||
</template>
|
@click="${this._handleClick}"
|
||||||
<state-badge
|
>
|
||||||
state-obj="[[_computeStateObj(item, hass.states)]]"
|
${
|
||||||
override-icon="[[item.icon]]"
|
this._config.show_name !== false
|
||||||
></state-badge>
|
? html`<div class="name">${
|
||||||
<template is="dom-if" if="[[_showInfo(_config.show_state)]]">
|
"name" in entityConf
|
||||||
<div>[[_computeState(item, hass.states)]]</div>
|
? entityConf.name
|
||||||
</template>
|
: computeStateName(stateObj)
|
||||||
</div>
|
}</div>`
|
||||||
</template>
|
: ""
|
||||||
</template>
|
}
|
||||||
|
<state-badge
|
||||||
|
.stateObj="${stateObj}"
|
||||||
|
.overrideIcon="${entityConf.icon}"
|
||||||
|
></state-badge>
|
||||||
|
${
|
||||||
|
this._config.show_state !== false
|
||||||
|
? html`<div>${computeStateDisplay(this.localize, stateObj)}</div>`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
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()}
|
||||||
|
<ha-card .header="${title}">
|
||||||
|
<div class="entities ${classMap({ "no-header": !title })}">
|
||||||
|
${repeat(
|
||||||
|
entities,
|
||||||
|
(entityConf) => entityConf.entity,
|
||||||
|
(entityConf) => this.renderEntity(entityConf)
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</ha-card>
|
</ha-card>
|
||||||
`;
|
`;
|
||||||
@ -82,9 +118,7 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
|
|||||||
|
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
hass: Object,
|
hass: {},
|
||||||
_config: Object,
|
|
||||||
_configEntities: Array,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,34 +140,13 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._configEntities = processConfigEntities(config.entities);
|
this._configEntities = processConfigEntities(config.entities);
|
||||||
}
|
if (this.hass) {
|
||||||
|
this.requestUpdate();
|
||||||
_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]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_handleClick(ev) {
|
_handleClick(ev) {
|
||||||
const config = ev.model.item;
|
const config = ev.currentTarget.entityConf;
|
||||||
const entityId = config.entity;
|
const entityId = config.entity;
|
||||||
switch (config.tap_action) {
|
switch (config.tap_action) {
|
||||||
case "toggle":
|
case "toggle":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user