-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdevice-presets.ts
More file actions
292 lines (250 loc) · 7.67 KB
/
device-presets.ts
File metadata and controls
292 lines (250 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Device Presets Manager Module
*
* Manages device presets and Home Assistant integration features.
* Bridges TRMNL add-on configuration with Home Assistant runtime state.
*
* Design Pattern:
* Integration Adapter Pattern - adapts external HA state (window.hass) to internal forms.
*
* NOTE: window.hass only available when running inside Home Assistant.
* NOTE: When adding HA integrations, always provide fallback behavior.
*
* @module html/js/device-presets
*/
import { LoadPresets } from './api-client.js'
import type { PresetsConfig } from '../../types/domain.js'
/** Home Assistant config structure */
interface HassConfig {
language?: string
}
/** Home Assistant themes structure */
interface HassThemes {
themes?: Record<string, unknown>
}
/** Dashboard entry from backend */
interface DashboardEntry {
path: string
title: string
}
/** Home Assistant global object */
interface Hass {
themes?: HassThemes
config?: HassConfig
dashboards?: DashboardEntry[] | string[]
}
// Extend Window to include Home Assistant global
declare global {
interface Window {
hass?: Hass
}
}
/**
* Manager coordinating device presets and Home Assistant integration.
*/
export class DevicePresetsManager {
#loadPresetsCmd: LoadPresets
#presets: PresetsConfig = {}
constructor() {
this.#loadPresetsCmd = new LoadPresets()
}
/**
* Loads device presets from API and renders dropdown options.
*/
async loadAndRenderPresets(): Promise<void> {
try {
this.#presets = await this.#loadPresetsCmd.call()
this.#renderPresetOptions()
} catch (err) {
console.warn('Failed to load presets:', err)
}
}
#renderPresetOptions(): void {
const select = document.getElementById(
'devicePreset',
) as HTMLSelectElement | null
if (!select) return
while (select.options.length > 1) {
select.remove(1)
}
Object.entries(this.#presets).forEach(([presetId, preset]) => {
const option = document.createElement('option')
option.value = presetId
option.textContent = preset.name || presetId
option.dataset.device = JSON.stringify(preset)
select.appendChild(option)
})
}
/**
* Public alias for #renderPresetOptions().
*/
renderPresets(): void {
this.#renderPresetOptions()
}
/**
* Restores all dropdown state after DOM re-render.
*/
afterDOMRender(schedule?: { theme?: string | null }): void {
this.renderPresets()
this.populateThemePicker(schedule?.theme ?? null)
this.populateDashboardPicker()
this.prefillLanguage()
}
/**
* Applies selected device preset to form inputs.
*/
applyDevicePreset(): boolean {
const select = document.getElementById(
'devicePreset',
) as HTMLSelectElement | null
if (!select) return false
const option = select.options[select.selectedIndex]
if (!option.value) {
document.getElementById('deviceInfo')?.classList.add('hidden')
return false
}
const device = JSON.parse(option.dataset.device || '{}')
const widthInput = document.getElementById(
's_width',
) as HTMLInputElement | null
const heightInput = document.getElementById(
's_height',
) as HTMLInputElement | null
if (widthInput && device.viewport?.width) {
widthInput.value = device.viewport.width
widthInput.dispatchEvent(new Event('change'))
}
if (heightInput && device.viewport?.height) {
heightInput.value = device.viewport.height
heightInput.dispatchEvent(new Event('change'))
}
// Update crop dimensions to match device viewport
const cropWidth = document.getElementById('s_crop_width') as HTMLInputElement | null
const cropHeight = document.getElementById('s_crop_height') as HTMLInputElement | null
if (cropWidth && device.viewport?.width) {
cropWidth.value = device.viewport.width
}
if (cropHeight && device.viewport?.height) {
cropHeight.value = device.viewport.height
}
if (device.rotate) {
const rotateSelect = document.getElementById(
's_rotate',
) as HTMLSelectElement | null
if (rotateSelect) {
rotateSelect.value = device.rotate
rotateSelect.dispatchEvent(new Event('change'))
}
}
if (device.format) {
const formatSelect = document.getElementById(
's_format',
) as HTMLSelectElement | null
if (formatSelect) {
formatSelect.value = device.format
formatSelect.dispatchEvent(new Event('change'))
}
}
const infoDiv = document.getElementById('deviceInfo')
const infoPara = infoDiv?.querySelector('p')
if (infoPara) {
infoPara.textContent = `Using ${device.name}: ${device.viewport.width}x${
device.viewport.height
}${device.rotate ? `, ${device.rotate}° rotation` : ''}`
}
infoDiv?.classList.remove('hidden')
return true
}
/**
* Populates theme dropdown from Home Assistant themes.
*/
populateThemePicker(selectedTheme: string | null = null): void {
const themeSelect = document.getElementById(
's_theme',
) as HTMLSelectElement | null
if (!themeSelect) return
themeSelect.innerHTML = '<option value="">Default</option>'
if (!window.hass?.themes?.themes) {
console.warn('No themes found in window.hass')
return
}
Object.keys(window.hass.themes.themes)
.sort()
.forEach((theme) => {
const option = document.createElement('option')
option.value = theme
option.textContent = theme
if (theme === selectedTheme) {
option.selected = true
}
themeSelect.appendChild(option)
})
}
/**
* Auto-fills language field from Home Assistant configuration.
*/
prefillLanguage(): void {
const langInput = document.getElementById(
's_lang',
) as HTMLInputElement | null
if (!langInput) return
if (window.hass?.config?.language && !langInput.value) {
langInput.value = window.hass.config.language
langInput.placeholder = window.hass.config.language
}
}
/**
* Populates dashboard dropdown from Home Assistant dashboards.
* Supports both { path, title } objects (new) and plain strings (legacy).
*/
populateDashboardPicker(): void {
const select = document.getElementById(
'dashboardSelector',
) as HTMLSelectElement | null
if (!select) return
while (select.options.length > 1) {
select.remove(1)
}
if (!window.hass?.dashboards || !Array.isArray(window.hass.dashboards)) {
console.warn('No dashboards found in window.hass - using defaults')
const defaults = ['/lovelace/0', '/home']
defaults.forEach((path) => {
const option = document.createElement('option')
option.value = path
option.textContent = path
select.appendChild(option)
})
return
}
window.hass.dashboards.forEach((entry) => {
const option = document.createElement('option')
if (typeof entry === 'string') {
option.value = entry
option.textContent = entry
} else {
option.value = entry.path
option.textContent = `${entry.title} (${entry.path})`
}
select.appendChild(option)
})
}
/**
* Copies selected dashboard path to dashboard input field.
*/
applyDashboardSelection(): boolean {
const select = document.getElementById(
'dashboardSelector',
) as HTMLSelectElement | null
const pathInput = document.getElementById(
's_path',
) as HTMLInputElement | null
if (!select || !pathInput) return false
if (select.value) {
pathInput.value = select.value
pathInput.dispatchEvent(new Event('change'))
select.value = ''
return true
}
return false
}
}