|
1 | 1 | import {$, $$} from "select-dom";
|
2 | 2 |
|
3 |
| -type NavExpandState = { [key: string]: boolean }; |
| 3 | +type NavExpandState = { |
| 4 | + current:string, |
| 5 | + selected: Record<string, boolean> |
| 6 | +}; |
4 | 7 | const PAGE_NAV_EXPAND_STATE_KEY = 'pagesNavState';
|
5 |
| -const navState = JSON.parse(sessionStorage.getItem(PAGE_NAV_EXPAND_STATE_KEY) ?? "{}") as NavExpandState |
6 | 8 |
|
7 | 9 | // Initialize the nav state from the session storage
|
8 | 10 | // Return a function to keep the nav state in the session storage that should be called before the page is unloaded
|
9 | 11 | function keepNavState(nav: HTMLElement): () => void {
|
10 |
| - const inputs = $$('input[type="checkbox"]', nav); |
11 |
| - if (navState) { |
12 |
| - inputs.forEach(input => { |
13 |
| - const key = input.id; |
14 |
| - if ('shouldExpand' in input.dataset && input.dataset['shouldExpand'] === 'true') { |
| 12 | + |
| 13 | + const currentNavigation = nav.dataset.currentNavigation; |
| 14 | + const currentPageId = nav.dataset.currentPageId; |
| 15 | + |
| 16 | + let navState = JSON.parse(sessionStorage.getItem(PAGE_NAV_EXPAND_STATE_KEY) ?? "{}") as NavExpandState |
| 17 | + if (navState.current !== currentNavigation) |
| 18 | + { |
| 19 | + sessionStorage.removeItem(PAGE_NAV_EXPAND_STATE_KEY); |
| 20 | + navState = { current: currentNavigation } as NavExpandState; |
| 21 | + } |
| 22 | + if (currentPageId) |
| 23 | + { |
| 24 | + const currentPageLink = $('a[id="page-' + currentPageId + '"]', nav); |
| 25 | + currentPageLink.classList.add('current'); |
| 26 | + currentPageLink.classList.add('pointer-events-none'); |
| 27 | + currentPageLink.classList.add('text-blue-elastic!'); |
| 28 | + currentPageLink.classList.add('font-semibold'); |
| 29 | + |
| 30 | + const parentIds = nav.dataset.currentPageParentIds?.split(',') ?? []; |
| 31 | + for (const parentId of parentIds) |
| 32 | + { |
| 33 | + const input = $('input[type="checkbox"][id=\"'+parentId+'\"]', nav) as HTMLInputElement; |
| 34 | + if (input) { |
15 | 35 | input.checked = true;
|
16 |
| - } else { |
17 |
| - if (key in navState) { |
18 |
| - input.checked = navState[key]; |
19 |
| - } |
| 36 | + const link = input.nextElementSibling as HTMLAnchorElement; |
| 37 | + link.classList.add('font-semibold'); |
20 | 38 | }
|
21 |
| - }); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // expand items previously selected |
| 43 | + for (const groupId in navState.selected) |
| 44 | + { |
| 45 | + const input = $('input[type="checkbox"][id=\"'+groupId+'\"]', nav) as HTMLInputElement; |
| 46 | + input.checked = navState.selected[groupId]; |
22 | 47 | }
|
23 |
| - |
| 48 | + |
24 | 49 | return () => {
|
25 |
| - const inputs = $$('input[type="checkbox"]', nav); |
26 |
| - const state: NavExpandState = inputs.reduce((state: NavExpandState, input) => { |
| 50 | + // store all expanded groups |
| 51 | + const inputs = $$('input[type="checkbox"]:checked', nav); |
| 52 | + const selectedMap: Record<string, boolean> = inputs |
| 53 | + .filter(input => input.checked) |
| 54 | + .reduce((state: Record<string, boolean>, input) => { |
27 | 55 | const key = input.id;
|
28 | 56 | const value = input.checked;
|
29 | 57 | return { ...state, [key]: value};
|
30 | 58 | }, {});
|
| 59 | + const state = { current: currentNavigation, selected: selectedMap }; |
31 | 60 | sessionStorage.setItem(PAGE_NAV_EXPAND_STATE_KEY, JSON.stringify(state));
|
32 | 61 | }
|
33 | 62 | }
|
|
0 commit comments