Skip to content
Merged
Changes from 3 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
68 changes: 50 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,58 @@ 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)) {
this._badges = [];

@balloob balloob Jul 13, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also set CSS display property of this.$.badges to hidden.

return;
}

const elements = [];
config.badges.forEach((entityId) => {
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;
}

_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 +178,22 @@ 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.state = hass.states[entityId];
element.hass = hass;
});
this._cards.forEach((element) => {
element.hass = hass;
});
}
}

Expand Down