Files
wyczarujprezent.pl/modules/dpdshipping/views/js/dpdshipping-pudo-iframe.js
2026-02-02 10:46:49 +01:00

180 lines
6.2 KiB
JavaScript

/*
* Copyright 2024 DPD Polska Sp. z o.o.
*
* NOTICE OF LICENSE
*
* Licensed under the EUPL-1.2 or later.
* You may not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
* It is also bundled with this package in the file LICENSE.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Licence is distributed on an AS IS basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions
* and limitations under the Licence.
*
* @author DPD Polska Sp. z o.o.
* @copyright 2024 DPD Polska Sp. z o.o.
* @license https://joinup.ec.europa.eu/software/page/eupl
*/
// noinspection JSUnresolvedReference
console.log('[DPD Pudo] Skrypt załadowany');
if (typeof dpdshippingEventCreated == 'undefined') {
var dpdshippingEventCreated = false;
}
if (typeof dpdshippingIframeCreated == 'undefined') {
var dpdshippingIframeCreated = false;
}
/**
* Tworzy iframe dla mapy PUDO
*/
function dpdshippingCreateIframe() {
console.log('[DPD Pudo] Próba utworzenia iframe...');
// Sprawdź czy iframe już istnieje
if (document.getElementById("dpdshipping-widget-pudo-iframe")) {
console.log('[DPD Pudo] Iframe już istnieje');
return true;
}
// Walidacja: sprawdź czy URL iframe jest zdefiniowany
if (typeof dpdshipping_iframe_url === 'undefined' || !dpdshipping_iframe_url) {
console.error('[DPD Pudo] Błąd: dpdshipping_iframe_url nie jest zdefiniowany lub jest pusty');
return false;
}
// Walidacja: sprawdź czy element docelowy istnieje
var dpdshipping_script = document.getElementById("dpdshipping-widget-pudo");
if (!dpdshipping_script) {
console.error('[DPD Pudo] Błąd: Nie znaleziono elementu #dpdshipping-widget-pudo');
return false;
}
var dpdshipping_iframe = document.createElement("iframe");
dpdshipping_iframe.setAttribute("id", "dpdshipping-widget-pudo-iframe");
dpdshipping_iframe.setAttribute("allow", "geolocation");
dpdshipping_iframe.style.width = "100%";
dpdshipping_iframe.style.border = "none";
dpdshipping_iframe.style.minHeight = "400px";
dpdshipping_iframe.style.height = "768px";
dpdshipping_iframe.onerror = function() {
console.error('[DPD Pudo] Błąd ładowania iframe');
};
dpdshipping_iframe.onload = function() {
console.log('[DPD Pudo] Iframe załadowany pomyślnie');
};
dpdshipping_iframe.src = dpdshipping_iframe_url;
dpdshipping_script.parentNode.insertBefore(dpdshipping_iframe, dpdshipping_script);
console.log('[DPD Pudo] Iframe dodany do DOM, URL:', dpdshipping_iframe_url);
window.dpdshipping_iframe = dpdshipping_iframe;
dpdshippingIframeCreated = true;
return true;
}
// Obserwator DOM
function dpdshippingObserveDOM() {
if (document.getElementById("dpdshipping-widget-pudo")) {
console.log('[DPD Pudo] Element znaleziony w DOM');
dpdshippingCreateIframe();
return;
}
if (typeof MutationObserver !== 'undefined') {
console.log('[DPD Pudo] Uruchamiam MutationObserver...');
var observer = new MutationObserver(function() {
if (document.getElementById("dpdshipping-widget-pudo")) {
console.log('[DPD Pudo] Element pojawił się w DOM (MutationObserver)');
observer.disconnect();
dpdshippingCreateIframe();
}
});
observer.observe(document.body, { childList: true, subtree: true });
setTimeout(function() {
observer.disconnect();
}, 30000);
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
console.log('[DPD Pudo] DOMContentLoaded');
dpdshippingObserveDOM();
});
} else {
console.log('[DPD Pudo] DOM już załadowany');
dpdshippingObserveDOM();
}
$(document).ready(function() {
console.log('[DPD Pudo] jQuery ready');
$(document).on('click', '.dpdshipping-pudo-open-map-btn, .dpdshipping-pudo-change-map-btn', function() {
console.log('[DPD Pudo] Kliknięto przycisk otwierania mapy');
setTimeout(function() {
dpdshippingCreateIframe();
}, 200);
});
$(document).on('show.bs.modal shown.bs.modal', '#dpdshippingPudoModal', function() {
console.log('[DPD Pudo] Modal pokazywany');
dpdshippingCreateIframe();
});
});
if (!dpdshippingEventCreated) {
var dpdshipping_eventListener = window[window.addEventListener ? "addEventListener" : "attachEvent"];
var dpdshipping_messageEvent = ("attachEvent" === dpdshipping_eventListener) ? "onmessage" : "message";
dpdshipping_eventListener(dpdshipping_messageEvent, function (a) {
if (typeof getDpdshippingIdPudoCarrier !== 'function' || typeof getDpdshippingSelectedCarrier !== 'function') {
return;
}
if (getDpdshippingIdPudoCarrier() === getDpdshippingSelectedCarrier()) {
var iframe = window.dpdshipping_iframe || document.getElementById("dpdshipping-widget-pudo-iframe");
if (a.data && a.data.height && !isNaN(a.data.height)) {
if (iframe) {
iframe.style.height = a.data.height + "px";
}
} else if (a.data && a.data.point_id) {
dpdShippingPointSelected(a.data.point_id);
}
}
}, !1);
dpdshippingEventCreated = true;
}
function dpdShippingPointSelected(pudoCode) {
if (!pudoCode) {
console.error('[DPD Pudo] Błąd: Brak kodu punktu');
return;
}
$('.container_dpdshipping_pudo_error').css("display", "none");
$('.dpdshipping-pudo-new-point').css("display", "none");
$('.dpdshipping-pudo-selected-point').css("display", "block");
if (typeof dpdshippingSavePudoCode === 'function') {
dpdshippingSavePudoCode(pudoCode, $('#dpdshippingPudoModal'));
}
if (typeof dpdshippingGetPudoAddress === 'function') {
dpdshippingGetPudoAddress(pudoCode, $('.dpdshipping-selected-point'));
}
console.log('[DPD Pudo] Wybrano punkt:', pudoCode);
}