1// toggling of navigation menus
2var toggleMainNav = document.getElementById('js-main-nav-toggle');
3if (toggleMainNav !== null) {
4 toggleMainNav.onclick = function() {
5 document.body.classList.toggle('has-active-main-nav');
9var toggleContentNav = document.getElementById('js-content-nav-toggle');
10if (toggleContentNav !== null) {
11 toggleContentNav.onclick = function() {
12 document.body.classList.toggle('has-active-content-nav');
16function initHotKeys() {
18 var bodyEl = document.body;
19 var searchInput = document.getElementById('search');
21 document.addEventListener('keydown', function (event) {
22 if (event.defaultPrevented) {
26 var key = event.key || event.keyCode;
28 // ESC to close search window
29 if (key === 'Escape' || key === 'Esc' || key === 27) {
30 document.body.classList.remove('has-active-search-window');
34 // / to close toggle window
35 if (key === '/' || key === 'Period' || key === 58) {
36 event.preventDefault();
37 document.body.classList.toggle('has-active-search-window');
38 if (bodyEl.classList.contains('has-active-search-window')) {
43 //when search is active TAB puts search input into focus
44 if (bodyEl.classList.contains('has-active-search-window')) {
45 if (key === 'Tab' || key === 'Tab' || key === 9) {
53// search functionality
54function debounce(func, wait) {
60 clearTimeout(timeout);
62 timeout = setTimeout(function () {
64 func.apply(context, args);
70// The strategy is as follows:
71// First, assign a value to each word in the document:
72// Words that correspond to search terms (stemmer aware): 40
74// First word in a sentence: 8
75// Then use a sliding window with a constant number of words and count the
76// sum of the values of the words within the window. Then use the window that got the
77// maximum sum. If there are multiple maximas, then get the last one.
78// Enclose the terms in <b>.
79function makeTeaser(body, terms) {
81 var NORMAL_WORD_WEIGHT = 2;
82 var FIRST_WORD_WEIGHT = 8;
83 //var TEASER_MAX_WORDS = 17;
84 var TEASER_MAX_CHARS = 63;
86 var stemmedTerms = terms.map(function (w) {
87 return elasticlunr.stemmer(w.toLowerCase());
90 var termFound = false;
92 var weighted = []; // contains elements of ["word", weight, index_in_document]
94 // split in sentences, then words
95 var sentences = body.toLowerCase().split(". ");
97 for (var i in sentences) {
98 var words = sentences[i].split(" ");
99 var value = FIRST_WORD_WEIGHT;
101 for (var j in words) {
104 if (word.length > 0) {
105 for (var k in stemmedTerms) {
106 if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
111 weighted.push([word, value, index]);
112 value = NORMAL_WORD_WEIGHT;
115 index += word.length;
116 index += 1; // ' ' or '.' if last word in sentence
119 index += 1; // because we split at a two-char boundary '. '
122 if (weighted.length === 0) {
126 var windowWeights = [];
129 // loop over words (weighted) till max chars is reached adding a counter to windowSize every time a new word has passed
131 for (var i = 0; i < weighted.length; i++) {
132 sumChars += weighted[i][0].length;
133 if (sumChars <= TEASER_MAX_CHARS) {
138 // We add a window with all the weights first
140 for (var i = 0; i < windowSize; i++) {
141 curSum += weighted[i][1];
143 windowWeights.push(curSum);
145 for (var i = 0; i < weighted.length - windowSize; i++) {
146 curSum -= weighted[i][1];
147 curSum += weighted[i + windowSize][1];
148 windowWeights.push(curSum);
151 // If we didn't find the term, just pick the first window
156 for (var i = windowWeights.length - 1; i >= 0; i--) {
157 if (windowWeights[i] > maxFound) {
158 maxFound = windowWeights[i];
165 var startIndex = weighted[maxSumIndex][2];
166 for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
167 var word = weighted[i];
168 if (startIndex < word[2]) {
169 // missing text from index to start of `word`
170 teaser.push(body.substring(startIndex, word[2]));
171 startIndex = word[2];
174 startIndex = word[2] + word[0].length;
175 teaser.push(body.substring(word[2], startIndex));
179 return teaser.join("");
182function formatSearchResultTitle(item) {
183 var pathArray = item.ref.split( '/' );
185 // first directory is in 4th element of split path array
186 var firstPartTitle = pathArray[3];
188 // last directory is in 2nd to last element of array
189 var lastPartTitle = item.doc.title;
191 var fullTitle = firstPartTitle + '<span class="gray90 inline-block pl2">' + ' / ' + lastPartTitle + '</span>';
196function formatSearchResultItem(item, terms) {
198 var li = document.createElement("li");
199 var createA = document.createElement("a");
200 li.appendChild(createA);
201 var teaserTitle = formatSearchResultTitle(item);
202 li.classList.add("search-results__item");
203 li.classList.add("ph3");
204 var hrefA = item.ref;
205 createA.setAttribute('href', hrefA);
206 createA.setAttribute('class','no-underline block pl6 pv3');
207 createA.innerHTML = `<span class="fs45 capitalize">${teaserTitle}</span>`;
208 createA.innerHTML += `<span class='pr7 fs5 none float-right'>→</span>`;
209 createA.innerHTML += `<div class="fs35 truncate mr10">${makeTeaser(item.doc.body, terms)}</div>`;
213function initSearch() {
214 var searchInput = document.getElementById("search");
215 var searchForm = document.getElementById("search-form");
216 var inputReset = searchForm.nextElementSibling;
222 var searchResults = document.querySelector(".search-results");
223 var searchResultsHeader = document.querySelector(".search-results__header");
224 var searchResultsItems = document.querySelector(".search-results__items");
228 expand: true, // turn on partial word search
234 var currentTerm = "";
235 var index = elasticlunr.Index.load(window.searchIndex);
236 // toggle search window
237 var toggleSearchWindow = document.getElementById('js-search-window-toggle');
238 var bodyEl = document.body;
239 var searchOverlay = document.getElementById('search-overlay');
240 var searchWindow = document.getElementById('search-window');
241 var searchActive = bodyEl.classList.contains('has-active-search-window');
243 if (toggleSearchWindow !== null) {
244 toggleSearchWindow.onclick = function() {
245 inputReset.style.display="none";
248 bodyEl.classList.toggle('has-active-search-window');
249 //put input in focus when activating search
252 searchInput.value = "";
253 searchResultsHeader.value = "";
254 searchResults.style.display = searchInput.value.trim() === "" ? "none" : "block";
256 searchOverlay.addEventListener('click', function(e){
257 if (!searchWindow.contains(e.target)){
258 bodyEl.classList.toggle('has-active-search-window');
264 searchInput.addEventListener("keyup", debounce(function() {
265 inputReset.style.display="block";
267 searchResultsHeader.value = "";
268 var term = searchInput.value.trim();
270 //removed to fix: a state where there’s a valid search term, but no results
272 if (term === currentTerm || !index) {
276 searchResults.style.display = term === "" ? "none" : "block";
277 searchResults.style.overflowY = term === "" ? "hidden" : "scroll";
278 searchResultsItems.innerHTML = "";
280 inputReset.style.display="none";
284 var results = index.search(term, options).filter(function (r) {
285 return r.doc.body !== "";
288 if (results.length === 0) {
289 searchResultsHeader.innerText = `No search results for '${term}'.`;
294 searchResultsHeader.innerText = `${results.length} search results for '${term}':`;
295 for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
296 if (!results[i].doc.body) {
299 searchResultsItems.appendChild(formatSearchResultItem(results[i], term.split(" ")));
303 // reset input button
304 inputReset.onclick=function(){
305 document.getElementById('search-form').reset();
307 searchForm.addEventListener("reset", function(event){
308 var e = document.createEvent('KeyboardEvent');
309 e.initEvent("keyup", false, true);
310 searchInput.dispatchEvent(e);
315if (document.readyState === "complete" ||
316 (document.readyState !== "loading" && !document.documentElement.doScroll)
318 initToggleSearchWindow();
320 document.addEventListener("DOMContentLoaded", function () {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list