Files
2026-04-13 15:50:16 +02:00

222 lines
7.9 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('.apartament-gallery-swiper').forEach(function (el) {
new Swiper(el, {
slidesPerView: 1,
spaceBetween: 10,
loop: true,
zoom: false,
navigation: {
nextEl: el.parentElement.querySelector('.swiper-button-next'),
prevEl: el.parentElement.querySelector('.swiper-button-prev'),
},
});
});
Fancybox.bind('[data-fancybox]', {
animated: true,
showClass: 'fancybox-fadeIn',
hideClass: 'fancybox-fadeOut',
});
});
// Historia cen
document.addEventListener('DOMContentLoaded', function () {
function formatPrice(value) {
if (value === null || value === undefined) {
return '';
}
var normalized = String(value)
.replace(/\u00a0/g, '')
.replace(/\s+/g, '')
.replace(/zł|zl/gi, '')
.replace(/[^0-9,.-]/g, '');
if (!normalized) {
return '';
}
var lastComma = normalized.lastIndexOf(',');
var lastDot = normalized.lastIndexOf('.');
if (lastComma !== -1 && lastDot !== -1) {
if (lastComma > lastDot) {
normalized = normalized.replace(/\./g, '').replace(',', '.');
} else {
normalized = normalized.replace(/,/g, '');
}
} else if (lastComma !== -1) {
normalized = normalized.replace(/\./g, '').replace(',', '.');
} else {
var dotParts = normalized.split('.');
if (dotParts.length > 2) {
normalized = dotParts.slice(0, -1).join('') + '.' + dotParts[dotParts.length - 1];
}
}
var amount = Number(normalized);
if (Number.isNaN(amount)) {
return String(value).trim();
}
return amount.toLocaleString('pl-PL', {
minimumFractionDigits: 0,
maximumFractionDigits: 0,
});
}
var overlay = document.getElementById('price-history-overlay');
var closeBtn = document.getElementById('price-history-close');
var elTitle = document.getElementById('price-history-title');
var elPrice = document.getElementById('price-history-price');
var elPriceM2= document.getElementById('price-history-sqm');
var elTbody = document.getElementById('price-history-tbody');
if (!overlay || !closeBtn || !elTitle || !elPrice || !elPriceM2 || !elTbody) {
console.warn('[historia-cen] Brakuje elementów popupa w DOM:', {
overlay: !!overlay, closeBtn: !!closeBtn,
elTitle: !!elTitle, elPrice: !!elPrice,
elPriceM2: !!elPriceM2, elTbody: !!elTbody
});
return;
}
function openPopup() {
overlay.setAttribute('aria-hidden', 'false');
overlay.classList.add('is-open');
document.body.style.overflow = 'hidden';
}
function closePopup() {
overlay.setAttribute('aria-hidden', 'true');
overlay.classList.remove('is-open');
document.body.style.overflow = '';
}
closeBtn.addEventListener('click', closePopup);
overlay.addEventListener('click', function (e) {
if (e.target === overlay) closePopup();
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && overlay.classList.contains('is-open')) closePopup();
});
document.querySelectorAll('.btn-historia-cen').forEach(function (btn) {
btn.addEventListener('click', function () {
var postId = this.dataset.postId;
if (!postId) return;
elTitle.textContent = 'Ładowanie...';
elPrice.textContent = '';
elPriceM2.textContent = '';
elTbody.innerHTML = '';
openPopup();
if (typeof apartamentsData === 'undefined') {
elTitle.textContent = 'Błąd konfiguracji';
return;
}
var formData = new FormData();
formData.append('action', 'apartamenty_get_price_history');
formData.append('post_id', postId);
formData.append('nonce', apartamentsData.nonce);
fetch(apartamentsData.ajaxUrl, {
method: 'POST',
body: formData,
credentials: 'same-origin',
})
.then(function (res) { return res.json(); })
.then(function (json) {
if (!json.success) {
elTitle.textContent = 'Brak danych';
return;
}
var d = json.data;
elTitle.textContent = d.title || '';
elPrice.textContent = d.price ? formatPrice(d.price) + ' zł' : '—';
elPriceM2.textContent = d.price_m2 ? formatPrice(d.price_m2) + ' zł' : '—';
if (!d.history || d.history.length === 0) {
elTbody.innerHTML = '<tr><td colspan="3">Brak historii cen</td></tr>';
return;
}
elTbody.innerHTML = d.history.map(function (row) {
return '<tr>' +
'<td>' + (row.recorded_at || '') + '</td>' +
'<td>' + (row.price_m2 ? formatPrice(row.price_m2) + ' zł/m²' : '—') + '</td>' +
'<td>' + (row.price ? formatPrice(row.price) + ' zł' : '—') + '</td>' +
'</tr>';
}).join('');
})
.catch(function () {
elTitle.textContent = 'Błąd ładowania';
});
});
});
document.querySelectorAll('.btn-parking-historia-cen').forEach(function (btn) {
btn.addEventListener('click', function () {
var parkingType = this.dataset.parkingType;
if (!parkingType) return;
elTitle.textContent = 'Ładowanie...';
elPrice.textContent = '';
elPriceM2.textContent = '';
elTbody.innerHTML = '';
openPopup();
if (typeof apartamentsData === 'undefined') {
elTitle.textContent = 'Błąd konfiguracji';
return;
}
var formData = new FormData();
formData.append('action', 'parking_get_price_history');
formData.append('parking_type', parkingType);
formData.append('nonce', apartamentsData.nonce);
fetch(apartamentsData.ajaxUrl, {
method: 'POST',
body: formData,
credentials: 'same-origin',
})
.then(function (res) { return res.json(); })
.then(function (json) {
if (!json.success) {
elTitle.textContent = 'Brak danych';
return;
}
var d = json.data;
elTitle.textContent = d.title || '';
elPrice.textContent = d.price ? formatPrice(d.price) + ' zł' : '—';
elPriceM2.textContent = d.price_m2 ? formatPrice(d.price_m2) + ' zł' : '—';
if (!d.history || d.history.length === 0) {
elTbody.innerHTML = '<tr><td colspan="3">Brak historii cen</td></tr>';
return;
}
elTbody.innerHTML = d.history.map(function (row) {
return '<tr>' +
'<td>' + (row.recorded_at || '') + '</td>' +
'<td>' + (row.price_m2 ? formatPrice(row.price_m2) + ' zł/m²' : '—') + '</td>' +
'<td>' + (row.price ? formatPrice(row.price) + ' zł' : '—') + '</td>' +
'</tr>';
}).join('');
})
.catch(function () {
elTitle.textContent = 'Błąd ładowania';
});
});
});
});