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

Site search (searchbar) #238

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions bin/updateSiteSearch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import fs from "fs";

const metadataFilePath = "templates/data/meta-info.json";
const siteSearchFilePath = "templates/data/site-search.json";

/* Read from site metadata file and create search directory based on existing information */
function updateSearchJSON() {
const searchList = [];

fs.readFile(metadataFilePath, (error, data) => {
if (error) {
console.log(`Error occurred in reading file ${metadataFilePath}`, error);
return;
}

const obj = JSON.parse(data);
let count = 0;

for (const [key, entry] of Object.entries(obj)) {
if (entry["searchable"] == true) {
const searchEntry = {};

try {
searchEntry["id"] = count;
searchEntry["title"] = entry["shortTitle"]? entry["shortTitle"] : entry["title"];
searchEntry["desc"] = entry["desc"];
searchEntry["link"] = `/${key}`;
searchList.push(searchEntry);
} catch (e) {
console.log(`Error in adding key ${key}: ${e}`);
}
}
count += 1;
}

try {
fs.writeFileSync(
siteSearchFilePath,
JSON.stringify(searchList, null, 2),
"utf-8",
);
} catch (e) {
console.log(`Error in writing file ${siteSearchFilePath}}: ${e}`);
}
});
}

updateSearchJSON();
1 change: 1 addition & 0 deletions content/head/combobox.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<link rel="stylesheet" type="text/css" href="css/combobox.css" >
<link rel="stylesheet" type="text/css" href="css/form.css" >
<link rel="stylesheet" type="text/css" href="css/site-search.css" >

<style>
.combobox-example {
Expand Down
2 changes: 1 addition & 1 deletion css/enable-flyout.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

163 changes: 163 additions & 0 deletions css/site-search.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions css/site.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions images/search-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 112 additions & 0 deletions js/modules/es4/site-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const search = new (function() {
// global constants
const { body } = document;
let inputTarget = null;

function removeListItems(listId) {

if (!list || !list.firstChild) return;
var list = document.getElementById(listId);
while (list.firstChild){
list.firstChild.remove();
}
}

function fetchSearchData() {
fetch('/templates/data/site-search.json')
.then(response => response.json())
.then(data => {
const inputField = document.querySelector('[role="combobox"]');
const list = document.getElementById('home-search__list');

inputField.addEventListener('input', () => {
const searchTerm = inputField.value.trim().toLowerCase();

removeListItems('home-search__list');
clearComboboxState();

// Separate title and descriptions to order accordingly
const titleMatches = [];
const descMatches = [];

data.forEach(item => {
const originalTitle = item.title;
const originalDesc = item.desc;

if (originalTitle.toLowerCase().includes(searchTerm)) {
titleMatches.push(item);
} else if (originalDesc.toLowerCase().includes(searchTerm)) {
descMatches.push(item);
}
});

// Helper function to populate list
const populateList = (items) => {
items.forEach(item => {
const listItem = document.createElement('li');
listItem.setAttribute('role', 'option');
listItem.setAttribute('tabindex', '-1');

const link = document.createElement('a');
link.setAttribute('href', item.link);
link.style.display = 'block';
link.style.textDecoration = 'none';

const title = document.createElement('strong');
title.innerHTML = highlightText(item.title, searchTerm);

const desc = document.createElement('p');
desc.innerHTML = highlightText(item.desc, searchTerm);
desc.style.margin = '4px 0';

link.appendChild(title);
link.appendChild(document.createElement('br'));
link.appendChild(desc);

listItem.appendChild(link);
list.appendChild(listItem);

listItem.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
link.click();
}
});
});
};

populateList(titleMatches);
populateList(descMatches);

enableComboboxes.init();
});
})
.catch(error => {
console.error('Error fetching the JSON file:', error);
});
}

function highlightText(text, searchTerm) {
if (!searchTerm) return text;
const regex = new RegExp(`(${searchTerm})`, "gi");
return text.replace(regex, '<span class="highlight">$1</span>');
}

function clearComboboxState() {
const listbox = document.querySelector('[role="listbox"]');
if (listbox) {
listbox.innerHTML = '';
}
enableComboboxes.list = [];
}

this.init = () => {
body.addEventListener('click', this.handleClick);
fetchSearchData();
}

this.handleClick = (e) => {
if (e.target != inputTarget){
removeListItems(e.target);
}
}
})
Loading
Loading