Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.11] Add new results landing page for website search #8019

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions _layouts/search_layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
---
layout: table_wrappers
---

<!DOCTYPE html>

<html lang="{{ site.lang | default: 'en-US' }}">
{% include head.html %}
<body>
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<symbol id="svg-arrow-right" viewBox="0 0 24 24">
<title>Expand</title>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right">
<polyline points="9 18 15 12 9 6"></polyline>
</svg>
</symbol>
</svg>

{% include header.html %}

<div class="main-content-wrap-home">
<div id="main-content" class="main-content" role="main">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Results Page Head from layout</title>
</head>
<div class="search-page">
<aside class="search-page--sidebar">
<h2>Filter results</h2>
<div class="version-selector-wrapper" id="version-panel">
<version-selector selected="{{ site.data.versions.current }}"></version-selector>
</div>
<form class="search-page--sidebar--category-filter">
<div class="search-page--sidebar--category-filter--checkbox">
<div>
<input type="checkbox" id="categoryAll" name="categoryGroup" value="All" class="category-checkbox" checked>
<label for="categoryAll">All</label>
</div>
<div class="search-page--sidebar--category-filter--checkbox-child">
<input type="checkbox" id="categoryDocumentation" name="categoryGroup" value="Documentation" class="category-checkbox">
<label for="categoryDocumentation">Documentation</label>
</div>
<div class="search-page--sidebar--category-filter--checkbox-child">
<input type="checkbox" id="categoryNews" name="categoryGroup" value="News" class="category-checkbox">
<label for="categoryNews">News</label>
</div>
</div>
</form>
</aside>
<div class="search-page--results">
<div class="search-page--results--input">
<input type="text" class="search-page--results--input-box" id="searchPageInput" placeholder="Search for anything" aria-label="Search {{ site.title }}" autocomplete="off">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" class="search-page--results--input-icon" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search">
<circle cx="11" cy="11" r="8"></circle>
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
</svg>
</div>
<main class="search-page--results--diplay">
<h1 class="search-page--results--diplay--header" id="searchPageResultsHeader"></h1>
<div class="search-page--results--diplay--container" id="searchPageResultsContainer">
<!-- API results will be displayed here -->
</div>
</main>
</div>
</div>
<a class="top-link" href="#top">
<svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg>
</a>
</div>
</div>

{% include footer.html %}
<script src="{{ '/assets/js/search.js' | relative_url }}"></script>
<script src="{{ '/assets/js/home-listener.js' | relative_url }}"></script>
<script>
function truncateSentence(sentence, maxLength) {
if (sentence.length <= maxLength) return sentence;
let words = sentence.split(' ');
let truncatedSentence = '';

for (let i = 0; i < words.length; i++) {
if (truncatedSentence.length + words[i].length + 1 > maxLength) {
break;
}
if (i > 0) {
truncatedSentence += ' ';
}
truncatedSentence += words[i];
}
return truncatedSentence + (truncatedSentence.length < sentence.length ? '...' : '');
}
</script>
<script>
const searchInput = document.getElementById('searchPageInput');
const checkboxes = document.querySelectorAll('.category-checkbox');

function getSelectedCategory() {
const allCheckbox = document.getElementById('categoryAll');
if (allCheckbox.checked) {
return "All";
}
return document.querySelector('input[name="categoryGroup"]:checked');
}

function triggerSearch(query) {
const searchResultsHeader = document.getElementById('searchPageResultsHeader');

if (query) {
trucatedQuery = truncateSentence(query, 20);
if (getSelectedCategory() == null) {
searchResultsHeader.textContent = 'Select the result type for your search.';
document.getElementById('searchPageResultsContainer').innerHTML = '';
return;
}
const selectedCategory = getSelectedCategory().value;
const searchType = selectedCategory == ("Documentation") ? "docs" : selectedCategory == ("News") ? "proj" : "all";
const urlPath = window.location.pathname;
const versionMatch = urlPath.match(/(\d+\.\d+)/);
const docsVersion = versionMatch ? versionMatch[1] : "latest";

searchResultsHeader.textContent = `Search results for "${trucatedQuery}"`;
doResultsPageSearch(query, searchType, docsVersion);
} else {
searchResultsHeader.textContent = 'Please input a Query for searching.';
document.getElementById('searchPageResultsContainer').innerHTML = '';
}
}

searchInput.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
event.preventDefault();
triggerSearch(searchInput.value.trim());
}
});

</script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const categoryAll = document.getElementById('categoryAll');
const categoryDocumentation = document.getElementById('categoryDocumentation');
const categoryNews = document.getElementById('categoryNews');
const searchInput = document.getElementById('searchPageInput');

function updateAllCheckbox() {
if (categoryDocumentation.checked && categoryNews.checked) {
categoryAll.checked = true;
} else {
categoryAll.checked = false;
}
}

function updateChildCheckboxes() {
if (categoryAll.checked) {
categoryDocumentation.checked = true;
categoryNews.checked = true;
} else {
categoryDocumentation.checked = false;
categoryNews.checked = false;
}
}

categoryAll.addEventListener('change', () => {
updateChildCheckboxes();
triggerSearch(searchInput.value.trim());
});
categoryDocumentation.addEventListener('change', () => {
updateAllCheckbox();
triggerSearch(searchInput.value.trim());
});
categoryNews.addEventListener('change', () => {
updateAllCheckbox();
triggerSearch(searchInput.value.trim());
});
});
</script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const searchInput = document.getElementById('searchPageInput');

