2 // styling of input fields
5var inputElements = document.querySelectorAll('input.tlon-input');
6for (var i = 0; i < inputElements.length; i++) {
7 //listen for changes in input
8 inputElements[i].addEventListener('input', function() {
9 // if input has a value do this
11 var inputContainer = this.parentNode;
12 var validInputs = inputContainer.querySelectorAll('input.tlon-input:valid');
13 var inputs = inputContainer.querySelectorAll('input.tlon-input');
14 if (validInputs.length !== 0) {
16 for (var i = 0; i < validInputs.length; i++) {
17 if (this.classList.contains('is-valid') == false ) {
18 validInputs[i].parentNode.classList.add('has-valid-input');
22 // doesn't have valid input
23 for (var i = 0; i < inputs.length; i++) {
24 inputs[i].parentNode.classList.remove('has-valid-input');
28 this.parentNode.classList.remove('has-valid-input');
34 // toggling of navigation menus
37var toggleMainNav = document.getElementById('js-main-nav-toggle');
38if (toggleMainNav !== null) {
39 toggleMainNav.onclick = function() {
40 document.body.classList.toggle('has-active-main-nav');
44var toggleContentNav = document.getElementById('js-content-nav-toggle');
45if (toggleContentNav !== null) {
46 toggleContentNav.onclick = function() {
47 document.body.classList.toggle('has-active-content-nav');
51/*function initToggleMenu() {
52 var menu = document.querySelector(".menu");
53 var menuIcon = document.querySelector(".menu-icon");
54 var page = document.querySelector(".page");
55 menuIcon.addEventListener("click", function() {
56 menu.classList.toggle("menu-hidden");
57 page.classList.toggle("page-without-menu");
65function initHotKeys() {
67 var bodyEl = document.body;
68 var searchInput = document.getElementById('search');
70 document.addEventListener('keydown', function (event) {
71 if (event.defaultPrevented) {
75 var key = event.key || event.keyCode;
77 // ESC to close search window
78 if (key === 'Escape' || key === 'Esc' || key === 27) {
79 document.body.classList.remove('has-active-search-window');
83 // / to close toggle window
84 if (key === '/' || key === 'Period' || key === 58) {
85 event.preventDefault();
86 document.body.classList.toggle('has-active-search-window');
87 if (bodyEl.classList.contains('has-active-search-window')) {
92 //when search is active TAB puts search input into focus
93 if (bodyEl.classList.contains('has-active-search-window')) {
94 if (key === 'Tab' || key === 'Tab' || key === 9) {
102 // search functionality
105function debounce(func, wait) {
110 var args = arguments;
111 clearTimeout(timeout);
113 timeout = setTimeout(function () {
115 func.apply(context, args);
121// The strategy is as follows:
122// First, assign a value to each word in the document:
123// Words that correspond to search terms (stemmer aware): 40
125// First word in a sentence: 8
126// Then use a sliding window with a constant number of words and count the
127// sum of the values of the words within the window. Then use the window that got the
128// maximum sum. If there are multiple maximas, then get the last one.
129// Enclose the terms in <b>.
130function makeTeaser(body, terms) {
131 console.log("make teaser");
132 var TERM_WEIGHT = 40;
133 var NORMAL_WORD_WEIGHT = 2;
134 var FIRST_WORD_WEIGHT = 8;
135 //var TEASER_MAX_WORDS = 17;
136 var TEASER_MAX_CHARS = 63;
139 var stemmedTerms = terms.map(function (w) {
140 return elasticlunr.stemmer(w.toLowerCase());
143 console.log(stemmedTerms);
144 // create array with full word + partial Words (smallest fragment is 2 characters)
145 // first iteration is full word, then next is subset of the original word
146 /*var stemmedTerms = [];
147 for (var i = terms[0].length; i>=2; i--) {
148 stemmedTerms.push(terms[0].substring(0,i));
150 console.log(stemmedTerms);*/
151 var termFound = false;
153 var weighted = []; // contains elements of ["word", weight, index_in_document]
155 // split in sentences, then words
156 var sentences = body.toLowerCase().split(". ");
158 for (var i in sentences) {
159 var words = sentences[i].split(" ");
160 var value = FIRST_WORD_WEIGHT;
162 for (var j in words) {
165 if (word.length > 0) {
166 for (var k in stemmedTerms) {
167 if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
172 weighted.push([word, value, index]);
173 value = NORMAL_WORD_WEIGHT;
176 index += word.length;
177 index += 1; // ' ' or '.' if last word in sentence
180 index += 1; // because we split at a two-char boundary '. '
183 if (weighted.length === 0) {
187 var windowWeights = [];
190 // loop over words (weighted) till max chars is reached adding a counter to windowSize every time a new word has passed
192 for (var i = 0; i < weighted.length; i++) {
193 sumChars += weighted[i][0].length;
194 if (sumChars <= TEASER_MAX_CHARS) {
199 // We add a window with all the weights first
201 for (var i = 0; i < windowSize; i++) {
202 curSum += weighted[i][1];
204 windowWeights.push(curSum);
206 for (var i = 0; i < weighted.length - windowSize; i++) {
207 curSum -= weighted[i][1];
208 curSum += weighted[i + windowSize][1];
209 windowWeights.push(curSum);
212 // If we didn't find the term, just pick the first window
217 for (var i = windowWeights.length - 1; i >= 0; i--) {
218 if (windowWeights[i] > maxFound) {
219 maxFound = windowWeights[i];
226 var startIndex = weighted[maxSumIndex][2];
227 for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
228 var word = weighted[i];
229 if (startIndex < word[2]) {
230 // missing text from index to start of `word`
231 teaser.push(body.substring(startIndex, word[2]));
232 startIndex = word[2];
235 // add dark color around search terms
236 if (word[1] === TERM_WEIGHT) {
237 teaser.push("<span class='gray70'>");
239 startIndex = word[2] + word[0].length;
240 teaser.push(body.substring(word[2], startIndex));
242 if (word[1] === TERM_WEIGHT) {
243 teaser.push("</span>");
247 return teaser.join("");
250function formatSearchResultTitle(path) {
251 var pathArray = path.split( '/' );
253 // first directory is in 4th element of split path array
254 var firstPartTitle = pathArray[3];
256 // last directory is in 2nd to last element of array
257 var lastPartTitle = pathArray[pathArray.length-2].replace(/-/g,' ');
259 var fullTitle = firstPartTitle + '<span class="gray90">' + ' / ' + lastPartTitle + '</span>';
264function formatSearchResultItem(item, terms) {
265 var li = document.createElement("li");
266 var createA = document.createElement("a");
267 li.appendChild(createA);
268 var teaserTitle = formatSearchResultTitle(item.ref);
269 li.classList.add("search-results__item");
270 var hrefA = item.ref;
271 createA.setAttribute('href', hrefA);
272 createA.setAttribute('class','no-underline block pl6 pv2');
273 createA.innerHTML = `<span class="fs45 capitalize gray30">${teaserTitle}</span>`;
274 createA.innerHTML += `<div class="fs35 gray30">${makeTeaser(item.doc.body, terms)}</div>`;
278function initSearch() {
279 console.log("init search");
280 var searchInput = document.getElementById("search");
286 var searchResults = document.querySelector(".search-results");
287 var searchResultsHeader = document.querySelector(".search-results__header");
288 var searchResultsItems = document.querySelector(".search-results__items");
299 var currentTerm = "";
300 var index = elasticlunr.Index.load(window.searchIndex);
302 // toggle search window
303 var toggleSearchWindow = document.getElementById('js-search-window-toggle');
304 var bodyEl = document.body;
305 var searchOverlay = document.getElementById('search-overlay');
306 var searchWindow = document.getElementById('search-window');
307 var searchActive = bodyEl.classList.contains('has-active-search-window');
308 if (toggleSearchWindow !== null) {
309 toggleSearchWindow.onclick = function() {
311 bodyEl.classList.toggle('has-active-search-window');
312 //put input in focus when activating search
314 searchInput.value = "";
315 searchResultsHeader.value = "";
316 searchResults.style.display = searchInput.value.trim() === "" ? "none" : "block";
318 searchOverlay.addEventListener('click', function(e){
319 if (!searchWindow.contains(e.target)){
320 bodyEl.classList.toggle('has-active-search-window');
326 searchInput.addEventListener("keyup", debounce(function() {
327 searchResultsHeader.value = "";
329 var term = searchInput.value.trim();
332 //removed to fix: a state where there’s a valid search term, but no results
334 if (term === currentTerm || !index) {
338 searchResults.style.display = term === "" ? "none" : "block";
339 searchResults.style.overflowY = term === "" ? "hidden" : "scroll";
340 searchResultsItems.innerHTML = "";
345 // change query in order to make partial words work
347 var results = index.search(term, options).filter(function (r) {
348 return r.doc.body !== "";
350 if (results.length === 0) {
351 searchResultsHeader.innerText = `No search results for '${term}'.`;
356 searchResultsHeader.innerText = `${results.length} search results for '${term}':`;
357 for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
358 if (!results[i].doc.body) {
361 searchResultsItems.appendChild(formatSearchResultItem(results[i], term.split(" ")));
366if (document.readyState === "complete" ||
367 (document.readyState !== "loading" && !document.documentElement.doScroll)
369 initToggleSearchWindow();
371 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