mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-09 10:26:35 +00:00
Lovelace: add basic support for badges (#1443)
* Lovelace: add basic support for badges * Lint * Check in entityId exists in states * Hide badges container if no badges * setProperties * lint
This commit is contained in:
parent
834528506a
commit
c11cf53f96
@ -1,6 +1,8 @@
|
|||||||
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
|
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
|
||||||
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
||||||
|
|
||||||
|
import '../../components/entity/ha-state-label-badge.js';
|
||||||
|
|
||||||
import applyThemesOnElement from '../../common/dom/apply_themes_on_element.js';
|
import applyThemesOnElement from '../../common/dom/apply_themes_on_element.js';
|
||||||
import debounce from '../../common/util/debounce.js';
|
import debounce from '../../common/util/debounce.js';
|
||||||
|
|
||||||
@ -17,6 +19,12 @@ class HUIView extends PolymerElement {
|
|||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#badges {
|
||||||
|
margin: 8px 16px;
|
||||||
|
font-size: 85%;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
#columns {
|
#columns {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
@ -53,7 +61,8 @@ class HUIView extends PolymerElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<div id='columns' on-rebuild-view='_debouncedConfigChanged'></div>
|
<div id="badges"></div>
|
||||||
|
<div id="columns" on-rebuild-view="_debouncedConfigChanged"></div>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,40 +72,60 @@ class HUIView extends PolymerElement {
|
|||||||
type: Object,
|
type: Object,
|
||||||
observer: '_hassChanged',
|
observer: '_hassChanged',
|
||||||
},
|
},
|
||||||
|
config: Object,
|
||||||
columns: {
|
columns: Number
|
||||||
type: Number,
|
|
||||||
},
|
|
||||||
|
|
||||||
config: {
|
|
||||||
type: Object,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static get observers() {
|
static get observers() {
|
||||||
return [
|
return [
|
||||||
// Put all properties in 1 observer so we only call configChanged once
|
// Put all properties in 1 observer so we only call configChanged once
|
||||||
'_configChanged(columns, config)'
|
'_createBadges(config)',
|
||||||
|
'_createCards(config, columns)'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this._elements = [];
|
this._cards = [];
|
||||||
|
this._badges = [];
|
||||||
this._debouncedConfigChanged = debounce(this._configChanged, 100);
|
this._debouncedConfigChanged = debounce(this._configChanged, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
_configChanged() {
|
_createBadges(config) {
|
||||||
|
const root = this.$.badges;
|
||||||
|
while (root.lastChild) {
|
||||||
|
root.removeChild(root.lastChild);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config || !config.badges || !Array.isArray(config.badges)) {
|
||||||
|
root.style.display = 'none';
|
||||||
|
this._badges = [];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const elements = [];
|
||||||
|
for (const entityId of config.badges) {
|
||||||
|
if (!(entityId in this.hass.states)) continue;
|
||||||
|
|
||||||
|
const element = document.createElement('ha-state-label-badge');
|
||||||
|
element.state = this.hass.states[entityId];
|
||||||
|
elements.push({ element, entityId });
|
||||||
|
root.appendChild(element);
|
||||||
|
}
|
||||||
|
this._badges = elements;
|
||||||
|
root.style.display = elements.length > 0 ? 'block' : 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
_createCards(config) {
|
||||||
const root = this.$.columns;
|
const root = this.$.columns;
|
||||||
const config = this.config;
|
|
||||||
|
|
||||||
while (root.lastChild) {
|
while (root.lastChild) {
|
||||||
root.removeChild(root.lastChild);
|
root.removeChild(root.lastChild);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config) {
|
if (!config) {
|
||||||
this._elements = [];
|
this._cards = [];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +180,7 @@ class HUIView extends PolymerElement {
|
|||||||
root.appendChild(columnEl);
|
root.appendChild(columnEl);
|
||||||
});
|
});
|
||||||
|
|
||||||
this._elements = elements;
|
this._cards = elements;
|
||||||
|
|
||||||
if ('theme' in config) {
|
if ('theme' in config) {
|
||||||
applyThemesOnElement(root, this.hass.themes, config.theme);
|
applyThemesOnElement(root, this.hass.themes, config.theme);
|
||||||
@ -159,9 +188,16 @@ class HUIView extends PolymerElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_hassChanged(hass) {
|
_hassChanged(hass) {
|
||||||
for (let i = 0; i < this._elements.length; i++) {
|
this._badges.forEach((badge) => {
|
||||||
this._elements[i].hass = hass;
|
const { element, entityId } = badge;
|
||||||
}
|
element.setProperties({
|
||||||
|
hass,
|
||||||
|
state: hass.states[entityId]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
this._cards.forEach((element) => {
|
||||||
|
element.hass = hass;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user