Skip to content

Commit

Permalink
Fix card size issues
Browse files Browse the repository at this point in the history
Closes #96 #80
  • Loading branch information
ofekashery committed Jan 3, 2021
1 parent ef46a0d commit 05813f3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 43 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Ofek Ashery
Copyright (c) 2021 Ofek Ashery

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.3
0.4.0
93 changes: 52 additions & 41 deletions vertical-stack-in-card.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
const vsinVersion = '0.3.3';
console.log(`%cvertical-stack-in-card\n%cVersion: ${vsinVersion}`, 'color: #1976d2; font-weight: bold;', '');
console.log(`%cvertical-stack-in-card\n%cVersion: ${'0.4.0'}`, 'color: #1976d2; font-weight: bold;', '');

class VerticalStackInCard extends HTMLElement {
constructor() {
super();
}

async setConfig(config) {
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 = [];

if (window.loadCardHelpers) {
this.helpers = await window.loadCardHelpers();
}

this.renderCard();
}

renderCard() {
async renderCard() {
const config = this._config;
if (window.loadCardHelpers) {
this.helpers = await window.loadCardHelpers();
}
const promises = config.cards.map((config) => this.createCardElement(config));
Promise.all(promises).then((cards) => {
cards.forEach((card) => {
if (card.updateComplete) {
card.updateComplete.then(() => this.styleCard(card));
} else {
this.styleCard(card);
}
});
this._refCards = await Promise.all(promises);

this._refCards = cards;
const card = document.createElement('ha-card');
const cardContent = document.createElement('div');
card.header = config.title;
card.style.overflow = 'hidden';
cards.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);

while (this.hasChildNodes()) {
this.removeChild(this.lastChild);
// Style cards
this._refCards.forEach((card) => {
if (card.updateComplete) {
card.updateComplete.then(() => this.styleCard(card));
} else {
this.styleCard(card);
}
this.appendChild(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);
while (this.hasChildNodes()) {
this.removeChild(this.lastChild);
}
this.appendChild(card);

// Calculate card size
this._cardSize.resolve();
}

async createCardElement(cardConfig) {
Expand Down Expand Up @@ -151,12 +154,20 @@ class VerticalStackInCard extends HTMLElement {
}
}

getCardSize() {
let totalSize = 0;
this._refCards.forEach((element) => {
totalSize += typeof element.getCardSize === 'function' ? element.getCardSize() : 1;
});
return totalSize;
_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);
}
}

Expand Down

0 comments on commit 05813f3

Please sign in to comment.