Stirling-PDF/src/main/resources/static/js/favourites.js

46 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-06-02 21:15:10 +02:00
function updateFavoritesDropdown() {
2023-08-23 00:03:21 +02:00
var dropdown = document.querySelector('#favoritesDropdown');
// Check if dropdown exists
if (!dropdown) {
console.error('Dropdown element with ID "favoritesDropdown" not found!');
return; // Exit the function
}
dropdown.innerHTML = ''; // Clear the current favorites
2023-06-02 21:15:10 +02:00
2023-08-23 00:03:21 +02:00
var hasFavorites = false;
2023-06-02 21:15:10 +02:00
2023-08-23 00:03:21 +02:00
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
if (localStorage.getItem(key) === 'favorite') {
// Find the corresponding navbar entry
var navbarEntry = document.querySelector(`a[href='${key}']`);
if (navbarEntry) {
// Create a new dropdown entry
var dropdownItem = document.createElement('a');
dropdownItem.className = 'dropdown-item';
dropdownItem.href = navbarEntry.href;
dropdownItem.innerHTML = navbarEntry.innerHTML;
dropdown.appendChild(dropdownItem);
hasFavorites = true;
} else {
console.warn(`Navbar entry not found for key: ${key}`);
}
}
}
2023-06-02 21:15:10 +02:00
2023-08-23 00:03:21 +02:00
// Show or hide the default item based on whether there are any favorites
if (!hasFavorites) {
var defaultItem = document.createElement('a');
defaultItem.className = 'dropdown-item';
defaultItem.textContent = noFavourites;
dropdown.appendChild(defaultItem);
}
2023-06-02 21:15:10 +02:00
}
2023-08-23 00:03:21 +02:00
// Ensure that the DOM content has been fully loaded before calling the function
document.addEventListener('DOMContentLoaded', function() {
console.log('DOMContentLoaded event fired');
updateFavoritesDropdown();
});