forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu-builder.html
106 lines (88 loc) · 3.47 KB
/
menu-builder.html
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
<script>
/**
* This is the script used in Webflow to render the docs menu on serverless.com
* It is stored here so that we have a central place to view and store this script.
*/
const { origin, pathname } = window.location;
const FixPath = (name) => name.replace(/^\/|\/$/g, '');
const currentPath = origin === 'http://127.0.0.1:5500' ? '' : FixPath(pathname);
const isWebFlowOrigin = origin.includes('webflow.io');
const toggleClass = (element, className) => element && element.classList.toggle(className);
const renderTreeNodes = (nodes) => {
const domNodes = [];
for (const menuText in nodes) {
const menuChildren = nodes[menuText];
const link = document.createElement('a');
const span = document.createElement('span');
const listItemTitleClass = 'list-item';
span.classList.add('list-item-label');
if (typeof menuChildren === 'string') {
link.classList.add(listItemTitleClass);
span.innerHTML = menuText;
link.appendChild(span);
const partialMenuLink = menuChildren ? `/${menuChildren}` : '';
const basename = isWebFlowOrigin
? `framework/docs${partialMenuLink.replaceAll('/', '-').replaceAll('.', '-')}`
: `framework/docs${partialMenuLink}`;
if (basename === currentPath) {
link.classList.add('active-list-item', 'item-active');
}
link.href = `${origin}/${basename}`;
domNodes.push(link);
} else {
const childNodes = renderTreeNodes(menuChildren);
const parentListItem = document.createElement('div');
const listWrapper = document.createElement('div');
parentListItem.classList.add(listItemTitleClass);
listWrapper.classList.add('nested-list');
span.classList.add('caret');
span.innerHTML = menuText;
childNodes.forEach((n) => {
listWrapper.appendChild(n);
});
parentListItem.appendChild(span);
parentListItem.appendChild(listWrapper);
/**
* If current path includes node path then add active class (to show collapsed list)
* */
const toggleListActiveClass = () => {
toggleClass(listWrapper, 'nested-list-active');
toggleClass(span, 'caret-down');
toggleClass(parentListItem, 'list-item-expanded');
};
// If the menu contains an active menu item we expand the menu
if (parentListItem.querySelectorAll('.active-list-item').length > 0) {
toggleListActiveClass();
}
span.addEventListener('click', function () {
toggleListActiveClass();
});
domNodes.push(parentListItem);
}
}
return domNodes;
};
const renderHits = (tree, selector) => {
const container = document.querySelector(selector);
const sidebarList = document.createElement('div');
const loaderWrapper = document.querySelector('.loader-wrapper');
if (loaderWrapper) {
loaderWrapper.remove();
}
const nodes = renderTreeNodes(tree);
/**
* Empty list to avoid adding the same items
*/
sidebarList.innerHTML = '';
nodes.forEach((node) => {
sidebarList.appendChild(node);
});
container.replaceChildren(sidebarList);
};
fetch('https://assets.serverless-extras.com/website/framework/docs/menu.json')
.then((response) => response.json())
.then((data) => {
renderHits(data, '#docs-sidebar-items');
renderHits(data, '#docs-sidebar-items-mobile');
});
</script>