Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 54 additions & 18 deletions src/panels/lovelace/hui-view.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { html } from '@polymer/polymer/lib/utils/html-tag.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 debounce from '../../common/util/debounce.js';

Expand All @@ -17,6 +19,12 @@ class HUIView extends PolymerElement {
position: relative;
}

#badges {
margin: 8px 16px;

This comment was marked as resolved.

font-size: 85%;
text-align: center;
}

#columns {
display: flex;
flex-direction: row;
Expand Down Expand Up @@ -53,7 +61,8 @@ class HUIView extends PolymerElement {
}
}
</style>
<div id='columns' on-rebuild-view='_debouncedConfigChanged'></div>
<div id="badges"></div>
<div id="columns" on-rebuild-view="_debouncedConfigChanged"></div>
`;
}

Expand All @@ -63,40 +72,60 @@ class HUIView extends PolymerElement {
type: Object,
observer: '_hassChanged',
},

columns: {
type: Number,
},

config: {
type: Object,
},
config: Object,
columns: Number
};
}

static get observers() {
return [
// Put all properties in 1 observer so we only call configChanged once
'_configChanged(columns, config)'
'_createBadges(config)',
'_createCards(config, columns)'
];
}

constructor() {
super();
this._elements = [];
this._cards = [];
this._badges = [];
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 config = this.config;

while (root.lastChild) {
root.removeChild(root.lastChild);
}

if (!config) {
this._elements = [];
this._cards = [];
return;
}

Expand Down Expand Up @@ -151,17 +180,24 @@ class HUIView extends PolymerElement {
root.appendChild(columnEl);
});

this._elements = elements;
this._cards = elements;

if ('theme' in config) {
applyThemesOnElement(root, this.hass.themes, config.theme);
}
}

_hassChanged(hass) {
for (let i = 0; i < this._elements.length; i++) {
this._elements[i].hass = hass;
}
this._badges.forEach((badge) => {
const { element, entityId } = badge;
element.setProperties({
hass,
state: hass.states[entityId]
});
});
this._cards.forEach((element) => {
element.hass = hass;
});
}
}

Expand Down