You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

91 lines
3.2 KiB

$(document).ready(function(){
//$(".alist li").click(function(){
//console.log("lolololol");
//var ctnt = $(this).find('.content-wrapper')
//$(ctnt).toggleClass("open")
//});
const sortState = {};
// Function to sort articles
function sortArticles(columnClass) {
const $list = $('.alist');
const $listItems = $list.children('li');
// Determine sort direction
if (!sortState[columnClass]) {
sortState[columnClass] = 'asc';
} else {
sortState[columnClass] = (sortState[columnClass] === 'asc') ? 'desc' : 'asc';
}
// Sort the list items based on the text of the specified column
$listItems.sort(function(a, b) {
const aText = $(a).find('.' + columnClass).text().trim().toLowerCase();
const bText = $(b).find('.' + columnClass).text().trim().toLowerCase();
// Special handling for period (numeric sorting)
if (columnClass === 'period') {
const aNum = parseInt(aText);
const bNum = parseInt(bText);
return sortState[columnClass] === 'asc'
? aNum - bNum
: bNum - aNum;
}
// Default alphabetical sorting
return sortState[columnClass] === 'asc'
? aText.localeCompare(bText)
: bText.localeCompare(aText);
});
// Detach and reappend sorted items to maintain DOM structure
$list.empty().append($listItems);
// Optional: Visual indication of sort direction
$('.legend li ').removeClass('sort-asc sort-desc');
$(`.legend li[data-sort="${columnClass}"]`).addClass(sortState[columnClass] === 'asc' ? 'sort-asc' : 'sort-desc');
//$(`.legend li:contains("${columnClass}")`).addClass(sortState[columnClass] === 'asc' ? 'sort-asc' : 'sort-desc');
//$(`.legend li `).attr('data-sort').addClass(sortState[columnClass] === 'asc' ? 'sort-asc' : 'sort-desc');
}
// Add click event to legend spans
$('.legend li ').on('click', function() {
// Get the class corresponding to the clicked span
//const columnClass = $(this).text().toLowerCase().replace(/\s+/g, '');
const columnClass = $(this).attr("data-sort")
// If the column exists in the article items, sort by that column
if ($('.alist li').first().find('.' + columnClass).length > 0) {
sortArticles(columnClass);
}
});
$(".alist li a ").click(function(e) {
// Prevent this event from interfering with sorting or default link behavior
// e.preventDefault();
console.log("lolololol");
var ctnt = $(this).find('.content-wrapper');
if (ctnt.length === 0) {
ctnt = $(this).closest('li').find('.content-wrapper');
}
$(ctnt).toggleClass("open");
});
//$(".alist").on('click', 'li', function(e) {
//// Prevent this event from interfering with sorting
//if ($(e.target).closest('.legend').length === 0) {
//console.log("lolololol");
//var ctnt = $(this).find('.content-wrapper');
//$(ctnt).toggleClass("open");
//}
//});
})