on 7f3a93c help sign in
urbit.org/static/scripts.js
297 lines 8.5 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 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');
70 }
71 searchOverlay.addEventListener('click', function(e){
72 if (!searchWindow.contains(e.target)){
73 bodyEl.classList.toggle('has-active-search-window');
74 }
75 })
76 }
81//
82 // search functionality
83//
85function debounce(func, wait) {
86 var timeout;
88 return function () {
89 var context = this;
90 var args = arguments;
91 clearTimeout(timeout);
93 timeout = setTimeout(function () {
94 timeout = null;
95 func.apply(context, args);
96 }, wait);
97 };
100// Taken from mdbook
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
104// Normal words: 2
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());
119 });
120 var termFound = false;
121 var index = 0;
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) {
132 var word = words[j];
134 if (word.length > 0) {
135 for (var k in stemmedTerms) {
136 if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
137 value = TERM_WEIGHT;
138 termFound = true;
139 }
140 }
141 weighted.push([word, value, index]);
142 value = NORMAL_WORD_WEIGHT;
143 }
145 index += word.length;
146 index += 1; // ' ' or '.' if last word in sentence
147 }
149 index += 1; // because we split at a two-char boundary '. '
150 }
152 if (weighted.length === 0) {
153 return body;
154 }
156 var windowWeights = [];
157 var windowSize = 0;
159 var sumChars = 0;
160 for (var i = 0; i < weighted.length; i++) {
161 sumChars += weighted[i][0].length;
162 if (sumChars <= TEASER_MAX_CHARS) {
163 windowSize++;
164 }
165 }
167 // We add a window with all the weights first
168 var curSum = 0;
169 for (var i = 0; i < windowSize; i++) {
170 curSum += weighted[i][1];
171 }
172 windowWeights.push(curSum);
174 for (var i = 0; i < weighted.length - windowSize; i++) {
175 curSum -= weighted[i][1];
176 curSum += weighted[i + windowSize][1];
177 windowWeights.push(curSum);
178 }
180 // If we didn't find the term, just pick the first window
181 var maxSumIndex = 0;
182 if (termFound) {
183 var maxFound = 0;
184 // backwards
185 for (var i = windowWeights.length - 1; i >= 0; i--) {
186 if (windowWeights[i] > maxFound) {
187 maxFound = windowWeights[i];
188 maxSumIndex = i;
189 }
190 }
191 }
193 var teaser = [];
194 var startIndex = weighted[maxSumIndex][2];
195 for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
196 var word = weighted[i];
197 if (startIndex < word[2]) {
198 // missing text from index to start of `word`
199 teaser.push(body.substring(startIndex, word[2]));
200 startIndex = word[2];
201 }
203 // add <em/> around search terms
204 if (word[1] === TERM_WEIGHT) {
205 teaser.push("<b>");
206 }
207 startIndex = word[2] + word[0].length;
208 teaser.push(body.substring(word[2], startIndex));
210 if (word[1] === TERM_WEIGHT) {
211 teaser.push("</b>");
212 }
213 }
214 teaser.push("…");
215 return teaser.join("");
218/*function trimTeaser(teaser) {
219 var maxLength = 63; // maximum number of characters to extract
220 var string = teaser;
221 var trimmedString = teaser.substr(0, maxLength); //trim the string to the maximum length
222 trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" "))) //re-trim if we are in the middle of a word
223}*/
225function trimTeaserTitle() {
226 //
229function formatSearchResultItem(item, terms) {
230 var li = document.createElement("li");
231 li.classList.add("search-results__item");
232 li.innerHTML = `<a href="${item.ref}">${item.ref}</a>`;
233 li.innerHTML += `<div class="search-results__teaser">${makeTeaser(item.doc.body, terms)}</div>`;
234 return li;
237function initSearch() {
238 var searchInput = document.getElementById("search");
239 if (!searchInput) {
240 return;
241 }
243 var searchResults = document.querySelector(".search-results");
244 var searchResultsHeader = document.querySelector(".search-results__header");
245 var searchResultsItems = document.querySelector(".search-results__items");
246 var MAX_ITEMS = 200;
248 var options = {
249 bool: "AND",
250 fields: {
251 title: {boost: 2},
252 body: {boost: 1},
253 }
254 };
255 var currentTerm = "";
256 var index = elasticlunr.Index.load(window.searchIndex);
258 searchInput.addEventListener("keyup", debounce(function() {
259 var term = searchInput.value.trim();
260 if (term === currentTerm || !index) {
261 return;
262 }
263 searchResults.style.display = term === "" ? "none" : "block";
264 searchResultsItems.innerHTML = "";
265 if (term === "") {
266 return;
267 }
269 var results = index.search(term, options).filter(function (r) {
270 return r.doc.body !== "";
271 });
272 if (results.length === 0) {
273 searchResultsHeader.innerText = `No search results for '${term}'.`;
274 return;
275 }
277 currentTerm = term;
278 searchResultsHeader.innerText = `${results.length} search results for '${term}':`;
279 for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
280 if (!results[i].doc.body) {
281 continue;
282 }
283 searchResultsItems.appendChild(formatSearchResultItem(results[i], term.split(" ")));
284 }
285 }, 150));
288if (document.readyState === "complete" ||
289 (document.readyState !== "loading" && !document.documentElement.doScroll)
290) {
291 initToggleSearchWindow();
292} else {
293 document.addEventListener("DOMContentLoaded", function () {
294 initToggleSearchWindow();
295 initSearch();
296 });