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

Commit 5eff860

Browse files
authored
Remove global count (#69)
1 parent 3fa49a5 commit 5eff860

File tree

3 files changed

+73
-39
lines changed

3 files changed

+73
-39
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
"@babel/preset-env": "^7.4.5",
2222
"browserslist": "^4.6.1",
2323
"del": "^4.1.1",
24+
"github-changelog": "git+https://github.com/agjohnson/github-changelog.git",
2425
"gulp": "^3.9.1",
2526
"gulp-autoprefixer": "^6.1.0",
2627
"gulp-babel": "^8.0.0",
2728
"gulp-csso": "^3.0.1",
2829
"gulp-rename": "^1.4.0",
2930
"gulp-uglify": "^3.0.2",
30-
"run-sequence": "^2.2.1",
31-
"github-changelog": "git+https://github.com/agjohnson/github-changelog.git"
31+
"run-sequence": "^2.2.1"
3232
},
3333
"browserslist": [
3434
"last 10 versions",

sphinx_search/static/js/rtd_sphinx_search.js

+70-36
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ const ANIMATION_TIME = 200;
55
const FETCH_RESULTS_DELAY = 250;
66
const CLEAR_RESULTS_DELAY = 300;
77

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-
128
/**
139
* Debounce the function.
1410
* Usage::
@@ -179,8 +175,9 @@ const _is_array = arr => {
179175
*
180176
* @param {Object} sectionData object containing the result data
181177
* @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
182179
*/
183-
const get_section_html = (sectionData, page_link) => {
180+
const get_section_html = (sectionData, page_link, id) => {
184181
let section_template =
185182
'<a href="<%= section_link %>"> \
186183
<div class="outer_div_page_results" id="<%= section_id %>"> \
@@ -220,7 +217,7 @@ const get_section_html = (sectionData, page_link) => {
220217

221218
let section_link = `${page_link}#${sectionData.id}`;
222219

223-
let section_id = "hit__" + COUNT;
220+
let section_id = "hit__" + id;
224221

225222
let section_html = $u.template(section_template, {
226223
section_link: section_link,
@@ -238,8 +235,9 @@ const get_section_html = (sectionData, page_link) => {
238235
*
239236
* @param {Object} domainData object containing the result data
240237
* @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
241239
*/
242-
const get_domain_html = (domainData, page_link) => {
240+
const get_domain_html = (domainData, page_link, id) => {
243241
let domain_template =
244242
'<a href="<%= domain_link %>"> \
245243
<div class="outer_div_page_results" id="<%= domain_id %>"> \
@@ -268,7 +266,7 @@ const get_domain_html = (domainData, page_link) => {
268266
domain_content = highlights.content[0];
269267
}
270268

271-
let domain_id = "hit__" + COUNT;
269+
let domain_id = "hit__" + id;
272270
domain_role_name = "[" + domain_role_name + "]";
273271

274272
let domain_html = $u.template(domain_template, {
@@ -286,9 +284,11 @@ const get_domain_html = (domainData, page_link) => {
286284
* Generate search results for a single page.
287285
*
288286
* @param {Object} resultData search results of a page
287+
* @param {String} projectName
288+
* @param {Number} id from the last section
289289
* @return {Object} a <div> node with the results of a single page
290290
*/
291-
const generateSingleResult = (resultData, projectName) => {
291+
const generateSingleResult = (resultData, projectName, id) => {
292292
let content = createDomNode("div");
293293

294294
let page_link_template =
@@ -330,18 +330,20 @@ const generateSingleResult = (resultData, projectName) => {
330330

331331
for (let i = 0; i < resultData.blocks.length; ++i) {
332332
let block = resultData.blocks[i];
333-
COUNT += 1;
334333
let html_structure = "";
335334

335+
id += 1;
336336
if (block.type === "section") {
337337
html_structure = get_section_html(
338338
block,
339-
page_link
339+
page_link,
340+
id,
340341
);
341342
} else if (block.type === "domain") {
342343
html_structure = get_domain_html(
343344
block,
344-
page_link
345+
page_link,
346+
id,
345347
);
346348
}
347349
content.innerHTML += html_structure;
@@ -362,15 +364,18 @@ const generateSuggestionsList = (data, projectName) => {
362364
});
363365

364366
let max_results = Math.min(MAX_SUGGESTIONS, data.results.length);
367+
let id = 0;
365368
for (let i = 0; i < max_results; ++i) {
366369
let search_result_single = createDomNode("div", {
367370
class: "search__result__single"
368371
});
369372

370-
let content = generateSingleResult(data.results[i], projectName);
373+
let content = generateSingleResult(data.results[i], projectName, id);
371374

372375
search_result_single.appendChild(content);
373376
search_result_box.appendChild(search_result_single);
377+
378+
id += data.results[i].blocks.length;
374379
}
375380
return search_result_box;
376381
};
@@ -379,7 +384,7 @@ const generateSuggestionsList = (data, projectName) => {
379384
* Removes .active class from all the suggestions.
380385
*/
381386
const removeAllActive = () => {
382-
const results = document.querySelectorAll(".outer_div_page_results");
387+
const results = document.querySelectorAll(".outer_div_page_results.active");
383388
const results_arr = Object.keys(results).map(i => results[i]);
384389
for (let i = 1; i <= results_arr.length; ++i) {
385390
results_arr[i - 1].classList.remove("active");
@@ -388,13 +393,12 @@ const removeAllActive = () => {
388393

389394
/**
390395
* 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.
393397
*
394-
* @param {Number} current_focus serial no. of suggestions which will be active
398+
* @param {Number} id of the suggestion to activate
395399
*/
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);
398402
// in case of no results or any error,
399403
// current_item will not be found in the DOM.
400404
if (current_item !== null) {
@@ -407,6 +411,51 @@ const addActive = current_focus => {
407411
}
408412
};
409413

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+
410459
/**
411460
* Returns initial search input field,
412461
* which is already present in the docs.
@@ -643,10 +692,6 @@ window.addEventListener("DOMContentLoaded", evt => {
643692
);
644693
let cross_icon = document.querySelector(".search__cross");
645694

646-
// this denotes the search suggestion which is currently selected
647-
// via tha ArrowUp/ArrowDown keys.
648-
let current_focus = 0;
649-
650695
// this stores the current request.
651696
let current_request = null;
652697

@@ -657,7 +702,6 @@ window.addEventListener("DOMContentLoaded", evt => {
657702

658703
search_outer_input.addEventListener("input", e => {
659704
let search_query = getSearchTerm();
660-
COUNT = 0;
661705

662706
let search_params = {
663707
q: search_query,
@@ -696,23 +740,13 @@ window.addEventListener("DOMContentLoaded", evt => {
696740
// if "ArrowDown is pressed"
697741
if (e.keyCode === 40) {
698742
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);
705744
}
706745

707746
// if "ArrowUp" is pressed.
708747
if (e.keyCode === 38) {
709748
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);
716750
}
717751

718752
// if "Enter" key is pressed.

0 commit comments

Comments
 (0)