Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 lines
4.7 KiB
JavaScript
Raw Normal View History

2023-06-02 20:15:10 +01:00
function updateFavoritesDropdown() {
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
if (!dropdown) {
console.error('Dropdown element with ID "favoritesDropdown" not found!');
2024-10-14 22:34:41 +01:00
return;
}
dropdown.innerHTML = '';
2023-06-02 20:15:10 +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
for (var i = 0; i < favoritesList.length; i++) {
var value = favoritesList[i];
if (value) {
var navbarEntry = document.querySelector(`a[data-bs-link='${value}']`);
if (navbarEntry) {
2024-10-14 22:34:41 +01:00
var featureName = navbarEntry.textContent.trim();
if (!addedFeatures.has(featureName)) {
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
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
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>';
removeButton.onclick = function (itemKey, event) {
2024-10-14 22:34:41 +01:00
event.preventDefault();
event.stopPropagation();
addToFavorites(itemKey);
2024-10-14 22:34:41 +01:00
updateFavoritesDropdown();
}.bind(null, value);
2024-10-14 22:34:41 +01:00
// Add click event to the content wrapper
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);
}
}
} else {
console.warn(`Navbar entry not found for : ${value}`);
2023-08-22 23:03:21 +01:00
}
}
2023-06-02 20:15:10 +01:00
if (!hasFavorites) {
var defaultItem = document.createElement('a');
defaultItem.className = 'dropdown-item';
defaultItem.textContent = noFavourites || 'No favorites added';
dropdown.appendChild(defaultItem);
}
2023-06-02 20:15:10 +01: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();
}
}
}