Skip to content

Commit ee15c5a

Browse files
committed
search: avoid iframe
* avoids search iframe for sensible rendering on iOS devices * modifies the main search page in similar manner Signed-off-by: Jiri Fiala <[email protected]>
1 parent a91890c commit ee15c5a

File tree

9 files changed

+257
-68
lines changed

9 files changed

+257
-68
lines changed

_javascripts/hc-search.js

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,105 @@
11
function hcSearchCategory(label, version) {
22
// 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
44

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");
1010

1111
// pressing enter in the input = search btn click
12-
query.addEventListener("keyup", function(event) {
12+
query.keyup( function(event) {
1313
event.preventDefault();
1414
if (event.keyCode == 13) {
1515
searchBtn.click();
1616
}
1717
});
1818

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();
3842
}
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");
4052

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
4558

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();
4967
}
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(/\<br\>/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()

_stylesheets/search.css

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,54 @@
1111
}
1212

1313
#hc-search-modal {
14+
background-color: rgb(0,0,0);
15+
background-color: rgba(0,0,0,0.4);
1416
display: none;
1517
position: fixed;
16-
z-index: 1;
17-
padding-top: 5%;
1818
left: 0;
1919
top: 0;
2020
width: 100%;
2121
height: 100%;
2222
overflow: auto;
23-
background-color: rgb(0,0,0);
24-
background-color: rgba(0,0,0,0.4);
23+
z-index: 10000;
24+
}
25+
@media screen and (max-width: 767px) {
26+
#hc-search-modal {
27+
padding-top: 80px;
28+
}
29+
}
30+
@media screen and (min-width: 768px) {
31+
#hc-search-modal {
32+
padding-top: 5%;
33+
}
2534
}
2635

2736
#hc-modal-content {
2837
background-color: #fefefe;
29-
margin: auto;
30-
padding: 20px;
3138
border: 1px solid #888;
32-
width: 80%;
33-
height: 80vh;
39+
padding: 20px;
40+
}
41+
@media screen and (max-width: 767px) {
42+
#hc-modal-content {
43+
height: 100%;
44+
width: 100%;
45+
}
46+
}
47+
@media screen and (min-width: 768px) {
48+
#hc-modal-content {
49+
height: 80vh;
50+
margin: auto;
51+
width: 80%;
52+
}
3453
}
3554

3655
#hc-modal-close {
3756
color: #aaaaaa;
3857
float: right;
3958
font-size: 28px;
4059
font-weight: bold;
60+
position: relative;
61+
z-index: 10001;
4162
}
4263

4364
#hc-modal-close:hover,
@@ -46,3 +67,15 @@
4667
text-decoration: none;
4768
cursor: pointer;
4869
}
70+
71+
#hc-search-results-wrapper {
72+
height: 75vh;
73+
padding-bottom: 15px;
74+
overflow: auto;
75+
}
76+
77+
@media screen and (max-width: 767px) {
78+
#hc-search-more-btn, #hc-search-progress-indicator {
79+
margin-bottom: 2em;
80+
}
81+
}

_templates/_page_openshift.html.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@
9494
<div id="hc-search-modal">
9595
<div id="hc-modal-content">
9696
<span id="hc-modal-close">&times;</span>
97-
<div id="hc-search-results"></div>
97+
<div id="hc-search-results-wrapper">
98+
<div id="hc-search-results"></div>
99+
<div id="hc-search-progress-indicator" class="text-center search-progress-indicator"><i class="fa fa-circle-o-notch fa-spin" style="font-size:42px"></i></div>
100+
<div class="text-center">
101+
<button id="hc-search-more-btn">Show more results</button>
102+
</div>
103+
</div>
98104
</div>
99105
</div>
100106
</div>

_templates/_search_dedicated.html.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
<div id="hc-search-modal">
77
<div id="hc-modal-content">
88
<span id="hc-modal-close">&times;</span>
9-
<div id="hc-search-results"></div>
9+
<div id="hc-search-results-wrapper">
10+
<div id="hc-search-results"></div>
11+
<div id="hc-search-progress-indicator" class="text-center search-progress-indicator"><i class="fa fa-circle-o-notch fa-spin" style="font-size:42px"></i></div>
12+
<div class="text-center">
13+
<button id="hc-search-more-btn">Show more results</button>
14+
</div>
15+
</div>
1016
</div>
1117
</div>
1218

_templates/_search_enterprise.html.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
<div id="hc-search-modal">
77
<div id="hc-modal-content">
88
<span id="hc-modal-close">&times;</span>
9-
<div id="hc-search-results"></div>
9+
<div id="hc-search-results-wrapper">
10+
<div id="hc-search-results"></div>
11+
<div id="hc-search-progress-indicator" class="text-center search-progress-indicator"><i class="fa fa-circle-o-notch fa-spin" style="font-size:42px"></i></div>
12+
<div class="text-center">
13+
<button id="hc-search-more-btn">Show more results</button>
14+
</div>
15+
</div>
1016
</div>
1117
</div>
1218

_templates/_search_online.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
<div id="hc-search-modal">
77
<div id="hc-modal-content">
88
<span id="hc-modal-close">&times;</span>
9-
<div id="hc-search-results"></div>
9+
<div id="hc-search-results-wrapper">
10+
<div id="hc-search-results"></div>
11+
<div id="hc-search-progress-indicator" class="text-center search-progress-indicator"><i class="fa fa-circle-o-notch fa-spin" style="font-size:42px"></i></div>
12+
<div class="text-center">
13+
<button id="hc-search-more-btn">Show more results</button>
14+
</div>
15+
</div>
1016
</div>
1117
</div>
1218

_templates/_search_origin.html.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
<div id="hc-search-modal">
77
<div id="hc-modal-content">
88
<span id="hc-modal-close">&times;</span>
9-
<div id="hc-search-results"></div>
9+
<div id="hc-search-results-wrapper">
10+
<div id="hc-search-results"></div>
11+
<div id="hc-search-progress-indicator" class="text-center search-progress-indicator"><i class="fa fa-circle-o-notch fa-spin" style="font-size:42px"></i></div>
12+
<div class="text-center">
13+
<button id="hc-search-more-btn">Show more results</button>
14+
</div>
15+
</div>
1016
</div>
1117
</div>
1218

_templates/_search_other.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
<div id="hc-search-modal">
77
<div id="hc-modal-content">
88
<span id="hc-modal-close">&times;</span>
9-
<div id="hc-search-results"></div>
9+
<div id="hc-search-results-wrapper">
10+
<div id="hc-search-results"></div>
11+
<div id="hc-search-progress-indicator" class="text-center search-progress-indicator"><i class="fa fa-circle-o-notch fa-spin" style="font-size:42px"></i></div>
12+
<div class="text-center">
13+
<button id="hc-search-more-btn">Show more results</button>
14+
</div>
15+
</div>
1016
</div>
1117
</div>
1218

0 commit comments

Comments
 (0)