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
- 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
- get_section_html(sectionData, page_link, id)
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
id (Number) – to be used in for this section
- get_domain_html(domainData, page_link, id)
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
id (Number) – to be used in for this section
- generateSingleResult(resultData, projectName, id)
Generate search results for a single page.
- This has the form:
- <div>
- <a href=”{link}”>
- <h2 class=”search__result__title”>
{title} <small class=”rtd_ui_search_subtitle”>{subtitle}</small> <br/>
</h2>
</a>
- <a href=”{link}”>
{section}
</a> <br class=”br-for-hits” />
- <a href=”{link}”>
{section}
</a> <br class=”br-for-hits” />
</div>
- Arguments
resultData (Object) – search results of a page
projectName (String) –
id (Number) – from the last section
- 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(id)
Add .active class to the search suggestion corresponding to id, and scroll to that suggestion smoothly.
- Arguments
id (Number) – of the suggestion to activate
- 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(api_endpoint:, parameters:, 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
api_endpoint: (String) – API endpoint
parameters: (Object) – search parameters
projectName: (String) – name (slug) of the project
- Returns
function – debounced function with debounce time of 500ms
- generateAndReturnInitialHtml(filters:)
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.
- Arguments
filters: (Array) – filters to be applied to the search. {[“Filter name”, “Filter value”]}
- 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.