@@ -11,9 +11,6 @@ const SEARCH_MODAL_CLOSED = "closed";
11
11
12
12
let SEARCH_MODAL_STATE = SEARCH_MODAL_CLOSED ;
13
13
14
- // this is used to store the total result counts,
15
- // which includes all the sections and domains of all the pages.
16
- let COUNT = 0 ;
17
14
18
15
/**
19
16
* Debounce the function.
@@ -173,8 +170,9 @@ const _is_array = arr => {
173
170
*
174
171
* @param {Object } sectionData object containing the result data
175
172
* @param {String } page_link link of the main page. It is used to construct the section link
173
+ * @param {Number } id to be used in for this section
176
174
*/
177
- const get_section_html = ( sectionData , page_link ) => {
175
+ const get_section_html = ( sectionData , page_link , id ) => {
178
176
let section_template =
179
177
'<a href="<%= section_link %>"> \
180
178
<div class="outer_div_page_results" id="<%= section_id %>"> \
@@ -214,7 +212,7 @@ const get_section_html = (sectionData, page_link) => {
214
212
215
213
let section_link = `${ page_link } #${ sectionData . id } ` ;
216
214
217
- let section_id = "hit__" + COUNT ;
215
+ let section_id = "hit__" + id ;
218
216
219
217
let section_html = $u . template ( section_template , {
220
218
section_link : section_link ,
@@ -232,8 +230,9 @@ const get_section_html = (sectionData, page_link) => {
232
230
*
233
231
* @param {Object } domainData object containing the result data
234
232
* @param {String } page_link link of the main page. It is used to construct the section link
233
+ * @param {Number } id to be used in for this section
235
234
*/
236
- const get_domain_html = ( domainData , page_link ) => {
235
+ const get_domain_html = ( domainData , page_link , id ) => {
237
236
let domain_template =
238
237
'<a href="<%= domain_link %>"> \
239
238
<div class="outer_div_page_results" id="<%= domain_id %>"> \
@@ -262,7 +261,7 @@ const get_domain_html = (domainData, page_link) => {
262
261
domain_content = highlights . content [ 0 ] ;
263
262
}
264
263
265
- let domain_id = "hit__" + COUNT ;
264
+ let domain_id = "hit__" + id ;
266
265
domain_role_name = "[" + domain_role_name + "]" ;
267
266
268
267
let domain_html = $u . template ( domain_template , {
@@ -280,9 +279,11 @@ const get_domain_html = (domainData, page_link) => {
280
279
* Generate search results for a single page.
281
280
*
282
281
* @param {Object } resultData search results of a page
282
+ * @param {String } projectName
283
+ * @param {Number } id from the last section
283
284
* @return {Object } a <div> node with the results of a single page
284
285
*/
285
- const generateSingleResult = ( resultData , projectName ) => {
286
+ const generateSingleResult = ( resultData , projectName , id ) => {
286
287
let content = createDomNode ( "div" ) ;
287
288
288
289
let page_link_template =
@@ -324,18 +325,20 @@ const generateSingleResult = (resultData, projectName) => {
324
325
325
326
for ( let i = 0 ; i < resultData . blocks . length ; ++ i ) {
326
327
let block = resultData . blocks [ i ] ;
327
- COUNT += 1 ;
328
328
let html_structure = "" ;
329
329
330
+ id += 1 ;
330
331
if ( block . type === "section" ) {
331
332
html_structure = get_section_html (
332
333
block ,
333
- page_link
334
+ page_link ,
335
+ id ,
334
336
) ;
335
337
} else if ( block . type === "domain" ) {
336
338
html_structure = get_domain_html (
337
339
block ,
338
- page_link
340
+ page_link ,
341
+ id ,
339
342
) ;
340
343
}
341
344
content . innerHTML += html_structure ;
@@ -356,15 +359,18 @@ const generateSuggestionsList = (data, projectName) => {
356
359
} ) ;
357
360
358
361
let max_results = Math . min ( MAX_SUGGESTIONS , data . results . length ) ;
362
+ let id = 0 ;
359
363
for ( let i = 0 ; i < max_results ; ++ i ) {
360
364
let search_result_single = createDomNode ( "div" , {
361
365
class : "search__result__single"
362
366
} ) ;
363
367
364
- let content = generateSingleResult ( data . results [ i ] , projectName ) ;
368
+ let content = generateSingleResult ( data . results [ i ] , projectName , id ) ;
365
369
366
370
search_result_single . appendChild ( content ) ;
367
371
search_result_box . appendChild ( search_result_single ) ;
372
+
373
+ id += data . results [ i ] . blocks . length ;
368
374
}
369
375
return search_result_box ;
370
376
} ;
@@ -373,7 +379,7 @@ const generateSuggestionsList = (data, projectName) => {
373
379
* Removes .active class from all the suggestions.
374
380
*/
375
381
const removeAllActive = ( ) => {
376
- const results = document . querySelectorAll ( ".outer_div_page_results" ) ;
382
+ const results = document . querySelectorAll ( ".outer_div_page_results.active " ) ;
377
383
const results_arr = Object . keys ( results ) . map ( i => results [ i ] ) ;
378
384
for ( let i = 1 ; i <= results_arr . length ; ++ i ) {
379
385
results_arr [ i - 1 ] . classList . remove ( "active" ) ;
@@ -382,13 +388,12 @@ const removeAllActive = () => {
382
388
383
389
/**
384
390
* Add .active class to the search suggestion
385
- * corresponding to serial number current_focus',
386
- * and scroll to that suggestion smoothly.
391
+ * corresponding to `id`, and scroll to that suggestion smoothly.
387
392
*
388
- * @param {Number } current_focus serial no. of suggestions which will be active
393
+ * @param {Number } id of the suggestion to activate
389
394
*/
390
- const addActive = current_focus => {
391
- const current_item = document . querySelector ( "#hit__" + current_focus ) ;
395
+ const addActive = ( id ) => {
396
+ const current_item = document . querySelector ( "#hit__" + id ) ;
392
397
// in case of no results or any error,
393
398
// current_item will not be found in the DOM.
394
399
if ( current_item !== null ) {
@@ -401,6 +406,51 @@ const addActive = current_focus => {
401
406
}
402
407
} ;
403
408
409
+
410
+ /*
411
+ * Select next/previous result.
412
+ * Go to the first result if already in the last result,
413
+ * or to the last result if already in the first result.
414
+ *
415
+ * @param {Boolean } forward.
416
+ */
417
+ const selectNextResult = ( forward ) => {
418
+ const all = document . querySelectorAll ( ".outer_div_page_results" ) ;
419
+ const current = document . querySelector ( ".outer_div_page_results.active" ) ;
420
+
421
+ let next_id = 1 ;
422
+ let last_id = 1 ;
423
+
424
+ if ( all . length > 0 ) {
425
+ let last = all [ all . length - 1 ] ;
426
+ if ( last . id !== null ) {
427
+ let match = last . id . match ( / \d + / ) ;
428
+ if ( match !== null ) {
429
+ last_id = Number ( match [ 0 ] ) ;
430
+ }
431
+ }
432
+ }
433
+
434
+ if ( current !== null && current . id !== null ) {
435
+ let match = current . id . match ( / \d + / ) ;
436
+ if ( match !== null ) {
437
+ next_id = Number ( match [ 0 ] ) ;
438
+ next_id += forward ? 1 : - 1 ;
439
+ }
440
+ }
441
+
442
+ // Cycle to the first or last result.
443
+ if ( next_id <= 0 ) {
444
+ next_id = last_id ;
445
+ } else if ( next_id > last_id ) {
446
+ next_id = 1 ;
447
+ }
448
+
449
+ removeAllActive ( ) ;
450
+ addActive ( next_id ) ;
451
+ } ;
452
+
453
+
404
454
/**
405
455
* Returns initial search input field,
406
456
* which is already present in the docs.
@@ -641,10 +691,6 @@ window.addEventListener("DOMContentLoaded", evt => {
641
691
) ;
642
692
let cross_icon = document . querySelector ( ".search__cross" ) ;
643
693
644
- // this denotes the search suggestion which is currently selected
645
- // via tha ArrowUp/ArrowDown keys.
646
- let current_focus = 0 ;
647
-
648
694
// this stores the current request.
649
695
let current_request = null ;
650
696
@@ -655,7 +701,6 @@ window.addEventListener("DOMContentLoaded", evt => {
655
701
656
702
search_outer_input . addEventListener ( "input" , e => {
657
703
let search_query = getSearchTerm ( ) ;
658
- COUNT = 0 ;
659
704
660
705
let search_params = {
661
706
q : search_query ,
@@ -694,23 +739,13 @@ window.addEventListener("DOMContentLoaded", evt => {
694
739
// if "ArrowDown is pressed"
695
740
if ( e . keyCode === 40 ) {
696
741
e . preventDefault ( ) ;
697
- current_focus += 1 ;
698
- if ( current_focus > COUNT ) {
699
- current_focus = 1 ;
700
- }
701
- removeAllActive ( ) ;
702
- addActive ( current_focus ) ;
742
+ selectNextResult ( true ) ;
703
743
}
704
744
705
745
// if "ArrowUp" is pressed.
706
746
if ( e . keyCode === 38 ) {
707
747
e . preventDefault ( ) ;
708
- current_focus -= 1 ;
709
- if ( current_focus < 1 ) {
710
- current_focus = COUNT ;
711
- }
712
- removeAllActive ( ) ;
713
- addActive ( current_focus ) ;
748
+ selectNextResult ( false ) ;
714
749
}
715
750
716
751
// if "Enter" key is pressed.
0 commit comments