Skip to content

Commit cfea66d

Browse files
committed
feat: add layout
1 parent 94b3cef commit cfea66d

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

public/demo/layout.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,24 @@ children:
402402
- type: el
403403
text: Landscape
404404
showForOrientation: landscape
405+
- showForNav: /layout/layout
406+
children:
407+
- type: md
408+
content: |
409+
To use Home Assistant cards with LCARS, you need to use the `ha-lcars-layout`
410+
layout. You can then add any home assistant card using the `ha-card` type.
411+
412+
```
413+
views:
414+
- path: api
415+
title: LCARS
416+
type: custom:ha-lcars-layout
417+
cards:
418+
- show_name: true
419+
show_icon: true
420+
type: button
421+
entity: input_boolean.test_boolean_1
422+
children:
423+
- type: ha-card
424+
cardIndex: 0
425+
```

src/ComponentRegistry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import AbsoluteContainer from './components/AbsoluteContainer.vue'
22
import AttributeFlow from './components/AttributeFlow.vue'
33
import AttributeList from './components/AttributeList.vue'
44
import AttributeTable from './components/AttributeTable.vue'
5+
import HACard from './components/HACard.vue'
56
import LCARSApi from './components/LCARSApi.vue'
67
import LCARSCol from './components/LCARSCol.vue'
78
import LCARSElement from './components/LCARSElement.vue'
@@ -39,6 +40,7 @@ export function registerAllComponents() {
3940
registerComponent('attribute-list', AttributeList)
4041
registerComponent('col', LCARSCol)
4142
registerComponent('el', LCARSElement)
43+
registerComponent('ha-card', HACard)
4244
registerComponent('modal', LCARSModal)
4345
registerComponent('md', LCARSMarkdown)
4446
registerComponent('menu-horizontal', MenuHorizontal)

src/HAConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const haConfig = ref<HAConfig>({
2323
children: [],
2424
} as HAConfig)
2525

26+
export const haCards = ref<Array<any>>([])
27+
2628
export const mixins = ref({} as any)
2729

2830
export function loadConfig(config: any) {

src/LCARSCustomLayout.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { haState } from './HAState'
2+
import { haCards, haConfig, loadConfig } from './HAConfig'
3+
import './editor.ts'
4+
5+
export class LCARSCustomLayout extends HTMLElement {
6+
private vueElement: any
7+
private test: boolean = false
8+
9+
static get observedAttributes() {
10+
return ['config', 'test']
11+
}
12+
13+
static get properties() {
14+
return {
15+
cards: { type: Array, attribute: false },
16+
}
17+
}
18+
19+
constructor() {
20+
super()
21+
this.attachShadow({ mode: 'open' })
22+
this.vueElement = null
23+
}
24+
25+
set loadTest(test: any) {
26+
this.setLoadTest(test)
27+
}
28+
29+
setLoadTest(test: any) {
30+
this.test = test
31+
if (this.vueElement) {
32+
this.vueElement.loadTest = test
33+
}
34+
}
35+
36+
set config(config: any) {
37+
this.setConfig(config)
38+
}
39+
40+
set panel(panel: any) {
41+
if (panel.config) {
42+
this.setConfig(panel.config)
43+
}
44+
}
45+
46+
set hass(hass: any) {
47+
haState.value = hass
48+
}
49+
50+
set cards(cards: Array<any>) {
51+
haCards.value = cards
52+
console.log(cards)
53+
}
54+
55+
setConfig(config: any) {
56+
loadConfig(config)
57+
58+
if (this.vueElement) {
59+
this.vueElement.config = config
60+
}
61+
}
62+
63+
attributeChangedCallback(name: string, oldValue: any, newValue: any) {
64+
if (name === 'test') {
65+
this.loadTest = true
66+
}
67+
if (name === 'config') {
68+
this.setConfig(newValue)
69+
}
70+
}
71+
72+
connectedCallback() {
73+
if (!this.vueElement) {
74+
this.vueElement = document.createElement('lcars-card')
75+
this.vueElement.config = haConfig.value ?? { children: [] }
76+
this.vueElement.loadTest = this.test
77+
this.shadowRoot?.appendChild(this.vueElement)
78+
79+
const head = document.head
80+
const link = document.createElement('link')
81+
82+
link.type = 'text/css'
83+
link.rel = 'stylesheet'
84+
link.href = 'https://fonts.googleapis.com/css2?family=Antonio:wght@400;700&display=swap'
85+
86+
head.appendChild(link)
87+
88+
// watch(haConfig, (newConfig) => {
89+
// this.setConfig(newConfig)
90+
// const event = new Event('config-changed', {
91+
// bubbles: true,
92+
// composed: true,
93+
// }) as any
94+
// event.detail = { config: newConfig }
95+
// this.dispatchEvent(event)
96+
// })
97+
}
98+
}
99+
100+
disconnectedCallback() {
101+
if (this.vueElement) {
102+
this.shadowRoot?.removeChild(this.vueElement)
103+
this.vueElement = null
104+
}
105+
}
106+
}

src/assets/config/demo.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ nav:
2222
- text: Alignment
2323
- text: Spacing
2424
- text: Orientation
25+
- text: Layout
2526
- text: Elements
2627
children:
2728
- text: Element

src/components/HACard.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script setup lang="ts">
2+
import { haCards } from '@/HAConfig'
3+
import { onMounted, ref } from 'vue'
4+
5+
const { cardIndex } = defineProps<{ cardIndex: number }>()
6+
7+
const cardRef = ref()
8+
9+
onMounted(() => {
10+
if (cardIndex < haCards.value?.length) {
11+
cardRef.value.appendChild(haCards.value[cardIndex])
12+
}
13+
})
14+
</script>
15+
16+
<template>
17+
<div ref="cardRef"></div>
18+
</template>

src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import LCARSCardCe from './LCARSCard.ce.vue'
33
import { LCARSCustomCard } from './LCARSCustomCard.ts'
44
import { registerAllComponents } from './ComponentRegistry.ts'
55
import './editor.ts'
6+
import { LCARSCustomLayout } from './LCARSCustomLayout.ts'
67

78
customElements.define('lcars-card', defineCustomElement(LCARSCardCe))
89
;(window as any).customCards = (window as any).customCards || []
@@ -20,4 +21,8 @@ if (!customElements.get('ha-lcars-panel')) {
2021
customElements.define('ha-lcars-panel', LCARSCustomCard)
2122
}
2223

24+
if (!customElements.get('ha-lcars-layout')) {
25+
customElements.define('ha-lcars-layout', LCARSCustomLayout)
26+
}
27+
2328
registerAllComponents()

0 commit comments

Comments
 (0)