JavaScript API Reference¶
Following are the functions that are defined in rtd_sphinx_search.js,
-
debounce
(func, wait)¶ Debounce the function. Usage:
let func = debounce(() => console.log("Hello World"), 3000); // calling the func func(); //cancelling the execution of the func (if not executed) func.cancel();
Arguments: - func (function) – function to be debounced
- wait (Number) – time to wait before running func (in miliseconds)
Returns: function – debounced function
-
convertObjToUrlParams
(obj)¶ Take an object as parameter and convert it to url params string.
Eg. if
obj = { 'a': 1, 'b': 2, 'c': ['hello', 'world'] }
, then it will return the stringa=1&b=2&c=hello,world
Arguments: - obj (Object) – the object to be converted
Returns: String|Array – object in url params form
-
updateUrl
()¶ Adds/removes “rtd_search” url parameter to the url.
-
createDomNode
(nodeName, attributes)¶ Create and return DOM nodes with passed attributes.
Arguments: - nodeName (String) – name of the node
- attributes (Object) – obj of attributes to be assigned to the node
Returns: Object – dom node with attributes
-
_is_string
(data)¶ Checks if data type is “string” or not
Arguments: - data (*) – data whose data-type is to be checked
Returns: Boolean – ‘true’ if type is “string” and length is > 0
-
_is_array
(data)¶ Checks if data type is a non-empty array
Arguments: - data (*) – data whose type is to be checked
Returns: Boolean – returns true if data is non-empty array, else returns false
-
get_section_html
(sectionData, page_link)¶ Generate and return html structure for a page section result.
Arguments: - sectionData (Object) – object containing the result data
- page_link (String) – link of the main page. It is used to construct the section link
-
get_domain_html
(domainData, page_link)¶ Generate and return html structure for a sphinx domain result.
Arguments: - domainData (Object) – object containing the result data
- page_link (String) – link of the main page. It is used to construct the section link
-
generateSingleResult
(resultData)¶ Generate search results for a single page.
Arguments: - resultData (Object) – search results of a page
Returns: Object – a <div> node with the results of a single page
-
generateSuggestionsList
(data, projectName)¶ Generate search suggestions list.
Arguments: - data (Object) – response data from the search backend
- projectName (String) – name (slug) of the project
Returns: Object – a <div> node with class “search__result__box” with results
-
removeAllActive
()¶ Removes .active class from all the suggestions.
-
addActive
(current_focus)¶ Add .active class to the search suggestion corresponding to serial number current_focus’, and scroll to that suggestion smoothly.
Arguments: - current_focus (Number) – serial no. of suggestions which will be active
-
getInputField
()¶ Returns initial search input field, which is already present in the docs.
Returns: Object – Input field node
-
removeResults
()¶ Removes all results from the search modal. It doesn’t close the search box.
-
getErrorDiv
(err_msg)¶ Creates and returns a div with error message and some styles.
Arguments: - err_msg (String) – error message to be displayed
-
fetchAndGenerateResults
(search_url, projectName)¶ Fetch the suggestions from search backend, and appends the results to <div class=”search__outer”> node, which is already created when the page was loaded.
Arguments: - search_url (String) – url on which request will be sent
- projectName (String) – name (slug) of the project
Returns: function – debounced function with debounce time of 500ms
-
generateAndReturnInitialHtml
()¶ Creates the initial html structure which will be appended to the <body> as soon as the page loads. This html structure will serve as the boilerplate to show our search results.
Returns: String – initial html structure
-
showSearchModal
(custom_query)¶ Opens the search modal.
Arguments: - custom_query (String) – if a custom query is provided, initialize the value of input field with it, or fallback to the value from the original search bar.
-
removeSearchModal
()¶ Closes the search modal.