Skip to content

Commit d1ad677

Browse files
committed
Test with lit
1 parent 06b2ea9 commit d1ad677

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
},
5353
"dependencies": {
5454
"custom-card-helpers": "^1.5.0",
55-
"home-assistant-js-websocket": "^5.1.0"
55+
"home-assistant-js-websocket": "^5.1.0",
56+
"lit-element": "^2.3.0"
5657
}
5758
}

src/decluttering-card.ts

+53-41
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
import {
2+
LitElement,
3+
html,
4+
customElement,
5+
property,
6+
TemplateResult,
7+
} from 'lit-element';
18
import {
29
HomeAssistant,
310
getLovelace,
@@ -8,26 +15,18 @@ import { DeclutteringCardConfig, TemplateConfig } from './types';
815
import deepReplace from './deep-replace';
916
import getLovelaceCast from './getLovelaceCast';
1017

11-
let helpers = (window as any).cardHelpers;
12-
const helperPromise = new Promise(async (resolve) => {
13-
if (helpers) resolve();
14-
if ((window as any).loadCardHelpers) {
15-
helpers = await (window as any).loadCardHelpers();
16-
(window as any).cardHelpers = helpers;
17-
resolve();
18-
}
19-
});
18+
const HELPERS = (window as any).loadCardHelpers ? (window as any).loadCardHelpers() : undefined;
2019

21-
class DeclutteringCard extends HTMLElement {
22-
private _card?: any;
20+
@customElement('decluttering-card')
21+
class DeclutteringCard extends LitElement {
22+
@property() private _card?: any;
2323

24-
constructor() {
25-
super();
26-
// Make use of shadowRoot to avoid conflicts when reusing
27-
this.attachShadow({ mode: 'open' });
28-
}
24+
@property() private _hass?: HomeAssistant;
25+
26+
@property() private _config?: DeclutteringCardConfig;
2927

3028
set hass(hass: HomeAssistant) {
29+
this._hass = hass;
3130
if (this._card) {
3231
this._card.hass = hass;
3332
}
@@ -45,37 +44,50 @@ class DeclutteringCard extends HTMLElement {
4544
if (!templateConfig || !templateConfig.card) {
4645
throw new Error(`The template "${config.template}" doesn't exist in decluttering_templates`);
4746
}
47+
this._config = deepReplace(config.variables, templateConfig);
48+
this._createCard(this._config).then((card) => {
49+
this._card = card;
50+
});
51+
}
4852

49-
const root = this.shadowRoot;
50-
while (root && root.hasChildNodes()) {
51-
root.removeChild(root.lastChild!);
52-
}
53-
const main = document.createElement('div');
54-
main.id = 'root';
55-
root!.appendChild(main);
53+
protected render(): TemplateResult | void {
54+
if (!this._hass || !this._card || !this._config)
55+
return html``;
5656

57-
if (helpers) {
58-
const element = helpers.createCardElement(deepReplace(config.variables, templateConfig));
59-
element.hass = this.hass;
60-
main!.appendChild(element);
61-
this._card = element;
57+
return html`<div>${this._card}</div>`;
58+
}
59+
60+
private async _createCard(config: any): Promise<any> {
61+
let element: any;
62+
if (HELPERS) {
63+
element = (await HELPERS).createCardElement(config);
6264
// fireEvent(element, 'll-rebuild');
63-
return element;
6465
} else {
65-
const element = createThing(deepReplace(config.variables, templateConfig));
66-
element.hass = this.hass;
67-
main!.appendChild(element);
68-
this._card = element;
69-
helperPromise.then(() => {
70-
fireEvent(element, 'll-rebuild', {});
71-
});
72-
return element;
66+
element = createThing(config);
67+
}
68+
if (this._hass) {
69+
element.hass = this._hass;
7370
}
71+
element.addEventListener(
72+
'll-rebuild',
73+
(ev) => {
74+
ev.stopPropagation();
75+
this._rebuildCard(element, config);
76+
},
77+
{
78+
once: true,
79+
},
80+
);
81+
return element;
7482
}
7583

76-
getCardSize() {
77-
return typeof this._card.getCardSize === 'function' ? this._card.getCardSize() : 1;
84+
private async _rebuildCard(element: any, config: any) {
85+
const newCard = await this._createCard(config);
86+
element.replaceWith(newCard);
7887
}
79-
}
8088

81-
customElements.define('decluttering-card', DeclutteringCard);
89+
public getCardSize(): number {
90+
return typeof this._card.getCardSize === 'function'
91+
? this._card.getCardSize() : 1;
92+
}
93+
}

0 commit comments

Comments
 (0)