|
1 | | -import { rackImagesState } from './stores'; |
| 1 | +import { rackImagesState, RackViewSelection } from './stores'; |
2 | 2 | import { getElements } from './util'; |
3 | 3 |
|
4 | 4 | import type { StateManager } from './state'; |
5 | 5 |
|
6 | | -type RackToggleState = { hidden: boolean }; |
| 6 | +export type RackViewState = { view: RackViewSelection }; |
7 | 7 |
|
8 | 8 | /** |
9 | | - * Toggle the Rack Image button to reflect the current state. If the current state is hidden and |
10 | | - * the images are therefore hidden, the button should say "Show Images". Likewise, if the current |
11 | | - * state is *not* hidden, and therefore the images are shown, the button should say "Hide Images". |
12 | | - * |
13 | | - * @param hidden Current State - `true` if images are hidden, `false` otherwise. |
14 | | - * @param button Button element. |
| 9 | + * Show or hide images and labels to build the desired rack view. |
15 | 10 | */ |
16 | | -function toggleRackImagesButton(hidden: boolean, button: HTMLButtonElement): void { |
17 | | - const text = hidden ? 'Show Images' : 'Hide Images'; |
18 | | - const selected = hidden ? '' : 'selected'; |
19 | | - button.setAttribute('selected', selected); |
20 | | - button.innerHTML = `<i class="mdi mdi-file-image-outline"></i> ${text}`; |
| 11 | +function setRackView( |
| 12 | + view: RackViewSelection, |
| 13 | + elevation: HTMLObjectElement, |
| 14 | +): void { |
| 15 | + switch(view) { |
| 16 | + case 'images-and-labels': { |
| 17 | + showRackElements('image.device-image', elevation); |
| 18 | + showRackElements('text.device-image-label', elevation); |
| 19 | + break; |
| 20 | + } |
| 21 | + case 'images-only': { |
| 22 | + showRackElements('image.device-image', elevation); |
| 23 | + hideRackElements('text.device-image-label', elevation); |
| 24 | + break; |
| 25 | + } |
| 26 | + case 'labels-only': { |
| 27 | + hideRackElements('image.device-image', elevation); |
| 28 | + hideRackElements('text.device-image-label', elevation); |
| 29 | + break; |
| 30 | + } |
| 31 | + } |
21 | 32 | } |
22 | 33 |
|
23 | | -/** |
24 | | - * Show all rack images. |
25 | | - */ |
26 | | -function showRackImages(): void { |
27 | | - for (const elevation of getElements<HTMLObjectElement>('.rack_elevation')) { |
28 | | - const images = elevation.contentDocument?.querySelectorAll('image.device-image') ?? []; |
29 | | - for (const image of images) { |
30 | | - image.classList.remove('hidden'); |
31 | | - } |
| 34 | +function showRackElements( |
| 35 | + selector: string, |
| 36 | + elevation: HTMLObjectElement, |
| 37 | +): void { |
| 38 | + const elements = elevation.contentDocument?.querySelectorAll(selector) ?? []; |
| 39 | + for (const element of elements) { |
| 40 | + element.classList.remove('hidden'); |
32 | 41 | } |
33 | 42 | } |
34 | 43 |
|
35 | | -/** |
36 | | - * Hide all rack images. |
37 | | - */ |
38 | | -function hideRackImages(): void { |
39 | | - for (const elevation of getElements<HTMLObjectElement>('.rack_elevation')) { |
40 | | - const images = elevation.contentDocument?.querySelectorAll('image.device-image') ?? []; |
41 | | - for (const image of images) { |
42 | | - image.classList.add('hidden'); |
43 | | - } |
| 44 | +function hideRackElements( |
| 45 | + selector: string, |
| 46 | + elevation: HTMLObjectElement, |
| 47 | +): void { |
| 48 | + const elements = elevation.contentDocument?.querySelectorAll(selector) ?? []; |
| 49 | + for (const element of elements) { |
| 50 | + element.classList.add('hidden'); |
44 | 51 | } |
45 | 52 | } |
46 | 53 |
|
47 | 54 | /** |
48 | | - * Toggle the visibility of device images and update the toggle button style. |
| 55 | + * Change the visibility of all racks in response to selection. |
49 | 56 | */ |
50 | | -function handleRackImageToggle( |
51 | | - target: HTMLButtonElement, |
52 | | - state: StateManager<RackToggleState>, |
| 57 | +function handleRackViewSelect( |
| 58 | + newView: RackViewSelection, |
| 59 | + state: StateManager<RackViewState>, |
53 | 60 | ): void { |
54 | | - const initiallyHidden = state.get('hidden'); |
55 | | - state.set('hidden', !initiallyHidden); |
56 | | - const hidden = state.get('hidden'); |
57 | | - |
58 | | - if (hidden) { |
59 | | - hideRackImages(); |
60 | | - } else { |
61 | | - showRackImages(); |
| 61 | + state.set('view', newView); |
| 62 | + for (const elevation of getElements<HTMLObjectElement>('.rack_elevation')) { |
| 63 | + setRackView(newView, elevation); |
62 | 64 | } |
63 | | - toggleRackImagesButton(hidden, target); |
64 | 65 | } |
65 | 66 |
|
66 | 67 | /** |
67 | | - * Add onClick callback for toggling rack elevation images. Synchronize the image toggle button |
68 | | - * text and display state of images with the local state. |
| 68 | + * Add change callback for selecting rack elevation images, and set |
| 69 | + * initial state of select and the images themselves |
69 | 70 | */ |
70 | 71 | export function initRackElevation(): void { |
71 | | - const initiallyHidden = rackImagesState.get('hidden'); |
72 | | - for (const button of getElements<HTMLButtonElement>('button.toggle-images')) { |
73 | | - toggleRackImagesButton(initiallyHidden, button); |
| 72 | + const initialView = rackImagesState.get('view'); |
74 | 73 |
|
75 | | - button.addEventListener( |
76 | | - 'click', |
| 74 | + for (const control of getElements<HTMLSelectElement>('select.rack-view')) { |
| 75 | + control.selectedIndex = [...control.options].findIndex(o => o.value == initialView); |
| 76 | + control.addEventListener( |
| 77 | + 'change', |
77 | 78 | event => { |
78 | | - handleRackImageToggle(event.currentTarget as HTMLButtonElement, rackImagesState); |
| 79 | + handleRackViewSelect((event.currentTarget as any).value as RackViewSelection, rackImagesState); |
79 | 80 | }, |
80 | 81 | false, |
81 | 82 | ); |
82 | 83 | } |
| 84 | + |
83 | 85 | for (const element of getElements<HTMLObjectElement>('.rack_elevation')) { |
84 | 86 | element.addEventListener('load', () => { |
85 | | - if (initiallyHidden) { |
86 | | - hideRackImages(); |
87 | | - } else if (!initiallyHidden) { |
88 | | - showRackImages(); |
89 | | - } |
| 87 | + setRackView(initialView, element); |
90 | 88 | }); |
91 | 89 | } |
92 | 90 | } |
0 commit comments