on b88193e help sign in
urbit.org/static/scripts.js
275 lines 7.8 kB history raw
1//
2 // styling of input fields
3//
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
10 if (this.value) {
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) {
15 // has valid input
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');
19 }
20 }
21 } else {
22 // doesn't have valid input
23 for (var i = 0; i < inputs.length; i++) {
24 inputs[i].parentNode.classList.remove('has-valid-input');
25 }
26 }
27 } else {
28 this.parentNode.classList.remove('has-valid-input');
29 }
30 });
33//
34 // toggling of navigation menus
35//
37var toggleMainNav = document.getElementById('js-main-nav-toggle');
38if (toggleMainNav !== null) {
39 toggleMainNav.onclick = function() {
40 document.body.classList.toggle('has-active-main-nav');
41 }
44var toggleContentNav = document.getElementById('js-content-nav-toggle');
45if (toggleContentNav !== null) {
46 toggleContentNav.onclick = function() {
47 document.body.classList.toggle('has-active-content-nav');
48 }
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");
58 });
59}*/
61function initToggleMenu() {
62 var toggleContentNav = document.getElementById('js-search-window-toggle');
63 if (toggleContentNav !== null) {
64 toggleContentNav.onclick = function() {
65 document.body.classList.toggle('has-active-search-window');
66 }
67 }
70var toggleContentNav = document.getElementById('js-search-window-toggle');
71if (toggleContentNav !== null) {
72 toggleContentNav.onclick = function() {
73 document.body.classList.toggle('has-active-search-window');
74 }
77//
78 // search functionality
79//
81function debounce(func, wait) {
82 var timeout;
84 return function () {
85 var context = this;
86 var args = arguments;
87 clearTimeout(timeout);
89 timeout = setTimeout(function () {
90 timeout = null;
91 func.apply(context, args);
92 }, wait);
93 };
96// Taken from mdbook
97// The strategy is as follows:
98// First, assign a value to each word in the document:
99// Words that correspond to search terms (stemmer aware): 40
100// Normal words: 2
101// First word in a sentence: 8
102// Then use a sliding window with a constant number of words and count the
103// sum of the values of the words within the window. Then use the window that got the
104// maximum sum. If there are multiple maximas, then get the last one.
105// Enclose the terms in <b>.
106function makeTeaser(body, terms) {
107 var TERM_WEIGHT = 40;
108 var NORMAL_WORD_WEIGHT = 2;
109 var FIRST_WORD_WEIGHT = 8;
110 var TEASER_MAX_WORDS = 17;
112 var stemmedTerms = terms.map(function (w) {
113 return elasticlunr.stemmer(w.toLowerCase());
114 });
115 var termFound = false;
116 var index = 0;
117 var weighted = []; // contains elements of ["word", weight, index_in_document]
119 // split in sentences, then words
120 var sentences = body.toLowerCase().split(". ");
122 for (var i in sentences) {
123 var words = sentences[i].split(" ");
124 var value = FIRST_WORD_WEIGHT;
126 for (var j in words) {
127 var word = words[j];
129 if (word.length > 0) {
130 for (var k in stemmedTerms) {
131 if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
132 value = TERM_WEIGHT;
133 termFound = true;
134 }
135 }
136 weighted.push([word, value, index]);
137 value = NORMAL_WORD_WEIGHT;
138 }
140 index += word.length;
141 index += 1; // ' ' or '.' if last word in sentence
142 }
144 index += 1; // because we split at a two-char boundary '. '
145 }
147 if (weighted.length === 0) {
148 return body;
149 }
151 var windowWeights = [];
152 var windowSize = Math.min(weighted.length, TEASER_MAX_WORDS);
153 // We add a window with all the weights first
154 var curSum = 0;
155 for (var i = 0; i < windowSize; i++) {
156 curSum += weighted[i][1];
157 }
158 windowWeights.push(curSum);
160 for (var i = 0; i < weighted.length - windowSize; i++) {
161 curSum -= weighted[i][1];
162 curSum += weighted[i + windowSize][1];
163 windowWeights.push(curSum);
164 }
166 // If we didn't find the term, just pick the first window
167 var maxSumIndex = 0;
168 if (termFound) {
169 var maxFound = 0;
170 // backwards
171 for (var i = windowWeights.length - 1; i >= 0; i--) {
172 if (windowWeights[i] > maxFound) {
173 maxFound = windowWeights[i];
174 maxSumIndex = i;
175 }
176 }
177 }
179 var teaser = [];
180 var startIndex = weighted[maxSumIndex][2];
181 for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
182 var word = weighted[i];
183 if (startIndex < word[2]) {
184 // missing text from index to start of `word`
185 teaser.push(body.substring(startIndex, word[2]));
186 startIndex = word[2];
187 }
189 // add <em/> around search terms
190 if (word[1] === TERM_WEIGHT) {
191 teaser.push("<b>");
192 }
193 startIndex = word[2] + word[0].length;
194 teaser.push(body.substring(word[2], startIndex));
196 if (word[1] === TERM_WEIGHT) {
197 teaser.push("</b>");
198 }
199 }
200 teaser.push("…");
201 return teaser.join("");
204function formatSearchResultItem(item, terms) {
205 var li = document.createElement("li");
206 li.classList.add("search-results__item");
207 li.innerHTML = `<a href="${item.ref}">${item.doc.title}</a>`;
208 li.innerHTML += `<div class="search-results__teaser">${makeTeaser(item.doc.body, terms)}</div>`;
209 return li;
212function initSearch() {
213 var searchInput = document.getElementById("search");
214 if (!searchInput) {
215 return;
216 }
218 var searchResults = document.querySelector(".search-results");
219 var searchResultsHeader = document.querySelector(".search-results__header");
220 var searchResultsItems = document.querySelector(".search-results__items");
221 var MAX_ITEMS = 10;
223 var options = {
224 bool: "AND",
225 fields: {
226 title: {boost: 2},
227 body: {boost: 1},
228 }
229 };
230 var currentTerm = "";
231 var index = elasticlunr.Index.load(window.searchIndex);
233 searchInput.addEventListener("keyup", debounce(function() {
234 var term = searchInput.value.trim();
235 if (term === currentTerm || !index) {
236 return;
237 }
238 searchResults.style.display = term === "" ? "none" : "block";
239 searchResultsItems.innerHTML = "";
240 if (term === "") {
241 return;
242 }
244 var results = index.search(term, options).filter(function (r) {
245 return r.doc.body !== "";
246 });
247 if (results.length === 0) {
248 searchResultsHeader.innerText = `No search results for '${term}'.`;
249 return;
250 }
252 currentTerm = term;
253 searchResultsHeader.innerText = `${results.length} search results for '${term}':`;
254 for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
255 if (!results[i].doc.body) {
256 continue;
257 }
258 // var item = document.createElement("li");
259 // item.innerHTML = formatSearchResultItem(results[i], term.split(" "));
260 console.log(results[i]);
261 searchResultsItems.appendChild(formatSearchResultItem(results[i], term.split(" ")));
262 }
263 }, 150));
266if (document.readyState === "complete" ||
267 (document.readyState !== "loading" && !document.documentElement.doScroll)
268) {
269 initToggleMenu();
270} else {
271 document.addEventListener("DOMContentLoaded", function () {
272 initToggleMenu();
273 initSearch();
274 });