4,554
edits
No edit summary Tag: Reverted |
No edit summary |
||
| (89 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
$(document).ready(function () { | |||
// Global variables | |||
var cards = $(".card"); | |||
var showArticleWrapper = $("#show-article-wrapper"); | |||
var areFiltersActive = false; | |||
// Make header-box in Home clickable | |||
$(".head-box").click(function () { | |||
window.location.href = "/Main_Page"; // Redirects to the home page | |||
}); | |||
// Loop through each card to format related articles | |||
cards.each(function () { | |||
// Check if the card has related articles | |||
var relatedArticles = $(this).find(".related-articles"); | |||
if (relatedArticles.length > 0) { | |||
// Get all the related article elements | |||
var relatedArticleElements = relatedArticles.find(".related-article"); | |||
// Create an array to store unique related articles | |||
var uniqueArticles = []; | |||
// Loop through each related article element | |||
relatedArticleElements.each(function () { | |||
// Remove <p> tags from the article | |||
$(this).find("p").remove(); | |||
// Convert the article HTML to a string | |||
var articleHTML = $(this)[0].outerHTML; | |||
// Check if the article HTML already exists in the uniqueArticles array | |||
if ($.inArray(articleHTML, uniqueArticles) === -1) { | |||
// If it doesn't exist, add it to the uniqueArticles array | |||
uniqueArticles.push(articleHTML); | |||
} | |||
}); | |||
// Clear the content of the related articles container | |||
relatedArticles.empty(); | |||
// Append the unique related articles back to the container | |||
relatedArticles.append(uniqueArticles.join("")); | |||
} | |||
}); | |||
// Utility Functions | |||
function sortChronologically() { | |||
var cards = $(".list-container .card").get(); | |||
cards.sort(function (a, b) { | |||
var numberA = parseInt( | |||
$(a).find(".entry-number").text().replace(/\[|\]/g, ""), | |||
10 | |||
); | |||
var numberB = parseInt( | |||
$(b).find(".entry-number").text().replace(/\[|\]/g, ""), | |||
10 | |||
); | |||
return numberB - numberA; // Descending order | |||
}); | |||
$.each(cards, function (index, item) { | |||
$(".list-container").append(item); | |||
}); | |||
} | |||
function randomizeCards(selector) { | |||
var cards = $(selector).get(); | |||
var i = cards.length, | |||
j, | |||
temp; | |||
while (--i > 0) { | |||
j = Math.floor(Math.random() * (i + 1)); | |||
temp = cards[i]; | |||
cards[i] = cards[j]; | |||
cards[j] = temp; | |||
} | |||
$.each(cards, function (index, item) { | |||
$(selector).parent().append(item); | |||
}); | |||
} | |||
function sortAlphabetically(selector) { | |||
var cards = $(selector).get(); | |||
cards.sort(function (a, b) { | |||
var titleA = $(a).find(".title").text().toUpperCase(); | |||
var titleB = $(b).find(".title").text().toUpperCase(); | |||
return titleA < titleB ? -1 : titleA > titleB ? 1 : 0; | |||
}); | |||
$.each(cards, function (index, item) { | |||
$(selector).parent().append(item); | |||
}); | |||
} | |||
function updateViews() { | |||
// Handle cards in the list view | |||
$(".home-chronicle-list div.list-container div.card:not(.event)").each( | |||
function () { | |||
if (!$(this).closest(".home-chronicle-block").length) { | |||
var title = $(this).find(".title").detach(); | |||
var images = $(this).find(".images").detach(); | |||
// Remove existing .title-images if it exists | |||
$(this).find(".title-images").remove(); | |||
var titleImagesContainer = $( | |||
'<div class="title-images"></div>' | |||
).append(images, title); | |||
$(this).find(".people").after(titleImagesContainer); | |||
} | |||
} | |||
); | |||
// Handle cards in the block view | |||
$(".home-chronicle-block div.list-container div.card:not(.event)").each( | |||
function () { | |||
// Remove .title-images container if it exists, re-attach .title and .images to their original places | |||
var titleImagesContainer = $(this).find(".title-images"); | |||
if (titleImagesContainer.length) { | |||
var title = titleImagesContainer.find(".title").detach(); | |||
var images = titleImagesContainer.find(".images").detach(); | |||
titleImagesContainer.remove(); | |||
$(this).find(".people").after(title); | |||
$(this).find(".type").after(images); | |||
} else { | |||
// If .title-images doesn't exist, ensure .images is placed correctly | |||
var images = $(this).find(".images").detach(); | |||
$(this).find(".type").after(images); | |||
} | |||
} | |||
); | |||
} | |||
function processEventCards() { | |||
$(".card.event").each(function () { | |||
var $card = $(this); | |||
var existingContainer = $card.find(".container-people-date"); | |||
// Create container if missing | |||
if (existingContainer.length === 0) { | |||
existingContainer = $('<div class="container-people-date"></div>'); | |||
$card.append(existingContainer); // temp placement | |||
} | |||
// Detach people and date | |||
var people = $card.find(".people").detach(); | |||
var date = $card.find(".date").detach(); | |||
// BLOCK VIEW (gallery) | |||
if ($card.closest(".home-chronicle-block").length) { | |||
existingContainer.append(people).append(date); | |||
// Place container after title | |||
if (!existingContainer.is($card.find(".title").next())) { | |||
$card.find(".title").after(existingContainer); | |||
} | |||
// LIST VIEW | |||
} else if ($card.closest(".home-chronicle-list").length) { | |||
// Only append .people into container | |||
existingContainer.empty().append(people); | |||
// Place container before title | |||
$card.find(".title").before(existingContainer); | |||
// Place date after title | |||
$card.find(".title").after(date); | |||
} | |||
}); | |||
} | |||
if ($("#home").length > 0) { | |||
// This code will only run only on the homepage. | |||
$(".home-block-view").show(); | |||
$(".home-chronicle-block-button, .home-block-view-button").addClass( | |||
"active-view-button" | |||
); | |||
// Initially hide list view sorting buttons and set the default sorted view for block | |||
$( | |||
".home-chronicle-list-button, .home-random-list-button, .home-alphabetical-list-button" | |||
).hide(); | |||
sortChronologically(); // Sort the block view chronologically by default | |||
updateLastVisibleCard(); | |||
updateWidthBlockView(); | |||
processEventCards(); | |||
updateViews(); | |||