forked from RossMcMillan92/lovelace-collapsable-cards
-
Notifications
You must be signed in to change notification settings - Fork 2
/
collapsable-cards.js
222 lines (185 loc) · 5.76 KB
/
collapsable-cards.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class CollapsableCards extends HTMLElement {
constructor() {
super();
this.containers = ["grid", "custom:collapsable-cards", "vertical-stack", "horizontal-stack"];
this.differentConfig = ["HUI-THERMOSTAT-CARD"];
}
setConfig(config) {
this.id = Math.round(Math.random() * 10000)
this._cardSize = {};
this._cardSize.promise = new Promise((resolve) => (this._cardSize.resolve = resolve));
if (!config || !config.cards || !Array.isArray(config.cards)) {
throw new Error('Supply the `cards` property');
}
let isMobile = window.matchMedia("only screen and (max-width: 760px)").matches;
if (config.defaultOpen == true) {
this.isToggled = true;
} else if (config.defaultOpen == 'desktop-only' && !isMobile) {
this.isToggled = true;
} else {
this.isToggled = false;
}
this._config = config;
this._refCards = [];
this._titleCard = null;
this.renderCard();
}
async renderCard() {
const config = this._config;
if (window.loadCardHelpers) {
this.helpers = await window.loadCardHelpers();
}
const promises = config.cards.map((config) => this.createCardElement(config));
this._refCards = await Promise.all(promises);
if (config.title_card) {
this._titleCard = await this.createCardElement(config.title_card);
}
// Create the card
const card = document.createElement('ha-expansion-panel');
this.card = card;
card.setAttribute('header', this._titleCard || this._config.title || 'Toggle');
card.setAttribute('outlined', '');
const cardList = document.createElement('div');
cardList.id = "root";
this._refCards.forEach((c) => cardList.appendChild(c));
while (this.hasChildNodes()) {
this.removeChild(this.lastChild);
}
this.injectStyles(card, this.getStyles());
card.appendChild(cardList);
this.injectStyles(this, 'ha-expansion-panel { --expansion-panel-content-padding: 0px }')
this.appendChild(card);
// Calculate card size
this._cardSize.resolve();
this.isToggled = config.defaultOpen === 'contain-toggled' ? this.isCardActive() : this.isToggled;
if (this.isToggled) card.setAttribute('expanded', '');
}
getEntitiesNames(card) {
card = card.hasOwnProperty('tagName') && this.differentConfig.includes(card.tagName) ? card.___config : card;
if (this.containers.includes(card.type))
return [].concat(... card.cards.map( (c) => this.getEntitiesNames(c) ));
if (card.hasOwnProperty('entity'))
return [card.entity];
if (card.hasOwnProperty('entities'))
return card.entities;
return [];
}
isCardActive() {
return this.getEntitiesNames(this._config).filter((e) => this._hass.states[e].state !== "off").length > 0
}
async createCardElement(cardConfig) {
const createError = (error, origConfig) => {
return createThing('hui-error-card', {
type: 'error',
error,
origConfig
});
};
const createThing = (tag, config) => {
if (this.helpers) {
if (config.type === 'divider') {
return this.helpers.createRowElement(config);
} else {
return this.helpers.createCardElement(config);
}
}
const element = document.createElement(tag);
try {
element.setConfig(config);
} catch (err) {
console.error(tag, err);
return createError(err.message, config);
}
return element;
};
let tag = cardConfig.type;
if (tag.startsWith('divider')) {
tag = `hui-divider-row`;
} else if (tag.startsWith('custom:')) {
tag = tag.substr('custom:'.length);
} else {
tag = `hui-${tag}-card`;
}
const element = createThing(tag, cardConfig);
element.hass = this._hass;
element.addEventListener(
'll-rebuild',
(ev) => {
ev.stopPropagation();
this.createCardElement(cardConfig).then(() => {
this.renderCard();
});
},
{ once: true }
);
return element;
}
redrawCard(oldHass) {
if (this._config.defaultOpen === 'contain-toggled' && this._haveEntitiesChanged(oldHass)) this.renderCard();
}
set hass(hass) {
let oldHass = this._hass;
this._hass = hass;
if (this._refCards) {
this._refCards.forEach((card) => {
card.hass = hass;
});
}
if (this._titleCard) {
this._titleCard.hass = hass;
}
this.redrawCard(oldHass);
}
_computeCardSize(card) {
if (typeof card.getCardSize === 'function') {
return card.getCardSize();
}
return customElements
.whenDefined(card.localName)
.then(() => this._computeCardSize(card))
.catch(() => 1);
}
_haveEntitiesChanged(oldHass) {
if (!this._hass || !oldHass) return true;
for (const entity of this.getEntitiesNames(this._config)) {
if (this._hass.states[entity].state !== oldHass.states[entity].state) return true;
}
return false;
}
async getCardSize() {
await this._cardSize.promise;
const sizes = await Promise.all(this._refCards.map(this._computeCardSize));
return sizes.reduce((a, b) => a + b);
}
injectStyles(element, css) {
const styleTag = document.createElement('style')
styleTag.innerHTML = css
element.appendChild(styleTag);
}
getStyles() {
return `
#root {
display: flex;
flex-direction: column;
height: 100%;
}
#root > * {
margin: var(
--vertical-stack-card-margin,
var(--stack-card-margin, 4px 0)
);
}
#root > *:last-child {
margin-bottom: 0;
}
`
}
}
customElements.define('collapsable-cards', CollapsableCards);
window.customCards = window.customCards || [];
window.customCards.push({
type: "collapsable-cards",
name: "Collapsable Card",
preview: false,
description: "The Collapsable Card allows you to hide other cards behind a dropdown toggle."
});