• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*! loadJS: load a JS file asynchronously. [c]2014 @scottjehl, Filament Group, Inc. (Based on https://goo.gl/REQGQ by Paul Irish). Licensed MIT */
2(function( w ){
3        var loadJS = function( src, cb, ordered ){
4                "use strict";
5                var tmp;
6                var ref = w.document.getElementsByTagName( "script" )[ 0 ];
7                var script = w.document.createElement( "script" );
8
9                if (typeof(cb) === 'boolean') {
10                        tmp = ordered;
11                        ordered = cb;
12                        cb = tmp;
13                }
14
15                script.src = src;
16                script.async = !ordered;
17                ref.parentNode.insertBefore( script, ref );
18
19                if (cb && typeof(cb) === "function") {
20                        script.onload = cb;
21                }
22                return script;
23        };
24        // commonjs
25        if( typeof module !== "undefined" ){
26                module.exports = loadJS;
27        }
28        else {
29                w.loadJS = loadJS;
30        }
31}( typeof global !== "undefined" ? global : this ));
32/*! end loadJS */
33
34// Remaining portions of this file are
35//
36// Copyright (c) 2019 Baldur Karlsson
37//
38// SPDX-License-Identifier: Apache-2.0
39
40var searchengine = undefined;
41
42// scroll to the first <a> linking to a chapter
43function scrollChapter(element, chapter) {
44  for(var i=0; i < element.children.length; i++) {
45    if(element.children[i].nodeName == "A" && element.children[i].href.indexOf(chapter) >= 0) {
46      element.children[i].scrollIntoView(true);
47      return true;
48    }
49
50    if(scrollChapter(element.children[i], chapter))
51      return true;
52  }
53
54  return false;
55}
56
57var results = undefined;
58var searchbox = undefined;
59var searchTimeout = undefined;
60
61function clearSearch() {
62  while(results.children.length > 0) {
63    results.removeChild(results.children[0]);
64  }
65
66  document.getElementById("resultsdiv").classList.remove("results");
67}
68
69function doSearch() {
70  clearSearch();
71
72  var searchtext = searchbox.value;
73
74  if(searchtext == '')
75    return;
76
77  if(searchtext.indexOf(' ') == -1 && searchtext.indexOf('\t') == -1 && searchtext.indexOf('"') == -1)
78    searchtext = searchtext + ' ' + searchtext + '*';
79
80  searchtext = searchtext.replace(/"/g, '')
81
82  var searchresults = searchengine.search(searchtext);
83
84  if(searchresults.length == 0) {
85    var r = document.createElement('LI');
86    r.innerHTML = 'No results';
87
88    results.appendChild(r);
89  }
90
91  document.getElementById("resultsdiv").classList.add("results");
92
93  for(var i=0; i < 10 && i < searchresults.length; i++) {
94    var a = document.createElement('A');
95    a.setAttribute('href', searchresults[i].ref);
96    a.innerHTML = searchlookup[searchresults[i].ref];
97
98    var r = document.createElement('LI');
99    r.appendChild(a);
100
101    results.appendChild(r);
102  }
103}
104
105function searchInput(e) {
106  if(searchTimeout !== undefined)
107    clearTimeout(searchTimeout);
108
109  searchTimeout = setTimeout(doSearch, 50);
110}
111
112function searchKeyDown(e) {
113  if(e.keyCode == 27) {
114    // escape
115    if(searchTimeout !== undefined)
116      clearTimeout(searchTimeout);
117
118    searchbox.value = '';
119
120    clearSearch();
121  } else if(e.keyCode == 10 || e.keyCode == 13) {
122    // enter/return
123    doSearch();
124  } else if(e.keyCode == 8) {
125    clearSearch();
126
127    searchInput(e);
128  }
129}
130
131document.addEventListener("DOMContentLoaded", function(event) {
132  // get the chapter name from the current URL
133  var chap = window.location.pathname.replace(/.*\//, '');
134
135  var toc = document.getElementById("toc");
136
137  // Scroll the sidebar to the appropriate chapter
138  if(chap != "") {
139    scrollChapter(toc, chap);
140    toc.scrollTop -= 96;
141  }
142
143  // add anchor links to code blocks
144  var blocks = document.getElementsByClassName("listingblock")
145
146  for(var i=0; i < blocks.length; i++) {
147    if(blocks[i].id.length > 0) {
148      var a = document.createElement("A");
149      a.innerHTML = '\u00B6';
150      a.setAttribute('class', 'link');
151      a.setAttribute('href', '#' + blocks[i].id);
152
153      blocks[i].insertBefore(a, blocks[i].childNodes[0]);
154    }
155  }
156
157  results = document.getElementById('results');
158  searchbox = document.getElementById('searchbox');
159
160  loadJS("lunr.js", function() {
161    loadJS(searchindexurl, function() {
162      searchengine = lunr.Index.load(searchindex);
163
164      searchbox.value = '';
165      searchbox.disabled = false;
166      searchbox.addEventListener('keydown', searchKeyDown, false);
167      searchbox.addEventListener('input', searchInput, false);
168    }, true);
169  }, true);
170});
171