78 lines
2.8 KiB
JavaScript
78 lines
2.8 KiB
JavaScript
function dpdPickupWidget($wrapper) {
|
|
let modal = null;
|
|
|
|
const $input = $wrapper.find('[data-delivery-DPDPickup]');
|
|
|
|
const deliveryId = parseInt($wrapper.attr('data-delivery-id-wrapper'));
|
|
const lang = $wrapper?.attr('data-delivery-lang-wrapper') ?? "cs";
|
|
const country = $wrapper?.attr('data-delivery-country-wrapper') ?? "CZ";
|
|
const enabledCountries = JSON.parse($wrapper?.attr('data-delivery-supported-countries-wrapper') || '[]');
|
|
const hiddenPoints = JSON.parse($wrapper?.attr('data-delivery-hidden-points-wrapper') || '[]');
|
|
|
|
// Construct the URL using the URL object
|
|
const iframeUrl = new URL('https://api.dpd.cz/widget/latest/index.html');
|
|
iframeUrl.searchParams.set('countries', country);
|
|
iframeUrl.searchParams.set('lang', lang);
|
|
|
|
// add hidden points params to url
|
|
const pointsParams = {box: {name: "disableLockers", value: true}};
|
|
|
|
hiddenPoints.map(function(key) {
|
|
iframeUrl.searchParams.set(pointsParams[key].name, pointsParams[key].value);
|
|
});
|
|
|
|
// Add all enabled countries to the query parameters
|
|
if (Array.isArray(enabledCountries)) {
|
|
enabledCountries.forEach(enabledCountry => {
|
|
iframeUrl.searchParams.append('enabledCountries', enabledCountry);
|
|
});
|
|
}
|
|
|
|
// 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 width="100%" height="100%" style="padding: 0; margin: 0;" sandbox="allow-scripts allow-same-origin" \n' +
|
|
' src="'+ iframeUrl.toString() +'" \n' +
|
|
'></iframe>\n'));
|
|
},
|
|
focusCustomClass: 'focus-delivery-widgets',
|
|
opened: false
|
|
});
|
|
}
|
|
modal.show();
|
|
});
|
|
|
|
// propsani adresy do hidden inputu
|
|
window.addEventListener("message", (event) => {
|
|
if (event.data.dpdWidget) {
|
|
const widgetData = event.data.dpdWidget;
|
|
const location = widgetData.location?.address ?? {};
|
|
|
|
const objectResult = {
|
|
id: widgetData.id,
|
|
firm: widgetData.name, // Název firmy
|
|
address: {
|
|
street: location.street ?? '',
|
|
city: location.city ?? '',
|
|
zip: location.zip ?? '',
|
|
country: location.country ?? ''
|
|
}
|
|
};
|
|
|
|
$input.val(JSON.stringify(objectResult)).trigger('change');
|
|
|
|
modal.hide();
|
|
}
|
|
}, false);
|
|
|
|
// zavreni widgetu
|
|
window.addEventListener("message", (event) => {
|
|
if (event.data.dpdWidget && event.data.dpdWidget.message === "widgetClose") {
|
|
modal.hide()
|
|
}
|
|
}, false);
|
|
}
|