|
| 1 | +// TODO: refactor to typescript. this was copied from the tabs implementation |
| 2 | + |
| 3 | +// @ts-check |
| 4 | + |
| 5 | +// Extra JS capability for selected applies switches to be synced |
| 6 | +// The selection is stored in local storage so that it persists across page loads. |
| 7 | + |
| 8 | +const as_id_to_elements = {} |
| 9 | +const storageKeyPrefix = 'sphinx-design-applies-switch-id-' |
| 10 | + |
| 11 | +function create_key(el: HTMLElement) { |
| 12 | + const syncId = el.getAttribute('data-sync-id') |
| 13 | + const syncGroup = el.getAttribute('data-sync-group') |
| 14 | + if (!syncId || !syncGroup) return null |
| 15 | + return [syncGroup, syncId, syncGroup + '--' + syncId] |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Initialize the applies switch selection. |
| 20 | + * |
| 21 | + */ |
| 22 | +function ready() { |
| 23 | + // Find all applies switches with sync data |
| 24 | + |
| 25 | + const groups = [] |
| 26 | + |
| 27 | + document.querySelectorAll('.applies-switch-label').forEach((label) => { |
| 28 | + if (label instanceof HTMLElement) { |
| 29 | + const data = create_key(label) |
| 30 | + if (data) { |
| 31 | + const [group, id, key] = data |
| 32 | + |
| 33 | + // add click event listener |
| 34 | + label.onclick = onAppliesSwitchLabelClick |
| 35 | + |
| 36 | + // store map of key to elements |
| 37 | + if (!as_id_to_elements[key]) { |
| 38 | + as_id_to_elements[key] = [] |
| 39 | + } |
| 40 | + as_id_to_elements[key].push(label) |
| 41 | + |
| 42 | + if (groups.indexOf(group) === -1) { |
| 43 | + groups.push(group) |
| 44 | + // Check if a specific switch has been selected via URL parameter |
| 45 | + const switchParam = new URLSearchParams( |
| 46 | + window.location.search |
| 47 | + ).get(group) |
| 48 | + if (switchParam) { |
| 49 | + window.sessionStorage.setItem( |
| 50 | + storageKeyPrefix + group, |
| 51 | + switchParam |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Check is a specific switch has been selected previously |
| 57 | + const previousId = window.sessionStorage.getItem( |
| 58 | + storageKeyPrefix + group |
| 59 | + ) |
| 60 | + if (previousId === id) { |
| 61 | + ;( |
| 62 | + label.previousElementSibling as HTMLInputElement |
| 63 | + ).checked = true |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Activate other switches with the same sync id. |
| 72 | + * |
| 73 | + * @this {HTMLElement} - The element that was clicked. |
| 74 | + */ |
| 75 | +function onAppliesSwitchLabelClick() { |
| 76 | + const data = create_key(this) |
| 77 | + if (!data) return |
| 78 | + const [group, id, key] = data |
| 79 | + for (const label of as_id_to_elements[key]) { |
| 80 | + if (label === this) continue |
| 81 | + label.previousElementSibling.checked = true |
| 82 | + } |
| 83 | + window.sessionStorage.setItem(storageKeyPrefix + group, id) |
| 84 | +} |
| 85 | + |
| 86 | +export function initAppliesSwitch() { |
| 87 | + ready() |
| 88 | +} |
0 commit comments