1/* global jQuery, SendcloudLocaleMessages */ 2jQuery(function ($) { 3 let widgetStates = {}; 4 let postalCodeInputField = document.getElementById('billing_postcode'); 5 6 if (postalCodeInputField) { 7 postalCodeInputField.addEventListener('change', (event) => { 8 if (SendcloudShippingData) { 9 SendcloudShippingData['postal_code'] = postalCodeInputField.value; 10 } 11 }); 12 } 13 14 /** 15 * @type {jQuery|HTMLElement|*} 16 */ 17 let $checkoutForm = $('form.checkout'); 18 19 if (0 === $checkoutForm.length || !window.renderScShippingOptionModule) { 20 return; 21 } 22 23 let $selectedShippingMethod, 24 shippingMethodSelectorPrefix, 25 mountElement, 26 deliveryMethod, 27 locale, 28 state, 29 renderedWidgetDestructor; 30 31 $(document.body).on('updated_checkout', function () { 32 window.renderScShippingOptionModule.then(onCheckoutUpdate); 33 }); 34 35 function onCheckoutUpdate(){ 36 if (renderedWidgetDestructor) { 37 renderedWidgetDestructor.call(); 38 } 39 $selectedShippingMethod = findSelectedShippingMethod(); 40 if (0 === $selectedShippingMethod.length) { 41 return; 42 } 43 44 initShippingMethodSelectorPrefix(); 45 46 let deliveryMethodConfig = $(`#${shippingMethodSelectorPrefix}_delivery_method`).html() || null; 47 mountElement = $(`#${shippingMethodSelectorPrefix}_mount_point`).get(0); 48 deliveryMethod = JSON.parse(deliveryMethodConfig); 49 locale = $(`#${shippingMethodSelectorPrefix}_locale`).val() || 'en-US'; 50 state = widgetStates[shippingMethodSelectorPrefix]; 51 52 if (mountElement) { 53 mountElement.classList.add('sc-delivery-method-mount-point'); 54 mountElement.setAttribute('data-sc-delivery-method-type', deliveryMethod.delivery_method_type) 55 } 56 57 let descriptionItems = document.getElementsByClassName('sc-delivery-method-description'); 58 for (let i = 0; i < descriptionItems.length; i++) { 59 descriptionItems[i].parentElement.querySelector('label').classList.add('sc-delivery-method-title'); 60 } 61 62 renderWidget(); 63 } 64 65 function initShippingMethodSelectorPrefix() { 66 shippingMethodSelectorPrefix = $selectedShippingMethod.attr('id'); 67 68 // If default prefix does not work try to adopt for the Woo 2.6.X versions where single shipping method id is 69 // not generated uniformly as when there are multiple shipping methods available on the checkout (instance id 70 // is committed from id attribute for versions 2.6.X) 71 if (!$(`#${shippingMethodSelectorPrefix}_delivery_method`).html()) { 72 shippingMethodSelectorPrefix += '_' + $selectedShippingMethod.val().replace(/:/g, ''); 73 } 74 } 75 76 /** 77 * @return {jQuery|HTMLElement|*} 78 */ 79 function findSelectedShippingMethod() { 80 let name = 'shipping_method'; 81 return $checkoutForm.find( 82 `select.${name}, input[name^="${name}"][type="radio"]:checked, input[name^="${name}"][type="hidden"]` 83 ); 84 } 85 86 function renderWidget() { 87 if (!mountElement || !deliveryMethod) { 88 return; 89 } 90 91 $(mountElement).on('scShippingOptionChange', onShippingOptionChange); 92 window.renderScShippingOption({ 93 mountElement: mountElement, 94 deliveryMethod: deliveryMethod, 95 shippingData: SendcloudShippingData ?? {}, 96 renderDate: new Date(), 97 locale: locale, 98 state: state, 99 localeMessages: SendcloudLocaleMessages, 100 101 }).then(function(destructorCallback) { 102 renderedWidgetDestructor = destructorCallback; 103 }); 104 } 105 106 function onShippingOptionChange(event) { 107 108 widgetStates[shippingMethodSelectorPrefix] = event.detail.state; 109 110 let $selectionDataElement = $(`#${shippingMethodSelectorPrefix}_submit_data`); 111 if (0 !== $selectionDataElement.length) { 112 $selectionDataElement.val(JSON.stringify({...deliveryMethod, ...event.detail.data})); 113 } 114 } 115});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.