-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathvertical-stack-in-card.js
203 lines (184 loc) · 5.7 KB
/
vertical-stack-in-card.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
console.log(
`%cvertical-stack-in-card\n%cVersion: ${'1.0.1'}`,
'color: #1976d2; font-weight: bold;',
''
);
class VerticalStackInCard extends HTMLElement {
constructor() {
super();
}
setConfig(config) {
this._cardSize = {};
this._cardSize.promise = new Promise(
(resolve) => (this._cardSize.resolve = resolve)
);
if (!config || !config.cards || !Array.isArray(config.cards)) {
throw new Error('Card config incorrect');
}
this._config = config;
this._refCards = [];
this.renderCard();
}
async renderCard() {
const config = this._config;
const promises = config.cards.map((config) =>
this._createCardElement(config)
);
this._refCards = await Promise.all(promises);
// Style cards
this._refCards.forEach((card) => {
if (card.updateComplete) {
card.updateComplete.then(() => this._styleCard(card));
} else {
this._styleCard(card);
}
});
// Create the card
const card = document.createElement('ha-card');
const cardContent = document.createElement('div');
card.header = config.title;
card.style.overflow = 'hidden';
this._refCards.forEach((card) => cardContent.appendChild(card));
if (config.horizontal) {
cardContent.style.display = 'flex';
cardContent.childNodes.forEach((card) => {
card.style.flex = '1 1 0';
card.style.minWidth = 0;
});
}
card.appendChild(cardContent);
const shadowRoot = this.shadowRoot || this.attachShadow({ mode: 'open' });
while (shadowRoot.hasChildNodes()) {
shadowRoot.removeChild(shadowRoot.lastChild);
}
shadowRoot.appendChild(card);
// Calculate card size
this._cardSize.resolve();
}
async _createCardElement(cardConfig) {
const helpers = await window.loadCardHelpers();
const element =
cardConfig.type === 'divider'
? helpers.createRowElement(cardConfig)
: helpers.createCardElement(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;
});
}
}
_styleCard(element) {
const config = this._config;
if (element.shadowRoot) {
if (element.shadowRoot.querySelector('ha-card')) {
let ele = element.shadowRoot.querySelector('ha-card');
ele.style.boxShadow = 'none';
ele.style.borderRadius = '0';
ele.style.border = 'none';
if ('styles' in config) {
Object.entries(config.styles).forEach(([key, value]) =>
ele.style.setProperty(key, value)
);
}
} else {
let searchEles = element.shadowRoot.getElementById('root');
if (!searchEles) {
searchEles = element.shadowRoot.getElementById('card');
}
if (!searchEles) return;
searchEles = searchEles.childNodes;
for (let i = 0; i < searchEles.length; i++) {
if (searchEles[i].style) {
searchEles[i].style.margin = '0px';
}
this._styleCard(searchEles[i]);
}
}
} else {
if (
typeof element.querySelector === 'function' &&
element.querySelector('ha-card')
) {
let ele = element.querySelector('ha-card');
ele.style.boxShadow = 'none';
ele.style.borderRadius = '0';
ele.style.border = 'none';
if ('styles' in config) {
Object.entries(config.styles).forEach(([key, value]) =>
ele.style.setProperty(key, value)
);
}
}
let searchEles = element.childNodes;
for (let i = 0; i < searchEles.length; i++) {
if (searchEles[i] && searchEles[i].style) {
searchEles[i].style.margin = '0px';
}
this._styleCard(searchEles[i]);
}
}
}
_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, 0);
}
static async getConfigElement() {
// Ensure the hui-stack-card-editor is loaded.
let cls = customElements.get('hui-vertical-stack-card');
if (!cls) {
const helpers = await window.loadCardHelpers();
helpers.createCardElement({ type: 'vertical-stack', cards: [] });
await customElements.whenDefined('hui-vertical-stack-card');
cls = customElements.get('hui-vertical-stack-card');
}
const configElement = await cls.getConfigElement();
// Patch setConfig to remove non-VSIC config options.
const originalSetConfig = configElement.setConfig;
configElement.setConfig = (config) =>
originalSetConfig.call(configElement, {
type: config.type,
title: config.title,
cards: config.cards || [],
});
return configElement;
}
static getStubConfig() {
return {
cards: [],
};
}
}
customElements.define('vertical-stack-in-card', VerticalStackInCard);
window.customCards = window.customCards || [];
window.customCards.push({
type: 'vertical-stack-in-card',
name: 'Vertical Stack In Card',
description: 'Group multiple cards into a single sleek card.',
preview: false,
documentationURL: 'https://github.com/ofekashery/vertical-stack-in-card',
});