forked from RossMcMillan92/lovelace-collapsable-cards
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollapsable-card.js
More file actions
343 lines (300 loc) · 10.1 KB
/
collapsable-card.js
File metadata and controls
343 lines (300 loc) · 10.1 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
console.log(`%ccollapsable-card\n%cVersion: ${"0.0.1"}`, "color: rebeccapurple; font-weight: bold;", "");
class CollapsableCard extends HTMLElement {
constructor() {
super();
}
setConfig(config) {
const alignments = {
left: "left",
center: "center",
right: "right",
justify: "space-between",
even: "space-even",
};
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;
}
if (!config.expand_upward) {
this.expand_upward = false;
} else {
this.expand_upward = config.expand_upward;
}
if (config.show_icon === undefined) {
this.show_icon = true;
} else {
this.show_icon = config.show_icon;
}
if (config.head === undefined) {
this.show_head = false;
} else {
this.show_head = true;
}
if (config.content_alignment && Object.keys(alignments).includes(config.content_alignment)) {
this.content_alignment = alignments[config.content_alignment];
}
// Set margin and padding with defaults
this.button_padding = config.button_padding !== undefined ? config.button_padding : (this.show_head ? "0px" : "16px");
this.content_padding = config.content_padding !== undefined ? config.content_padding : "8px";
this.card_margin = config.card_margin !== undefined ? config.card_margin : "0";
this._config = config;
this._refCards = [];
this.renderCard();
}
async renderCard() {
const config = this._config;
if (window.loadCardHelpers) {
this.helpers = await window.loadCardHelpers();
}
if (this.show_head) {
this.head = await this.createCardElement(config.head);
this.head.className = "head-card-" + this.id;
}
const promises = config.cards.map((config) => this.createCardElement(config));
this._refCards = await Promise.all(promises);
// Create the card
const card = document.createElement("ha-card");
this.card = card;
const cardList = document.createElement("div");
this.cardList = cardList;
card.style.overflow = "hidden";
this._refCards.forEach((card) => cardList.appendChild(card));
this.cardList.className = "card-list-" + this.id;
this.cardList.classList[this.isToggled ? "add" : "remove"]("is-toggled");
// create the button
const toggleButton = this.createToggleButton();
if (this.expand_upward == true) {
card.appendChild(cardList);
card.appendChild(toggleButton);
} else {
card.appendChild(toggleButton);
card.appendChild(cardList);
}
while (this.hasChildNodes()) {
this.removeChild(this.lastChild);
}
this.appendChild(card);
// Calculate card size
this._cardSize.resolve();
const styleTag = document.createElement("style");
styleTag.innerHTML = this.getStyles();
card.appendChild(styleTag);
if (config.defaultOpen === "contain-toggled" && config.cards.filter((c) => this.checkActiveCard(c)).length > 0) {
toggleButton.click();
}
}
checkActiveCard(card) {
const containers = ["grid", "vertical-stack", "horizontal-stack"];
return containers.includes(card.type)
? card.cards.filter((c) => this.checkActiveCard(c)).length > 0
: this._hass.states[card.entity]?.state !== "off";
}
createToggleButton() {
const toggleButton = document.createElement("button");
if (this.show_head) {
toggleButton.appendChild(this.head);
} else if (this._config.expand_text && !this.isToggled) {
toggleButton.innerHTML = this._config.expand_text;
} else if (this._config.collapse_text && this.isToggled) {
toggleButton.innerHTML = this._config.collapse_text;
} else {
toggleButton.innerHTML = this._config.title || "Toggle";
}
toggleButton.className = "card-content toggle-button-" + this.id;
if (!this.show_head) {
toggleButton.addEventListener("click", () => {
this.isToggled = !this.isToggled;
this.styleCard(this.isToggled);
});
}
if (this.show_head) {
toggleButton.appendChild(this.head);
}
if (this.show_icon) {
const icon = document.createElement("ha-icon");
icon.className = "toggle-button__icon-" + this.id;
icon.setAttribute("icon", this._config.expand_upward ? "mdi:chevron-up" : "mdi:chevron-down");
if (this.show_head) {
icon.addEventListener("click", () => {
this.isToggled = !this.isToggled;
this.styleCard(this.isToggled);
});
}
this.icon = icon;
toggleButton.appendChild(icon);
}
this.toggleButton = toggleButton;
return toggleButton;
}
styleCard(isToggled) {
this.cardList.classList[isToggled ? "add" : "remove"]("is-toggled");
if (this.show_icon) {
const openIcon = this.expand_upward ? "mdi:chevron-up" : "mdi:chevron-down";
const closeIcon = this.expand_upward ? "mdi:chevron-down" : "mdi:chevron-up";
this.icon.setAttribute("icon", isToggled ? closeIcon : openIcon);
}
if (this._config.expand_text || this._config.collapse_text) {
this.toggleButton.innerHTML = isToggled ? this._config.collapse_text : this._config.expand_text;
if (this.show_icon) {
this.toggleButton.appendChild(this.icon);
}
}
}
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;
}
set hass(hass) {
this._hass = hass;
if (this._refCards) {
this._refCards.forEach((card) => {
card.hass = hass;
});
}
if (this.head) {
this.head.hass = hass;
}
}
_computeCardSize(card) {
if (typeof card.getCardSize === "function") {
return card.getCardSize();
}
return customElements
.whenDefined(card.localName)
.then(() => this._computeCardSize(card))
.catch(() => 1);
}
async getCardSize() {
await this._cardSize.promise;
const sizes = await Promise.all(this._refCards.map(this._computeCardSize));
return sizes.reduce((a, b) => a + b);
}
getStyles() {
return `
.head-card-${this.id} {
grid-column: 1;
grid-row: 1;
}
.toggle-button-${this.id} {
color: var(--primary-text-color);
text-align: left;
background: var(--card-background-color);
border: none;
margin-top: ${this.expand_upward && this.isToggled ? `0;` : `${this.card_margin};`}
margin-bottom: ${!this.expand_upward && this.isToggled ? `0;` : `${this.card_margin};`}
display: ${this.show_head ? "grid" : "flex"};
justify-content: ${this.content_alignment ? this.content_alignment : "space-between"};
align-items: center;
width: 100%;
padding: ${this.button_padding};
border-radius: var(--ha-card-border-radius, 4px);
cursor: pointer;
${this.show_head ? "align-self: start;" : ""}
${this._config.buttonStyle || ""}
}
.toggle-button-${this.id}:focus {
outline: none;
background-color: var(--card-background-color);
}
.card-list-${this.id} {
position: absolute;
width: 1px;
height: 1px;
margin: 0;
padding: 0;
overflow: hidden;
clip: rect(0 0 0 0);
clip-path: inset(50%);
border: 0;
white-space: nowrap;
}
.card-list-${this.id}.is-toggled {
position: unset;
width: unset;
height: unset;
margin: unset;
${this.expand_upward ? `margin-top: ${this.card_margin};` : `margin-bottom: ${this.card_margin};`}
padding: unset;
overflow: unset;
clip: unset;
clip-path: unset;
border: unset;
white-space: unset;
${this.show_head ? "" : this.expand_upward ? `padding-bottom: ${this.content_padding};` : ""}
${this.show_head ? "" : this.expand_upward ? "" : `padding-top: ${this.content_padding};`}
}
.toggle-button__icon-${this.id} {
color: var(--paper-item-icon-color, #aaa);
${this.show_head ? "grid-column: 1;" : ""}
${this.show_head ? "grid-row: 1;" : ""}
${this.show_head ? "position: absolute;" : ""}
${this.show_head ? "top: 16px;" : ""}
${this.show_head ? "right: 16px;" : ""}
}
.type-custom-collapsable-card {
background: transparent;
}
`;
}
}
customElements.define("collapsable-card", CollapsableCard);
window.customCards = window.customCards || [];
window.customCards.push({
type: "collapsable-card",
name: "Collapsable Card",
preview: false,
description: "The Collapsable Card allows you to hide other cards behind a dropdown toggle.",
});