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

57 lines
2.4 KiB
JavaScript

function DHLServicePointWidget($wrapper) {
let modal = null;
const $input = $wrapper.find('[data-delivery-DHLServicePoint]');
const deliveryId = parseInt($wrapper.attr('data-delivery-id-wrapper'));
const deliveryData = JSON.parse($input.val());
$(document).on('click', '[data-delivery-id="' + deliveryId + '"]', (e) => {
if (modal === null) {
modal = wpj.focus.create({
onDemandEndpoint: (element) => {
// doc: https://static.dhlparcel.nl/components/servicepoint-locator-component%40latest/
$.getScript('https://static.dhlparcel.nl/components/servicepoint-locator-component@latest/servicepoint-locator-component.js').done(function() {
// Load ServicePoint Locator
// Load ServicePoint Locator
let configElement = $('#dhl-servicepoint-locator-component');
let country = $(configElement).attr('data-country');
let limit = $(configElement).attr('data-limit');
let apiKey = $(configElement).attr('data-maps-key');
let locale = $(configElement).attr('data-locale');
let options = {
language: locale,
country: country,
limit: limit,
header: true,
resizable: true,
onSelect: function(info) {
$input.val(JSON.stringify(info)).trigger('change');
modal.hide();
}
};
if (apiKey.length > 0) {
options.googleMapsApiKey = apiKey;
}
new dhl.servicepoint.Locator(element, options);
setTimeout(() => {
$(element).append($('<a href="#" class="focus-close focus-close-dhl" data-focus="close"><span class="fc lightbox_close"></span></a>'));
}, 500);
});
},
focusCustomClass: 'focus-delivery-widgets',
opened: false
});
}
modal.show();
});
$('#cart').on('cartdeliverychange', function(e, data) {
if (parseInt(data.delivery_id) === deliveryId && deliveryData.length === 0) {
setTimeout(() => {
$wrapper.find(':button').trigger('click');
}, 100);
}
});
}