Skip to content

Commit

Permalink
search: avoid iframe
Browse files Browse the repository at this point in the history
* avoids search iframe for sensible rendering on iOS devices
* modifies the main search page in similar manner

Signed-off-by: Jiri Fiala <[email protected]>
  • Loading branch information
jiri-fiala committed Jul 1, 2019
1 parent a91890c commit ee15c5a
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 68 deletions.
126 changes: 90 additions & 36 deletions _javascripts/hc-search.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,105 @@
function hcSearchCategory(label, version) {
// optional version filters search results for a single specific product version
// currently can be used with OCP and Origin only
// currently can be used with OCP and OKD docs only

var modalSearch = document.getElementById("hc-search-modal");
var searchBtn = document.getElementById("hc-search-btn");
var closeModal = document.getElementById("hc-modal-close");
var searchResults = document.getElementById("hc-search-results");
var query = document.getElementById("hc-search-input");
// elements used repeatedly:
var modalSearch = $("#hc-search-modal");
var searchBtn = $("#hc-search-btn");
var closeModal = $("#hc-modal-close");
var query = $("#hc-search-input");

// pressing enter in the input = search btn click
query.addEventListener("keyup", function(event) {
query.keyup( function(event) {
event.preventDefault();
if (event.keyCode == 13) {
searchBtn.click();
}
});

//prepare iframe (without source)
var iframe = document.createElement("iframe");
iframe.frameBorder=0;
iframe.width="100%";
iframe.height=0.7*window.innerHeight;
iframe.id="search-result-iframe";

// open the modal and finalize the iframe on click
searchBtn.onclick = function() {
if (query.value) {
modalSearch.style.display = "block";
// limit search to a signle version, if specified
var urlFilter = (typeof version === "undefined" || version == "Branch Build") ? "" : (" url:*\\/" + version + "\\/*");
var iframeSrc = "https://help.openshift.com/customsearch.html?q=" +
encodeURIComponent(query.value) +
encodeURIComponent(urlFilter) +
"&l=" + encodeURIComponent(label);
iframe.setAttribute("src", iframeSrc);
searchResults.appendChild(iframe);
// open the modal and fetch the first set of results on click
searchBtn.click(function() {
if (query.val()) {
// remove any results from previous searches
$("#hc-search-results").empty();
var searchParams = {
si: 0,
q: query.val(),
label: label,
urlFilter: (typeof version === "undefined" || version == "Branch Build") ? "" : (" url:*\\/" + version + "\\/*")
};
modalSearch.show();
hcsearch(searchParams);
}
});

// hide search modal by 'X' or by clicking outside of the modal
closeModal.click(function() {
modalSearch.hide();
});
$(window).click(function(event) {
if ($(event.target).is(modalSearch)) {
modalSearch.hide();
}
}
});
} // hcSearchCategory(label, version)

// fetch search results
function hcsearch(searchParams) {
// elements used repeatedly
var hcMoreBtn = $("#hc-search-more-btn");
var hcSearchIndicator = $("#hc-search-progress-indicator");
var hcSearchResult = $("#hc-search-results");

// hide search modal
closeModal.onclick = function() {
modalSearch.style.display = "none";
}
// the "searchprovider" is to return a JSON response in the expected format
var searchprovider = "https://help.openshift.com/search/search_custom.php";
var searchReq = { "q" : searchParams.q + searchParams.urlFilter,
"l" : searchParams.label,
"si" : searchParams.si } // q = query, l = label

window.onclick = function(event) {
if (event.target == modalSearch) {
modalSearch.style.display = "none";
hcMoreBtn.hide();
hcSearchIndicator.show();
$.get(searchprovider, searchReq).done(function (hcsearchresults) {
// GET success
if (hcsearchresults == "") {
// success, but no response (response code mismatch)
$("#hc-search-result").append("<p><strong>An error occured while retrieving search results. Please try again later.</strong></p>");
hcSearchIndicator.hide();
}
}
} // hcSearchCategory(label)
if (hcsearchresults.response.result) {
// if there are any results
$(hcsearchresults.response.result).each(function () {
var row = '<div class="search-result-item"><a href="' + this.url +
'" target="_blank">' + this.title + '</a>';
row += '<p class="excerpt">' + this.content_description.replace(/\<br\>/g, ' ') + '</p></div>';
hcSearchResult.append(row);
});
if (hcsearchresults.response.page_number < hcsearchresults.response.page_count) {
// if there are more results beyond the retrieved ones
// index of the first item on the next page (first item = 0, first page = 1)
searchParams.si = hcsearchresults.response.page_number * hcsearchresults.response.page_size;
// replace any existing click handler with one to fetch the next set of results
hcMoreBtn.off('click');
hcMoreBtn.click(function() {
hcsearch(searchParams);
});
hcMoreBtn.show();
} else {
// no more results beyond the retrieved ones
hcSearchResult.append("<p><strong>No more results.</strong></p>");
}
} else {
if (searchParams.si > 0) {
// no results reurned, but some already displayed
hcSearchResult.append("<p><strong>No more results.</strong></p>");
} else {
// no results on initial search
hcSearchResult.append("<p><strong>No results found. Try rewording your search.</strong></p>");
}
}
hcSearchIndicator.hide();
}).fail(function(response) {
// GET error
hcSearchResult.append("<p><strong>An error occured while retrieving search results. Please try again later.</strong></p>");
hcSearchIndicator.hide();
});
} // function hcsearch()
49 changes: 41 additions & 8 deletions _stylesheets/search.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,54 @@
}

#hc-search-modal {
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
display: none;
position: fixed;
z-index: 1;
padding-top: 5%;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
z-index: 10000;
}
@media screen and (max-width: 767px) {
#hc-search-modal {
padding-top: 80px;
}
}
@media screen and (min-width: 768px) {
#hc-search-modal {
padding-top: 5%;
}
}

