-
Notifications
You must be signed in to change notification settings - Fork 204
/
search.js
152 lines (130 loc) · 4.58 KB
/
search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { Popover } from 'bootstrap';
import lunr from 'lunr';
var lunrIndex = null;
var lunrData = null;
var search = null;
document.addEventListener('DOMContentLoaded', function () {
var oReq = new XMLHttpRequest();
oReq.open('GET', '/search/lunr-index.json');
oReq.addEventListener('load', function () {
lunrData = JSON.parse(oReq.response);
lunrIndex = lunr.Index.load(lunrData.index);
search = new Search();
search.init();
});
oReq.send();
});
(function () {
var Search = (window.Search = function () {
this.SEARCH_INPUT_ID = '#input-search';
this.POPOVER_CLASS = '.popover';
this.POPOVER_OPTIONS = {
html: true,
container: 'body',
placement: 'bottom',
trigger: 'manual',
content: function () {
return search.getPopoverContent();
},
};
this.LIMIT_RESULTS = 10;
this.popoverContent = '';
this.popoverHandler = null;
this.processText = function (text) {
if (!text || text === '') return this.hidePopover();
this.showPopover(text);
};
this.initializePopover = function () {
this.popover = new Popover(this.searchInput, this.POPOVER_OPTIONS);
};
this.hidePopover = function () {
const popover = Popover.getInstance(this.searchInput);
popover && popover.hide();
this.searchArrows.destroy();
};
this.showPopover = function (text) {
this.popoverContent = this.generatePopoverContent(text);
const popover = Popover.getInstance(this.searchInput);
popover.show();
document.querySelector('.popover-body').innerHTML = this.popoverContent;
};
this.generatePopoverContent = function (text) {
var results = lunrIndex.search(text);
if (!results.length) return 'No results found';
results = results.slice(0, this.LIMIT_RESULTS);
var uniqueResults = this.uniqueResults(results);
var generated = '<ul class="search-list-ul">';
uniqueResults.forEach(function (res) {
var store = lunrData.docs[res.ref];
// title
var title = document.createElement('h4');
title.textContent = store.title;
var titleLink = document.createElement('a');
titleLink.href = store.url;
titleLink.appendChild(title);
var li = document.createElement('li');
li.className = 'search-list-li';
li.appendChild(titleLink);
li.appendChild(document.createElement('br'));
// description if provided
if (store.description !== null) {
var description = document.createElement('p');
description.appendChild(document.createTextNode(store.description));
li.appendChild(description);
}
// separator
var liSep = document.createElement('li');
liSep.className = 'separator';
liSep.appendChild(document.createElement('hr'));
generated += [li, liSep].map((el) => el.outerHTML).join('');
});
return generated + '</ul>';
};
this.uniqueResults = function (results) {
var uniqueResults = [];
var titles = [];
[].forEach.call(results, function (el) {
if (titles.indexOf(lunrData.docs[el.ref].title) === -1) {
titles.push(lunrData.docs[el.ref].title);
uniqueResults.push(el);
}
});
return uniqueResults;
};
this.initializePopoverEvents = function () {
var self = this;
this.searchInput.addEventListener('keydown', function (e) {
if (e.which == 27) return self.hidePopover(); // esc key
});
this.searchInput.addEventListener('input', function (e) {
var text = this.value;
self.processText(text);
});
this.searchInput.addEventListener('focus', function (e) {
var text = this.value;
if (text === '') return;
self.showPopover(text);
});
this.searchInput.addEventListener('shown.bs.popover', function () {
self.popoverHandler = document.querySelector(self.POPOVER_CLASS);
self.popoverHandler.addEventListener('click', function (e) {
e.stopPropagation();
});
self.searchArrows.init();
});
this.searchInput.addEventListener('click', function (e) {
e.stopPropagation();
});
window.addEventListener('click', self.hidePopover.bind(self));
};
});
Search.prototype.init = function () {
this.searchInput = document.querySelector(this.SEARCH_INPUT_ID);
this.searchArrows = new SearchArrows();
this.initializePopoverEvents();
this.initializePopover();
};
Search.prototype.getPopoverContent = function () {
return this.popoverContent;
};
})();