-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.js
More file actions
144 lines (129 loc) · 4.82 KB
/
docs.js
File metadata and controls
144 lines (129 loc) · 4.82 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
/* =========================================================
Termpolis docs — sidebar highlighting + client search
No build step. Pure vanilla JS.
========================================================= */
(function () {
var sections = Array.prototype.slice.call(document.querySelectorAll('.docs-section'));
var tocLinks = Array.prototype.slice.call(document.querySelectorAll('.docs-toc a'));
var searchInput = document.getElementById('docs-search-input');
var sidebarGroups = Array.prototype.slice.call(document.querySelectorAll('.docs-sidebar-eyebrow'));
// Map section-id -> toc link for O(1) highlight updates.
var tocById = {};
tocLinks.forEach(function (link) {
var href = link.getAttribute('href') || '';
var id = href.indexOf('#') === 0 ? href.slice(1) : '';
if (id) tocById[id] = link;
});
// ---- Scroll spy -----------------------------------------
function setActive(id) {
tocLinks.forEach(function (l) { l.classList.remove('is-active'); });
var link = tocById[id];
if (link) link.classList.add('is-active');
}
if ('IntersectionObserver' in window) {
var visible = {};
var obs = new IntersectionObserver(
function (entries) {
entries.forEach(function (e) {
visible[e.target.id] = e.isIntersecting;
});
// Pick the earliest visible section in DOM order.
for (var i = 0; i < sections.length; i++) {
if (visible[sections[i].id]) {
setActive(sections[i].id);
return;
}
}
},
{ rootMargin: '-28% 0px -60% 0px', threshold: 0 }
);
sections.forEach(function (s) { obs.observe(s); });
}
// ---- Client-side search --------------------------------
// Pre-index each section's text so filtering is fast.
var index = sections.map(function (s) {
return {
id: s.id,
el: s,
text: (s.textContent || '').toLowerCase(),
heading: (s.querySelector('h2') || {}).textContent || ''
};
});
function getEmptyNode() {
var node = document.getElementById('docs-search-empty');
if (!node) {
node = document.createElement('div');
node.id = 'docs-search-empty';
node.className = 'docs-search-empty';
node.textContent = 'No sections match your search.';
var container = document.getElementById('docs-content');
if (container) container.appendChild(node);
}
return node;
}
function clearFilter() {
index.forEach(function (entry) { entry.el.classList.remove('is-hidden'); });
tocLinks.forEach(function (l) { l.classList.remove('is-hidden'); });
sidebarGroups.forEach(function (g) { g.classList.remove('is-hidden'); });
var empty = document.getElementById('docs-search-empty');
if (empty) empty.remove();
}
function runFilter(q) {
if (!q) { clearFilter(); return; }
var query = q.toLowerCase().trim();
if (!query) { clearFilter(); return; }
var anyMatch = false;
var matchedIds = {};
index.forEach(function (entry) {
var matches = entry.text.indexOf(query) !== -1;
if (matches) { matchedIds[entry.id] = true; anyMatch = true; }
entry.el.classList.toggle('is-hidden', !matches);
});
tocLinks.forEach(function (l) {
var href = l.getAttribute('href') || '';
var id = href.indexOf('#') === 0 ? href.slice(1) : '';
var shouldHide = id && !matchedIds[id];
l.classList.toggle('is-hidden', !!shouldHide);
});
// Hide sidebar-group eyebrows whose items are all hidden.
sidebarGroups.forEach(function (eyebrow) {
var list = eyebrow.nextElementSibling;
if (!list) return;
var items = list.querySelectorAll('a');
var anyVisible = false;
for (var i = 0; i < items.length; i++) {
if (!items[i].classList.contains('is-hidden')) { anyVisible = true; break; }
}
eyebrow.classList.toggle('is-hidden', !anyVisible);
});
var empty = document.getElementById('docs-search-empty');
if (!anyMatch) {
getEmptyNode();
} else if (empty) {
empty.remove();
}
}
if (searchInput) {
var timer = null;
searchInput.addEventListener('input', function () {
if (timer) clearTimeout(timer);
var val = searchInput.value;
timer = setTimeout(function () { runFilter(val); }, 120);
});
// '/' focuses the search unless a field is already focused.
window.addEventListener('keydown', function (e) {
if (e.key === '/' && document.activeElement !== searchInput) {
var tag = (document.activeElement && document.activeElement.tagName) || '';
if (tag !== 'INPUT' && tag !== 'TEXTAREA') {
e.preventDefault();
searchInput.focus();
}
}
if (e.key === 'Escape' && document.activeElement === searchInput) {
searchInput.value = '';
clearFilter();
searchInput.blur();
}
});
}
})();