diff options
Diffstat (limited to 'doc/html/mkdocs/js/search.js')
-rw-r--r-- | doc/html/mkdocs/js/search.js | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/doc/html/mkdocs/js/search.js b/doc/html/mkdocs/js/search.js new file mode 100644 index 00000000..d5c86616 --- /dev/null +++ b/doc/html/mkdocs/js/search.js | |||
@@ -0,0 +1,88 @@ | |||
1 | require([ | ||
2 | base_url + '/mkdocs/js/mustache.min.js', | ||
3 | base_url + '/mkdocs/js/lunr.min.js', | ||
4 | 'text!search-results-template.mustache', | ||
5 | 'text!../search_index.json', | ||
6 | ], function (Mustache, lunr, results_template, data) { | ||
7 | "use strict"; | ||
8 | |||
9 | function getSearchTerm() | ||
10 | { | ||
11 | var sPageURL = window.location.search.substring(1); | ||
12 | var sURLVariables = sPageURL.split('&'); | ||
13 | for (var i = 0; i < sURLVariables.length; i++) | ||
14 | { | ||
15 | var sParameterName = sURLVariables[i].split('='); | ||
16 | if (sParameterName[0] == 'q') | ||
17 | { | ||
18 | return decodeURIComponent(sParameterName[1].replace(/\+/g, '%20')); | ||
19 | } | ||
20 | } | ||
21 | } | ||
22 | |||
23 | var index = lunr(function () { | ||
24 | this.field('title', {boost: 10}); | ||
25 | this.field('text'); | ||
26 | this.ref('location'); | ||
27 | }); | ||
28 | |||
29 | data = JSON.parse(data); | ||
30 | var documents = {}; | ||
31 | |||
32 | for (var i=0; i < data.docs.length; i++){ | ||
33 | var doc = data.docs[i]; | ||
34 | doc.location = base_url + doc.location; | ||
35 | index.add(doc); | ||
36 | documents[doc.location] = doc; | ||
37 | } | ||
38 | |||
39 | var search = function(){ | ||
40 | |||
41 | var query = document.getElementById('mkdocs-search-query').value; | ||
42 | var search_results = document.getElementById("mkdocs-search-results"); | ||
43 | while (search_results.firstChild) { | ||
44 | search_results.removeChild(search_results.firstChild); | ||
45 | } | ||
46 | |||
47 | if(query === ''){ | ||
48 | return; | ||
49 | } | ||
50 | |||
51 | var results = index.search(query); | ||
52 | |||
53 | if (results.length > 0){ | ||
54 | for (var i=0; i < results.length; i++){ | ||
55 | var result = results[i]; | ||
56 | doc = documents[result.ref]; | ||
57 | doc.base_url = base_url; | ||
58 | doc.summary = doc.text.substring(0, 200); | ||
59 | var html = Mustache.to_html(results_template, doc); | ||
60 | search_results.insertAdjacentHTML('beforeend', html); | ||
61 | } | ||
62 | } else { | ||
63 | search_results.insertAdjacentHTML('beforeend', "<p>No results found</p>"); | ||
64 | } | ||
65 | |||
66 | if(jQuery){ | ||
67 | /* | ||
68 | * We currently only automatically hide bootstrap models. This | ||
69 | * requires jQuery to work. | ||
70 | */ | ||
71 | jQuery('#mkdocs_search_modal a').click(function(){ | ||
72 | jQuery('#mkdocs_search_modal').modal('hide'); | ||
73 | }); | ||
74 | } | ||
75 | |||
76 | }; | ||
77 | |||
78 | var search_input = document.getElementById('mkdocs-search-query'); | ||
79 | |||
80 | var term = getSearchTerm(); | ||
81 | if (term){ | ||
82 | search_input.value = term; | ||
83 | search(); | ||
84 | } | ||
85 | |||
86 | search_input.addEventListener("keyup", search); | ||
87 | |||
88 | }); | ||