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

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

67 lines
2.9 KiB
JavaScript
Raw Normal View History

2023-06-02 21:15:10 +02:00
document.addEventListener('DOMContentLoaded', function() {
2023-08-28 11:17:02 +02:00
setLanguageForDropdown('.lang_dropdown-item');
2023-06-02 21:15:10 +02:00
const defaultLocale = document.documentElement.lang || 'en_GB';
const storedLocale = localStorage.getItem('languageCode') || defaultLocale;
const dropdownItems = document.querySelectorAll('.lang_dropdown-item');
for (let i = 0; i < dropdownItems.length; i++) {
const item = dropdownItems[i];
item.classList.remove('active');
if (item.dataset.languageCode === storedLocale) {
item.classList.add('active');
}
item.addEventListener('click', handleDropdownItemClick);
}
});
2023-08-28 11:17:02 +02:00
function setLanguageForDropdown(dropdownClass) {
const defaultLocale = document.documentElement.lang || 'en_GB';
const storedLocale = localStorage.getItem('languageCode') || defaultLocale;
const dropdownItems = document.querySelectorAll(dropdownClass);
for (let i = 0; i < dropdownItems.length; i++) {
const item = dropdownItems[i];
item.classList.remove('active');
if (item.dataset.languageCode === storedLocale) {
item.classList.add('active');
}
item.addEventListener('click', handleDropdownItemClick);
}
}
2023-06-02 21:15:10 +02:00
function handleDropdownItemClick(event) {
2023-08-20 22:57:19 +02:00
event.preventDefault();
const languageCode = event.currentTarget.dataset.bsLanguageCode; // change this to event.currentTarget
if (languageCode) {
localStorage.setItem('languageCode', languageCode);
2023-06-02 21:15:10 +02:00
2023-08-20 22:57:19 +02:00
const currentUrl = window.location.href;
if (currentUrl.indexOf('?lang=') === -1) {
window.location.href = currentUrl + '?lang=' + languageCode;
} else {
window.location.href = currentUrl.replace(/\?lang=\w{2,}/, '?lang=' + languageCode);
}
} else {
console.error("Language code is not set for this item."); // for debugging
}
2023-06-02 21:15:10 +02:00
}
2023-08-20 22:57:19 +02:00
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('.nav-item.dropdown').forEach((element) => {
const dropdownMenu = element.querySelector(".dropdown-menu");
2023-08-23 00:03:21 +02:00
if (dropdownMenu.id !== 'favoritesDropdown' && dropdownMenu.children.length <= 2 && dropdownMenu.querySelectorAll("hr.dropdown-divider").length === dropdownMenu.children.length) {
2023-08-20 22:57:19 +02:00
if (element.previousElementSibling && element.previousElementSibling.classList.contains('nav-item') && element.previousElementSibling.classList.contains('nav-item-separator')) {
element.previousElementSibling.remove();
}
element.remove();
}
});
2023-06-02 21:15:10 +02:00
//Sort languages by alphabet
2023-08-20 22:57:19 +02:00
const list = Array.from(document.querySelector('.dropdown-menu[aria-labelledby="languageDropdown"]').children).filter(child => child.matches('a'));
2023-06-02 21:15:10 +02:00
list.sort(function(a, b) {
2024-01-05 19:00:30 +01:00
return a.textContent.toUpperCase().localeCompare(b.textContent.toUpperCase());
2023-08-20 22:57:19 +02:00
}).forEach(node => document.querySelector('.dropdown-menu[aria-labelledby="languageDropdown"]').appendChild(node));
2023-06-02 21:15:10 +02:00
});