Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 23e1efb

Browse files
committed
Remove global count
1 parent 2b91726 commit 23e1efb

File tree

2 files changed

+71
-36
lines changed

2 files changed

+71
-36
lines changed

sphinx_search/static/js/rtd_sphinx_search.js

+70-35
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ const SEARCH_MODAL_CLOSED = "closed";
1111

1212
let SEARCH_MODAL_STATE = SEARCH_MODAL_CLOSED;
1313

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;
1714

1815
/**
1916
* Debounce the function.
@@ -173,8 +170,9 @@ const _is_array = arr => {
173170
*
174171
* @param {Object} sectionData object containing the result data
175172
* @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
176174
*/
177-
const get_section_html = (sectionData, page_link) => {
175+
const get_section_html = (sectionData, page_link, id) => {
178176
let section_template =
179177
'<a href="<%= section_link %>"> \
180178
<div class="outer_div_page_results" id="<%= section_id %>"> \
@@ -214,7 +212,7 @@ const get_section_html = (sectionData, page_link) => {
214212

215213
let section_link = `${page_link}#${sectionData.id}`;
216214

217-
let section_id = "hit__" + COUNT;
215+
let section_id = "hit__" + id;
218216

219217
let section_html = $u.template(section_template, {
220218
section_link: section_link,
@@ -232,8 +230,9 @@ const get_section_html = (sectionData, page_link) => {
232230
*
233231
* @param {Object} domainData object containing the result data
234232
* @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
235234
*/
236-
const get_domain_html = (domainData, page_link) => {
235+
const get_domain_html = (domainData, page_link, id) => {
237236
let domain_template =
238237
'<a href="<%= domain_link %>"> \
239238
<div class="outer_div_page_results" id="<%= domain_id %>"> \
@@ -262,7 +261,7 @@ const get_domain_html = (domainData, page_link) => {
262261
domain_content = highlights.content[0];
263262
}
264263

265-
let domain_id = "hit__" + COUNT;
264+
let domain_id = "hit__" + id;
266265
domain_role_name = "[" + domain_role_name + "]";
267266

268267
let domain_html = $u.template(domain_template, {
@@ -280,9 +279,11 @@ const get_domain_html = (domainData, page_link) => {
280279
* Generate search results for a single page.
281280
*
282281
* @param {Object} resultData search results of a page
282+
* @param {String} projectName
283+
* @param {Number} id from the last section
283284
* @return {Object} a <div> node with the results of a single page
284285
*/
285-
const generateSingleResult = (resultData, projectName) => {
286+
const generateSingleResult = (resultData, projectName, id) => {
286287
let content = createDomNode("div");
287288

288289
let page_link_template =
@@ -324,18 +325,20 @@ const generateSingleResult = (resultData, projectName) => {
324325

325326
for (let i = 0; i < resultData.blocks.length; ++i) {
326327
let block = resultData.blocks[i];
327-
COUNT += 1;
328328
let html_structure = "";
329329

330+
id += 1;
330331
if (block.type === "section") {
331332
html_structure = get_section_html(
332333
block,
333-
page_link
334+
page_link,
335+
id,
334336
);
335337
} else if (block.type === "domain") {
336338
html_structure = get_domain_html(
337339
block,
338-
page_link
340+
page_link,
341+
id,
339342
);
340343
}
341344
content.innerHTML += html_structure;
@@ -356,15 +359,18 @@ const generateSuggestionsList = (data, projectName) => {
356359
});
357360

358361
let max_results = Math.min(MAX_SUGGESTIONS, data.results.length);
362+
let id = 0;
359363
for (let i = 0; i < max_results; ++i) {
360364
let search_result_single = createDomNode("div", {
361365
class: "search__result__single"
362366
});
363367

364-
let content = generateSingleResult(data.results[i], projectName);
368+
let content = generateSingleResult(data.results[i], projectName, id);
365369

366370
search_result_single.appendChild(content);
367371
search_result_box.appendChild(search_result_single);
372+
373+
id += data.results[i].blocks.length;
368374
}
369375
return search_result_box;
370376
};
@@ -373,7 +379,7 @@ const generateSuggestionsList = (data, projectName) => {
373379
* Removes .active class from all the suggestions.
374380
*/
375381
const removeAllActive = () => {
376-
const results = document.querySelectorAll(".outer_div_page_results");
382+
const results = document.querySelectorAll(".outer_div_page_results.active");
377383
const results_arr = Object.keys(results).map(i => results[i]);
378384
for (let i = 1; i <= results_arr.length; ++i) {
379385
results_arr[i - 1].classList.remove("active");
@@ -382,13 +388,12 @@ const removeAllActive = () => {
382388

383389
/**
384390
* 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.
387392
*
388-
* @param {Number} current_focus serial no. of suggestions which will be active
393+
* @param {Number} id of the suggestion to activate
389394
*/
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);
392397
// in case of no results or any error,
393398
// current_item will not be found in the DOM.
394399
if (current_item !== null) {
@@ -401,6 +406,51 @@ const addActive = current_focus => {
401406
}
402407
};
403408

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+
404454
/**
405455
* Returns initial search input field,
406456
* which is already present in the docs.
@@ -641,10 +691,6 @@ window.addEventListener("DOMContentLoaded", evt => {
641691
);
642692
let cross_icon = document.querySelector(".search__cross");
643693

644-
// this denotes the search suggestion which is currently selected
645-
// via tha ArrowUp/ArrowDown keys.
646-
let current_focus = 0;
647-
648694
// this stores the current request.
649695
let current_request = null;
650696

@@ -655,7 +701,6 @@ window.addEventListener("DOMContentLoaded", evt => {
655701

656702
search_outer_input.addEventListener("input", e => {
657703
let search_query = getSearchTerm();
658-
COUNT = 0;
659704

660705
let search_params = {
661706
q: search_query,
@@ -694,23 +739,13 @@ window.addEventListener("DOMContentLoaded", evt => {
694739
// if "ArrowDown is pressed"
695740
if (e.keyCode === 40) {
696741
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);
703743
}
704744

705745
// if "ArrowUp" is pressed.
706746
if (e.keyCode === 38) {
707747
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);
714749
}
715750

716751
// if "Enter" key is pressed.

0 commit comments

Comments
 (0)