Files
kupshop/web/common/static/wpj/delivery/wpj.GLSParcelShop.widget.js
2025-08-02 16:30:27 +02:00

64 lines
2.1 KiB
JavaScript

function GLSParcelShopWidget(deliveryId, $input, activeCountry = null) {
let modal = null;
let src = 'https://maps.gls-czech.cz/?find=1';
const deliveryData = $input.val() ? JSON.parse($input.val()) : null;
if(typeof activeCountry === "string") {
if(activeCountry.toUpperCase() === 'SK') {
src += "&ctrcode=SK";
}
if(activeCountry.toUpperCase() === 'HU') {
src += "&ctrcode=HU&lng=en";
}
if(activeCountry.toUpperCase() === 'RO') {
src += "&ctrcode=RO&lng=en";
}
if(activeCountry.toUpperCase() === 'SI') {
src += "&ctrcode=SI&lng=en";
}
if(activeCountry.toUpperCase() === 'HR') {
src += "&ctrcode=HR&lng=en";
}
}
function handlePoint(m) {
if (m.origin === 'https://maps.gls-czech.cz' && m.data.parcelshop) {
if (modal !== null) {
var ps = m.data.parcelshop.detail;
$input.val(JSON.stringify(ps)).trigger('change');
modal.hide();
}
}
}
// Click je na dokument, protože se reloadem na FE odpojí eventa
$(document).on('click', '[data-delivery-id="' + deliveryId + '"]', (e) => {
if (modal === null) {
modal = wpj.focus.create({
onDemandEndpoint: (element) => {
$(element).html($('<a href="#" class="focus-close" data-focus="close"><span class="fc lightbox_close"></span></a>' +
'<iframe src="' + src + '" width="100%" height="100%" style="padding: 0; margin: 0;" sandbox="allow-scripts allow-same-origin" allow="geolocation"></iframe>'));
},
focusCustomClass: 'focus-delivery-widgets',
opened: false,
show: function() {
window.addEventListener('message', handlePoint);
},
hide: function() {
window.removeEventListener('message', handlePoint);
}
});
}
modal.show();
});
$('#cart').on('cartdeliverychange', function(e, data) {
if (parseInt(data.delivery_id) === deliveryId && !deliveryData) {
setTimeout(() => {
$input.trigger('click');
}, 100);
}
});
}