2023-07-20 00:15:16 +02:00
|
|
|
// Toggle search bar when the search icon is clicked
|
|
|
|
document.querySelector('#search-icon').addEventListener('click', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var searchBar = document.querySelector('#navbarSearch');
|
|
|
|
searchBar.classList.toggle('show');
|
|
|
|
});
|
|
|
|
window.onload = function() {
|
|
|
|
var items = document.querySelectorAll('.dropdown-item, .nav-link');
|
|
|
|
var dummyContainer = document.createElement('div');
|
|
|
|
dummyContainer.style.position = 'absolute';
|
|
|
|
dummyContainer.style.visibility = 'hidden';
|
|
|
|
dummyContainer.style.whiteSpace = 'nowrap'; // Ensure we measure full width
|
|
|
|
document.body.appendChild(dummyContainer);
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
var maxWidth = 0;
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
items.forEach(function(item) {
|
|
|
|
var clone = item.cloneNode(true);
|
|
|
|
dummyContainer.appendChild(clone);
|
|
|
|
var width = clone.offsetWidth;
|
|
|
|
if (width > maxWidth) {
|
|
|
|
maxWidth = width;
|
|
|
|
}
|
|
|
|
dummyContainer.removeChild(clone);
|
|
|
|
});
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
document.body.removeChild(dummyContainer);
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
// Store max width for later use
|
|
|
|
window.navItemMaxWidth = maxWidth;
|
|
|
|
};
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
// Show search results as user types in search box
|
|
|
|
document.querySelector('#navbarSearchInput').addEventListener('input', function(e) {
|
|
|
|
var searchText = e.target.value.toLowerCase();
|
|
|
|
var items = document.querySelectorAll('.dropdown-item, .nav-link');
|
|
|
|
var resultsBox = document.querySelector('#searchResults');
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
// Clear any previous results
|
|
|
|
resultsBox.innerHTML = '';
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
items.forEach(function(item) {
|
|
|
|
var titleElement = item.querySelector('.icon-text');
|
|
|
|
var iconElement = item.querySelector('.icon');
|
|
|
|
var itemHref = item.getAttribute('href');
|
2023-08-20 22:57:19 +02:00
|
|
|
var tags = item.getAttribute('data-bs-tags') || ""; // If no tags, default to empty string
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
if (titleElement && iconElement && itemHref !== '#') {
|
|
|
|
var title = titleElement.innerText;
|
2023-07-24 00:05:02 +02:00
|
|
|
if ((title.toLowerCase().indexOf(searchText) !== -1 || tags.toLowerCase().indexOf(searchText) !== -1) && !resultsBox.querySelector(`a[href="${item.getAttribute('href')}"]`)) {
|
2023-07-20 00:15:16 +02:00
|
|
|
var result = document.createElement('a');
|
|
|
|
result.href = itemHref;
|
|
|
|
result.classList.add('dropdown-item');
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
var resultIcon = document.createElement('img');
|
|
|
|
resultIcon.src = iconElement.src;
|
|
|
|
resultIcon.alt = 'icon';
|
|
|
|
resultIcon.classList.add('icon');
|
|
|
|
result.appendChild(resultIcon);
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
var resultText = document.createElement('span');
|
|
|
|
resultText.textContent = title;
|
|
|
|
resultText.classList.add('icon-text');
|
|
|
|
result.appendChild(resultText);
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
resultsBox.appendChild(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-02-11 17:47:00 +01:00
|
|
|
|
2023-07-20 00:15:16 +02:00
|
|
|
// Set the width of the search results box to the maximum width
|
|
|
|
resultsBox.style.width = window.navItemMaxWidth + 'px';
|
|
|
|
});
|
2024-02-11 17:47:00 +01:00
|
|
|
|
|
|
|
|