50 lines
2.0 KiB
JavaScript
50 lines
2.0 KiB
JavaScript
|
|
function initSorter() {
|
|
const listContainer = document.querySelector('.s-res-list');
|
|
const sortRadios = document.querySelectorAll('.sort-radio');
|
|
|
|
// wenn keine liste vorhanden, abbrechen
|
|
if (!listContainer || sortRadios.length === 0) return;
|
|
|
|
sortRadios.forEach(radio => {
|
|
radio.addEventListener('change', function() {
|
|
const cards = Array.from(listContainer.querySelectorAll('.s-res-item'));
|
|
const sortValue = this.value;
|
|
|
|
cards.sort((a, b) => {
|
|
if (sortValue === 'likes') {
|
|
const likesA = parseInt(a.getAttribute('data-likes') || '0', 10);
|
|
const likesB = parseInt(b.getAttribute('data-likes') || '0', 10);
|
|
return likesB - likesA;
|
|
}
|
|
else if (sortValue === 'alphabet') {
|
|
// alphabetische sortierung
|
|
const titleA = a.querySelector('.s-res-link').textContent.trim().toLowerCase();
|
|
const titleB = b.querySelector('.s-res-link').textContent.trim().toLowerCase();
|
|
return titleA.localeCompare(titleB);
|
|
}
|
|
else if (sortValue === 'newest' || sortValue === 'oldest') {
|
|
// hoehere ID wird als neuer gesehen
|
|
const urlA = a.querySelector('.s-res-link').getAttribute('href');
|
|
const urlB = b.querySelector('.s-res-link').getAttribute('href');
|
|
|
|
const idA = parseInt(urlA.match(/id=(\d+)/)[1], 10);
|
|
const idB = parseInt(urlB.match(/id=(\d+)/)[1], 10);
|
|
|
|
return sortValue === 'newest' ? idB - idA : idA - idB;
|
|
}
|
|
return 0;
|
|
});
|
|
|
|
listContainer.innerHTML = '';
|
|
cards.forEach(card => listContainer.appendChild(card));
|
|
});
|
|
});
|
|
}
|
|
|
|
// ist das DOM bereits vollständig aufgebaut?
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', initSorter);
|
|
} else {
|
|
initSorter();
|
|
} |