Remove card-maker dependency. Update for tap_action

This commit is contained in:
Thomas Lovén 2020-01-08 12:10:03 +01:00
parent a00f7c94ac
commit 3b1eca2c90
2 changed files with 40 additions and 64 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,12 +1,10 @@
import { LitElement, html, css } from "card-tools/src/lit-element.js"; import { LitElement, html, css } from "card-tools/src/lit-element.js";
import { hass } from "card-tools/src/hass.js"; import { hass } from "card-tools/src/hass.js";
import "card-tools/src/card-maker.js"; import { DOMAINS_HIDE_MORE_INFO, createEntityRow } from "card-tools/src/lovelace-element.js";
import { DOMAINS_HIDE_MORE_INFO } from "card-tools/src/lovelace-element.js";
class FoldEntityRow extends LitElement { class FoldEntityRow extends LitElement {
static get properties() { static get properties() {
return { return {
_hass: {},
open: Boolean, open: Boolean,
items: {}, items: {},
}; };
@ -22,33 +20,43 @@ class FoldEntityRow extends LitElement {
this._config = Object.assign({}, defaults, config); this._config = Object.assign({}, defaults, config);
this.open = this.open || this._config.open; this.open = this.open || this._config.open;
this.head = this._config.head; let head = this._config.head;
if (this._config.entity) if (this._config.entity)
this.head = this._config.entity; head = this._config.entity;
if (typeof this.head === "string") if (typeof head === "string")
this.head = {entity: this.head}; head = {entity: head};
// Items are taken from the first available of the following // Items are taken from the first available of the following
// - The group specified as head // - The group specified as head
// - config entities: (this allows auto-population of the list) // - config entities: (this allows auto-population of the list)
// - config items: (for backwards compatibility - not recommended) // - config items: (for backwards compatibility - not recommended)
this.items = this._config.items; let items = this._config.items;
if (this._config.entities) if (this._config.entities)
this.items = this._config.entities; items = this._config.entities;
if (this.head.entity && this.head.entity.startsWith("group.") && !this.items) if (head.entity && head.entity.startsWith("group.") && !items)
this.items = hass().states[this.head.entity].attributes.entity_id; items = hass().states[head.entity].attributes.entity_id;
const fix_config = (config) => {
if(typeof config === "string")
config = {entity: config};
return Object.assign({}, this._config.group_config, config);
} }
clickRow(ev) { this.head = createEntityRow(head);
ev.stopPropagation(); this.head.hass = hass();
this.head.addEventListener("click", (ev) => {
const config = ev.target.parentElement._config; if(!this.hasMoreInfo(head) && !head.tap_action)
if(this.hasMoreInfo(config) || config.tap_action) {
customElements.get('hui-entities-card').prototype._handleClick.bind(this)(config);
} else if(ev.target.parentElement.hasAttribute('head')) {
this.toggle(ev); this.toggle(ev);
} });
this.head.setAttribute('head', 'head');
this.rows = items.map((i) => {
const row = createEntityRow(fix_config(i));
row.hass = hass();
if(this.hasMoreInfo(i))
row.classList.add("state-card-dialog");
return row;
});
} }
toggle(ev) { toggle(ev) {
@ -66,7 +74,7 @@ class FoldEntityRow extends LitElement {
firstUpdated() { firstUpdated() {
// If the header is a section-row, adjust the divider line a bit to look better // If the header is a section-row, adjust the divider line a bit to look better
const headRow = this.shadowRoot.querySelector("#head > entity-row-maker"); const headRow = this.head;
headRow.updateComplete.then(() => { headRow.updateComplete.then(() => {
const element = headRow.querySelector("hui-section-row"); const element = headRow.querySelector("hui-section-row");
if(element) { if(element) {
@ -78,27 +86,14 @@ class FoldEntityRow extends LitElement {
} }
set hass(hass) { set hass(hass) {
this._hass = hass; this.rows.forEach((e) => e.hass = hass);
this.head.hass = hass;
} }
render() { render() {
if (this._entities)
this._entities.forEach((e) => e.hass = this._hass);
const fix_config = (config) => {
if(typeof config === "string")
config = {entity: config};
return Object.assign({}, this._config.group_config, config);
}
return html` return html`
<div id="head" ?open=${this.open}> <div id="head" ?open=${this.open}>
<entity-row-maker ${this.head}
.config=${this.head}
.hass=${this._hass}
@click=${this.clickRow}
head
></entity-row-maker>
<ha-icon <ha-icon
@click=${this.toggle} @click=${this.toggle}
icon=${this.open ? "mdi:chevron-up" : "mdi:chevron-down"} icon=${this.open ? "mdi:chevron-up" : "mdi:chevron-down"}
@ -113,14 +108,7 @@ class FoldEntityRow extends LitElement {
: '' : ''
} }
> >
${this.items.map(i => html` ${this.rows}
<entity-row-maker
.config=${fix_config(i)}
.hass=${this._hass}
@click=${this.clickRow}
class=${this.hasMoreInfo(i) ? 'state-card-dialog' : ''}
></entity-row-maker>
`)}
</div> </div>
`; `;
} }
@ -133,7 +121,7 @@ class FoldEntityRow extends LitElement {
cursor: pointer; cursor: pointer;
align-items: center; align-items: center;
} }
#head entity-row-maker { #head :not(ha-icon) {
flex-grow: 1; flex-grow: 1;
max-width: calc(100% - var(--toggle-icon-width)); max-width: calc(100% - var(--toggle-icon-width));
} }