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

Commit 166d5ae

Browse files
authored
Merge pull request #25 from readthedocs/link-to-full-page
Link To Search UI
2 parents 1b2c81a + 0e94954 commit 166d5ae

File tree

4 files changed

+314
-108
lines changed

4 files changed

+314
-108
lines changed

sphinx_search/_static/js/rtd_sphinx_search.js

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,64 @@ const debounce = (func, wait) => {
4646
/**
4747
* Take an object as parameter and convert it to
4848
* url params string.
49-
* Eg. if obj = { 'a': 1, 'b': 2 }, then it will return
50-
* the string a=1&b=2.
49+
* Eg. if obj = { 'a': 1, 'b': 2, 'c': ['hello', 'world'] }, then it will return
50+
* the string a=1&b=2&c=hello,world
5151
*
5252
* @param {Object} obj the object to be converted
53-
* @return {String} object in url params form
53+
* @return {String|Array} object in url params form
5454
*/
5555
const convertObjToUrlParams = obj => {
56-
const params = Object.keys(obj)
57-
.map(function(key) {
58-
const s = key + "=" + obj[key];
56+
let params = Object.keys(obj).map(function(key) {
57+
if (_is_string(key)) {
58+
const s = key + "=" + encodeURI(obj[key]);
5959
return s;
60-
})
61-
.join("&");
62-
return params;
60+
}
61+
});
62+
63+
// removing empty strings from the 'params' array
64+
let final_params = [];
65+
for (let i = 0; i < params.length; ++i) {
66+
if (_is_string(params[i])) {
67+
final_params.push(params[i]);
68+
}
69+
}
70+
if (final_params.length === 1) {
71+
return final_params[0];
72+
} else {
73+
let final_url_params = final_params.join("&");
74+
return final_url_params;
75+
}
76+
};
77+
78+
/**
79+
* Adds/removes "rtd_search" url parameter to the url.
80+
*/
81+
const updateUrl = () => {
82+
let origin = window.location.origin;
83+
let path = window.location.pathname;
84+
let url_params = $.getQueryParameters();
85+
let hash = window.location.hash;
86+
87+
// SEARCH_QUERY should not be an empty string
88+
if (_is_string(SEARCH_QUERY)) {
89+
url_params.rtd_search = SEARCH_QUERY;
90+
} else {
91+
delete url_params.rtd_search;
92+
}
93+
94+
let window_location_search = convertObjToUrlParams(url_params) + hash;
95+
96+
// this happens during the tests,
97+
// when window.location.origin is "null" in Firefox
98+
// then correct URL is contained by window.location.pathname
99+
// which starts with "file://"
100+
let url = path + "?" + window_location_search;
101+
if (origin.substring(0, 4) === "http") {
102+
url = origin + url;
103+
}
104+
105+
// update url
106+
window.history.pushState({}, null, url);
63107
};
64108

65109
/**
@@ -552,8 +596,10 @@ const generateAndReturnInitialHtml = () => {
552596

553597
/**
554598
* Opens the search modal.
599+
*
600+
* @param {String} custom_query if a custom query is provided, initialise the value of input field with it
555601
*/
556-
const showSearchModal = () => {
602+
const showSearchModal = custom_query => {
557603
// removes previous results (if there are any).
558604
removeResults();
559605

@@ -568,7 +614,14 @@ const showSearchModal = () => {
568614
".search__outer__input"
569615
);
570616
if (search_outer_input !== null) {
571-
search_outer_input.value = "";
617+
if (
618+
typeof custom_query !== "undefined" &&
619+
_is_string(custom_query)
620+
) {
621+
search_outer_input.value = custom_query;
622+
} else {
623+
search_outer_input.value = "";
624+
}
572625
search_outer_input.focus();
573626
}
574627
});
@@ -588,6 +641,12 @@ const removeSearchModal = () => {
588641
search_outer_input.blur();
589642
}
590643

644+
// reset SEARCH_QUERY
645+
SEARCH_QUERY = "";
646+
647+
// update url (remove 'rtd_search' param)
648+
updateUrl();
649+
591650
$(".search__outer__wrapper").fadeOut(400);
592651
};
593652

@@ -628,7 +687,7 @@ window.addEventListener("DOMContentLoaded", evt => {
628687
COUNT = 0;
629688

630689
let search_params = {
631-
q: encodeURIComponent(SEARCH_QUERY),
690+
q: SEARCH_QUERY,
632691
project: project,
633692
version: version,
634693
language: language
@@ -653,6 +712,9 @@ window.addEventListener("DOMContentLoaded", evt => {
653712
// is debounced here.
654713
debounce(removeResults, 600)();
655714
}
715+
716+
// update URL
717+
updateUrl();
656718
});
657719

658720
search_outer_input.addEventListener("keydown", e => {
@@ -723,5 +785,19 @@ window.addEventListener("DOMContentLoaded", evt => {
723785
removeSearchModal();
724786
}
725787
});
788+
789+
// if "rtd_search" is present in URL parameters,
790+
// then open the search modal and show the results
791+
// for the value of "rtd_search"
792+
let url_params = $.getQueryParameters();
793+
if (_is_array(url_params.rtd_search)) {
794+
let query = decodeURIComponent(url_params.rtd_search);
795+
showSearchModal(query);
796+
search_outer_input.value = query;
797+
798+
let event = document.createEvent("Event");
799+
event.initEvent("input", true, true);
800+
search_outer_input.dispatchEvent(event);
801+
}
726802
}
727803
});

0 commit comments

Comments
 (0)