2023-06-02 20:15:10 +01:00
|
|
|
function updateFavoritesDropdown() {
|
2025-01-30 18:55:33 +00:00
|
|
|
const favoritesList = JSON.parse(localStorage.getItem('favoritesList')) || [];
|
|
|
|
|
|
|
|
for (var i = 0; i < localStorage.length; i++) {
|
|
|
|
var key = localStorage.key(i);
|
|
|
|
var value = localStorage.getItem(key);
|
|
|
|
|
|
|
|
if (value === 'favorite') {
|
|
|
|
const index = favoritesList.indexOf(key);
|
|
|
|
if (index === -1) {
|
|
|
|
favoritesList.push(key);
|
|
|
|
localStorage.removeItem(key);
|
|
|
|
console.log(`Added to favorites: ${key}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var dropdown = document.querySelector('#favoritesDropdown');
|
2024-02-11 11:47:00 -05:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
if (!dropdown) {
|
|
|
|
console.error('Dropdown element with ID "favoritesDropdown" not found!');
|
2024-10-14 22:34:41 +01:00
|
|
|
return;
|
2024-02-16 22:49:06 +01:00
|
|
|
}
|
2025-01-30 18:55:33 +00:00
|
|
|
dropdown.innerHTML = '';
|
2023-06-02 20:15:10 +01:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
var hasFavorites = false;
|
2024-10-14 22:34:41 +01:00
|
|
|
var addedFeatures = new Set();
|
2023-06-02 20:15:10 +01:00
|
|
|
|
2025-01-30 18:55:33 +00:00
|
|
|
for (var i = 0; i < favoritesList.length; i++) {
|
|
|
|
var value = favoritesList[i];
|
|
|
|
if (value) {
|
|
|
|
var navbarEntry = document.querySelector(`a[data-bs-link='${value}']`);
|
2024-02-16 22:49:06 +01:00
|
|
|
if (navbarEntry) {
|
2024-10-14 22:34:41 +01:00
|
|
|
var featureName = navbarEntry.textContent.trim();
|
|
|
|
|
|
|
|
if (!addedFeatures.has(featureName)) {
|
2025-01-30 18:55:33 +00:00
|
|
|
var dropdownItem = document.createElement('div');
|
|
|
|
dropdownItem.className = 'dropdown-item d-flex justify-content-between align-items-center';
|
2024-10-14 22:34:41 +01:00
|
|
|
|
|
|
|
// Create a wrapper for the original content
|
2025-01-30 18:55:33 +00:00
|
|
|
var contentWrapper = document.createElement('div');
|
|
|
|
contentWrapper.className = 'd-flex align-items-center flex-grow-1';
|
|
|
|
contentWrapper.style.textDecoration = 'none';
|
|
|
|
contentWrapper.style.color = 'inherit';
|
2024-10-14 22:34:41 +01:00
|
|
|
|
|
|
|
// Clone the original content
|
|
|
|
var originalContent = navbarEntry.querySelector('div').cloneNode(true);
|
|
|
|
contentWrapper.appendChild(originalContent);
|
|
|
|
|
|
|
|
// Create the remove button
|
2025-01-30 18:55:33 +00:00
|
|
|
var removeButton = document.createElement('button');
|
|
|
|
removeButton.className = 'btn btn-sm btn-link p-0 ml-2';
|
2024-10-14 22:34:41 +01:00
|
|
|
removeButton.innerHTML = '<i class="material-symbols-rounded close-icon" style="font-size: 18px;">close</i>';
|
2025-01-30 18:55:33 +00:00
|
|
|
removeButton.onclick = function (itemKey, event) {
|
2024-10-14 22:34:41 +01:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2025-01-30 18:55:33 +00:00
|
|
|
addToFavorites(itemKey);
|
2024-10-14 22:34:41 +01:00
|
|
|
updateFavoritesDropdown();
|
2025-01-30 18:55:33 +00:00
|
|
|
}.bind(null, value);
|
2024-10-14 22:34:41 +01:00
|
|
|
|
|
|
|
// Add click event to the content wrapper
|
2025-01-30 18:55:33 +00:00
|
|
|
contentWrapper.onclick = function (itemHref, event) {
|
2024-10-14 22:34:41 +01:00
|
|
|
event.preventDefault();
|
|
|
|
window.location.href = itemHref;
|
|
|
|
}.bind(null, navbarEntry.href);
|
|
|
|
|
|
|
|
dropdownItem.appendChild(contentWrapper);
|
|
|
|
dropdownItem.appendChild(removeButton);
|
|
|
|
dropdown.appendChild(dropdownItem);
|
|
|
|
hasFavorites = true;
|
|
|
|
addedFeatures.add(featureName);
|
|
|
|
}
|
2024-02-16 22:49:06 +01:00
|
|
|
}
|
2025-01-30 18:55:33 +00:00
|
|
|
} else {
|
|
|
|
console.warn(`Navbar entry not found for : ${value}`);
|
2023-08-22 23:03:21 +01:00
|
|
|
}
|
2024-02-16 22:49:06 +01:00
|
|
|
}
|
2023-06-02 20:15:10 +01:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
if (!hasFavorites) {
|
2025-01-30 18:55:33 +00:00
|
|
|
var defaultItem = document.createElement('a');
|
|
|
|
defaultItem.className = 'dropdown-item';
|
|
|
|
defaultItem.textContent = noFavourites || 'No favorites added';
|
2024-02-16 22:49:06 +01:00
|
|
|
dropdown.appendChild(defaultItem);
|
|
|
|
}
|
2023-06-02 20:15:10 +01:00
|
|
|
}
|
2025-01-30 18:55:33 +00:00
|
|
|
|
|
|
|
function updateFavoriteIcons() {
|
|
|
|
const favoritesList = JSON.parse(localStorage.getItem('favoritesList')) || [];
|
|
|
|
|
|
|
|
// Select all favorite icons
|
|
|
|
document.querySelectorAll('.favorite-icon').forEach((icon) => {
|
|
|
|
const endpoint = icon.getAttribute('data-endpoint');
|
|
|
|
const parent = icon.closest('.dropdown-item');
|
|
|
|
|
|
|
|
// Determine if the icon belongs to groupRecent or groupFavorites
|
|
|
|
const isInGroupRecent = parent?.closest('#groupRecent') !== null;
|
|
|
|
const isInGroupFavorites = parent?.closest('#groupFavorites') !== null;
|
|
|
|
|
|
|
|
if (isInGroupRecent) {
|
|
|
|
icon.style.display = 'none';
|
|
|
|
} else if (isInGroupFavorites) {
|
|
|
|
icon.textContent = 'close_small';
|
|
|
|
icon.style.color = 'palevioletred';
|
|
|
|
} else {
|
|
|
|
icon.textContent = favoritesList.includes(endpoint) ? 'close_small' : 'add';
|
|
|
|
icon.className = favoritesList.includes(endpoint)
|
|
|
|
? 'material-symbols-rounded favorite-icon close-icon'
|
|
|
|
: 'material-symbols-rounded favorite-icon add-icon';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addToFavorites(entryId) {
|
|
|
|
if (entryId) {
|
|
|
|
const favoritesList = JSON.parse(localStorage.getItem('favoritesList')) || [];
|
|
|
|
const index = favoritesList.indexOf(entryId);
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
favoritesList.push(entryId);
|
|
|
|
console.log(`Added to favorites: ${entryId}`);
|
|
|
|
} else {
|
|
|
|
favoritesList.splice(index, 1);
|
|
|
|
console.log(`Removed from favorites: ${entryId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem('favoritesList', JSON.stringify(favoritesList));
|
|
|
|
updateFavoritesDropdown();
|
|
|
|
updateFavoriteIcons();
|
|
|
|
const currentPath = window.location.pathname;
|
|
|
|
if (currentPath.includes('home-legacy')) {
|
|
|
|
syncFavoritesLegacy();
|
|
|
|
} else {
|
|
|
|
initializeCards();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|