@@ -46,22 +46,47 @@ 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 */
5555const convertObjToUrlParams = obj => {
5656 const params = Object . keys ( obj )
5757 . map ( function ( key ) {
58- const s = key + "=" + obj [ key ] ;
59- return s ;
58+ if ( _is_string ( key ) ) {
59+ const s = key + "=" + encodeURI ( obj [ key ] ) ;
60+ return s ;
61+ }
6062 } )
6163 . join ( "&" ) ;
6264 return params ;
6365} ;
6466
67+ /**
68+ * Adds/removes "rtd_search" url parameter to the url.
69+ */
70+ const updateUrl = ( ) => {
71+ let origin = window . location . origin ;
72+ let path = window . location . pathname ;
73+ let url_params = $ . getQueryParameters ( ) ;
74+
75+ if ( _is_string ( SEARCH_QUERY ) ) {
76+ url_params . rtd_search = SEARCH_QUERY ;
77+ } else {
78+ delete url_params . rtd_search ;
79+ }
80+
81+ let window_location_search = convertObjToUrlParams ( url_params ) ;
82+
83+ // new url
84+ const url = origin + path + "?" + window_location_search ;
85+
86+ // update url
87+ window . history . pushState ( { } , null , url ) ;
88+ } ;
89+
6590/**
6691 * Create and return DOM nodes
6792 * with passed attributes.
@@ -552,8 +577,10 @@ const generateAndReturnInitialHtml = () => {
552577
553578/**
554579 * Opens the search modal.
580+ *
581+ * @param {String } custom_query if a custom query is provided, initialise the value of input field with it
555582 */
556- const showSearchModal = ( ) => {
583+ const showSearchModal = custom_query => {
557584 // removes previous results (if there are any).
558585 removeResults ( ) ;
559586
@@ -568,7 +595,14 @@ const showSearchModal = () => {
568595 ".search__outer__input"
569596 ) ;
570597 if ( search_outer_input !== null ) {
571- search_outer_input . value = "" ;
598+ if (
599+ typeof custom_query !== "undefined" &&
600+ _is_string ( custom_query )
601+ ) {
602+ search_outer_input . value = custom_query ;
603+ } else {
604+ search_outer_input . value = "" ;
605+ }
572606 search_outer_input . focus ( ) ;
573607 }
574608 } ) ;
@@ -603,9 +637,13 @@ window.addEventListener("DOMContentLoaded", evt => {
603637 let initialHtml = generateAndReturnInitialHtml ( ) ;
604638 document . body . innerHTML += initialHtml ;
605639
606- let search_outer_wrapper = document . querySelector ( '.search__outer__wrapper' ) ;
607- let search_outer_input = document . querySelector ( '.search__outer__input' ) ;
608- let cross_icon = document . querySelector ( '.search__cross' ) ;
640+ let search_outer_wrapper = document . querySelector (
641+ ".search__outer__wrapper"
642+ ) ;
643+ let search_outer_input = document . querySelector (
644+ ".search__outer__input"
645+ ) ;
646+ let cross_icon = document . querySelector ( ".search__cross" ) ;
609647
610648 // this denotes the search suggestion which is currently selected
611649 // via tha ArrowUp/ArrowDown keys.
@@ -624,7 +662,7 @@ window.addEventListener("DOMContentLoaded", evt => {
624662 COUNT = 0 ;
625663
626664 let search_params = {
627- q : encodeURIComponent ( SEARCH_QUERY ) ,
665+ q : SEARCH_QUERY ,
628666 project : project ,
629667 version : version ,
630668 language : language
@@ -649,6 +687,9 @@ window.addEventListener("DOMContentLoaded", evt => {
649687 // is debounced here.
650688 debounce ( removeResults , 600 ) ( ) ;
651689 }
690+
691+ // update URL
692+ updateUrl ( ) ;
652693 } ) ;
653694
654695 search_outer_input . addEventListener ( "keydown" , e => {
@@ -719,5 +760,19 @@ window.addEventListener("DOMContentLoaded", evt => {
719760 removeSearchModal ( ) ;
720761 }
721762 } ) ;
763+
764+ // if "rtd_search" is present in URL parameters,
765+ // then open the search modal and show the results
766+ // for the value of "rtd_search"
767+ let url_params = $ . getQueryParameters ( ) ;
768+ if ( _is_array ( url_params . rtd_search ) ) {
769+ let query = decodeURIComponent ( url_params . rtd_search ) ;
770+ showSearchModal ( query ) ;
771+ search_outer_input . value = query ;
772+
773+ let event = document . createEvent ( "Event" ) ;
774+ event . initEvent ( "input" , true , true ) ;
775+ search_outer_input . dispatchEvent ( event ) ;
776+ }
722777 }
723778} ) ;
0 commit comments