|
37 | 37 | }}/> |
38 | 38 |
|
39 | 39 | <script lang="ts"> |
40 | | - import type { HomeAssistant } from './types'; |
| 40 | + import type { ExpanderCardDomEventDetail, HomeAssistant } from './types'; |
41 | 41 | import Card from './Card.svelte'; |
42 | 42 | import { onMount } from 'svelte'; |
43 | 43 | import type { ExpanderConfig } from './configtype'; |
|
59 | 59 | const showButtonUsers = (config['show-button-users'] === undefined || config['show-button-users']?.includes(hass?.user.name)); |
60 | 60 |
|
61 | 61 |
|
62 | | - function toggleOpen() { |
| 62 | + function toggleOpen(openState?: boolean) { |
63 | 63 | if (animationTimeout) { |
64 | 64 | clearTimeout(animationTimeout); |
65 | 65 | animationTimeout = null; |
66 | 66 | } |
67 | | - const openState = !open; |
| 67 | + const newOpenState = openState !== undefined ? openState : !open; |
68 | 68 | if (config.animation) { |
69 | | - animationState = openState ? 'opening' : 'closing'; |
70 | | - if (openState) { |
| 69 | + animationState = newOpenState ? 'opening' : 'closing'; |
| 70 | + if (newOpenState) { |
71 | 71 | setOpenState(true); |
72 | 72 | animationTimeout = setTimeout(() => { |
73 | 73 | animationState = 'idle'; |
|
81 | 81 | }, 350); |
82 | 82 | } |
83 | 83 | } else { |
84 | | - setOpenState(openState); |
| 84 | + setOpenState(newOpenState); |
85 | 85 | // animation state is always 'idle' if no animation |
86 | 86 | } |
87 | 87 | } |
|
98 | 98 | } |
99 | 99 | } |
100 | 100 |
|
| 101 | + function handleDomEvent(event: Event) { |
| 102 | + const data: ExpanderCardDomEventDetail = (event as CustomEvent).detail?.['expander-card']?.data; |
| 103 | + if (data?.['expander-card-id'] === config['expander-card-id']) { |
| 104 | + if (data.action === 'open' && !open) { |
| 105 | + toggleOpen(true); |
| 106 | + } else if (data.action === 'close' && open) { |
| 107 | + toggleOpen(false); |
| 108 | + } else if (data.action === 'toggle') { |
| 109 | + toggleOpen(); |
| 110 | + } |
| 111 | + } |
| 112 | + }; |
| 113 | +
|
| 114 | + function cleanup() { |
| 115 | + document.body.removeEventListener('ll-custom', handleDomEvent); |
| 116 | + }; |
| 117 | +
|
101 | 118 | onMount(() => { |
102 | 119 | const minWidthExpanded = config['min-width-expanded']; |
103 | 120 | const maxWidthExpanded = config['max-width-expanded']; |
|
135 | 152 | setOpenState(config.expanded); |
136 | 153 | } |
137 | 154 | } |
| 155 | +
|
| 156 | + document.body.addEventListener('ll-custom', handleDomEvent); |
| 157 | +
|
| 158 | + return cleanup; |
138 | 159 | }); |
139 | 160 |
|
140 | 161 | const buttonClick = (event: MouseEvent) => { |
|
0 commit comments