-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5203: Persist Show/Hide Images state in rack view
- Loading branch information
1 parent
d9a6f11
commit 9c247d9
Showing
7 changed files
with
114 additions
and
47 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { rackImagesState } from './stores'; | ||
import { getElements } from './util'; | ||
|
||
import type { StateManager } from './state'; | ||
|
||
type RackToggleState = { hidden: boolean }; | ||
|
||
/** | ||
* Toggle the Rack Image button to reflect the current state. If the current state is hidden and | ||
* the images are therefore hidden, the button should say "Show Images". Likewise, if the current | ||
* state is *not* hidden, and therefore the images are shown, the button should say "Hide Images". | ||
* | ||
* @param hidden Current State - `true` if images are hidden, `false` otherwise. | ||
* @param button Button element. | ||
*/ | ||
function toggleRackImagesButton(hidden: boolean, button: HTMLButtonElement): void { | ||
const text = hidden ? 'Show Images' : 'Hide Images'; | ||
const selected = hidden ? '' : 'selected'; | ||
button.setAttribute('selected', selected); | ||
button.innerHTML = `<i class="mdi mdi-file-image-outline"></i> ${text}`; | ||
} | ||
|
||
/** | ||
* Show all rack images. | ||
*/ | ||
function showRackImages(): void { | ||
for (const elevation of getElements<HTMLObjectElement>('.rack_elevation')) { | ||
const images = elevation.contentDocument?.querySelectorAll('image.device-image') ?? []; | ||
for (const image of images) { | ||
image.classList.remove('hidden'); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Hide all rack images. | ||
*/ | ||
function hideRackImages(): void { | ||
for (const elevation of getElements<HTMLObjectElement>('.rack_elevation')) { | ||
const images = elevation.contentDocument?.querySelectorAll('image.device-image') ?? []; | ||
for (const image of images) { | ||
image.classList.add('hidden'); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Toggle the visibility of device images and update the toggle button style. | ||
*/ | ||
function handleRackImageToggle( | ||
target: HTMLButtonElement, | ||
state: StateManager<RackToggleState>, | ||
): void { | ||
const initiallyHidden = state.get('hidden'); | ||
state.set('hidden', !initiallyHidden); | ||
const hidden = state.get('hidden'); | ||
|
||
if (hidden) { | ||
hideRackImages(); | ||
} else { | ||
showRackImages(); | ||
} | ||
toggleRackImagesButton(hidden, target); | ||
} | ||
|
||
/** | ||
* Add onClick callback for toggling rack elevation images. Synchronize the image toggle button | ||
* text and display state of images with the local state. | ||
*/ | ||
export function initRackElevation() { | ||
const initiallyHidden = rackImagesState.get('hidden'); | ||
for (const button of getElements<HTMLButtonElement>('button.toggle-images')) { | ||
toggleRackImagesButton(initiallyHidden, button); | ||
|
||
button.addEventListener( | ||
'click', | ||
event => { | ||
handleRackImageToggle(event.currentTarget as HTMLButtonElement, rackImagesState); | ||
}, | ||
false, | ||
); | ||
} | ||
for (const element of getElements<HTMLObjectElement>('.rack_elevation')) { | ||
element.addEventListener('load', () => { | ||
if (initiallyHidden) { | ||
hideRackImages(); | ||
} else if (!initiallyHidden) { | ||
showRackImages(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './rackImages'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { createState } from '../state'; | ||
|
||
export const rackImagesState = createState<{ hidden: boolean }>( | ||
{ hidden: false }, | ||
{ persist: true }, | ||
); |