@@ -5,10 +5,6 @@ const ANIMATION_TIME = 200;
5
5
const FETCH_RESULTS_DELAY = 250 ;
6
6
const CLEAR_RESULTS_DELAY = 300 ;
7
7
8
- // this is used to store the total result counts,
9
- // which includes all the sections and domains of all the pages.
10
- let COUNT = 0 ;
11
-
12
8
/**
13
9
* Debounce the function.
14
10
* Usage::
@@ -179,8 +175,9 @@ const _is_array = arr => {
179
175
*
180
176
* @param {Object } sectionData object containing the result data
181
177
* @param {String } page_link link of the main page. It is used to construct the section link
178
+ * @param {Number } id to be used in for this section
182
179
*/
183
- const get_section_html = ( sectionData , page_link ) => {
180
+ const get_section_html = ( sectionData , page_link , id ) => {
184
181
let section_template =
185
182
'<a href="<%= section_link %>"> \
186
183
<div class="outer_div_page_results" id="<%= section_id %>"> \
@@ -220,7 +217,7 @@ const get_section_html = (sectionData, page_link) => {
220
217
221
218
let section_link = `${ page_link } #${ sectionData . id } ` ;
222
219
223
- let section_id = "hit__" + COUNT ;
220
+ let section_id = "hit__" + id ;
224
221
225
222
let section_html = $u . template ( section_template , {
226
223
section_link : section_link ,
@@ -238,8 +235,9 @@ const get_section_html = (sectionData, page_link) => {
238
235
*
239
236
* @param {Object } domainData object containing the result data
240
237
* @param {String } page_link link of the main page. It is used to construct the section link
238
+ * @param {Number } id to be used in for this section
241
239
*/
242
- const get_domain_html = ( domainData , page_link ) => {
240
+ const get_domain_html = ( domainData , page_link , id ) => {
243
241
let domain_template =
244
242
'<a href="<%= domain_link %>"> \
245
243
<div class="outer_div_page_results" id="<%= domain_id %>"> \
@@ -268,7 +266,7 @@ const get_domain_html = (domainData, page_link) => {
268
266
domain_content = highlights . content [ 0 ] ;
269
267
}
270
268
271
- let domain_id = "hit__" + COUNT ;
269
+ let domain_id = "hit__" + id ;
272
270
domain_role_name = "[" + domain_role_name + "]" ;
273
271
274
272
let domain_html = $u . template ( domain_template , {
@@ -286,9 +284,11 @@ const get_domain_html = (domainData, page_link) => {
286
284
* Generate search results for a single page.
287
285
*
288
286
* @param {Object } resultData search results of a page
287
+ * @param {String } projectName
288
+ * @param {Number } id from the last section
289
289
* @return {Object } a <div> node with the results of a single page
290
290
*/
291
- const generateSingleResult = ( resultData , projectName ) => {
291
+ const generateSingleResult = ( resultData , projectName , id ) => {
292
292
let content = createDomNode ( "div" ) ;
293
293
294
294
let page_link_template =
@@ -330,18 +330,20 @@ const generateSingleResult = (resultData, projectName) => {
330
330
331
331
for ( let i = 0 ; i < resultData . blocks . length ; ++ i ) {
332
332
let block = resultData . blocks [ i ] ;
333
- COUNT += 1 ;
334
333
let html_structure = "" ;
335
334
335
+ id += 1 ;
336
336
if ( block . type === "section" ) {
337
337
html_structure = get_section_html (
338
338
block ,
339
- page_link
339
+ page_link ,
340
+ id ,
340
341
) ;
341
342
} else if ( block . type === "domain" ) {
342
343
html_structure = get_domain_html (
343
344
block ,
344
- page_link
345
+ page_link ,
346
+ id ,
345
347
) ;
346
348
}
347
349
content . innerHTML += html_structure ;
@@ -362,15 +364,18 @@ const generateSuggestionsList = (data, projectName) => {
362
364
} ) ;
363
365
364
366
let max_results = Math . min ( MAX_SUGGESTIONS , data . results . length ) ;
367
+ let id = 0 ;
365
368
for ( let i = 0 ; i < max_results ; ++ i ) {
366
369
let search_result_single = createDomNode ( "div" , {
367
370
class : "search__result__single"
368
371
} ) ;
369
372
370
- let content = generateSingleResult ( data . results [ i ] , projectName ) ;
373
+ let content = generateSingleResult ( data . results [ i ] , projectName , id ) ;
371
374
372
375
search_result_single . appendChild ( content ) ;
373
376
search_result_box . appendChild ( search_result_single ) ;
377
+
378
+ id += data . results [ i ] . blocks . length ;
374
379
}
375
380
return search_result_box ;
376
381
} ;
@@ -379,7 +384,7 @@ const generateSuggestionsList = (data, projectName) => {
379
384
* Removes .active class from all the suggestions.
380
385
*/
381
386
const removeAllActive = ( ) => {
382
- const results = document . querySelectorAll ( ".outer_div_page_results" ) ;
387
+ const results = document . querySelectorAll ( ".outer_div_page_results.active " ) ;
383
388
const results_arr = Object . keys ( results ) . map ( i => results [ i ] ) ;
384
389
for ( let i = 1 ; i <= results_arr . length ; ++ i ) {
385
390
results_arr [ i - 1 ] . classList . remove ( "active" ) ;
@@ -388,13 +393,12 @@ const removeAllActive = () => {
388
393
389
394
/**
390
395
* Add .active class to the search suggestion
391
- * corresponding to serial number current_focus',
392
- * and scroll to that suggestion smoothly.
396
+ * corresponding to `id`, and scroll to that suggestion smoothly.
393
397
*
394
- * @param {Number } current_focus serial no. of suggestions which will be active
398
+ * @param {Number } id of the suggestion to activate
395
399
*/
396
- const addActive = current_focus => {
397
- const current_item = document . querySelector ( "#hit__" + current_focus ) ;
400
+ const addActive = ( id ) => {
401
+ const current_item = document . querySelector ( "#hit__" + id ) ;
398
402
// in case of no results or any error,
399
403
// current_item will not be found in the DOM.
400
404
if ( current_item !== null ) {
@@ -407,6 +411,51 @@ const addActive = current_focus => {
407
411
}
408
412
} ;
409
413
414
+
415
+ /*
416
+ * Select next/previous result.
417
+ * Go to the first result if already in the last result,
418
+ * or to the last result if already in the first result.
419
+ *
420
+ * @param {Boolean } forward.
421
+ */
422
+ const selectNextResult = ( forward ) => {
423
+ const all = document . querySelectorAll ( ".outer_div_page_results" ) ;
424
+ const current = document . querySelector ( ".outer_div_page_results.active" ) ;
425
+
426
+ let next_id = 1 ;
427
+ let last_id = 1 ;
428
+
429
+ if ( all . length > 0 ) {
430
+ let last = all [ all . length - 1 ] ;
431
+ if ( last . id !== null ) {
432
+ let match = last . id . match ( / \d + / ) ;
433
+ if ( match !== null ) {
434
+ last_id = Number ( match [ 0 ] ) ;
435
+ }
436
+ }
437
+ }
438
+
439
+ if ( current !== null && current . id !== null ) {
440
+ let match = current . id . match ( / \d + / ) ;
441
+ if ( match !== null ) {
442
+ next_id = Number ( match [ 0 ] ) ;
443
+ next_id += forward ? 1 : - 1 ;
444
+ }
445
+ }
446
+
447
+ // Cycle to the first or last result.
448
+ if ( next_id <= 0 ) {
449
+ next_id = last_id ;
450
+ } else if ( next_id > last_id ) {
451
+ next_id = 1 ;
452
+ }
453
+
454
+ removeAllActive ( ) ;
455
+ addActive ( next_id ) ;
456
+ } ;
457
+
458
+
410
459
/**
411
460
* Returns initial search input field,
412
461
* which is already present in the docs.
@@ -643,10 +692,6 @@ window.addEventListener("DOMContentLoaded", evt => {
643
692
) ;
644
693
let cross_icon = document . querySelector ( ".search__cross" ) ;
645
694
646
- // this denotes the search suggestion which is currently selected
647
- // via tha ArrowUp/ArrowDown keys.
648
- let current_focus = 0 ;
649
-
650
695
// this stores the current request.
651
696
let current_request = null ;
652
697
@@ -657,7 +702,6 @@ window.addEventListener("DOMContentLoaded", evt => {
657
702
658
703
search_outer_input . addEventListener ( "input" , e => {
659
704
let search_query = getSearchTerm ( ) ;
660
- COUNT = 0 ;
661
705
662
706
let search_params = {
663
707
q : search_query ,
@@ -696,23 +740,13 @@ window.addEventListener("DOMContentLoaded", evt => {
696
740
// if "ArrowDown is pressed"
697
741
if ( e . keyCode === 40 ) {
698
742
e . preventDefault ( ) ;
699
- current_focus += 1 ;
700
- if ( current_focus > COUNT ) {
701
- current_focus = 1 ;
702
- }
703
- removeAllActive ( ) ;
704
- addActive ( current_focus ) ;
743
+ selectNextResult ( true ) ;
705
744
}
706
745
707
746
// if "ArrowUp" is pressed.
708
747
if ( e . keyCode === 38 ) {
709
748
e . preventDefault ( ) ;
710
- current_focus -= 1 ;
711
- if ( current_focus < 1 ) {
712
- current_focus = COUNT ;
713
- }
714
- removeAllActive ( ) ;
715
- addActive ( current_focus ) ;
749
+ selectNextResult ( false ) ;
716
750
}
717
751
718
752
// if "Enter" key is pressed.
0 commit comments