forked from thomasloven/lovelace-card-modder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card-modder.js
70 lines (57 loc) · 1.63 KB
/
card-modder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {html, LitElement} from "https://unpkg.com/@polymer/lit-element?module";
class CardModder extends LitElement {
static get properties() {
return {
hass: Object,
config: Object,
};
}
render()
{
return html`${this.card}`;
}
async setConfig(config) {
this.config = config;
let tag = this.config.card.type;
if(tag.startsWith("custom:"))
tag = tag.substr(7);
else
tag = `hui-${tag}-card`;
this.card = document.createElement(tag);
this.card.setConfig(config.card);
if(this.card.updateComplete) {
await this.card.updateComplete;
}
this._cardMod();
}
async _cardMod() {
let target = this.card;
let maxDelay = 5000;
while(maxDelay) {
if(this.card.shadowRoot &&
this.card.shadowRoot.querySelector("ha-card")) {
target = this.card.shadowRoot.querySelector("ha-card");
break;
} else if(this.card.querySelector("ha-card")) {
target = this.card.querySelector("ha-card");
break;
} else if(this.card.firstChild && this.card.firstChild.shadowRoot &&
this.card.firstChild.shadowRoot.querySelector("ha-card")) {
target = this.card.firstChild.shadowRoot.querySelector("ha-card");
break;
}
maxDelay -= 100;
await new Promise(resolve => setTimeout( () => resolve() , 100));
}
for(var k in this.config.style) {
target.style.setProperty(k, this.config.style[k]);
}
}
set hass(hass) {
if(this.card) this.card.hass = hass;
}
getCardSize() {
return this.card.getCardSize();
}
}
customElements.define('card-modder', CardModder);