forked from RossMcMillan92/lovelace-collapsable-cards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapsable-cards.js
207 lines (177 loc) · 5.68 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
console.log(`%ccollapsable-cards\n%cVersion: ${'0.0.1'}`, 'color: rebeccapurple; font-weight: bold;', '');
class VerticalStackInCard extends HTMLElement {
constructor() {
super();
}
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');
}
this.isToggled = config.defaultOpen || false
this._config = config;
this._refCards = [];
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);
// 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()
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);
}
createToggleButton() {
const toggleButton = document.createElement('button');
toggleButton.innerHTML = this._config.title || 'Toggle'
toggleButton.className = 'card-content toggle-button-' + this.id
toggleButton.addEventListener('click', () => {
this.isToggled = !this.isToggled
this.styleCard(this.isToggled)
})
const icon = document.createElement('ha-icon');
icon.className = 'toggle-button__icon-' + this.id
icon.setAttribute('icon', 'mdi:chevron-down')
this.icon = icon
toggleButton.appendChild(icon)
return toggleButton
}
styleCard(isToggled) {
this.cardList.classList[isToggled ? 'add' : 'remove']('is-toggled')
this.icon.setAttribute('icon', isToggled ? 'mdi:chevron-up' : 'mdi:chevron-down')
}
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;
});
}
}
_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 `
.toggle-button-${this.id} {
color: var(--primary-text-color);
text-align: left;
background: none;
border: none;
margin: 0;
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
border-radius: var(--ha-card-border-radius, 4px);
${this._config.buttonStyle || ''}
}
.toggle-button-${this.id}:focus {
outline: none;
background-color: var(--divider-color);
}
.card-list-${this.id} {
display: none;
}
.card-list-${this.id}.is-toggled {
display: block;
}
.toggle-button__icon-${this.id} {
color: var(--paper-item-icon-color, #aaa);
}
.type-custom-collapsable-cards {
background: transparent;
}
`;
}
}
customElements.define('collapsable-cards', VerticalStackInCard);
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."
});