1
+ import {
2
+ LitElement ,
3
+ html ,
4
+ customElement ,
5
+ property ,
6
+ TemplateResult ,
7
+ } from 'lit-element' ;
1
8
import {
2
9
HomeAssistant ,
3
10
getLovelace ,
@@ -8,26 +15,18 @@ import { DeclutteringCardConfig, TemplateConfig } from './types';
8
15
import deepReplace from './deep-replace' ;
9
16
import getLovelaceCast from './getLovelaceCast' ;
10
17
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 ;
20
19
21
- class DeclutteringCard extends HTMLElement {
22
- private _card ?: any ;
20
+ @customElement ( 'decluttering-card' )
21
+ class DeclutteringCard extends LitElement {
22
+ @property ( ) private _card ?: any ;
23
23
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 ;
29
27
30
28
set hass ( hass : HomeAssistant ) {
29
+ this . _hass = hass ;
31
30
if ( this . _card ) {
32
31
this . _card . hass = hass ;
33
32
}
@@ -45,37 +44,50 @@ class DeclutteringCard extends HTMLElement {
45
44
if ( ! templateConfig || ! templateConfig . card ) {
46
45
throw new Error ( `The template "${ config . template } " doesn't exist in decluttering_templates` ) ;
47
46
}
47
+ this . _config = deepReplace ( config . variables , templateConfig ) ;
48
+ this . _createCard ( this . _config ) . then ( ( card ) => {
49
+ this . _card = card ;
50
+ } ) ;
51
+ }
48
52
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 `` ;
56
56
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 ) ;
62
64
// fireEvent(element, 'll-rebuild');
63
- return element ;
64
65
} 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 ;
73
70
}
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 ;
74
82
}
75
83
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 ) ;
78
87
}
79
- }
80
88
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