forked from Start9Labs/start-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabs.js
More file actions
160 lines (142 loc) · 5.1 KB
/
Copy pathtabs.js
File metadata and controls
160 lines (142 loc) · 5.1 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
/**
* Whether `container` directly owns a tab with the given name.
*
* Scoped to `:scope > .mdbook-tabs > .mdbook-tab` so a *nested* tabs container
* (e.g. `{{#tabs}}` inside a tab's content) doesn't count toward its parent.
*
* @param {Element} container
* @param {string} name
* @returns {boolean}
*/
const containerOwnsTab = (container, name) => {
for (const tab of container.querySelectorAll(
':scope > .mdbook-tabs > .mdbook-tab',
)) {
if (tab instanceof HTMLElement && tab.dataset.tabname === name) {
return true;
}
}
return false;
};
/**
* Switch `container` to the tab named `name`.
*
* No-op (returns false) if the container has no such tab. This is important for
* `global` tab groups: a selection made on another page where the group has a
* different set of tabs must NOT blank this block or activate the wrong tab —
* instead we leave the pre-rendered first tab (or current selection) intact.
*
* @param {Element} container
* @param {string} name
* @returns {boolean} whether the switch was applied
*/
const changeTab = (container, name) => {
if (!containerOwnsTab(container, name)) {
return false;
}
for (const child of container.children) {
if (!(child instanceof HTMLElement)) {
continue;
}
if (child.classList.contains('mdbook-tabs')) {
for (const tab of child.children) {
if (tab instanceof HTMLElement) {
tab.classList.toggle('active', tab.dataset.tabname === name);
}
}
} else if (child.classList.contains('mdbook-tab-content')) {
child.classList.toggle('hidden', child.dataset.tabname !== name);
}
}
return true;
};
/**
* Update URL query params to reflect current tab selections.
* Only includes params for tab globals present on the page.
*/
const updateTabUrlParams = () => {
const params = new URLSearchParams(window.location.search);
const seen = new Set();
document
.querySelectorAll('.mdbook-tabs-container[data-tabglobal]')
.forEach((container) => {
const global = container.dataset.tabglobal;
if (!global || seen.has(global)) return;
seen.add(global);
const active = container.querySelector(
':scope > .mdbook-tabs > .mdbook-tab.active',
);
if (active && active.dataset.tabname) {
params.set(global, active.dataset.tabname);
}
});
const search = params.toString() ? '?' + params.toString() : '';
history.replaceState(
null,
'',
window.location.pathname + search + window.location.hash,
);
};
document.addEventListener('DOMContentLoaded', () => {
for (const tab of document.querySelectorAll('.mdbook-tab')) {
tab.addEventListener('click', () => {
if (
!(tab instanceof HTMLElement) ||
!tab.parentElement ||
!tab.parentElement.parentElement
) {
return;
}
const container = tab.parentElement.parentElement;
const name = tab.dataset.tabname;
const global = container.dataset.tabglobal;
changeTab(container, name);
if (global) {
localStorage.setItem(`mdbook-tabs-${global}`, name);
for (const globalContainer of document.querySelectorAll(
`.mdbook-tabs-container[data-tabglobal="${global}"]`,
)) {
changeTab(globalContainer, name);
}
}
updateTabUrlParams();
});
}
const containers = document.querySelectorAll(
'.mdbook-tabs-container[data-tabglobal]',
);
// Phase 1: restore from localStorage. changeTab() ignores containers that
// don't own the stored tab, so a selection made on another page (where the
// group has different tabs) won't blank or mis-select this one.
for (const container of containers) {
const name = localStorage.getItem(
`mdbook-tabs-${container.dataset.tabglobal}`,
);
if (name) {
changeTab(container, name);
}
}
// Phase 2: URL params override localStorage (used by deep links such as
// `/start-os/trust-ca.html?platform=Mac`). Only persist a param that
// actually applied to at least one container on this page.
for (const [key, value] of new URLSearchParams(window.location.search)) {
if (!/^[\w-]+$/.test(key)) {
continue;
}
let applied = false;
for (const container of document.querySelectorAll(
`.mdbook-tabs-container[data-tabglobal="${key}"]`,
)) {
if (changeTab(container, value)) {
applied = true;
}
}
if (applied) {
localStorage.setItem(`mdbook-tabs-${key}`, value);
}
}
// Phase 3: reflect the resolved state back into the URL.
if (document.querySelector('.mdbook-tabs-container[data-tabglobal]')) {
updateTabUrlParams();
}
});