forked from cockroachdb/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitSidebar.js
148 lines (130 loc) · 4.37 KB
/
initSidebar.js
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
$(function() {
const $sidebar = $('#sidebar');
const $sidebars = $('.js-sidebar');
const navgocoOptions = {
caretHtml: '',
accordion: true,
openClass: 'active',
save: false,
cookie: {
name: 'navgoco',
expires: false,
path: '/'
},
slide: {
duration: 150,
easing: 'swing'
}
};
// Initialize navgoco with config options
$sidebar.navgoco($.extend(navgocoOptions, {
onToggleAfter: function() {
if ($(this.el).attr('id') == 'sidebar' &&
$('#version-switcher').hasClass('open')) {
closeVersionSwitcher();
}
}
}));
$sidebar.show();
// Loop over the left sidebar and top nav mobile version
$sidebars.each((k, sidebar)=>{
// console.log(sidebar);
// sidebar.navgoco($.extend(navgocoOptions, {
// onToggleAfter: function() {
// if ($(this.el).attr('id') == 'sidebar' &&
// $('#version-switcher').hasClass('open')) {
// closeVersionSwitcher();
// }
// }
// }));
// sidebar.show();
});
});
// called from sidebar.js.html
function renderSidebar(sidebar) {
// We derive the version from the URL rather than hardcoding
// `page.version` so that the source of pages for "named"
// versions, like stable and edge, can be identical to the
// source for the underlying version, like v1.0 or v1.1.
// Otherwise, the sidebar for a `stable` page would
// inappropriately link to the underlying `v1.0` page instead
// of the `stable` alias.
const pageVersion = (function () {
const pathComponents = location.pathname
.replace(sidebar.baseUrl, '')
.replace(/^\//, '')
.split('/');
// The version is the first directory component in the URL,
// if it exists.
if (pathComponents.length > 1 && sidebar.isVersionDirectory(pathComponents[0])) {
return pathComponents[0];
}
// Non-versioned pages link to stable docs.
return "stable";
})();
// Given a sidebar hierarchy (see _data/sidebar-data-v1.0.json
// for an example), returns a jQuery <ul> element with the
// following structure:
//
// <ul>
// <li class="tier-1">
// <a href="{{ item.url }}">{{ item.title }}</a>
// <ul>
// {% for item in item.items %}
// <li class="tier-2">...</li>
// {% endfor %}
// </ul>
// </li>
// </ul>
//
// Additionally injects breadcrumbs for the active sidebar
// entry, if any, into the `.collapsed-header` element above.
function renderItems(items, paths) {
if (!items || items.length == 0)
return $();
const lis = items.map(function (item) {
const urls = (item.urls || []).map(function (url) {
var url = url.replace("${VERSION}", pageVersion);
// This condition makes it possible to use external
// urls in the sidebar.
if (!/^https?:/.test(url)) {
url = sidebar.baseUrl + url;
}
return url;
});
// this ensures page will be highlighted in sidebar even if URL is accessed without `.html` appended
const activePathname = location.pathname.slice(-5) === '.html' ? location.pathname : location.pathname + '.html';
const active = (urls.indexOf(activePathname) !== -1);
if (active) {
// This mutation inside an otherwise pure function is
// unfortunate, but doing it here avoids a separate
// traversal of the sidebar data.
const breadcrumbs = $("<div>")
.addClass("collapsed-header__pre")
.html(paths.join("<div class=\"arrow-down arrow-down--pre\"></div>\n"));
const title = $("<div>").html(item.title);
$(".collapsed-header").empty().append(breadcrumbs, title);
}
const subitems = renderItems(item.items, paths.concat(item.title));
const a = $("<a>")
.attr("href", urls[0] || "#")
.html(item.title);
if (subitems.length > 0 && !item.is_top_level) {
a.append(" ").append($("<div>").addClass("nav-expand"));
}
return $("<li>")
.addClass("tier-" + (paths.length + 1))
.toggleClass("active", active)
.toggleClass("visited", active)
.append(a)
.append(subitems);
});
return $("<ul>").append(lis);
}
const $sidebar = $('#sidebar');
const $html = renderItems(sidebar.items, []).html();
$sidebar.html($html)
.find("li.active")
.parents('li')
.toggleClass("active");
};