1(function ($) { 2 'use strict'; 3 $(document).ready(function () { 4 5 let savedTerminalId = undefined; 6 7 const terminalMatcher = (params, data) => { 8 const originalMatcher = $.fn.select2.defaults.defaults.matcher; 9 const result = originalMatcher(params, data); 10 if ( 11 result && 12 data.children && 13 result.children && 14 data.children.length 15 ) { 16 if ( 17 data.children.length !== result.children.length && 18 data.text.toLowerCase().includes(params.term.toLowerCase()) 19 ) { 20 result.children = data.children; 21 } 22 return result; 23 } 24 return null; 25 } 26 27 const recurse = (parent) => { 28 29 if (parent.nodeName === 'SELECT' && parent.className === 'woo_lithuaniapost_lpexpress_terminal_id') { 30 $(parent).select2({ 31 width: 'resolve', 32 matcher(params, data) { 33 return terminalMatcher(params, data); 34 }, 35 }); 36 return; 37 } 38 if (parent.childNodes) { 39 [...parent.childNodes].forEach(recurse); 40 } 41 }; 42 43 // select the target node 44 var target = $('.cart-collaterals, #order_review'); 45 let terminalIdElem = $('.woo_lithuaniapost_lpexpress_terminal_id'); 46 if (terminalIdElem.length > 0) { 47 $(terminalIdElem).select2({ 48 width: 'resolve', 49 matcher(params, data) { 50 return terminalMatcher(params, data); 51 }, 52 }); 53 } 54 55 if (target) { 56 // create an observer instance 57 var observer = new MutationObserver(function (mutations) { 58 //loop through the detected mutations(added controls) 59 mutations.forEach(function (mutation) { 60 for (const node of mutation.addedNodes) { 61 recurse(node); 62 } 63 }); 64 }); 65 let obsConfig = { 66 childList: true, 67 characterData: true, 68 attributes: true, 69 subtree: true 70 }; 71 target.each(function () { 72 observer.observe(this, obsConfig); 73 }); 74 75 // later, you can stop observing 76 //observer.disconnect(); 77 78 $(target).on('change', terminalIdElem, function () { 79 const selectedOption = $(this).find(":selected"); 80 const terminalId = selectedOption.attr('value') ?? selectedOption.val(); 81 if (terminalId && terminalId !== savedTerminalId && !isNaN(terminalId)) { 82 $.ajax({
83 type: "POST", 84 dataType: "json", 85 url: woo_lithuaniapost.ajax_url, 86 data: { 87 action: "save_selected_lpexpress_terminal", 88 terminal: this.value, 89 terminal_id: terminalId 90 }, success: function () { 91 savedTerminalId = terminalId; 92 } 93 }); 94 } 95 96 }); 97 } 98 }); 99})(jQuery);
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.