const getQueryParam = (param) => {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(param);
};

const searchQuery = getQueryParam('q');

if (searchQuery) {
searchInput.value = decodeURIComponent(searchQuery);
triggerSearch(searchInput.value.trim());
}
});
</script>
</body>
</html>
65 changes: 65 additions & 0 deletions _sass/custom/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,71 @@ body {
border-bottom: 1px solid #eeebee;
}

.search-page {
display: flex;
align-items: flex-start;
justify-content: center;
gap: 20px;
margin: 0 auto;
}

.search-page--sidebar {
flex: 1;
max-width: 200px;
flex: 0 0 200px;
}

.search-page--sidebar--category-filter--checkbox-child {
padding-left: 20px;
}

.search-page--results {
flex: 3;
display: flex;
flex-direction: column;
align-items: center;
max-width: 60%;
}

.search-page--results--input {
width: 100%;
position: relative;
}

.search-page--results--input-box {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
}

.search-page--results--input-icon {
position: absolute;
top: 35%;
right: 10px;
transform: translateY(-50%);
pointer-events: none;
color: #333;
}

.search-page--results--diplay {
width: 100%;
position: relative;
flex-flow: column nowrap;
}

.search-page--results--diplay--header {
text-align: center;
margin-bottom: 20px;
background-color: transparent;
}

.search-page--results--diplay--container--item {
margin-bottom: 1%;
display: block;
}

@mixin body-text($color: #000) {
color: $color;
font-family: 'Open Sans';
Expand Down
81 changes: 77 additions & 4 deletions assets/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
const CLASSNAME_HIGHLIGHTED = 'highlighted';

const canSmoothScroll = 'scrollBehavior' in document.documentElement.style;
const docsVersion = elInput.getAttribute('data-docs-version');

//Extract version from the URL path
const urlPath = window.location.pathname;
const versionMatch = urlPath.match(/(\d+\.\d+)/);
const docsVersion = versionMatch ? versionMatch[1] : elInput.getAttribute('data-docs-version');

let _showingResults = false,
animationFrame,
Expand Down Expand Up @@ -46,7 +50,7 @@

case 'Enter':
e.preventDefault();
navToHighlightedResult();
navToResult();
break;
}
});
Expand Down Expand Up @@ -247,9 +251,19 @@
}
};

const navToHighlightedResult = () => {
const navToResultsPage = () => {
const query = encodeURIComponent(elInput.value);
window.location.href = `/docs/${docsVersion}/search.html?q=${query}`;
}

const navToResult = () => {
const searchResultClassName = 'top-banner-search--field-with-results--field--wrapper--search-component--search-results--result';
elResults.querySelector(`.${searchResultClassName}.highlighted a[href]`)?.click?.();
const element = elResults.querySelector(`.${searchResultClassName}.highlighted a[href]`);
if (element) {
element.click?.();
} else {
navToResultsPage();
}
};

const recordEvent = (name, data) => {
Expand All @@ -261,3 +275,62 @@
};
});
})();


window.doResultsPageSearch = async (query, type, version) => {
console.log("Running results page search!");

const searchResultsContainer = document.getElementById('searchPageResultsContainer');

try {
const response = await fetch(`https://search-api.opensearch.org/search?q=${query}&v=${version}&t=${type}`);
const data = await response.json();
// Clear any previous search results
searchResultsContainer.innerHTML = '';

if (data.results && data.results.length > 0) {
data.results.forEach(result => {
const resultElement = document.createElement('div');
resultElement.classList.add('search-page--results--diplay--container--item');

const contentCite = document.createElement('cite');
const crumbs = [...result.ancestors];
if (result.type === 'DOCS') crumbs.unshift(`OpenSearch ${result.versionLabel || result.version}`);
else if (result.type) crumbs.unshift(result.type);
contentCite.textContent = crumbs.join(' › ')?.replace?.(/</g, '&lt;');
contentCite.style.fontSize = '.8em';

const titleLink = document.createElement('a');
titleLink.href = result.url;
titleLink.textContent = result.title;
titleLink.style.fontSize = '1.5em';
titleLink.style.fontWeight = 'bold';
titleLink.style.display = 'block';

const contentSpan = document.createElement('span');
contentSpan.textContent = result.content;
contentSpan.style.display = 'block';

resultElement.appendChild(contentCite);
resultElement.appendChild(titleLink);
resultElement.appendChild(contentSpan);

// Append the result element to the searchResultsContainer
searchResultsContainer.appendChild(resultElement);

const breakline = document.createElement('hr');
breakline.style.border = '.5px solid #ccc';
breakline.style.margin = 'auto';
searchResultsContainer.appendChild(breakline);
});
} else {
const noResultsElement = document.createElement('div');
noResultsElement.textContent = 'No results found.';
noResultsElement.style.fontSize = '2em';
searchResultsContainer.appendChild(noResultsElement);
}
} catch (error) {
console.error('Error fetching search results:', error);
searchResultsContainer.innerHTML = 'An error occurred while fetching search results. Please try again later.';
}
}
12 changes: 12 additions & 0 deletions search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
layout: search_layout
title: OpenSearch Documentation Search Results Page
nav_order: 1
has_children: false
nav_exclude: true
permalink: /search.html
---

{% include banner.html %}

{% include cards.html %}
Loading