Skip to content

Commit

Permalink
Convert hui-vertical-stack-card to TypeScript/LitElement (home-assist…
Browse files Browse the repository at this point in the history
…ant#1846)

Failed to rebase previous branch and am taking my working changes and applying to a new branch based off of current master.

Updated tslint.json to allow for prefixed `_` to variable names
  • Loading branch information
iantrich authored and balloob committed Oct 24, 2018
1 parent 47fb8a5 commit cf2171e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 94 deletions.
92 changes: 0 additions & 92 deletions src/panels/lovelace/cards/hui-vertical-stack-card.js

This file was deleted.

93 changes: 93 additions & 0 deletions src/panels/lovelace/cards/hui-vertical-stack-card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { html, LitElement } from "@polymer/lit-element";

import computeCardSize from "../common/compute-card-size.js";
import createCardElement from "../common/create-card-element.js";

import { LovelaceCard, LovelaceConfig } from "../types";
import { HomeAssistant } from "../../../types";

interface Config extends LovelaceConfig {
cards: LovelaceConfig[];
}

class HuiVerticalStackCard extends LitElement implements LovelaceCard {
protected config?: Config;
private _hass?: HomeAssistant;

static get properties() {
return {
config: {},
};
}

set hass(hass: HomeAssistant) {
this._hass = hass;
for (const el of this.shadowRoot!.querySelectorAll("#root > *")) {
const element = el as LovelaceCard;
element.hass = this._hass;
}
}

public getCardSize() {
let totalSize = 0;
for (const element of this.shadowRoot!.querySelectorAll("#root > *")) {
totalSize += computeCardSize(element);
}

return totalSize;
}

public setConfig(config: Config) {
if (!config || !config.cards || !Array.isArray(config.cards)) {
throw new Error("Card config incorrect");
}
this.config = config;
}

protected render() {
if (!this.config) {
return html``;
}

return html`
${this.renderStyle()}
<div id="root">
${this.config.cards.map((card) => this.createCardElement(card))}
</div>
`;
}

private renderStyle() {
return html`
<style>
#root {
display: flex;
flex-direction: column;
}
#root > * {
margin: 4px 0 4px 0;
}
#root > *:first-child {
margin-top: 0;
}
#root > *:last-child {
margin-bottom: 0;
}
</style>
`;
}

private createCardElement(card: LovelaceConfig): LovelaceCard {
const element = createCardElement(card) as LovelaceCard;
element.hass = this._hass;
return element;
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-vertical-stack-card": HuiVerticalStackCard;
}
}

customElements.define("hui-vertical-stack-card", HuiVerticalStackCard);
2 changes: 1 addition & 1 deletion src/panels/lovelace/common/create-card-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import "../cards/hui-picture-entity-card";
import "../cards/hui-picture-glance-card";
import "../cards/hui-plant-status-card.js";
import "../cards/hui-sensor-card.js";
import "../cards/hui-vertical-stack-card.js";
import "../cards/hui-vertical-stack-card.ts";
import "../cards/hui-weather-forecast-card";
import "../cards/hui-gauge-card.js";

Expand Down
8 changes: 7 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"interface-name": false,
"no-submodule-imports": false,
"ordered-imports": false,
"object-literal-sort-keys": false
"object-literal-sort-keys": false,
"variable-name": [
true,
"ban-keywords",
"check-format",
"allow-leading-underscore"
]
}
}

0 comments on commit cf2171e

Please sign in to comment.