@@ -46,20 +46,64 @@ const debounce = (func, wait) => {
46
46
/**
47
47
* Take an object as parameter and convert it to
48
48
* 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
51
51
*
52
52
* @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
54
54
*/
55
55
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 ] ) ;
59
59
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 ) ;
63
107
} ;
64
108
65
109
/**
@@ -552,8 +596,10 @@ const generateAndReturnInitialHtml = () => {
552
596
553
597
/**
554
598
* Opens the search modal.
599
+ *
600
+ * @param {String } custom_query if a custom query is provided, initialise the value of input field with it
555
601
*/
556
- const showSearchModal = ( ) => {
602
+ const showSearchModal = custom_query => {
557
603
// removes previous results (if there are any).
558
604
removeResults ( ) ;
559
605
@@ -568,7 +614,14 @@ const showSearchModal = () => {
568
614
".search__outer__input"
569
615
) ;
570
616
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
+ }
572
625
search_outer_input . focus ( ) ;
573
626
}
574
627
} ) ;
@@ -588,6 +641,12 @@ const removeSearchModal = () => {
588
641
search_outer_input . blur ( ) ;
589
642
}
590
643
644
+ // reset SEARCH_QUERY
645
+ SEARCH_QUERY = "" ;
646
+
647
+ // update url (remove 'rtd_search' param)
648
+ updateUrl ( ) ;
649
+
591
650
$ ( ".search__outer__wrapper" ) . fadeOut ( 400 ) ;
592
651
} ;
593
652
@@ -628,7 +687,7 @@ window.addEventListener("DOMContentLoaded", evt => {
628
687
COUNT = 0 ;
629
688
630
689
let search_params = {
631
- q : encodeURIComponent ( SEARCH_QUERY ) ,
690
+ q : SEARCH_QUERY ,
632
691
project : project ,
633
692
version : version ,
634
693
language : language
@@ -653,6 +712,9 @@ window.addEventListener("DOMContentLoaded", evt => {
653
712
// is debounced here.
654
713
debounce ( removeResults , 600 ) ( ) ;
655
714
}
715
+
716
+ // update URL
717
+ updateUrl ( ) ;
656
718
} ) ;
657
719
658
720
search_outer_input . addEventListener ( "keydown" , e => {
@@ -723,5 +785,19 @@ window.addEventListener("DOMContentLoaded", evt => {
723
785
removeSearchModal ( ) ;
724
786
}
725
787
} ) ;
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
+ }
726
802
}
727
803
} ) ;
0 commit comments