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");
61function initToggleSearchWindow() {
62 var toggleSearchWindow = document.getElementById('js-search-window-toggle');
63 var bodyEl = document.body;
64 var searchOverlay = document.getElementById('search-overlay');
65 var searchWindow = document.getElementById('search-window');
66 var searchActive = bodyEl.classList.contains('has-active-search-window');
67 if (toggleSearchWindow !== null) {
68 toggleSearchWindow.onclick = function() {
69 bodyEl.classList.toggle('has-active-search-window');
71 searchOverlay.addEventListener('click', function(e){
72 if (!searchWindow.contains(e.target)){
73 bodyEl.classList.toggle('has-active-search-window');
82 // search functionality
85function debounce(func, wait) {
91 clearTimeout(timeout);
93 timeout = setTimeout(function () {
95 func.apply(context, args);
101// The strategy is as follows:
102// First, assign a value to each word in the document:
103// Words that correspond to search terms (stemmer aware): 40
105// First word in a sentence: 8
106// Then use a sliding window with a constant number of words and count the
107// sum of the values of the words within the window. Then use the window that got the
108// maximum sum. If there are multiple maximas, then get the last one.
109// Enclose the terms in <b>.
110function makeTeaser(body, terms) {
111 var TERM_WEIGHT = 40;
112 var NORMAL_WORD_WEIGHT = 2;
113 var FIRST_WORD_WEIGHT = 8;
114 //var TEASER_MAX_WORDS = 17;
115 var TEASER_MAX_CHARS = 63;
117 var stemmedTerms = terms.map(function (w) {
118 return elasticlunr.stemmer(w.toLowerCase());
120 var termFound = false;
122 var weighted = []; // contains elements of ["word", weight, index_in_document]
124 // split in sentences, then words
125 var sentences = body.toLowerCase().split(". ");
127 for (var i in sentences) {
128 var words = sentences[i].split(" ");
129 var value = FIRST_WORD_WEIGHT;
131 for (var j in words) {
134 if (word.length > 0) {
135 for (var k in stemmedTerms) {
136 if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
141 weighted.push([word, value, index]);
142 value = NORMAL_WORD_WEIGHT;
145 index += word.length;
146 index += 1; // ' ' or '.' if last word in sentence
149 index += 1; // because we split at a two-char boundary '. '
152 if (weighted.length === 0) {
156 var windowWeights = [];
159 // loop over words (weighted) till max chars is reached adding a counter to windowSize every time a new word has passed
161 for (var i = 0; i < weighted.length; i++) {
162 sumChars += weighted[i][0].length;
163 if (sumChars <= TEASER_MAX_CHARS) {
168 // We add a window with all the weights first
170 for (var i = 0; i < windowSize; i++) {
171 curSum += weighted[i][1];
173 windowWeights.push(curSum);
175 for (var i = 0; i < weighted.length - windowSize; i++) {
176 curSum -= weighted[i][1];
177 curSum += weighted[i + windowSize][1];
178 windowWeights.push(curSum);
181 // If we didn't find the term, just pick the first window
186 for (var i = windowWeights.length - 1; i >= 0; i--) {
187 if (windowWeights[i] > maxFound) {
188 maxFound = windowWeights[i];
195 var startIndex = weighted[maxSumIndex][2];
196 for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
197 var word = weighted[i];
198 if (startIndex < word[2]) {
199 // missing text from index to start of `word`
200 teaser.push(body.substring(startIndex, word[2]));
201 startIndex = word[2];
204 // add <em/> around search terms
205 if (word[1] === TERM_WEIGHT) {
208 startIndex = word[2] + word[0].length;
209 teaser.push(body.substring(word[2], startIndex));
211 if (word[1] === TERM_WEIGHT) {
216 return teaser.join("");
219function formatSearchResultTitle(path) {
220 var pathArray = path.split( 'docs/' );
221 var slug = pathArray[1];
222 var slugArray = slug.split( '/' );
223 var lastPartTitle = slugArray.splice(slugArray.length - 2, slugArray.length).join('');
224 var firstPartTitle = slugArray.join(' / ');
226 var fullTitle = firstPartTitle + ' / ' + '<span class="search-results__item__title__slug-end">' + lastPartTitle + '</span>';
231function formatSearchResultItem(item, terms) {
232 var li = document.createElement("li");
233 var createA = document.createElement("a");
234 li.appendChild(createA);
235 var teaserTitle = formatSearchResultTitle(item.ref);
236 li.classList.add("search-results__item");
237 var hrefA = item.ref;
238 createA.setAttribute('href', hrefA);
239 createA.innerHTML = `<span class="search-results__item__title">${teaserTitle}</span>`;
240 createA.innerHTML += `<div class="search-results__teaser">${makeTeaser(item.doc.body, terms)}</div>`;
244function initSearch() {
245 var searchInput = document.getElementById("search");
250 var searchResults = document.querySelector(".search-results");
251 var searchResultsHeader = document.querySelector(".search-results__header");
252 var searchResultsItems = document.querySelector(".search-results__items");
262 var currentTerm = "";
263 var index = elasticlunr.Index.load(window.searchIndex);
265 searchInput.addEventListener("keyup", debounce(function() {
266 var term = searchInput.value.trim();
267 if (term === currentTerm || !index) {
270 searchResults.style.display = term === "" ? "none" : "block";
271 searchResults.style.overflowY = term === "" ? "hidden" : "scroll";
272 searchResultsItems.innerHTML = "";
277 var results = index.search(term, options).filter(function (r) {
278 return r.doc.body !== "";
280 if (results.length === 0) {
281 searchResultsHeader.innerText = `No search results for '${term}'.`;
286 searchResultsHeader.innerText = `${results.length} search results for '${term}':`;
287 for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
288 if (!results[i].doc.body) {
291 searchResultsItems.appendChild(formatSearchResultItem(results[i], term.split(" ")));
296if (document.readyState === "complete" ||
297 (document.readyState !== "loading" && !document.documentElement.doScroll)
299 initToggleSearchWindow();
301 document.addEventListener("DOMContentLoaded", function () {
302 initToggleSearchWindow();
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