-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathra-navigation.js
More file actions
330 lines (294 loc) · 10.9 KB
/
ra-navigation.js
File metadata and controls
330 lines (294 loc) · 10.9 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
let allMenus, navLinks, versionsLinks;
const STORYBOOK_PATH_META_SELECTOR = 'meta[name="storybook_path"]';
function hideTips() {
const tipElement = document.getElementById('tip');
const tipContainer = document.getElementById('tip-container');
if (tipElement) {
tipElement.remove();
}
if (tipContainer) {
tipContainer.remove();
}
}
// eslint-disable-next-line no-unused-vars
function slugify(text) {
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w-]+/g, '') // Remove all non-word chars
.replace(/--+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
function navigationFitScroll() {
const scrollIntoView = window.sessionStorage.getItem('scrollIntoView');
if (scrollIntoView !== 'false') {
const activeMenu = document.querySelector('.sidenav li.active');
if (activeMenu) activeMenu.parentNode.scrollIntoView();
}
window.sessionStorage.removeItem('scrollIntoView');
}
function buildPageToC() {
const M = window.M;
M.Sidenav.init(document.querySelectorAll('.sidenav'));
// Initialize version selector
M.Dropdown.init(document.querySelectorAll('.dropdown-trigger'));
const Prism = window.Prism;
Prism.highlightAll();
/* Generate table of contents */
if (document.querySelector('.toc') != null) {
const tocbot = window.tocbot;
tocbot.init({
// Where to render the table of contents
tocSelector: '.toc',
positionFixedSelector: '.toc',
// Where to grab the headings to build the table of contents
contentSelector: '.toc-content',
// More options
headingSelector: 'h2, h3, h4',
includeHtml: true,
collapseDepth: 2,
hasInnerContainers: true,
});
const storybookPathMetaElement = document.querySelector(
STORYBOOK_PATH_META_SELECTOR
);
let storybookPathMetaContent;
if (storybookPathMetaElement) {
storybookPathMetaContent = document.querySelector(
STORYBOOK_PATH_META_SELECTOR
).content;
}
const tocList = document.querySelector('.toc-list');
if (!tocList || !storybookPathMetaContent) {
return;
}
const storybookListItem = document.createElement('li');
storybookListItem.className = 'toc-list-item';
const storybookLink = document.createElement('a');
storybookLink.className = 'toc-link';
storybookLink.href = `https://react-admin-storybook.vercel.app?path=/story/${storybookPathMetaContent}`;
storybookLink.textContent = 'Storybook';
storybookLink.target = '_blank';
storybookLink.rel = 'noopener noreferrer';
const storybookLaunchIcon = document.createElement('img');
storybookLaunchIcon.src = './img/icons/launch.png';
storybookLaunchIcon.alt = 'Open Storybook';
storybookLaunchIcon.className = 'toc-link-icon';
storybookListItem.appendChild(storybookLink);
storybookLink.appendChild(storybookLaunchIcon);
tocList.appendChild(storybookListItem);
}
}
function replaceContent(text) {
const tocContainer = document.querySelector('.toc-container');
let tmpElement;
if (tocContainer) {
tocContainer.className =
text.trim() !== ''
? 'toc-container col hide-on-small-only m3'
: 'toc-container';
tmpElement = document.createElement('div');
tmpElement.innerHTML = text;
}
const content = document.querySelector('.container');
const tmpContent = tmpElement.querySelector('.container');
if (content && tmpContent) {
content.innerHTML = tmpContent.innerHTML;
}
const newStorybookPathMeta = tmpElement.querySelector(
STORYBOOK_PATH_META_SELECTOR
);
const newStorybookPathContent = newStorybookPathMeta?.content ?? '';
const storybookPathMetaElement = document.querySelector(
STORYBOOK_PATH_META_SELECTOR
);
if (storybookPathMetaElement && newStorybookPathContent) {
document
.querySelector(STORYBOOK_PATH_META_SELECTOR)
.setAttribute('content', newStorybookPathContent);
} else if (newStorybookPathContent) {
const metaElement = document.createElement('meta');
metaElement.setAttribute('name', 'storybook_path');
metaElement.setAttribute('content', newStorybookPathContent);
document.head.appendChild(metaElement);
} else {
// Remove the meta element if it doesn't exist in the new content
storybookPathMetaElement?.remove();
}
window.scrollTo(0, 0);
buildPageToC();
navigationFitScroll();
}
function changeSelectedMenu() {
const activeMenu = document.querySelector(`.sidenav li.active`);
activeMenu && activeMenu.classList.remove('active');
const newActiveMenu = allMenus.find(
menuEl => menuEl.href === window.location.href
);
newActiveMenu && newActiveMenu.parentNode.classList.add('active');
}
function toggleDockBlocks(status) {
const docBlock = document.querySelector('.docBlocks');
const needHelp = document.querySelector('.needHelp');
if (status) {
if (docBlock) docBlock.style.display = 'grid';
if (needHelp) needHelp.style.display = 'block';
} else {
if (docBlock) docBlock.style.display = 'none';
if (needHelp) needHelp.style.display = 'none';
}
}
function loadNewsletterScript() {
/* Load the script only of the form is in the DOM */
if (document.querySelector('#sib-form') != null) {
const script = document.createElement('script');
script.src = 'https://sibforms.com/forms/end-form/build/main.js';
script.type = 'text/javascript';
script.id = 'newsletter_script';
document.head.appendChild(script);
} else {
document.getElementById('newsletter_script')?.remove();
}
}
/**
* Beginner mode
*/
let beginnerMode = window.localStorage.getItem('beginner-mode') === 'true';
function hideNonBeginnerDoc() {
const chapters = document.querySelectorAll('.sidenav > ul li');
chapters.forEach(chapter => {
if (!chapter.classList.contains('beginner')) {
chapter.style.display = 'none';
}
});
document.querySelectorAll('.beginner-mode-on').forEach(el => {
el.style.display = 'block';
});
}
function showNonBeginnerDoc() {
const chapters = document.querySelectorAll('.sidenav > ul li');
chapters.forEach(chapter => {
chapter.style.display = 'list-item';
});
document.querySelectorAll('.beginner-mode-on').forEach(el => {
el.style.display = 'none';
});
}
document.addEventListener('DOMContentLoaded', () => {
const beginnerModeTrigger = document.getElementById(
'beginner-mode-trigger'
);
if (beginnerModeTrigger) {
beginnerModeTrigger.addEventListener('click', () => {
beginnerMode = !beginnerMode;
if (beginnerMode) {
window.localStorage.setItem('beginner-mode', 'true');
hideNonBeginnerDoc();
} else {
window.localStorage.removeItem('beginner-mode');
showNonBeginnerDoc();
}
});
beginnerModeTrigger.checked = beginnerMode;
if (beginnerMode) {
hideNonBeginnerDoc();
}
}
});
// Replace full page reloads by a fill of the content area
// so that the side navigation keeps its state
// use a global event listener to also catch links inside the content area
document.addEventListener('click', event => {
const link = event.target.closest('a');
if (!link) {
return; // click not on a link
}
const location = document.location.href.split('#')[0];
const href = link.href;
if (href.indexOf(`${location}#`) === 0) {
return; // click on an anchor in the current page
}
if (!navLinks.includes(href)) {
return; // not a navigation link
}
window.sessionStorage.setItem(
'scrollIntoView',
link.closest('.sidenav') ? 'false' : 'true'
);
// now we're sure it's an internal navigation link
// transform it to an AJAX call
event.preventDefault();
// update versions links
const currentPage = href.split('/').pop();
versionsLinks.forEach(link => {
link.href =
link.href.substr(0, link.href.lastIndexOf('/') + 1) + currentPage;
});
// fetch the new content
fetch(href)
.then(res => res.text())
.then(replaceContent)
.then(() => import('./ra-doc-exec.js'))
.then(docExecModule => {
if (href.includes('documentation.html')) {
docExecModule.showTip();
} else {
hideTips();
}
docExecModule.buildJSCodeBlocksFromTS();
})
.then(loadNewsletterScript);
// change the URL
window.history.pushState(null, null, href);
changeSelectedMenu();
});
// make back button work again
window.addEventListener('popstate', () => {
if (document.location.href.indexOf('#') !== -1) {
// popstate triggered by a click on an anchor, not back button
return;
}
if (window.location.pathname.includes('/documentation.html')) {
fetch(window.location.pathname)
.then(res => res.text())
.then(replaceContent)
.then(() => import('./ra-doc-exec.js'))
.then(docExecModule => {
document.querySelector('.DocSearch-content').innerHTML = '';
toggleDockBlocks(true);
docExecModule.showTip();
});
} else {
// fetch the new content
fetch(window.location.pathname)
.then(res => res.text())
.then(replaceContent)
.then(() => {
toggleDockBlocks(false);
})
.then(hideTips)
.then(() => import('./ra-doc-exec.js'))
.then(docExecModule => docExecModule.buildJSCodeBlocksFromTS())
.then(loadNewsletterScript);
}
changeSelectedMenu();
});
window.addEventListener('DOMContentLoaded', () => {
allMenus = Array.from(document.querySelectorAll(`.sidenav a.nav-link`));
navLinks = allMenus
.filter(link => !link.classList.contains('external'))
.map(link => link.href);
versionsLinks = Array.from(document.querySelectorAll('#versions > li > a'));
buildPageToC();
navigationFitScroll();
loadNewsletterScript();
if (window.location.pathname.includes('/documentation.html')) {
import('./ra-doc-exec.js').then(docExecModule => {
document.querySelector('.DocSearch-content').innerHTML = '';
toggleDockBlocks(true);
docExecModule.showTip();
});
}
});