1 line
3.1 KiB
JavaScript
1 line
3.1 KiB
JavaScript
(function(){window.WPO_IPS_PeppolEndpointDerivation=window.WPO_IPS_PeppolEndpointDerivation||{},window.WPO_IPS_PeppolEndpointDerivation.init=function(a,b){function c(...a){p&&console.log("[WPO IPS Peppol]",...a)}function d(a){if(!a)return"";const b=document.querySelector(a);return b?((b.value||"")+"").trim():""}function e(a){const b=((a||"")+"").toUpperCase();return o.includes(b)}function f(a){return((a||"")+"").replace(/\s+/g,"").toUpperCase().trim()}function g(a,c){if(!a)return!1;const d=(c??"")+"",e=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,"value")?.set,f=!!a.readOnly;return a.readOnly=!1,e?e.call(a,d):a.value=d,a.dispatchEvent(new Event("input",{bubbles:!0})),a.dispatchEvent(new Event("change",{bubbles:!0})),a.readOnly=f,"function"==typeof b.onSetFieldValue&&b.onSetFieldValue(a,d),!0}function h(a,b){a&&requestAnimationFrame(()=>{g(a,b),requestAnimationFrame(()=>g(a,b))})}function i(a,c){if("function"==typeof b.fetchEndpoint)return b.fetchEndpoint(a,c);const d=window.wp?.apiFetch;return d?d({path:r,method:"POST",data:{billing_country:((a||"")+"").toUpperCase(),vat:(c||"")+""}}):Promise.resolve(null)}function j(c,d,e){if(!c)return;let f=c.querySelector(".wpo-ips-override");return e?f?f:(f=document.createElement("a"),f.href="#",f.className="wpo-ips-override",f.textContent=a.override_link_text||"Override (edit manually)",f.addEventListener("click",a=>{a.preventDefault();const b=d?(d.value||"")+"":"";u=!0,v=b,m("override-click"),d&&(b&&h(d,b),d.focus())}),"function"==typeof b.appendOverrideLink?b.appendOverrideLink(c,d,f):c.appendChild(f),f):void(f&&f.remove())}function k(a,b,c){a.readOnly=c,a.style.pointerEvents=c?"none":"",a.setAttribute("aria-disabled",c?"true":"false"),a.classList.toggle("wpo-is-locked",c),b&&b.classList.toggle("wpo-ips-locked-wrap",c)}function l(a){function b(){const b=(a.value||"")+"";b&&(v=b)}!a||a.__wpoPeppolManualBound||(a.__wpoPeppolManualBound=!0,a.addEventListener("input",()=>{u&&(v=(a.value||"")+"")}),a.addEventListener("pointerdown",()=>{u&&b()}),a.addEventListener("mousedown",()=>{u&&b()}),a.addEventListener("focus",()=>{u&&v&&h(a,v)}),a.addEventListener("blur",()=>{u&&(b(),v&&h(a,v))}))}function m(a="unknown"){A=!1;const h=b.getEndpointNodes&&b.getEndpointNodes(),m=h?h.wrapper:null,n=h?h.endpoint:null;if(!n)return;l(n);const o=((b.getBillingCountry?b.getBillingCountry():"")+"").toUpperCase(),p=d(q);(o!==x||p!==w)&&(u=!1,v="",x=o,w=p,t=null);const r=!u;if(e(o)&&!u&&""!==p){const a=f(p),b=o+"|"+a;t&&t.key===b?t.value&&g(n,t.value):(s&&clearTimeout(s),s=setTimeout(()=>{i(o,a).then(a=>{const c=((a?.id||"")+"").trim();t={key:b,value:c},c&&g(n,c)}).catch(()=>{})},250))}r===y&&n===z||(j(m,n,r),k(n,m,r),y=r,z=n,c("lock state applied",{source:a,country:o,vatValue:p,locked:r,manualOverride:u,lastValue:t?t.value:"",manualOverrideValue:v}))}function n(a){A||(A=!0,requestAnimationFrame(()=>m(a)))}const o=Array.isArray(a.countries)?a.countries.map(a=>(a+"").toUpperCase()):[],p=!!a.debug,q=a.vat_field_selector,r=a.peppol_autofill_endpoint_route;let s=null,t=null,u=!1,v="",w=null,x=null,y=null,z=null,A=!1;return{apply:m,schedule:n,log:c}}})(); |