#hc-modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
height: 80vh;
padding: 20px;
}
@media screen and (max-width: 767px) {
#hc-modal-content {
height: 100%;
width: 100%;
}
}
@media screen and (min-width: 768px) {
#hc-modal-content {
height: 80vh;
margin: auto;
width: 80%;
}
}

#hc-modal-close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
position: relative;
z-index: 10001;
}

#hc-modal-close:hover,
Expand All @@ -46,3 +67,15 @@
text-decoration: none;
cursor: pointer;
}

#hc-search-results-wrapper {
height: 75vh;
padding-bottom: 15px;
overflow: auto;
}

@media screen and (max-width: 767px) {
#hc-search-more-btn, #hc-search-progress-indicator {
margin-bottom: 2em;
}
}
8 changes: 7 additions & 1 deletion _templates/_page_openshift.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@
<div id="hc-search-modal">
<div id="hc-modal-content">
<span id="hc-modal-close">&times;</span>
<div id="hc-search-results"></div>
<div id="hc-search-results-wrapper">
<div id="hc-search-results"></div>
<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>
<div class="text-center">
<button id="hc-search-more-btn">Show more results</button>
</div>
</div>
</div>
</div>
</div>
Expand Down
8 changes: 7 additions & 1 deletion _templates/_search_dedicated.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
<div id="hc-search-modal">
<div id="hc-modal-content">
<span id="hc-modal-close">&times;</span>
<div id="hc-search-results"></div>
<div id="hc-search-results-wrapper">
<div id="hc-search-results"></div>
<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>
<div class="text-center">
<button id="hc-search-more-btn">Show more results</button>
</div>
</div>
</div>
</div>

Expand Down
8 changes: 7 additions & 1 deletion _templates/_search_enterprise.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
<div id="hc-search-modal">
<div id="hc-modal-content">
<span id="hc-modal-close">&times;</span>
<div id="hc-search-results"></div>
<div id="hc-search-results-wrapper">
<div id="hc-search-results"></div>
<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>
<div class="text-center">
<button id="hc-search-more-btn">Show more results</button>
</div>
</div>
</div>
</div>

Expand Down
8 changes: 7 additions & 1 deletion _templates/_search_online.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
<div id="hc-search-modal">
<div id="hc-modal-content">
<span id="hc-modal-close">&times;</span>
<div id="hc-search-results"></div>
<div id="hc-search-results-wrapper">
<div id="hc-search-results"></div>
<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>
<div class="text-center">
<button id="hc-search-more-btn">Show more results</button>
</div>
</div>
</div>
</div>

Expand Down
8 changes: 7 additions & 1 deletion _templates/_search_origin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
<div id="hc-search-modal">
<div id="hc-modal-content">
<span id="hc-modal-close">&times;</span>
<div id="hc-search-results"></div>
<div id="hc-search-results-wrapper">
<div id="hc-search-results"></div>
<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>
<div class="text-center">
<button id="hc-search-more-btn">Show more results</button>
</div>
</div>
</div>
</div>

Expand Down
8 changes: 7 additions & 1 deletion _templates/_search_other.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
<div id="hc-search-modal">
<div id="hc-modal-content">
<span id="hc-modal-close">&times;</span>
<div id="hc-search-results"></div>
<div id="hc-search-results-wrapper">
<div id="hc-search-results"></div>
<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>
<div class="text-center">
<button id="hc-search-more-btn">Show more results</button>
</div>
</div>
</div>
</div>

Expand Down
Loading

0 comments on commit ee15c5a

Please sign in to comment.