-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathautosuggest.js
636 lines (546 loc) · 16.4 KB
/
autosuggest.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* eslint-disable camelcase, no-underscore-dangle, no-use-before-define */
import {
findAncestorByClass,
escapeDoubleQuotes,
replaceGlobally,
debounce,
} from './utils/helpers';
import 'element-closest';
import 'promise-polyfill/src/polyfill';
import 'whatwg-fetch';
const { epas } = window;
// Ensure we have an endpoint URL, or
// else this shouldn't happen
if (epas.endpointUrl && epas.endpointUrl !== '') {
init();
// Publically expose API
window.epasAPI = {
hideAutosuggestBox,
updateAutosuggestBox,
esSearch,
buildSearchQuery,
};
}
/**
* Submit the search form
*
* @param {Node} input - input element
*/
function submitSearchForm(input) {
input.closest('form').submit();
}
/**
* Set the expanded aria state on the input
*
* @param {boolean} haveOptions - whether or not the autosuggest list contains results
* @param {Node} input - search input
*/
function toggleInputAria(haveOptions, input) {
input.setAttribute('aria-expanded', haveOptions);
}
/**
* Set the active descendant aria attribute input
*
* @param {string} id - id of the currently selected element
* @param {Node} input - search input
*/
function setInputActiveDescendant(id, input) {
input.setAttribute('aria-activedescendant', id);
}
/**
* Take selected item and fill the search input
*
* @param {Node} input - input element
* @param {string} text - new input value
*/
function selectAutosuggestItem(input, text) {
input.value = text; // eslint-disable-line no-param-reassign
}
/**
* Fires events when autosuggest results are clicked,
* and if GA tracking is activated
*
* @param {object} detail - value to pass on to the Custom Event
*/
function triggerAutosuggestEvent(detail) {
const event = new CustomEvent('ep-autosuggest-click', { detail });
window.dispatchEvent(event);
if (
detail.searchTerm &&
parseInt(epas.triggerAnalytics, 10) === 1 &&
typeof gtag === 'function'
) {
const action = `click - ${detail.searchTerm}`;
// eslint-disable-next-line no-undef
gtag('event', action, {
event_category: 'EP :: Autosuggest',
event_label: detail.url,
transport_type: 'beacon',
});
}
}
/**
* Navigate to the selected item, and provides
* event hook for JS customizations, like GA
*
* @param {string} searchTerm - user defined search term
* @param {string} url - post url from dataset in search result
*/
function goToAutosuggestItem(searchTerm, url) {
const detail = {
searchTerm,
url,
};
triggerAutosuggestEvent(detail);
window.location.href = url;
}
/**
* Respond to an item selection based on the predefined behavior.
* If epas.action is set to "navigate" (the default), redirects the browser to the URL of the selected item
* If epas.action is set to any other value (such as "search"), fill in the value and perform the search
*
* @param {Node} input - search input
* @param {Node} element - search term result item
* @returns {Function} calls the submitSearchForm function
*/
function selectItem(input, element) {
if (epas.action === 'navigate') {
return goToAutosuggestItem(input.value, element.dataset.url);
}
selectAutosuggestItem(input, element.innerText);
return submitSearchForm(input);
}
/**
* Build the search query from the search text - the query is generated in PHP
* and passed into the front end as window.epas = { "query...
*
* @returns {string} json string
*/
function getJsonQuery() {
if (typeof window.epas === 'undefined') {
const error = 'No epas object defined';
// eslint-disable-next-line no-console
console.warn(error);
return { error };
}
return window.epas;
}
/**
* Build the search query from the search text
*
* @param {string} searchText - user search string
* @param {string} placeholder - placeholder text to replace
* @param {object} options - Autosuggest settings
* @param {string} options.query - JSON query string to pass to ElasticSearch
* @returns {string} json representation of search query
*/
function buildSearchQuery(searchText, placeholder, { query }) {
const newQuery = replaceGlobally(query, placeholder, searchText);
return newQuery;
}
/**
* Build the ajax request
*
* @param {string} query - json string
* @param {string} searchTerm - user search term
* @returns {object} AJAX object request
*/
async function esSearch(query, searchTerm) {
const fetchConfig = {
body: query,
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
};
if (epas?.http_headers && typeof epas.http_headers === 'object') {
Object.keys(epas.http_headers).forEach((name) => {
fetchConfig.headers[name] = epas.http_headers[name];
});
}
// only applies headers if using ep.io endpoint
if (epas.addSearchTermHeader) {
fetchConfig.headers['EP-Search-Term'] = searchTerm;
}
try {
const response = await window.fetch(epas.endpointUrl, fetchConfig);
if (!response.ok) {
throw Error(response.statusText);
}
const data = await response.json();
// allow for filtered data before returning it to
// be output on the front end
if (typeof window.epDataFilter !== 'undefined') {
return window.epDataFilter(data, searchTerm);
}
return data;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
return error;
}
}
/**
* Update the auto suggest box with new options or hide if none
*
* @param {Array} options - search results
* @param {string} input - search string
* @returns {boolean} return true
*/
function updateAutosuggestBox(options, input) {
let i;
let itemString = '';
// get the search term for use later on
const { value } = input;
const container = findAncestorByClass(input, 'ep-autosuggest-container');
const resultsContainer = container.querySelector('.ep-autosuggest');
const suggestList = resultsContainer.querySelector('.autosuggest-list');
// empty the the list of all child nodes
while (suggestList.firstChild) {
suggestList.removeChild(suggestList.firstChild);
}
if (options.length > 0) {
resultsContainer.style = 'display: block;';
} else {
resultsContainer.style = 'display: none;';
}
// anticipating the future... a setting where we configure
// a limit of results to show, and optionally append a
// link to "all results" or something of that nature
const resultsLimit = options.length;
// create markup for list items
// eslint-disable-next-line
for ( i = 0; resultsLimit > i; ++i ) {
const text = options[i]._source.post_title;
const url = options[i]._source.permalink;
const escapedText = escapeDoubleQuotes(text);
const searchParts = value.trim().split(' ');
let resultsText = escapedText;
if (epas.highlightingEnabled) {
// uses some regex magic to match upper/lower/capital case
const regex = new RegExp(`\\b(${searchParts.join('|')})`, 'gi');
resultsText = resultsText.replace(
regex,
(word) =>
`<${epas.highlightingTag} class="${epas.highlightingClass} ep-autosuggest-highlight">${word}</${epas.highlightingTag}>`,
);
}
let itemHTML = `<li class="autosuggest-item" role="option" aria-selected="false" id="autosuggest-option-${i}">
<a href="${url}" class="autosuggest-link" data-search="${escapedText}" data-url="${url}" tabindex="-1">
${resultsText}
</a>
</li>`;
if (typeof window.epAutosuggestItemHTMLFilter !== 'undefined') {
itemHTML = window.epAutosuggestItemHTMLFilter(itemHTML, options[i], i, value);
}
itemString += itemHTML;
}
// append list items to the list
suggestList.innerHTML = itemString;
const autosuggestItems = Array.from(document.querySelectorAll('.autosuggest-link'));
suggestList.addEventListener('click', (event) => {
event.preventDefault();
const target =
event.target.tagName === epas.highlightingTag?.toUpperCase()
? event.target.parentElement
: event.target;
if (autosuggestItems.includes(target)) {
selectItem(input, target);
}
});
return true;
}
/**
* Hide the auto suggest box
*
* @returns {boolean} returns true
*/
function hideAutosuggestBox() {
const lists = document.querySelectorAll('.autosuggest-list');
const containers = document.querySelectorAll('.ep-autosuggest');
// empty all EP results lists
lists.forEach((list) => {
while (list.firstChild) {
list.removeChild(list.firstChild);
}
});
// hide all EP results containers
containers.forEach((container) => {
// eslint-disable-next-line
container.style = 'display: none;';
});
return true;
}
/**
* Checks for any manually ordered posts and puts them in the correct place
*
* @param {Array} hits - ES results
* @param {string} searchTerm - user search term
* @returns {object} formatted hits
*/
function checkForOrderedPosts(hits, searchTerm) {
const toInsert = {};
const taxName = 'ep_custom_result';
const lowerCaseSearchTerm = searchTerm.toLowerCase();
const filteredHits = hits.filter((hit) => {
// Should we retain this hit in its current position?
let retain = true;
if (undefined !== hit._source.terms && undefined !== hit._source.terms[taxName]) {
hit._source.terms[taxName].forEach((currentTerm) => {
if (currentTerm.name.toLowerCase() === lowerCaseSearchTerm) {
toInsert[currentTerm.term_order] = hit;
retain = false;
}
});
}
return retain;
});
const orderedInserts = {};
Object.keys(toInsert)
.sort()
.forEach((key) => {
orderedInserts[key] = toInsert[key];
});
if (Object.keys(orderedInserts).length > 0) {
Object.keys(orderedInserts).forEach((key) => {
const insertItem = orderedInserts[key];
filteredHits.splice(key - 1, 0, insertItem);
});
}
return hits;
}
/**
* Add class to the form element while suggestions are being loaded
*
* @param {boolean} isLoading - whether suggestions are loading
* @param {Node} input - search input field
*/
function setFormIsLoading(isLoading, input) {
const form = input.closest('form');
if (isLoading) {
form.classList.add('is-loading');
} else {
form.classList.remove('is-loading');
}
}
/**
* init method called if the epas endpoint is defined
*/
function init() {
const epInputNodes = document.querySelectorAll(
`.ep-autosuggest, input[type="search"], .search-field, ${epas.selector}`,
);
// build the container into which we place the search results.
// These will be cloned later for each instance
// of autosuggest inputs
const epAutosuggest = document.createElement('div');
epAutosuggest.classList.add('ep-autosuggest');
const autosuggestList = document.createElement('ul');
autosuggestList.classList.add('autosuggest-list');
autosuggestList.setAttribute('role', 'listbox');
epAutosuggest.appendChild(autosuggestList);
// Build the auto-suggest containers
// excluding the facet search field
const epInputs = Array.from(epInputNodes).filter(
(node) => !node.classList.contains('facet-search'),
);
epInputs.forEach((input) => {
const epContainer = document.createElement('div');
epContainer.classList.add('ep-autosuggest-container');
// Disable autocomplete
input.setAttribute('autocomplete', 'off');
// insert the container - later we will place
// the input inside this container
input.insertAdjacentElement('afterend', epContainer);
// move the input inside the container
const form = input.closest('form');
const container = form.querySelector('.ep-autosuggest-container');
container.appendChild(input);
const clonedContainer = epAutosuggest.cloneNode(true);
input.insertAdjacentElement('afterend', clonedContainer);
// announce that this is has been done
const event = new CustomEvent('elasticpress.input.moved');
input.dispatchEvent(event);
});
if (epInputs.length > 0) {
epAutosuggest.setAttribute(
'style',
`
top: ${epInputs[0].offsetHeight - 1};
background-color: ${getComputedStyle(epInputs[0], 'background-color')}
`,
);
}
// to be used by the handleUpDown function
// to keep track of the currently selected result
let currentIndex;
// these are the keycodes we listen for in handleUpDown,
// and in handleKeyup
const keyCodes = [
38, // up
40, // down
13, // enter
];
/**
* Handles keyup event on the search input
*
* @param {event} event - keyup event
*/
const handleUpDown = (event) => {
if (!keyCodes.includes(event.keyCode)) {
return;
}
const input = event.target;
const container = findAncestorByClass(input, 'ep-autosuggest-container');
const suggestList = container.querySelector('.autosuggest-list');
const results = suggestList.children;
/**
* helper function to get the currently selected result
*
* @returns {number} index of the selected search result
*/
const getSelectedResultIndex = () => {
const resultsArr = Array.from(results);
return resultsArr.findIndex((result) => result.classList.contains('selected'));
};
/**
* helper function to deselect results
*/
const deSelectResults = () => {
results.forEach((result) => {
result.classList.remove('selected');
result.setAttribute('aria-selected', 'false');
});
};
/**
* helper function to selected the next result
*/
const selectNextResult = () => {
if (currentIndex >= 0) {
const el = results[currentIndex];
el.classList.add('selected');
el.setAttribute('aria-selected', 'true');
setInputActiveDescendant(el.id, input);
}
};
// select next or previous based on keyCode
// if enter, navigate to that element
switch (event.keyCode) {
case 38: // Up
// don't go less than the 0th index
currentIndex = currentIndex - 1 >= 0 ? currentIndex - 1 : 0;
deSelectResults();
break;
case 40: // Down
if (typeof currentIndex === 'undefined') {
// index is not yet defined, so let's
// start with the first one
currentIndex = 0;
} else {
const current = getSelectedResultIndex();
// check for existence of next result
if (results[current + 1]) {
currentIndex = current + 1;
deSelectResults();
}
}
break;
case 13: // Enter
if (results[currentIndex].classList.contains('selected')) {
// navigate to the item defined in the span's data-url attribute
selectItem(input, results[currentIndex].querySelector('.autosuggest-link'));
}
break;
default:
// No item selected
break;
}
// only check next element if up and down key pressed
if (results[currentIndex] && results[currentIndex].classList.contains('autosuggest-item')) {
selectNextResult();
} else {
deSelectResults();
}
// keep cursor from heading back to the beginning in the input
if (event.keyCode === 38) {
// return false;
event.preventDefault();
}
};
/**
* Calls the ajax request, and outputs the results.
* Called by the handleKeyup callback, debounced.
*
* @param {Node} input - search input field
*/
const fetchResults = async (input) => {
const searchText = input.value;
const placeholder = 'ep_autosuggest_placeholder';
// retrieves the PHP-genereated query to pass to ElasticSearch
const queryJSON = getJsonQuery();
if (queryJSON.error) {
return;
}
if (searchText.length >= 2) {
setFormIsLoading(true, input);
const query = buildSearchQuery(searchText, placeholder, queryJSON);
// fetch the results
const response = await esSearch(query, searchText);
if (response && response._shards && response._shards.successful > 0) {
const hits = checkForOrderedPosts(response.hits.hits, searchText);
if (hits.length === 0) {
hideAutosuggestBox();
} else {
updateAutosuggestBox(hits, input);
}
} else {
hideAutosuggestBox();
}
setFormIsLoading(false, input);
} else if (searchText.length === 0) {
hideAutosuggestBox();
}
};
/**
* Callback for keyup in Autosuggest container.
*
* Calls a debounced function to get the search results via
* ajax request.
*
* @param {event} event - keyup event
*/
const handleKeyup = (event) => {
event.preventDefault();
const { target, key, keyCode } = event;
if (key === 'Escape' || key === 'Esc' || keyCode === 27) {
hideAutosuggestBox();
toggleInputAria(false, target);
setInputActiveDescendant('', target);
return;
}
if (keyCodes.includes(keyCode) && target.value !== '') {
handleUpDown(event);
return;
}
const input = event.target;
const debounceFetchResults = debounce(fetchResults, 200);
debounceFetchResults(input);
};
/**
* Listen for any events:
*
* keyup
* send them for a query to the Elasticsearch server
* handle up and down keys to move between results
*
* blur
* hide the autosuggest box
*/
epInputs.forEach((input) => {
input.addEventListener('keyup', handleKeyup);
input.addEventListener('blur', function () {
window.setTimeout(hideAutosuggestBox, 200);
});
});
}