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

77 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-07-05 23:21:43 +02:00
function filterCards() {
var input = document.getElementById("searchBar");
var filter = input.value.toUpperCase();
var cards = document.querySelectorAll(".feature-card");
2023-07-05 23:21:43 +02:00
for (var i = 0; i < cards.length; i++) {
var card = cards[i];
var title = card.querySelector("h5.card-title").innerText;
var text = card.querySelector("p.card-text").innerText;
2024-02-11 17:47:00 +01:00
// Get the navbar tags associated with the card
var navbarItem = document.querySelector(`a.dropdown-item[href="${card.id}"]`);
var navbarTags = navbarItem ? navbarItem.getAttribute("data-bs-tags") : "";
2023-07-24 00:05:02 +02:00
var content = title + " " + text + " " + navbarTags;
2023-07-05 23:21:43 +02:00
if (content.toUpperCase().indexOf(filter) > -1) {
card.style.display = "";
} else {
card.style.display = "none";
2023-07-05 23:21:43 +02:00
}
}
2023-07-05 23:21:43 +02:00
}
2023-06-03 00:43:22 +02:00
function toggleFavorite(element) {
var img = element.querySelector("img");
var card = element.closest(".feature-card");
var cardId = card.id;
if (img.src.endsWith("star.svg")) {
img.src = "images/star-fill.svg";
card.classList.add("favorite");
localStorage.setItem(cardId, "favorite");
} else {
img.src = "images/star.svg";
card.classList.remove("favorite");
localStorage.removeItem(cardId);
}
reorderCards();
updateFavoritesDropdown();
filterCards();
2023-06-03 00:43:22 +02:00
}
function reorderCards() {
var container = document.querySelector(".features-container");
var cards = Array.from(container.getElementsByClassName("feature-card"));
cards.sort(function (a, b) {
var aIsFavorite = localStorage.getItem(a.id) === "favorite";
var bIsFavorite = localStorage.getItem(b.id) === "favorite";
if (aIsFavorite && !bIsFavorite) {
return -1;
}
if (!aIsFavorite && bIsFavorite) {
return 1;
}
return 0;
});
cards.forEach(function (card) {
container.appendChild(card);
});
2023-06-03 00:43:22 +02:00
}
function initializeCards() {
var cards = document.querySelectorAll(".feature-card");
cards.forEach(function (card) {
var cardId = card.id;
var img = card.querySelector(".favorite-icon img");
if (localStorage.getItem(cardId) === "favorite") {
img.src = "images/star-fill.svg";
card.classList.add("favorite");
}
});
reorderCards();
updateFavoritesDropdown();
filterCards();
2023-06-03 00:43:22 +02:00
}
2023-07-05 23:21:43 +02:00
window.onload = initializeCards;