1
1
function hcSearchCategory ( label , version ) {
2
2
// optional version filters search results for a single specific product version
3
- // currently can be used with OCP and Origin only
3
+ // currently can be used with OCP and OKD docs only
4
4
5
- var modalSearch = document . getElementById ( "hc-search-modal" ) ;
6
- var searchBtn = document . getElementById ( " hc-search-btn ") ;
7
- var closeModal = document . getElementById ( " hc-modal-close ") ;
8
- var searchResults = document . getElementById ( " hc-search-results ") ;
9
- var query = document . getElementById ( " hc-search-input") ;
5
+ // elements used repeatedly:
6
+ var modalSearch = $ ( "# hc-search-modal ") ;
7
+ var searchBtn = $ ( "# hc-search-btn ") ;
8
+ var closeModal = $ ( "# hc-modal-close ") ;
9
+ var query = $ ( "# hc-search-input") ;
10
10
11
11
// pressing enter in the input = search btn click
12
- query . addEventListener ( " keyup" , function ( event ) {
12
+ query . keyup ( function ( event ) {
13
13
event . preventDefault ( ) ;
14
14
if ( event . keyCode == 13 ) {
15
15
searchBtn . click ( ) ;
16
16
}
17
17
} ) ;
18
18
19
- //prepare iframe (without source)
20
- var iframe = document . createElement ( "iframe" ) ;
21
- iframe . frameBorder = 0 ;
22
- iframe . width = "100%" ;
23
- iframe . height = 0.7 * window . innerHeight ;
24
- iframe . id = "search-result-iframe" ;
25
-
26
- // open the modal and finalize the iframe on click
27
- searchBtn . onclick = function ( ) {
28
- if ( query . value ) {
29
- modalSearch . style . display = "block" ;
30
- // limit search to a signle version, if specified
31
- var urlFilter = ( typeof version === "undefined" || version == "Branch Build" ) ? "" : ( " url:*\\/" + version + "\\/*" ) ;
32
- var iframeSrc = "https://help.openshift.com/customsearch.html?q=" +
33
- encodeURIComponent ( query . value ) +
34
- encodeURIComponent ( urlFilter ) +
35
- "&l=" + encodeURIComponent ( label ) ;
36
- iframe . setAttribute ( "src" , iframeSrc ) ;
37
- searchResults . appendChild ( iframe ) ;
19
+ // open the modal and fetch the first set of results on click
20
+ searchBtn . click ( function ( ) {
21
+ if ( query . val ( ) ) {
22
+ // remove any results from previous searches
23
+ $ ( "#hc-search-results" ) . empty ( ) ;
24
+ var searchParams = {
25
+ si : 0 ,
26
+ q : query . val ( ) ,
27
+ label : label ,
28
+ urlFilter : ( typeof version === "undefined" || version == "Branch Build" ) ? "" : ( " url:*\\/" + version + "\\/*" )
29
+ } ;
30
+ modalSearch . show ( ) ;
31
+ hcsearch ( searchParams ) ;
32
+ }
33
+ } ) ;
34
+
35
+ // hide search modal by 'X' or by clicking outside of the modal
36
+ closeModal . click ( function ( ) {
37
+ modalSearch . hide ( ) ;
38
+ } ) ;
39
+ $ ( window ) . click ( function ( event ) {
40
+ if ( $ ( event . target ) . is ( modalSearch ) ) {
41
+ modalSearch . hide ( ) ;
38
42
}
39
- }
43
+ } ) ;
44
+ } // hcSearchCategory(label, version)
45
+
46
+ // fetch search results
47
+ function hcsearch ( searchParams ) {
48
+ // elements used repeatedly
49
+ var hcMoreBtn = $ ( "#hc-search-more-btn" ) ;
50
+ var hcSearchIndicator = $ ( "#hc-search-progress-indicator" ) ;
51
+ var hcSearchResult = $ ( "#hc-search-results" ) ;
40
52
41
- // hide search modal
42
- closeModal . onclick = function ( ) {
43
- modalSearch . style . display = "none" ;
44
- }
53
+ // the "searchprovider" is to return a JSON response in the expected format
54
+ var searchprovider = "https://help.openshift.com/search/search_custom.php" ;
55
+ var searchReq = { "q" : searchParams . q + searchParams . urlFilter ,
56
+ "l" : searchParams . label ,
57
+ "si" : searchParams . si } // q = query, l = label
45
58
46
- window . onclick = function ( event ) {
47
- if ( event . target == modalSearch ) {
48
- modalSearch . style . display = "none" ;
59
+ hcMoreBtn . hide ( ) ;
60
+ hcSearchIndicator . show ( ) ;
61
+ $ . get ( searchprovider , searchReq ) . done ( function ( hcsearchresults ) {
62
+ // GET success
63
+ if ( hcsearchresults == "" ) {
64
+ // success, but no response (response code mismatch)
65
+ $ ( "#hc-search-result" ) . append ( "<p><strong>An error occured while retrieving search results. Please try again later.</strong></p>" ) ;
66
+ hcSearchIndicator . hide ( ) ;
49
67
}
50
- }
51
- } // hcSearchCategory(label)
68
+ if ( hcsearchresults . response . result ) {
69
+ // if there are any results
70
+ $ ( hcsearchresults . response . result ) . each ( function ( ) {
71
+ var row = '<div class="search-result-item"><a href="' + this . url +
72
+ '" target="_blank">' + this . title + '</a>' ;
73
+ row += '<p class="excerpt">' + this . content_description . replace ( / \< b r \> / g, ' ' ) + '</p></div>' ;
74
+ hcSearchResult . append ( row ) ;
75
+ } ) ;
76
+ if ( hcsearchresults . response . page_number < hcsearchresults . response . page_count ) {
77
+ // if there are more results beyond the retrieved ones
78
+ // index of the first item on the next page (first item = 0, first page = 1)
79
+ searchParams . si = hcsearchresults . response . page_number * hcsearchresults . response . page_size ;
80
+ // replace any existing click handler with one to fetch the next set of results
81
+ hcMoreBtn . off ( 'click' ) ;
82
+ hcMoreBtn . click ( function ( ) {
83
+ hcsearch ( searchParams ) ;
84
+ } ) ;
85
+ hcMoreBtn . show ( ) ;
86
+ } else {
87
+ // no more results beyond the retrieved ones
88
+ hcSearchResult . append ( "<p><strong>No more results.</strong></p>" ) ;
89
+ }
90
+ } else {
91
+ if ( searchParams . si > 0 ) {
92
+ // no results reurned, but some already displayed
93
+ hcSearchResult . append ( "<p><strong>No more results.</strong></p>" ) ;
94
+ } else {
95
+ // no results on initial search
96
+ hcSearchResult . append ( "<p><strong>No results found. Try rewording your search.</strong></p>" ) ;
97
+ }
98
+ }
99
+ hcSearchIndicator . hide ( ) ;
100
+ } ) . fail ( function ( response ) {
101
+ // GET error
102
+ hcSearchResult . append ( "<p><strong>An error occured while retrieving search results. Please try again later.</strong></p>" ) ;
103
+ hcSearchIndicator . hide ( ) ;
104
+ } ) ;
105
+ } // function hcsearch()
0 commit comments