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

217 lines
7.8 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 COD] Skrypt załadowany');
if (typeof dpsShippingCodEventCreated == 'undefined') {
var dpsShippingCodEventCreated = false;
}
if (typeof dpsShippingCodIframeCreated == 'undefined') {
var dpsShippingCodIframeCreated = false;
}
/**
* Tworzy iframe dla mapy PUDO COD
*/
function dpsShippingCodCreateIframe() {
console.log('[DPD Pudo COD] Próba utworzenia iframe...');
// Sprawdź czy iframe już istnieje
if (document.getElementById("dpdshipping-widget-pudo-cod-iframe")) {
console.log('[DPD Pudo COD] Iframe już istnieje');
return true;
}
// Walidacja: sprawdź czy URL iframe jest zdefiniowany
if (typeof dpdshipping_iframe_cod_url === 'undefined' || !dpdshipping_iframe_cod_url) {
console.error('[DPD Pudo COD] Błąd: dpdshipping_iframe_cod_url nie jest zdefiniowany lub jest pusty');
return false;
}
// Walidacja: sprawdź czy element docelowy istnieje
var dpsShippingCod_script = document.getElementById("dpdshipping-widget-pudo-cod");
if (!dpsShippingCod_script) {
console.error('[DPD Pudo COD] Błąd: Nie znaleziono elementu #dpdshipping-widget-pudo-cod');
return false;
}
var dpsShippingCod_iframe = document.createElement("iframe");
dpsShippingCod_iframe.setAttribute("id", "dpdshipping-widget-pudo-cod-iframe");
dpsShippingCod_iframe.setAttribute("allow", "geolocation");
dpsShippingCod_iframe.style.width = "100%";
dpsShippingCod_iframe.style.border = "none";
dpsShippingCod_iframe.style.minHeight = "400px";
dpsShippingCod_iframe.style.height = "768px";
dpsShippingCod_iframe.onerror = function() {
console.error('[DPD Pudo COD] Błąd ładowania iframe');
};
dpsShippingCod_iframe.onload = function() {
console.log('[DPD Pudo COD] Iframe załadowany pomyślnie');
};
dpsShippingCod_iframe.src = dpdshipping_iframe_cod_url;
dpsShippingCod_script.parentNode.insertBefore(dpsShippingCod_iframe, dpsShippingCod_script);
console.log('[DPD Pudo COD] Iframe dodany do DOM, URL:', dpdshipping_iframe_cod_url);
window.dpsShippingCod_iframe = dpsShippingCod_iframe;
dpsShippingCodIframeCreated = true;
return true;
}
// Obserwator DOM
function dpsShippingCodObserveDOM() {
if (document.getElementById("dpdshipping-widget-pudo-cod")) {
console.log('[DPD Pudo COD] Element znaleziony w DOM');
dpsShippingCodCreateIframe();
return;
}
if (typeof MutationObserver !== 'undefined') {
console.log('[DPD Pudo COD] Uruchamiam MutationObserver...');
var observer = new MutationObserver(function() {
if (document.getElementById("dpdshipping-widget-pudo-cod")) {
console.log('[DPD Pudo COD] Element pojawił się w DOM (MutationObserver)');
observer.disconnect();
dpsShippingCodCreateIframe();
}
});
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 COD] DOMContentLoaded');
dpsShippingCodObserveDOM();
});
} else {
console.log('[DPD Pudo COD] DOM już załadowany');
dpsShippingCodObserveDOM();
}
$(document).ready(function() {
console.log('[DPD Pudo COD] jQuery ready');
$(document).on('click', '.dpdshipping-pudo-cod-open-map-btn, .dpdshipping-pudo-cod-change-map-btn', function() {
console.log('[DPD Pudo COD] Kliknięto przycisk otwierania mapy');
setTimeout(function() {
dpsShippingCodCreateIframe();
}, 200);
});
$(document).on('show.bs.modal shown.bs.modal', '#dpdshippingPudoCodModal', function() {
console.log('[DPD Pudo COD] Modal pokazywany');
dpsShippingCodCreateIframe();
});
});
if (!dpsShippingCodEventCreated) {
var dpsShippingCod_eventListener = window[window.addEventListener ? "addEventListener" : "attachEvent"];
var dpsShippingCod_messageEvent = ("attachEvent" === dpsShippingCod_eventListener) ? "onmessage" : "message";
dpsShippingCod_eventListener(dpsShippingCod_messageEvent, function (a) {
if (typeof getDpdshippingIdPudoCodCarrier !== 'function' || typeof getDpdshippingSelectedCarrier !== 'function') {
return;
}
if (getDpdshippingIdPudoCodCarrier() === getDpdshippingSelectedCarrier()) {
var iframe = window.dpsShippingCod_iframe || document.getElementById("dpdshipping-widget-pudo-cod-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) {
dpdShippingCodPointSelected(a.data.point_id);
}
}
}, !1);
dpsShippingCodEventCreated = true;
}
function dpdShippingCodPointSelected(pudoCode) {
if (!pudoCode) {
console.error('[DPD Pudo COD] Błąd: Brak kodu punktu');
return;
}
$('.container_dpdshipping_pudo_cod_error').css("display", "none");
$('.dpdshipping-pudo-cod-new-point').css("display", "none");
$('.dpdshipping-pudo-cod-selected-point').css("display", "block");
if (typeof dpdshippingSavePudoCode === 'function') {
dpdshippingSavePudoCode(pudoCode, $('#dpdshippingPudoCodModal'));
}
if (typeof dpdshippingGetPudoAddress === 'function') {
dpdshippingGetPudoAddress(pudoCode, $('.dpdshipping-cod-selected-point'));
}
if (typeof dpdshippingIsPointWithCod === 'function') {
dpdshippingIsPointWithCod(pudoCode);
} else {
dpdshippingIsPointWithCodInternal(pudoCode);
}
console.log('[DPD Pudo COD] Wybrano punkt:', pudoCode);
}
function dpdshippingIsPointWithCodInternal(pudoCode) {
if (typeof dpdshipping_pickup_is_point_with_cod_ajax_url === 'undefined') {
console.error('[DPD Pudo COD] Brak URL dla sprawdzenia COD');
return;
}
$.ajax({
url: dpdshipping_pickup_is_point_with_cod_ajax_url,
type: 'GET',
data: {
dpdshipping_token: dpdshipping_token,
dpdshipping_csrf: dpdshipping_csrf,
dpdshipping_pudo_code: pudoCode
},
success: function (response) {
var resultJson = JSON.parse(response);
if (resultJson.success && resultJson.data && Number(resultJson.data) === 1) {
$('.container_dpdshipping_pudo_cod_warning').css("display", "none");
} else {
$('.container_dpdshipping_pudo_cod_warning').css("display", "block");
if (typeof dpdshippingDisableOrderProcessBtn === 'function') {
dpdshippingDisableOrderProcessBtn();
}
}
},
error: function (error) {
console.error('[DPD Pudo COD] Błąd sprawdzania COD:', error);
}
});
}