(function () {
// Cache dla często używanych selektorów
const selectors = {
menuIconEu: '.menu-icon-eu',
breadcrumbItem: '.breadcrumb__item',
sections: 'section',
mobileMenuButton: 'a.menu__button.menu__button--mobile',
desktopMenuButton: 'a.menu__button.menu__button--desktop',
skipButton: 'a.btn-skip.btn-skip--base',
main: 'main',
toTop: '#toTop',
scrollToTop: '#scrollToTop',
articlesGrid: '.articles-grid.js-pagination-result-container-news',
publicationDate: 'time.article-tile__publication-date',
mailtoLinks: 'a[href^="mailto:"]',
informationBlockBtn: '.information-block__btn'
};
document.addEventListener('DOMContentLoaded', () => {
const pathname = document.location.pathname;
const isSpecialPage = pathname.includes('bip') ||
pathname.includes('sklep') ||
pathname.includes('/mapa-parku');
if (!isSpecialPage) {
initializeMainPageFeatures(pathname);
}
// Funkcje specyficzne dla mapy parku
if (pathname.includes('/mapa-parku')) {
initializeMapLegend();
}
});
function initializeMainPageFeatures(pathname) {
// Wszystkie funkcje dla głównych stron
addEuProjectsLink();
addBackButtonToBreadcrumbs();
removeMobileShopLink();
addScrollToTopButton();
addPublicationDates(pathname);
cloakEmailAddresses();
removeMultikonto();
removeFormButtonFromZalatwSprawa(pathname);
}
// 1. Dodanie linku do projektów unijnych
function addEuProjectsLink() {
const euIcon = document.querySelector(selectors.menuIconEu);
if (!euIcon) return;
const wrapper = document.createElement('a');
wrapper.href = '/projekty-unijne';
wrapper.setAttribute('aria-label', 'Projekty unijne');
euIcon.parentNode.insertBefore(wrapper, euIcon);
wrapper.appendChild(euIcon);
// Aktualizacja tytułów SVG
const svgTitles = euIcon.querySelectorAll('title');
svgTitles.forEach(title => title.textContent = 'Projekty unijne');
}
// 2. Dodanie przycisku powrotu do breadcrumbs
function addBackButtonToBreadcrumbs() {
const breadcrumbItems = document.querySelectorAll(selectors.breadcrumbItem);
const sections = document.querySelectorAll(selectors.sections);
if (breadcrumbItems.length === 0 || sections.length === 0) return;
const lastSection = sections[sections.length - 1];
if (lastSection.className === 'banner') return;
const secondLastCrumb = breadcrumbItems[breadcrumbItems.length - 2];
if (!secondLastCrumb?.firstElementChild?.href) return;
const backButtonHtml = `
`;
lastSection.insertAdjacentHTML('afterend', backButtonHtml);
}
// 3. Usunięcie linku sklepu z menu mobilnego
function removeMobileShopLink() {
const mobileMenuButton = document.querySelector(selectors.mobileMenuButton);
if (!mobileMenuButton) return;
const parentElement = mobileMenuButton.parentElement;
const isShopPage = document.location.pathname.includes('/sklep');
parentElement.style.gridTemplateColumns = isShopPage ?
'repeat(4,1fr)' : 'repeat(3,1fr)';
mobileMenuButton.remove();
}
// 4. Dodanie przycisku przewijania do góry
function addScrollToTopButton() {
const existingSkipButton = document.querySelector(selectors.skipButton);
// Sprawdź czy przycisk już istnieje lub czy istnieje przycisk skip
if ((existingSkipButton && existingSkipButton.clientHeight > 0) ||
document.querySelector(selectors.toTop)) {
return;
}
const main = document.querySelector(selectors.main);
if (!main) return;
const buttonHtml = `
`;
main.insertAdjacentHTML('beforeend', buttonHtml);
// Dodanie funkcjonalności przewijania
const scrollButton = document.querySelector(selectors.toTop);
let ticking = false;
function updateScrollButton() {
const shouldShow = document.documentElement.scrollTop > 1000;
scrollButton.style.display = shouldShow ? 'block' : 'none';
ticking = false;
}
function onScroll() {
if (!ticking) {
requestAnimationFrame(updateScrollButton);
ticking = true;
}
}
window.addEventListener('scroll', onScroll, { passive: true });
}
// 5. Dodanie dat publikacji
function addPublicationDates(pathname) {
if (pathname !== '/aktualnosci' && pathname !== '/') return;
function updatePublicationDates() {
const publicationDates = document.querySelectorAll(selectors.publicationDate);
publicationDates.forEach(date => {
if (date.hasAttribute('datetime') &&
!date.textContent.includes('Opublikowano: ')) {
date.textContent = `Opublikowano: ${date.dateTime}`;
}
});
}
// Pierwsze uruchomienie
updatePublicationDates();
// Observer tylko na stronie aktualności (przycisk "pokaż więcej")
if (pathname === '/aktualnosci') {
const articlesGrid = document.querySelector(selectors.articlesGrid);
if (articlesGrid) {
const observer = new MutationObserver(updatePublicationDates);
observer.observe(articlesGrid, {
subtree: true,
childList: true,
attributes: false,
characterData: false
});
}
}
}
// 6. Maskowanie adresów email
function cloakEmailAddresses() {
const mailLinks = document.querySelectorAll(selectors.mailtoLinks);
if (mailLinks.length === 0) return;
mailLinks.forEach(mailLink => {
const email = mailLink.getAttribute('href')?.replace('mailto:', '');
if (!email) return;
mailLink.innerHTML = mailLink.innerHTML.replace('@', '@');
mailLink.setAttribute('href', '#');
mailLink.addEventListener('click', (e) => {
e.preventDefault();
window.location.href = `mailto:${email}`;
});
});
}
// 7. Usunięcie multikonta
function removeMultikonto() {
const multikontoButton = document.querySelector(selectors.desktopMenuButton);
multikontoButton?.remove();
}
// 8. Usunięcie przycisku formularza z "załatw sprawę"
function removeFormButtonFromZalatwSprawa(pathname) {
if (!pathname.includes('/zalatw-sprawe')) return;
const formButton = document.querySelector(selectors.informationBlockBtn);
formButton?.remove();
}
// 9. Korekta legendy mapy
function initializeMapLegend() {
const trailLabel = document.querySelector('label[for="hiking_trail"]');
const prTrailElement = document.getElementById('pieszy/rowerowy');
const pnTrailElement = document.getElementById('pieszy/narciarski');
if (trailLabel) {
trailLabel.textContent = 'Pieszy/Rowerowy';
const iconPath = trailLabel.previousElementSibling?.childNodes?.[3];
if (iconPath) {
iconPath.innerHTML = `
`;
}
}
// Usunięcie niepotrzebnych elementów
[prTrailElement, pnTrailElement]
.filter(Boolean)
.forEach(element => element.parentElement?.remove());
}
})();