1 line
2.4 KiB
JavaScript
1 line
2.4 KiB
JavaScript
jQuery(function(a){function b(){const b=d&&d.visibilityMode?d.visibilityMode:"always";let c=a();if("toggle"===b){c=a(".wpo-ips-peppol-conditional").closest(".form-row");const b=a("#peppol_invoice");return b.length?void c.toggle(b.is(":checked")):void 0}if("company"===b){c=a(".wpo-ips-peppol-company-conditional").closest(".form-row");const b=a("#billing_company");return b.length?void c.toggle(0<a.trim(b.val()).length):void 0}}function c(a){const b=document.querySelector(a);return b?((b.value||"")+"").trim():""}const d=window.wpoIpsPeppol||{};if(b(),a(document).on("change","#peppol_invoice",b),a(document).on("input change","#billing_company",b),!d.endpoint_derivation)return;const e=d.billing_country_selector||"#billing_country",f=d.vat_field_selector||"",g=d.peppol_input_wrapper_selector||"",h=g?g+" input":"",i=window.WPO_IPS_PeppolEndpointDerivation.init(d,{getBillingCountry(){return c(e)},getEndpointNodes(){const a={wrapper:g?document.querySelector(g):null,endpoint:h?document.querySelector(h):null};return i.log("getEndpointNodes",{wrapperSelector:g,endpointSelector:h,wrapperFound:!!a.wrapper,endpointFound:!!a.endpoint,currentValue:a.endpoint?(a.endpoint.value||"")+"":""}),a},appendOverrideLink(a,b,c){b&&b.parentNode?b.insertAdjacentElement("afterend",c):a.appendChild(c)},fetchEndpoint(b,c){i.log("fetchEndpoint",{billingCountry:b,vatValue:c,hasApiFetch:!!window.wp?.apiFetch,hasWpApiSettingsRoot:!!window.wpApiSettings?.root,route:d.peppol_autofill_endpoint_route});const e=window.wp?.apiFetch;if(e)return e({path:d.peppol_autofill_endpoint_route,method:"POST",data:{billing_country:((b||"")+"").toUpperCase(),vat:(c||"")+""}});const f=window.wpApiSettings?.root?window.wpApiSettings.root+"":"",g=window.wpApiSettings?.nonce?window.wpApiSettings.nonce+"":"";if(f){const e=((d.peppol_autofill_endpoint_route||"")+"").replace(/^\//,""),h=f.replace(/\/$/,"")+"/"+e;return a.ajax({url:h,method:"POST",dataType:"json",data:{billing_country:((b||"")+"").toUpperCase(),vat:(c||"")+""},beforeSend:function(a){g&&a.setRequestHeader("X-WP-Nonce",g)}})}return a.Deferred().resolve(null).promise()}});a(document).on("input change",e+", "+f,function(){i.log("input/change event",{target:this&&this.name?this.name:this&&this.id?this.id:"",country:c(e),vat:f?c(f):""}),i.schedule("input")}),a(document.body).on("updated_checkout updated_shipping_method",function(a){i.log("wc event",{type:a&&a.type?a.type:""}),i.schedule("wc-updated")}),i.log("init schedule"),i.schedule("init")}); |