@vitraun/webar 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- (function(){"use strict";var v="VITRAUN_CONFIG",p="vtrn-vto-session",b=18e5,c=1e4,f="en-US",d=80;function s(i,t,e){var n=i.getAttribute(t);return n!=null&&n!==""?n:e}function h(i,t){if(i==null||i==="")return t;var e=String(i).toLowerCase();return e==="true"||e==="1"||e==="yes"}function m(i){if(i==null||i==="")return null;var t=parseFloat(String(i));return Number.isFinite(t)?t:null}function S(i){return!i||typeof i!="string"?[]:i.split(",").map(function(t){return t.trim()}).filter(Boolean)}function k(i,t){var e=s(i,t,"");if(!e||typeof e!="string")return null;try{return JSON.parse(e)}catch{return null}}function T(i){try{return JSON.parse(i)}catch{return null}}function _(){return typeof window>"u"||!window.location||!window.location.origin?null:window.location.origin}function g(){return typeof window>"u"||!window.location||!window.location.href?null:window.location.href}function P(i){if(!i||typeof i!="string")return null;try{return new URL(i).origin}catch{return null}}function C(i){var t=String(i||"").trim().toLowerCase();if(!t)throw new Error("The env attribute is required.");if(!/^[a-z0-9-]+$/.test(t))throw new Error("The env attribute contains an invalid value.");return t}function O(i){return i?String(i).trim().toLowerCase():"web"}function I(i,t){if(i==null||i==="")return t;var e=parseInt(String(i),10);return!Number.isInteger(e)||e<=0||e>65535?t:e}function E(i,t){return"https://api."+i+".vitraun.com:"+t+"/v1/public/virtual-tryon-embed-init"}function U(i,t,e,n){var o=t||f,l="https://app."+i+".vitraun.com:"+n+"/"+encodeURIComponent(o)+"/virtual-tryon";return e&&(l+="?simulation=1"),l}function x(i,t,e,n){return!t||t<=0?i:new Promise(function(o,l){var a=!1,r=setTimeout(function(){if(!a){a=!0;try{n&&typeof n.abort=="function"&&n.abort()}catch{}l(new Error(e||"The operation timed out."))}},t);i.then(function(u){a||(a=!0,clearTimeout(r),o(u))}).catch(function(u){a||(a=!0,clearTimeout(r),l(u))})})}function w(){if(typeof window>"u"||!window.crypto||!window.crypto.randomUUID)return"00000000-0000-4000-8000-000000000000";try{var i=sessionStorage.getItem(p);if(i){var t=JSON.parse(i);if(t&&typeof t.id=="string"&&typeof t.exp=="number"&&Date.now()<t.exp)return t.id}}catch{}var e=window.crypto.randomUUID(),n=Date.now()+b;try{sessionStorage.setItem(p,JSON.stringify({id:e,exp:n}))}catch{}return e}class R extends HTMLElement{static get observedAttributes(){return["merchant-id","widget-id","env","api-port","app-port","lang","isolated-sku","apply-skus","flow","basket-opens-in","checkout-opens-in","use-simple-page","show-details","show-product-price","price-from","price-to","currency-symbol","is-simulation-mode","simulation-inline-config","container-min-height","app-id","platform","bundle-id","package-name","app-version","attestation-token","debug","init-timeout-ms"]}constructor(){super(),this.attachShadow({mode:"open"}),this._state={merchantId:"",widgetId:"",env:"",apiPort:d,appPort:d,lang:f,isolatedSku:null,applySkus:[],iframeOrigin:null,flow:"checkout",basketOpensIn:"event",checkoutOpensIn:"event",useSimplePage:!0,showDetails:!0,showProductPrice:!1,priceFrom:null,priceTo:null,currencySymbol:null,isSimulationMode:!1,simulationInlineConfig:null,appId:null,platform:"web",bundleId:null,packageName:null,appVersion:null,attestationToken:null,debug:!1,sessionToken:null,tryonSessionId:null,resolvedInitUrl:null,resolvedIframeUrl:null,initTimeoutMs:c},this._boundHandleMessage=this._handleMessage.bind(this),this._initPromise=null,this._initStateKey=null,this._activeInitRequestId=0,this._renderVersion=0,this._abortController=null}connectedCallback(){this._readAttributes(),this._render(),typeof window<"u"&&window.addEventListener("message",this._boundHandleMessage)}disconnectedCallback(){if(typeof window<"u"&&window.removeEventListener("message",this._boundHandleMessage),this._abortController&&typeof this._abortController.abort=="function")try{this._abortController.abort()}catch{}}attributeChangedCallback(t,e,n){e!==n&&(this._readAttributes(),this._render())}_log(){if(!(!this._state.debug||typeof console>"u"||!console.log)){var t=Array.prototype.slice.call(arguments);t.unshift("[vitraun-vto]"),console.log.apply(console,t)}}_readAttributes(){var t=k(this,"simulation-inline-config"),e=!1;t&&t.features&&typeof t.features.showProductPrice=="boolean"&&(e=t.features.showProductPrice);var n=!0;t&&t.features&&typeof t.features.useSimplePage=="boolean"&&(n=t.features.useSimplePage);var o="checkout";t&&t.features&&(t.features.flow==="catalog"||t.features.flow==="checkout")&&(o=t.features.flow);var l=!0;t&&t.features&&typeof t.features.showDetails=="boolean"&&(l=t.features.showDetails);var a="";try{a=C(s(this,"env",""))}catch{a=""}var r=parseInt(s(this,"init-timeout-ms",String(c)),10);(!Number.isFinite(r)||r<0)&&(r=c),this._state={merchantId:s(this,"merchant-id",""),widgetId:s(this,"widget-id",""),env:a,apiPort:I(s(this,"api-port",String(d)),d),appPort:I(s(this,"app-port",String(d)),d),lang:s(this,"lang",f),isolatedSku:s(this,"isolated-sku",null)||null,applySkus:S(s(this,"apply-skus","")),iframeOrigin:this._state&&this._state.iframeOrigin?this._state.iframeOrigin:null,flow:s(this,"flow",o),basketOpensIn:s(this,"basket-opens-in","event"),checkoutOpensIn:s(this,"checkout-opens-in","event"),useSimplePage:h(s(this,"use-simple-page",""),n),showDetails:h(s(this,"show-details",""),l),showProductPrice:h(s(this,"show-product-price",""),e),priceFrom:m(s(this,"price-from",null)),priceTo:m(s(this,"price-to",null)),currencySymbol:s(this,"currency-symbol",null)||null,isSimulationMode:h(s(this,"is-simulation-mode","false"),!1),simulationInlineConfig:t,appId:s(this,"app-id",null)||null,platform:O(s(this,"platform",null)),bundleId:s(this,"bundle-id",null)||null,packageName:s(this,"package-name",null)||null,appVersion:s(this,"app-version",null)||null,attestationToken:s(this,"attestation-token",null)||null,debug:h(s(this,"debug","false"),!1),sessionToken:this._state&&this._state.sessionToken?this._state.sessionToken:null,tryonSessionId:this._state&&this._state.tryonSessionId?this._state.tryonSessionId:null,resolvedInitUrl:this._state&&this._state.resolvedInitUrl?this._state.resolvedInitUrl:null,resolvedIframeUrl:this._state&&this._state.resolvedIframeUrl?this._state.resolvedIframeUrl:null,initTimeoutMs:r}}_emit(t,e){this.dispatchEvent(new CustomEvent(t,{detail:e??null,bubbles:!0,composed:!0}))}_setStatus(t,e){this.setAttribute("data-status",t),this._emit("statusChange",{status:t,detail:e||null})}_getIframeElement(){return this.shadowRoot?this.shadowRoot.querySelector("iframe"):null}_handleMessage(t){var e=t.data;if(!(!e||typeof e!="object")){var n=this._getIframeElement();if(!(!n||!n.contentWindow)&&t.source===n.contentWindow&&!(!this._state.iframeOrigin||t.origin!==this._state.iframeOrigin)){var o=e.type,l=e.detail;if(o==="VTO_USAGE"){this._emit("vtoUsage",l);return}var a=["addToCart","removeFromCart","redirectToCart","analysisFinished"];a.indexOf(o)!==-1&&this._emit(o,l)}}}_getInitStateKey(){return JSON.stringify({merchantId:this._state.merchantId,widgetId:this._state.widgetId,env:this._state.env,apiPort:this._state.apiPort,appPort:this._state.appPort,lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,simulationInlineConfig:this._state.simulationInlineConfig,appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken})}_buildResolvedUrls(){if(!this._state.env)throw new Error("The env attribute is required.");var t=U(this._state.env,this._state.lang,this._state.isSimulationMode,this._state.appPort);return{initUrl:E(this._state.env,this._state.apiPort),iframeUrl:t,iframeOrigin:P(t)}}_buildInitPayload(){var t=w();return{merchantId:this._state.merchantId,widgetId:this._state.widgetId,tryonSessionId:t,lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,inlineVirtualTryonConfig:this._state.isSimulationMode&&this._state.simulationInlineConfig&&typeof this._state.simulationInlineConfig=="object"?this._state.simulationInlineConfig:null,app:{appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken},context:{origin:_(),pageUrl:g(),userAgent:typeof navigator<"u"&&navigator.userAgent?navigator.userAgent:null,referrer:typeof document<"u"&&document.referrer?document.referrer:null,timezone:typeof Intl<"u"&&Intl.DateTimeFormat?Intl.DateTimeFormat().resolvedOptions().timeZone:null}}}_normalizeInitResponse(t,e,n){if(!t||typeof t!="object")throw new Error("Invalid init response.");var o=typeof t.sessionToken=="string"&&t.sessionToken?t.sessionToken:null,l=typeof t.tryonSessionId=="string"&&t.tryonSessionId?t.tryonSessionId:n;if(!o)throw new Error("The init API did not return sessionToken.");return{sessionToken:o,tryonSessionId:l,initUrl:e.initUrl,iframeUrl:e.iframeUrl,iframeOrigin:e.iframeOrigin}}_initializeSessionIfNeeded(){var t=this;if(!this._state.merchantId)return Promise.reject(new Error("The merchant-id attribute is required."));if(!this._state.widgetId)return Promise.reject(new Error("The widget-id attribute is required."));if(!this._state.env)return Promise.reject(new Error("The env attribute is required."));var e=this._getInitStateKey();if(this._initPromise&&this._initStateKey===e)return this._initPromise;if(this._abortController&&typeof this._abortController.abort=="function")try{this._abortController.abort()}catch{}this._initStateKey=e,this._activeInitRequestId+=1;var n=this._activeInitRequestId;this._setStatus("loading");var o;try{o=this._buildResolvedUrls()}catch(r){return Promise.reject(r)}var l=this._buildInitPayload();this._state.tryonSessionId=l.tryonSessionId,this._abortController=typeof AbortController<"u"?new AbortController:null;var a=fetch(o.initUrl,{method:"POST",headers:{"Content-Type":"application/json","X-Merchant-Id":this._state.merchantId,"X-Widget-Id":this._state.widgetId},body:JSON.stringify(l),signal:this._abortController?this._abortController.signal:void 0}).then(function(r){return r.ok?r.json():r.text().then(function(u){var y=T(u),N=y&&y.message||u||"Failed to initialize session ("+r.status+").";throw new Error(N)})}).then(function(r){return t._normalizeInitResponse(r,o,l.tryonSessionId)});return this._initPromise=x(a,this._state.initTimeoutMs,"Timed out while initializing the Vitraun session.",this._abortController).then(function(r){return n!==t._activeInitRequestId||(t._state.sessionToken=r.sessionToken,t._state.tryonSessionId=r.tryonSessionId,t._state.resolvedInitUrl=r.initUrl,t._state.resolvedIframeUrl=r.iframeUrl,t._state.iframeOrigin=r.iframeOrigin,t._setStatus("ready")),r}).catch(function(r){throw n!==t._activeInitRequestId||(t._state.sessionToken=null,t._state.resolvedInitUrl=null,t._state.resolvedIframeUrl=null,t._state.iframeOrigin=null,t._setStatus("error",{message:r&&r.message?r.message:"Initialization failed."})),r}).finally(function(){n===t._activeInitRequestId&&(t._initPromise=null)}),this._initPromise}_sendConfigToIframe(){var t=this._getIframeElement();if(!(!t||!t.contentWindow)){var e=this._state.iframeOrigin;if(!e){this._log("Config not sent: iframeOrigin is missing.");return}var n={merchantId:this._state.merchantId,widgetId:this._state.widgetId,sessionToken:this._state.sessionToken,tryonSessionId:this._state.tryonSessionId||w(),lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,app:{appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken},hostPageOrigin:_(),hostPageUrl:g()};this._state.isSimulationMode&&this._state.simulationInlineConfig&&typeof this._state.simulationInlineConfig=="object"&&(n.inlineVirtualTryonConfig=this._state.simulationInlineConfig),t.contentWindow.postMessage({type:v,payload:n},e)}}_renderLoading(){if(this.shadowRoot){var t=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var e=document.createElement("style");e.textContent=":host { display:block; width:100%; min-height:"+t+"; box-sizing:border-box; }.vitraun-loading {min-height:"+t+";display:flex;align-items:center;justify-content:center;border:1px solid #ececec;background:#fafafa;color:#333;font:14px/1.4 Arial, sans-serif;padding:16px;box-sizing:border-box;border-radius:8px;text-align:center;}";var n=document.createElement("div");n.className="vitraun-loading",n.textContent="Loading Vitraun...",this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(n)}}_renderError(t){if(this.shadowRoot){var e=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var n=document.createElement("style");n.textContent=":host { display:block; width:100%; min-height:"+e+"; box-sizing:border-box; }.vitraun-error {min-height:"+e+";display:flex;align-items:center;justify-content:center;border:1px solid #f1d5d5;background:#fff7f7;color:#7a1f1f;font:14px/1.4 Arial, sans-serif;padding:16px;box-sizing:border-box;border-radius:8px;text-align:center;}";var o=document.createElement("div");o.className="vitraun-error",o.textContent=t||"Failed to load Vitraun.",this.shadowRoot.appendChild(n),this.shadowRoot.appendChild(o)}}_renderIframe(t,e){if(this.shadowRoot){var n=this,o=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var l=document.createElement("style");l.textContent=":host { display:block; width:100%; min-height:"+o+"; box-sizing:border-box; }";var a=document.createElement("iframe");a.src=t,a.title="Vitraun VTO",a.style.cssText="width:100%;height:100%;border:0;min-height:"+o,a.setAttribute("allow","camera"),a.setAttribute("referrerpolicy","strict-origin-when-cross-origin"),a.onload=function(){e===n._renderVersion&&[0,50,150,400,1e3].forEach(function(r){setTimeout(function(){e===n._renderVersion&&n._sendConfigToIframe()},r)})},this.shadowRoot.appendChild(l),this.shadowRoot.appendChild(a)}}_render(){if(this.shadowRoot){this._renderVersion+=1;var t=this._renderVersion;if(!this._state.merchantId){this._setStatus("error",{message:"The merchant-id attribute is required."}),this._renderError("The merchant-id attribute is required.");return}if(!this._state.widgetId){this._setStatus("error",{message:"The widget-id attribute is required."}),this._renderError("The widget-id attribute is required.");return}if(!this._state.env){this._setStatus("error",{message:"The env attribute is required."}),this._renderError("The env attribute is required.");return}this._renderLoading(),this._initializeSessionIfNeeded().then(function(e){t===this._renderVersion&&this._renderIframe(e.iframeUrl,t)}.bind(this)).catch(function(e){t===this._renderVersion&&this._renderError(e&&e.message?e.message:"Failed to load Vitraun.")}.bind(this))}}open(){return this.style.display="",Promise.resolve()}close(){return this.style.display="none",Promise.resolve()}}typeof customElements<"u"&&!customElements.get("vitraun-vto")&&customElements.define("vitraun-vto",R)})();
1
+ (function(){"use strict";var y="VITRAUN_CONFIG",p="vtrn-vto-session",b=18e5,c=1e4,f="en-US",d=80;function s(i,t,e){var n=i.getAttribute(t);return n!=null&&n!==""?n:e}function h(i,t){if(i==null||i==="")return t;var e=String(i).toLowerCase();return e==="true"||e==="1"||e==="yes"}function m(i){if(i==null||i==="")return null;var t=parseFloat(String(i));return Number.isFinite(t)?t:null}function S(i){return!i||typeof i!="string"?[]:i.split(",").map(function(t){return t.trim()}).filter(Boolean)}function T(i,t){var e=s(i,t,"");if(!e||typeof e!="string")return null;try{return JSON.parse(e)}catch{return null}}function k(i){try{return JSON.parse(i)}catch{return null}}function g(){return typeof window>"u"||!window.location||!window.location.origin?null:window.location.origin}function _(){return typeof window>"u"||!window.location||!window.location.href?null:window.location.href}function P(i){if(!i||typeof i!="string")return null;try{return new URL(i).origin}catch{return null}}function C(i){var t=String(i||"").trim().toLowerCase();if(!t)throw new Error("The env attribute is required.");if(!/^[a-z0-9-]+$/.test(t))throw new Error("The env attribute contains an invalid value.");return t}function O(i){return i?String(i).trim().toLowerCase():"web"}function I(i,t){if(i==null||i==="")return t;var e=parseInt(String(i),10);return!Number.isInteger(e)||e<=0||e>65535?t:e}function E(i,t){return"https://app."+i+".vitraun.com:"+t+"/v1/public/virtual-tryon-embed-init"}function U(i,t){return{merchantId:i.merchantId,widgetId:i.widgetId,tryonSessionId:t,app:{appId:i.appId,platform:i.platform,bundleId:i.bundleId,packageName:i.packageName,appVersion:i.appVersion,attestationToken:i.attestationToken},context:{origin:g(),pageUrl:_(),userAgent:typeof navigator<"u"&&navigator.userAgent?navigator.userAgent:null,referrer:typeof document<"u"&&document.referrer?document.referrer:null,timezone:typeof Intl<"u"&&Intl.DateTimeFormat?Intl.DateTimeFormat().resolvedOptions().timeZone:null}}}function R(i,t,e,n){var o=t||f,l="https://app."+i+".vitraun.com:"+n+"/"+encodeURIComponent(o)+"/virtual-tryon";return e&&(l+="?simulation=1"),l}function x(i,t,e,n){return!t||t<=0?i:new Promise(function(o,l){var a=!1,r=setTimeout(function(){if(!a){a=!0;try{n&&typeof n.abort=="function"&&n.abort()}catch{}l(new Error(e||"The operation timed out."))}},t);i.then(function(u){a||(a=!0,clearTimeout(r),o(u))}).catch(function(u){a||(a=!0,clearTimeout(r),l(u))})})}function w(){if(typeof window>"u"||!window.crypto||!window.crypto.randomUUID)return"00000000-0000-4000-8000-000000000000";try{var i=sessionStorage.getItem(p);if(i){var t=JSON.parse(i);if(t&&typeof t.id=="string"&&typeof t.exp=="number"&&Date.now()<t.exp)return t.id}}catch{}var e=window.crypto.randomUUID(),n=Date.now()+b;try{sessionStorage.setItem(p,JSON.stringify({id:e,exp:n}))}catch{}return e}class N extends HTMLElement{static get observedAttributes(){return["merchant-id","widget-id","env","api-port","app-port","lang","isolated-sku","apply-skus","flow","basket-opens-in","checkout-opens-in","use-simple-page","show-details","show-product-price","price-from","price-to","currency-symbol","is-simulation-mode","simulation-inline-config","container-min-height","app-id","platform","bundle-id","package-name","app-version","attestation-token","debug","init-timeout-ms"]}constructor(){super(),this.attachShadow({mode:"open"}),this._state={merchantId:"",widgetId:"",env:"",apiPort:d,appPort:d,lang:f,isolatedSku:null,applySkus:[],iframeOrigin:null,flow:"checkout",basketOpensIn:"event",checkoutOpensIn:"event",useSimplePage:!0,showDetails:!0,showProductPrice:!1,priceFrom:null,priceTo:null,currencySymbol:null,isSimulationMode:!1,simulationInlineConfig:null,appId:null,platform:"web",bundleId:null,packageName:null,appVersion:null,attestationToken:null,debug:!1,sessionToken:null,tryonSessionId:null,resolvedInitUrl:null,resolvedIframeUrl:null,initTimeoutMs:c},this._boundHandleMessage=this._handleMessage.bind(this),this._initPromise=null,this._initStateKey=null,this._activeInitRequestId=0,this._renderVersion=0,this._abortController=null}connectedCallback(){this._readAttributes(),this._render(),typeof window<"u"&&window.addEventListener("message",this._boundHandleMessage)}disconnectedCallback(){if(typeof window<"u"&&window.removeEventListener("message",this._boundHandleMessage),this._abortController&&typeof this._abortController.abort=="function")try{this._abortController.abort()}catch{}}attributeChangedCallback(t,e,n){e!==n&&(this._readAttributes(),this._render())}_log(){if(!(!this._state.debug||typeof console>"u"||!console.log)){var t=Array.prototype.slice.call(arguments);t.unshift("[vitraun-vto]"),console.log.apply(console,t)}}_readAttributes(){var t=T(this,"simulation-inline-config"),e=!1;t&&t.features&&typeof t.features.showProductPrice=="boolean"&&(e=t.features.showProductPrice);var n=!0;t&&t.features&&typeof t.features.useSimplePage=="boolean"&&(n=t.features.useSimplePage);var o="checkout";t&&t.features&&(t.features.flow==="catalog"||t.features.flow==="checkout")&&(o=t.features.flow);var l=!0;t&&t.features&&typeof t.features.showDetails=="boolean"&&(l=t.features.showDetails);var a="";try{a=C(s(this,"env",""))}catch{a=""}var r=parseInt(s(this,"init-timeout-ms",String(c)),10);(!Number.isFinite(r)||r<0)&&(r=c),this._state={merchantId:s(this,"merchant-id",""),widgetId:s(this,"widget-id",""),env:a,apiPort:I(s(this,"api-port",String(d)),d),appPort:I(s(this,"app-port",String(d)),d),lang:s(this,"lang",f),isolatedSku:s(this,"isolated-sku",null)||null,applySkus:S(s(this,"apply-skus","")),iframeOrigin:this._state&&this._state.iframeOrigin?this._state.iframeOrigin:null,flow:s(this,"flow",o),basketOpensIn:s(this,"basket-opens-in","event"),checkoutOpensIn:s(this,"checkout-opens-in","event"),useSimplePage:h(s(this,"use-simple-page",""),n),showDetails:h(s(this,"show-details",""),l),showProductPrice:h(s(this,"show-product-price",""),e),priceFrom:m(s(this,"price-from",null)),priceTo:m(s(this,"price-to",null)),currencySymbol:s(this,"currency-symbol",null)||null,isSimulationMode:h(s(this,"is-simulation-mode","false"),!1),simulationInlineConfig:t,appId:s(this,"app-id",null)||null,platform:O(s(this,"platform",null)),bundleId:s(this,"bundle-id",null)||null,packageName:s(this,"package-name",null)||null,appVersion:s(this,"app-version",null)||null,attestationToken:s(this,"attestation-token",null)||null,debug:h(s(this,"debug","false"),!1),sessionToken:this._state&&this._state.sessionToken?this._state.sessionToken:null,tryonSessionId:this._state&&this._state.tryonSessionId?this._state.tryonSessionId:null,resolvedInitUrl:this._state&&this._state.resolvedInitUrl?this._state.resolvedInitUrl:null,resolvedIframeUrl:this._state&&this._state.resolvedIframeUrl?this._state.resolvedIframeUrl:null,initTimeoutMs:r}}_emit(t,e){this.dispatchEvent(new CustomEvent(t,{detail:e??null,bubbles:!0,composed:!0}))}_setStatus(t,e){this.setAttribute("data-status",t),this._emit("statusChange",{status:t,detail:e||null})}_getIframeElement(){return this.shadowRoot?this.shadowRoot.querySelector("iframe"):null}_handleMessage(t){var e=t.data;if(!(!e||typeof e!="object")){var n=this._getIframeElement();if(!(!n||!n.contentWindow)&&t.source===n.contentWindow&&!(!this._state.iframeOrigin||t.origin!==this._state.iframeOrigin)){var o=e.type,l=e.detail;if(o==="VTO_USAGE"){this._emit("vtoUsage",l);return}var a=["addToCart","removeFromCart","redirectToCart","analysisFinished"];a.indexOf(o)!==-1&&this._emit(o,l)}}}_getInitStateKey(){return JSON.stringify({merchantId:this._state.merchantId,widgetId:this._state.widgetId,env:this._state.env,apiPort:this._state.apiPort,appPort:this._state.appPort,lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,simulationInlineConfig:this._state.simulationInlineConfig,appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken})}_buildResolvedUrls(){if(!this._state.env)throw new Error("The env attribute is required.");var t=R(this._state.env,this._state.lang,this._state.isSimulationMode,this._state.appPort);return{initUrl:E(this._state.env,this._state.apiPort),iframeUrl:t,iframeOrigin:P(t)}}_buildInitPayload(){var t=w();return{tryonSessionId:t,initRequestBody:U(this._state,t)}}_normalizeInitResponse(t,e,n){if(!t||typeof t!="object")throw new Error("Invalid init response.");var o=typeof t.sessionToken=="string"&&t.sessionToken?t.sessionToken:null,l=typeof t.tryonSessionId=="string"&&t.tryonSessionId?t.tryonSessionId:n;if(!o)throw new Error("The init API did not return sessionToken.");return{sessionToken:o,tryonSessionId:l,initUrl:e.initUrl,iframeUrl:e.iframeUrl,iframeOrigin:e.iframeOrigin}}_initializeSessionIfNeeded(){var t=this;if(!this._state.merchantId)return Promise.reject(new Error("The merchant-id attribute is required."));if(!this._state.widgetId)return Promise.reject(new Error("The widget-id attribute is required."));if(!this._state.env)return Promise.reject(new Error("The env attribute is required."));var e=this._getInitStateKey();if(this._initPromise&&this._initStateKey===e)return this._initPromise;if(this._abortController&&typeof this._abortController.abort=="function")try{this._abortController.abort()}catch{}this._initStateKey=e,this._activeInitRequestId+=1;var n=this._activeInitRequestId;this._setStatus("loading");var o;try{o=this._buildResolvedUrls()}catch(r){return Promise.reject(r)}var l=this._buildInitPayload();this._state.tryonSessionId=l.tryonSessionId,this._abortController=typeof AbortController<"u"?new AbortController:null;var a=fetch(o.initUrl,{method:"POST",headers:{"Content-Type":"application/json","X-Merchant-Id":this._state.merchantId,"X-Widget-Id":this._state.widgetId},body:JSON.stringify(l.initRequestBody),signal:this._abortController?this._abortController.signal:void 0}).then(function(r){return r.ok?r.json():r.text().then(function(u){var v=k(u),A=v&&v.message||u||"Failed to initialize session ("+r.status+").";throw new Error(A)})}).then(function(r){return t._normalizeInitResponse(r,o,l.tryonSessionId)});return this._initPromise=x(a,this._state.initTimeoutMs,"Timed out while initializing the Vitraun session.",this._abortController).then(function(r){return n!==t._activeInitRequestId||(t._state.sessionToken=r.sessionToken,t._state.tryonSessionId=r.tryonSessionId,t._state.resolvedInitUrl=r.initUrl,t._state.resolvedIframeUrl=r.iframeUrl,t._state.iframeOrigin=r.iframeOrigin,t._setStatus("ready")),r}).catch(function(r){throw n!==t._activeInitRequestId||(t._state.sessionToken=null,t._state.resolvedInitUrl=null,t._state.resolvedIframeUrl=null,t._state.iframeOrigin=null,t._setStatus("error",{message:r&&r.message?r.message:"Initialization failed."})),r}).finally(function(){n===t._activeInitRequestId&&(t._initPromise=null)}),this._initPromise}_sendConfigToIframe(){var t=this._getIframeElement();if(!(!t||!t.contentWindow)){var e=this._state.iframeOrigin;if(!e){this._log("Config not sent: iframeOrigin is missing.");return}var n={merchantId:this._state.merchantId,widgetId:this._state.widgetId,sessionToken:this._state.sessionToken,tryonSessionId:this._state.tryonSessionId||w(),lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,app:{appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken},hostPageOrigin:g(),hostPageUrl:_()};this._state.isSimulationMode&&this._state.simulationInlineConfig&&typeof this._state.simulationInlineConfig=="object"&&(n.inlineVirtualTryonConfig=this._state.simulationInlineConfig),t.contentWindow.postMessage({type:y,payload:n},e)}}_renderLoading(){if(this.shadowRoot){var t=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var e=document.createElement("style");e.textContent=":host { display:block; width:100%; min-height:"+t+"; box-sizing:border-box; }.vitraun-loading {min-height:"+t+";display:flex;align-items:center;justify-content:center;border:1px solid #ececec;background:#fafafa;color:#333;font:14px/1.4 Arial, sans-serif;padding:16px;box-sizing:border-box;border-radius:8px;text-align:center;}";var n=document.createElement("div");n.className="vitraun-loading",n.textContent="Loading Vitraun...",this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(n)}}_renderError(t){if(this.shadowRoot){var e=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var n=document.createElement("style");n.textContent=":host { display:block; width:100%; min-height:"+e+"; box-sizing:border-box; }.vitraun-error {min-height:"+e+";display:flex;align-items:center;justify-content:center;border:1px solid #f1d5d5;background:#fff7f7;color:#7a1f1f;font:14px/1.4 Arial, sans-serif;padding:16px;box-sizing:border-box;border-radius:8px;text-align:center;}";var o=document.createElement("div");o.className="vitraun-error",o.textContent=t||"Failed to load Vitraun.",this.shadowRoot.appendChild(n),this.shadowRoot.appendChild(o)}}_renderIframe(t,e){if(this.shadowRoot){var n=this,o=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var l=document.createElement("style");l.textContent=":host { display:block; width:100%; min-height:"+o+"; box-sizing:border-box; }";var a=document.createElement("iframe");a.src=t,a.title="Vitraun VTO",a.style.cssText="width:100%;height:100%;border:0;min-height:"+o,a.setAttribute("allow","camera"),a.setAttribute("referrerpolicy","strict-origin-when-cross-origin"),a.onload=function(){e===n._renderVersion&&[0,50,150,400,1e3].forEach(function(r){setTimeout(function(){e===n._renderVersion&&n._sendConfigToIframe()},r)})},this.shadowRoot.appendChild(l),this.shadowRoot.appendChild(a)}}_render(){if(this.shadowRoot){this._renderVersion+=1;var t=this._renderVersion;if(!this._state.merchantId){this._setStatus("error",{message:"The merchant-id attribute is required."}),this._renderError("The merchant-id attribute is required.");return}if(!this._state.widgetId){this._setStatus("error",{message:"The widget-id attribute is required."}),this._renderError("The widget-id attribute is required.");return}if(!this._state.env){this._setStatus("error",{message:"The env attribute is required."}),this._renderError("The env attribute is required.");return}this._renderLoading(),this._initializeSessionIfNeeded().then(function(e){t===this._renderVersion&&this._renderIframe(e.iframeUrl,t)}.bind(this)).catch(function(e){t===this._renderVersion&&this._renderError(e&&e.message?e.message:"Failed to load Vitraun.")}.bind(this))}}open(){return this.style.display="",Promise.resolve()}close(){return this.style.display="none",Promise.resolve()}}typeof customElements<"u"&&!customElements.get("vitraun-vto")&&customElements.define("vitraun-vto",N)})();
@@ -1 +1 @@
1
- "use strict";var VitraunWebAR=(()=>{(function(){"use strict";var v="VITRAUN_CONFIG",p="vtrn-vto-session",b=18e5,c=1e4,f="en-US",d=80;function s(i,t,e){var n=i.getAttribute(t);return n!=null&&n!==""?n:e}function h(i,t){if(i==null||i==="")return t;var e=String(i).toLowerCase();return e==="true"||e==="1"||e==="yes"}function m(i){if(i==null||i==="")return null;var t=parseFloat(String(i));return Number.isFinite(t)?t:null}function S(i){return!i||typeof i!="string"?[]:i.split(",").map(function(t){return t.trim()}).filter(Boolean)}function k(i,t){var e=s(i,t,"");if(!e||typeof e!="string")return null;try{return JSON.parse(e)}catch(n){return null}}function T(i){try{return JSON.parse(i)}catch(t){return null}}function _(){return typeof window=="undefined"||!window.location||!window.location.origin?null:window.location.origin}function g(){return typeof window=="undefined"||!window.location||!window.location.href?null:window.location.href}function P(i){if(!i||typeof i!="string")return null;try{return new URL(i).origin}catch(t){return null}}function C(i){var t=String(i||"").trim().toLowerCase();if(!t)throw new Error("The env attribute is required.");if(!/^[a-z0-9-]+$/.test(t))throw new Error("The env attribute contains an invalid value.");return t}function O(i){return i?String(i).trim().toLowerCase():"web"}function I(i,t){if(i==null||i==="")return t;var e=parseInt(String(i),10);return!Number.isInteger(e)||e<=0||e>65535?t:e}function E(i,t){return"https://api."+i+".vitraun.com:"+t+"/v1/public/virtual-tryon-embed-init"}function U(i,t,e,n){var o=t||f,l="https://app."+i+".vitraun.com:"+n+"/"+encodeURIComponent(o)+"/virtual-tryon";return e&&(l+="?simulation=1"),l}function x(i,t,e,n){return!t||t<=0?i:new Promise(function(o,l){var a=!1,r=setTimeout(function(){if(!a){a=!0;try{n&&typeof n.abort=="function"&&n.abort()}catch(u){}l(new Error(e||"The operation timed out."))}},t);i.then(function(u){a||(a=!0,clearTimeout(r),o(u))}).catch(function(u){a||(a=!0,clearTimeout(r),l(u))})})}function w(){if(typeof window=="undefined"||!window.crypto||!window.crypto.randomUUID)return"00000000-0000-4000-8000-000000000000";try{var i=sessionStorage.getItem(p);if(i){var t=JSON.parse(i);if(t&&typeof t.id=="string"&&typeof t.exp=="number"&&Date.now()<t.exp)return t.id}}catch(o){}var e=window.crypto.randomUUID(),n=Date.now()+b;try{sessionStorage.setItem(p,JSON.stringify({id:e,exp:n}))}catch(o){}return e}class R extends HTMLElement{static get observedAttributes(){return["merchant-id","widget-id","env","api-port","app-port","lang","isolated-sku","apply-skus","flow","basket-opens-in","checkout-opens-in","use-simple-page","show-details","show-product-price","price-from","price-to","currency-symbol","is-simulation-mode","simulation-inline-config","container-min-height","app-id","platform","bundle-id","package-name","app-version","attestation-token","debug","init-timeout-ms"]}constructor(){super(),this.attachShadow({mode:"open"}),this._state={merchantId:"",widgetId:"",env:"",apiPort:d,appPort:d,lang:f,isolatedSku:null,applySkus:[],iframeOrigin:null,flow:"checkout",basketOpensIn:"event",checkoutOpensIn:"event",useSimplePage:!0,showDetails:!0,showProductPrice:!1,priceFrom:null,priceTo:null,currencySymbol:null,isSimulationMode:!1,simulationInlineConfig:null,appId:null,platform:"web",bundleId:null,packageName:null,appVersion:null,attestationToken:null,debug:!1,sessionToken:null,tryonSessionId:null,resolvedInitUrl:null,resolvedIframeUrl:null,initTimeoutMs:c},this._boundHandleMessage=this._handleMessage.bind(this),this._initPromise=null,this._initStateKey=null,this._activeInitRequestId=0,this._renderVersion=0,this._abortController=null}connectedCallback(){this._readAttributes(),this._render(),typeof window!="undefined"&&window.addEventListener("message",this._boundHandleMessage)}disconnectedCallback(){if(typeof window!="undefined"&&window.removeEventListener("message",this._boundHandleMessage),this._abortController&&typeof this._abortController.abort=="function")try{this._abortController.abort()}catch(t){}}attributeChangedCallback(t,e,n){e!==n&&(this._readAttributes(),this._render())}_log(){if(!(!this._state.debug||typeof console=="undefined"||!console.log)){var t=Array.prototype.slice.call(arguments);t.unshift("[vitraun-vto]"),console.log.apply(console,t)}}_readAttributes(){var t=k(this,"simulation-inline-config"),e=!1;t&&t.features&&typeof t.features.showProductPrice=="boolean"&&(e=t.features.showProductPrice);var n=!0;t&&t.features&&typeof t.features.useSimplePage=="boolean"&&(n=t.features.useSimplePage);var o="checkout";t&&t.features&&(t.features.flow==="catalog"||t.features.flow==="checkout")&&(o=t.features.flow);var l=!0;t&&t.features&&typeof t.features.showDetails=="boolean"&&(l=t.features.showDetails);var a="";try{a=C(s(this,"env",""))}catch(u){a=""}var r=parseInt(s(this,"init-timeout-ms",String(c)),10);(!Number.isFinite(r)||r<0)&&(r=c),this._state={merchantId:s(this,"merchant-id",""),widgetId:s(this,"widget-id",""),env:a,apiPort:I(s(this,"api-port",String(d)),d),appPort:I(s(this,"app-port",String(d)),d),lang:s(this,"lang",f),isolatedSku:s(this,"isolated-sku",null)||null,applySkus:S(s(this,"apply-skus","")),iframeOrigin:this._state&&this._state.iframeOrigin?this._state.iframeOrigin:null,flow:s(this,"flow",o),basketOpensIn:s(this,"basket-opens-in","event"),checkoutOpensIn:s(this,"checkout-opens-in","event"),useSimplePage:h(s(this,"use-simple-page",""),n),showDetails:h(s(this,"show-details",""),l),showProductPrice:h(s(this,"show-product-price",""),e),priceFrom:m(s(this,"price-from",null)),priceTo:m(s(this,"price-to",null)),currencySymbol:s(this,"currency-symbol",null)||null,isSimulationMode:h(s(this,"is-simulation-mode","false"),!1),simulationInlineConfig:t,appId:s(this,"app-id",null)||null,platform:O(s(this,"platform",null)),bundleId:s(this,"bundle-id",null)||null,packageName:s(this,"package-name",null)||null,appVersion:s(this,"app-version",null)||null,attestationToken:s(this,"attestation-token",null)||null,debug:h(s(this,"debug","false"),!1),sessionToken:this._state&&this._state.sessionToken?this._state.sessionToken:null,tryonSessionId:this._state&&this._state.tryonSessionId?this._state.tryonSessionId:null,resolvedInitUrl:this._state&&this._state.resolvedInitUrl?this._state.resolvedInitUrl:null,resolvedIframeUrl:this._state&&this._state.resolvedIframeUrl?this._state.resolvedIframeUrl:null,initTimeoutMs:r}}_emit(t,e){this.dispatchEvent(new CustomEvent(t,{detail:e!=null?e:null,bubbles:!0,composed:!0}))}_setStatus(t,e){this.setAttribute("data-status",t),this._emit("statusChange",{status:t,detail:e||null})}_getIframeElement(){return this.shadowRoot?this.shadowRoot.querySelector("iframe"):null}_handleMessage(t){var e=t.data;if(!(!e||typeof e!="object")){var n=this._getIframeElement();if(!(!n||!n.contentWindow)&&t.source===n.contentWindow&&!(!this._state.iframeOrigin||t.origin!==this._state.iframeOrigin)){var o=e.type,l=e.detail;if(o==="VTO_USAGE"){this._emit("vtoUsage",l);return}var a=["addToCart","removeFromCart","redirectToCart","analysisFinished"];a.indexOf(o)!==-1&&this._emit(o,l)}}}_getInitStateKey(){return JSON.stringify({merchantId:this._state.merchantId,widgetId:this._state.widgetId,env:this._state.env,apiPort:this._state.apiPort,appPort:this._state.appPort,lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,simulationInlineConfig:this._state.simulationInlineConfig,appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken})}_buildResolvedUrls(){if(!this._state.env)throw new Error("The env attribute is required.");var t=U(this._state.env,this._state.lang,this._state.isSimulationMode,this._state.appPort);return{initUrl:E(this._state.env,this._state.apiPort),iframeUrl:t,iframeOrigin:P(t)}}_buildInitPayload(){var t=w();return{merchantId:this._state.merchantId,widgetId:this._state.widgetId,tryonSessionId:t,lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,inlineVirtualTryonConfig:this._state.isSimulationMode&&this._state.simulationInlineConfig&&typeof this._state.simulationInlineConfig=="object"?this._state.simulationInlineConfig:null,app:{appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken},context:{origin:_(),pageUrl:g(),userAgent:typeof navigator!="undefined"&&navigator.userAgent?navigator.userAgent:null,referrer:typeof document!="undefined"&&document.referrer?document.referrer:null,timezone:typeof Intl!="undefined"&&Intl.DateTimeFormat?Intl.DateTimeFormat().resolvedOptions().timeZone:null}}}_normalizeInitResponse(t,e,n){if(!t||typeof t!="object")throw new Error("Invalid init response.");var o=typeof t.sessionToken=="string"&&t.sessionToken?t.sessionToken:null,l=typeof t.tryonSessionId=="string"&&t.tryonSessionId?t.tryonSessionId:n;if(!o)throw new Error("The init API did not return sessionToken.");return{sessionToken:o,tryonSessionId:l,initUrl:e.initUrl,iframeUrl:e.iframeUrl,iframeOrigin:e.iframeOrigin}}_initializeSessionIfNeeded(){var t=this;if(!this._state.merchantId)return Promise.reject(new Error("The merchant-id attribute is required."));if(!this._state.widgetId)return Promise.reject(new Error("The widget-id attribute is required."));if(!this._state.env)return Promise.reject(new Error("The env attribute is required."));var e=this._getInitStateKey();if(this._initPromise&&this._initStateKey===e)return this._initPromise;if(this._abortController&&typeof this._abortController.abort=="function")try{this._abortController.abort()}catch(r){}this._initStateKey=e,this._activeInitRequestId+=1;var n=this._activeInitRequestId;this._setStatus("loading");var o;try{o=this._buildResolvedUrls()}catch(r){return Promise.reject(r)}var l=this._buildInitPayload();this._state.tryonSessionId=l.tryonSessionId,this._abortController=typeof AbortController!="undefined"?new AbortController:null;var a=fetch(o.initUrl,{method:"POST",headers:{"Content-Type":"application/json","X-Merchant-Id":this._state.merchantId,"X-Widget-Id":this._state.widgetId},body:JSON.stringify(l),signal:this._abortController?this._abortController.signal:void 0}).then(function(r){return r.ok?r.json():r.text().then(function(u){var y=T(u),N=y&&y.message||u||"Failed to initialize session ("+r.status+").";throw new Error(N)})}).then(function(r){return t._normalizeInitResponse(r,o,l.tryonSessionId)});return this._initPromise=x(a,this._state.initTimeoutMs,"Timed out while initializing the Vitraun session.",this._abortController).then(function(r){return n!==t._activeInitRequestId||(t._state.sessionToken=r.sessionToken,t._state.tryonSessionId=r.tryonSessionId,t._state.resolvedInitUrl=r.initUrl,t._state.resolvedIframeUrl=r.iframeUrl,t._state.iframeOrigin=r.iframeOrigin,t._setStatus("ready")),r}).catch(function(r){throw n!==t._activeInitRequestId||(t._state.sessionToken=null,t._state.resolvedInitUrl=null,t._state.resolvedIframeUrl=null,t._state.iframeOrigin=null,t._setStatus("error",{message:r&&r.message?r.message:"Initialization failed."})),r}).finally(function(){n===t._activeInitRequestId&&(t._initPromise=null)}),this._initPromise}_sendConfigToIframe(){var t=this._getIframeElement();if(!(!t||!t.contentWindow)){var e=this._state.iframeOrigin;if(!e){this._log("Config not sent: iframeOrigin is missing.");return}var n={merchantId:this._state.merchantId,widgetId:this._state.widgetId,sessionToken:this._state.sessionToken,tryonSessionId:this._state.tryonSessionId||w(),lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,app:{appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken},hostPageOrigin:_(),hostPageUrl:g()};this._state.isSimulationMode&&this._state.simulationInlineConfig&&typeof this._state.simulationInlineConfig=="object"&&(n.inlineVirtualTryonConfig=this._state.simulationInlineConfig),t.contentWindow.postMessage({type:v,payload:n},e)}}_renderLoading(){if(this.shadowRoot){var t=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var e=document.createElement("style");e.textContent=":host { display:block; width:100%; min-height:"+t+"; box-sizing:border-box; }.vitraun-loading {min-height:"+t+";display:flex;align-items:center;justify-content:center;border:1px solid #ececec;background:#fafafa;color:#333;font:14px/1.4 Arial, sans-serif;padding:16px;box-sizing:border-box;border-radius:8px;text-align:center;}";var n=document.createElement("div");n.className="vitraun-loading",n.textContent="Loading Vitraun...",this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(n)}}_renderError(t){if(this.shadowRoot){var e=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var n=document.createElement("style");n.textContent=":host { display:block; width:100%; min-height:"+e+"; box-sizing:border-box; }.vitraun-error {min-height:"+e+";display:flex;align-items:center;justify-content:center;border:1px solid #f1d5d5;background:#fff7f7;color:#7a1f1f;font:14px/1.4 Arial, sans-serif;padding:16px;box-sizing:border-box;border-radius:8px;text-align:center;}";var o=document.createElement("div");o.className="vitraun-error",o.textContent=t||"Failed to load Vitraun.",this.shadowRoot.appendChild(n),this.shadowRoot.appendChild(o)}}_renderIframe(t,e){if(this.shadowRoot){var n=this,o=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var l=document.createElement("style");l.textContent=":host { display:block; width:100%; min-height:"+o+"; box-sizing:border-box; }";var a=document.createElement("iframe");a.src=t,a.title="Vitraun VTO",a.style.cssText="width:100%;height:100%;border:0;min-height:"+o,a.setAttribute("allow","camera"),a.setAttribute("referrerpolicy","strict-origin-when-cross-origin"),a.onload=function(){e===n._renderVersion&&[0,50,150,400,1e3].forEach(function(r){setTimeout(function(){e===n._renderVersion&&n._sendConfigToIframe()},r)})},this.shadowRoot.appendChild(l),this.shadowRoot.appendChild(a)}}_render(){if(this.shadowRoot){this._renderVersion+=1;var t=this._renderVersion;if(!this._state.merchantId){this._setStatus("error",{message:"The merchant-id attribute is required."}),this._renderError("The merchant-id attribute is required.");return}if(!this._state.widgetId){this._setStatus("error",{message:"The widget-id attribute is required."}),this._renderError("The widget-id attribute is required.");return}if(!this._state.env){this._setStatus("error",{message:"The env attribute is required."}),this._renderError("The env attribute is required.");return}this._renderLoading(),this._initializeSessionIfNeeded().then(function(e){t===this._renderVersion&&this._renderIframe(e.iframeUrl,t)}.bind(this)).catch(function(e){t===this._renderVersion&&this._renderError(e&&e.message?e.message:"Failed to load Vitraun.")}.bind(this))}}open(){return this.style.display="",Promise.resolve()}close(){return this.style.display="none",Promise.resolve()}}typeof customElements!="undefined"&&!customElements.get("vitraun-vto")&&customElements.define("vitraun-vto",R)})();})();
1
+ "use strict";var VitraunWebAR=(()=>{(function(){"use strict";var y="VITRAUN_CONFIG",p="vtrn-vto-session",b=18e5,c=1e4,f="en-US",d=80;function s(i,t,e){var n=i.getAttribute(t);return n!=null&&n!==""?n:e}function h(i,t){if(i==null||i==="")return t;var e=String(i).toLowerCase();return e==="true"||e==="1"||e==="yes"}function m(i){if(i==null||i==="")return null;var t=parseFloat(String(i));return Number.isFinite(t)?t:null}function S(i){return!i||typeof i!="string"?[]:i.split(",").map(function(t){return t.trim()}).filter(Boolean)}function T(i,t){var e=s(i,t,"");if(!e||typeof e!="string")return null;try{return JSON.parse(e)}catch(n){return null}}function k(i){try{return JSON.parse(i)}catch(t){return null}}function g(){return typeof window=="undefined"||!window.location||!window.location.origin?null:window.location.origin}function _(){return typeof window=="undefined"||!window.location||!window.location.href?null:window.location.href}function P(i){if(!i||typeof i!="string")return null;try{return new URL(i).origin}catch(t){return null}}function C(i){var t=String(i||"").trim().toLowerCase();if(!t)throw new Error("The env attribute is required.");if(!/^[a-z0-9-]+$/.test(t))throw new Error("The env attribute contains an invalid value.");return t}function O(i){return i?String(i).trim().toLowerCase():"web"}function I(i,t){if(i==null||i==="")return t;var e=parseInt(String(i),10);return!Number.isInteger(e)||e<=0||e>65535?t:e}function E(i,t){return"https://app."+i+".vitraun.com:"+t+"/v1/public/virtual-tryon-embed-init"}function U(i,t){return{merchantId:i.merchantId,widgetId:i.widgetId,tryonSessionId:t,app:{appId:i.appId,platform:i.platform,bundleId:i.bundleId,packageName:i.packageName,appVersion:i.appVersion,attestationToken:i.attestationToken},context:{origin:g(),pageUrl:_(),userAgent:typeof navigator!="undefined"&&navigator.userAgent?navigator.userAgent:null,referrer:typeof document!="undefined"&&document.referrer?document.referrer:null,timezone:typeof Intl!="undefined"&&Intl.DateTimeFormat?Intl.DateTimeFormat().resolvedOptions().timeZone:null}}}function R(i,t,e,n){var o=t||f,l="https://app."+i+".vitraun.com:"+n+"/"+encodeURIComponent(o)+"/virtual-tryon";return e&&(l+="?simulation=1"),l}function x(i,t,e,n){return!t||t<=0?i:new Promise(function(o,l){var a=!1,r=setTimeout(function(){if(!a){a=!0;try{n&&typeof n.abort=="function"&&n.abort()}catch(u){}l(new Error(e||"The operation timed out."))}},t);i.then(function(u){a||(a=!0,clearTimeout(r),o(u))}).catch(function(u){a||(a=!0,clearTimeout(r),l(u))})})}function w(){if(typeof window=="undefined"||!window.crypto||!window.crypto.randomUUID)return"00000000-0000-4000-8000-000000000000";try{var i=sessionStorage.getItem(p);if(i){var t=JSON.parse(i);if(t&&typeof t.id=="string"&&typeof t.exp=="number"&&Date.now()<t.exp)return t.id}}catch(o){}var e=window.crypto.randomUUID(),n=Date.now()+b;try{sessionStorage.setItem(p,JSON.stringify({id:e,exp:n}))}catch(o){}return e}class N extends HTMLElement{static get observedAttributes(){return["merchant-id","widget-id","env","api-port","app-port","lang","isolated-sku","apply-skus","flow","basket-opens-in","checkout-opens-in","use-simple-page","show-details","show-product-price","price-from","price-to","currency-symbol","is-simulation-mode","simulation-inline-config","container-min-height","app-id","platform","bundle-id","package-name","app-version","attestation-token","debug","init-timeout-ms"]}constructor(){super(),this.attachShadow({mode:"open"}),this._state={merchantId:"",widgetId:"",env:"",apiPort:d,appPort:d,lang:f,isolatedSku:null,applySkus:[],iframeOrigin:null,flow:"checkout",basketOpensIn:"event",checkoutOpensIn:"event",useSimplePage:!0,showDetails:!0,showProductPrice:!1,priceFrom:null,priceTo:null,currencySymbol:null,isSimulationMode:!1,simulationInlineConfig:null,appId:null,platform:"web",bundleId:null,packageName:null,appVersion:null,attestationToken:null,debug:!1,sessionToken:null,tryonSessionId:null,resolvedInitUrl:null,resolvedIframeUrl:null,initTimeoutMs:c},this._boundHandleMessage=this._handleMessage.bind(this),this._initPromise=null,this._initStateKey=null,this._activeInitRequestId=0,this._renderVersion=0,this._abortController=null}connectedCallback(){this._readAttributes(),this._render(),typeof window!="undefined"&&window.addEventListener("message",this._boundHandleMessage)}disconnectedCallback(){if(typeof window!="undefined"&&window.removeEventListener("message",this._boundHandleMessage),this._abortController&&typeof this._abortController.abort=="function")try{this._abortController.abort()}catch(t){}}attributeChangedCallback(t,e,n){e!==n&&(this._readAttributes(),this._render())}_log(){if(!(!this._state.debug||typeof console=="undefined"||!console.log)){var t=Array.prototype.slice.call(arguments);t.unshift("[vitraun-vto]"),console.log.apply(console,t)}}_readAttributes(){var t=T(this,"simulation-inline-config"),e=!1;t&&t.features&&typeof t.features.showProductPrice=="boolean"&&(e=t.features.showProductPrice);var n=!0;t&&t.features&&typeof t.features.useSimplePage=="boolean"&&(n=t.features.useSimplePage);var o="checkout";t&&t.features&&(t.features.flow==="catalog"||t.features.flow==="checkout")&&(o=t.features.flow);var l=!0;t&&t.features&&typeof t.features.showDetails=="boolean"&&(l=t.features.showDetails);var a="";try{a=C(s(this,"env",""))}catch(u){a=""}var r=parseInt(s(this,"init-timeout-ms",String(c)),10);(!Number.isFinite(r)||r<0)&&(r=c),this._state={merchantId:s(this,"merchant-id",""),widgetId:s(this,"widget-id",""),env:a,apiPort:I(s(this,"api-port",String(d)),d),appPort:I(s(this,"app-port",String(d)),d),lang:s(this,"lang",f),isolatedSku:s(this,"isolated-sku",null)||null,applySkus:S(s(this,"apply-skus","")),iframeOrigin:this._state&&this._state.iframeOrigin?this._state.iframeOrigin:null,flow:s(this,"flow",o),basketOpensIn:s(this,"basket-opens-in","event"),checkoutOpensIn:s(this,"checkout-opens-in","event"),useSimplePage:h(s(this,"use-simple-page",""),n),showDetails:h(s(this,"show-details",""),l),showProductPrice:h(s(this,"show-product-price",""),e),priceFrom:m(s(this,"price-from",null)),priceTo:m(s(this,"price-to",null)),currencySymbol:s(this,"currency-symbol",null)||null,isSimulationMode:h(s(this,"is-simulation-mode","false"),!1),simulationInlineConfig:t,appId:s(this,"app-id",null)||null,platform:O(s(this,"platform",null)),bundleId:s(this,"bundle-id",null)||null,packageName:s(this,"package-name",null)||null,appVersion:s(this,"app-version",null)||null,attestationToken:s(this,"attestation-token",null)||null,debug:h(s(this,"debug","false"),!1),sessionToken:this._state&&this._state.sessionToken?this._state.sessionToken:null,tryonSessionId:this._state&&this._state.tryonSessionId?this._state.tryonSessionId:null,resolvedInitUrl:this._state&&this._state.resolvedInitUrl?this._state.resolvedInitUrl:null,resolvedIframeUrl:this._state&&this._state.resolvedIframeUrl?this._state.resolvedIframeUrl:null,initTimeoutMs:r}}_emit(t,e){this.dispatchEvent(new CustomEvent(t,{detail:e!=null?e:null,bubbles:!0,composed:!0}))}_setStatus(t,e){this.setAttribute("data-status",t),this._emit("statusChange",{status:t,detail:e||null})}_getIframeElement(){return this.shadowRoot?this.shadowRoot.querySelector("iframe"):null}_handleMessage(t){var e=t.data;if(!(!e||typeof e!="object")){var n=this._getIframeElement();if(!(!n||!n.contentWindow)&&t.source===n.contentWindow&&!(!this._state.iframeOrigin||t.origin!==this._state.iframeOrigin)){var o=e.type,l=e.detail;if(o==="VTO_USAGE"){this._emit("vtoUsage",l);return}var a=["addToCart","removeFromCart","redirectToCart","analysisFinished"];a.indexOf(o)!==-1&&this._emit(o,l)}}}_getInitStateKey(){return JSON.stringify({merchantId:this._state.merchantId,widgetId:this._state.widgetId,env:this._state.env,apiPort:this._state.apiPort,appPort:this._state.appPort,lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,simulationInlineConfig:this._state.simulationInlineConfig,appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken})}_buildResolvedUrls(){if(!this._state.env)throw new Error("The env attribute is required.");var t=R(this._state.env,this._state.lang,this._state.isSimulationMode,this._state.appPort);return{initUrl:E(this._state.env,this._state.apiPort),iframeUrl:t,iframeOrigin:P(t)}}_buildInitPayload(){var t=w();return{tryonSessionId:t,initRequestBody:U(this._state,t)}}_normalizeInitResponse(t,e,n){if(!t||typeof t!="object")throw new Error("Invalid init response.");var o=typeof t.sessionToken=="string"&&t.sessionToken?t.sessionToken:null,l=typeof t.tryonSessionId=="string"&&t.tryonSessionId?t.tryonSessionId:n;if(!o)throw new Error("The init API did not return sessionToken.");return{sessionToken:o,tryonSessionId:l,initUrl:e.initUrl,iframeUrl:e.iframeUrl,iframeOrigin:e.iframeOrigin}}_initializeSessionIfNeeded(){var t=this;if(!this._state.merchantId)return Promise.reject(new Error("The merchant-id attribute is required."));if(!this._state.widgetId)return Promise.reject(new Error("The widget-id attribute is required."));if(!this._state.env)return Promise.reject(new Error("The env attribute is required."));var e=this._getInitStateKey();if(this._initPromise&&this._initStateKey===e)return this._initPromise;if(this._abortController&&typeof this._abortController.abort=="function")try{this._abortController.abort()}catch(r){}this._initStateKey=e,this._activeInitRequestId+=1;var n=this._activeInitRequestId;this._setStatus("loading");var o;try{o=this._buildResolvedUrls()}catch(r){return Promise.reject(r)}var l=this._buildInitPayload();this._state.tryonSessionId=l.tryonSessionId,this._abortController=typeof AbortController!="undefined"?new AbortController:null;var a=fetch(o.initUrl,{method:"POST",headers:{"Content-Type":"application/json","X-Merchant-Id":this._state.merchantId,"X-Widget-Id":this._state.widgetId},body:JSON.stringify(l.initRequestBody),signal:this._abortController?this._abortController.signal:void 0}).then(function(r){return r.ok?r.json():r.text().then(function(u){var v=k(u),A=v&&v.message||u||"Failed to initialize session ("+r.status+").";throw new Error(A)})}).then(function(r){return t._normalizeInitResponse(r,o,l.tryonSessionId)});return this._initPromise=x(a,this._state.initTimeoutMs,"Timed out while initializing the Vitraun session.",this._abortController).then(function(r){return n!==t._activeInitRequestId||(t._state.sessionToken=r.sessionToken,t._state.tryonSessionId=r.tryonSessionId,t._state.resolvedInitUrl=r.initUrl,t._state.resolvedIframeUrl=r.iframeUrl,t._state.iframeOrigin=r.iframeOrigin,t._setStatus("ready")),r}).catch(function(r){throw n!==t._activeInitRequestId||(t._state.sessionToken=null,t._state.resolvedInitUrl=null,t._state.resolvedIframeUrl=null,t._state.iframeOrigin=null,t._setStatus("error",{message:r&&r.message?r.message:"Initialization failed."})),r}).finally(function(){n===t._activeInitRequestId&&(t._initPromise=null)}),this._initPromise}_sendConfigToIframe(){var t=this._getIframeElement();if(!(!t||!t.contentWindow)){var e=this._state.iframeOrigin;if(!e){this._log("Config not sent: iframeOrigin is missing.");return}var n={merchantId:this._state.merchantId,widgetId:this._state.widgetId,sessionToken:this._state.sessionToken,tryonSessionId:this._state.tryonSessionId||w(),lang:this._state.lang,isolatedSku:this._state.isolatedSku,applySkus:this._state.applySkus,flow:this._state.flow,basketOpensIn:this._state.basketOpensIn,checkoutOpensIn:this._state.checkoutOpensIn,useSimplePage:this._state.useSimplePage,showDetails:this._state.showDetails,showProductPrice:this._state.showProductPrice,priceFrom:this._state.priceFrom,priceTo:this._state.priceTo,currencySymbol:this._state.currencySymbol,isSimulationMode:this._state.isSimulationMode,app:{appId:this._state.appId,platform:this._state.platform,bundleId:this._state.bundleId,packageName:this._state.packageName,appVersion:this._state.appVersion,attestationToken:this._state.attestationToken},hostPageOrigin:g(),hostPageUrl:_()};this._state.isSimulationMode&&this._state.simulationInlineConfig&&typeof this._state.simulationInlineConfig=="object"&&(n.inlineVirtualTryonConfig=this._state.simulationInlineConfig),t.contentWindow.postMessage({type:y,payload:n},e)}}_renderLoading(){if(this.shadowRoot){var t=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var e=document.createElement("style");e.textContent=":host { display:block; width:100%; min-height:"+t+"; box-sizing:border-box; }.vitraun-loading {min-height:"+t+";display:flex;align-items:center;justify-content:center;border:1px solid #ececec;background:#fafafa;color:#333;font:14px/1.4 Arial, sans-serif;padding:16px;box-sizing:border-box;border-radius:8px;text-align:center;}";var n=document.createElement("div");n.className="vitraun-loading",n.textContent="Loading Vitraun...",this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(n)}}_renderError(t){if(this.shadowRoot){var e=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var n=document.createElement("style");n.textContent=":host { display:block; width:100%; min-height:"+e+"; box-sizing:border-box; }.vitraun-error {min-height:"+e+";display:flex;align-items:center;justify-content:center;border:1px solid #f1d5d5;background:#fff7f7;color:#7a1f1f;font:14px/1.4 Arial, sans-serif;padding:16px;box-sizing:border-box;border-radius:8px;text-align:center;}";var o=document.createElement("div");o.className="vitraun-error",o.textContent=t||"Failed to load Vitraun.",this.shadowRoot.appendChild(n),this.shadowRoot.appendChild(o)}}_renderIframe(t,e){if(this.shadowRoot){var n=this,o=s(this,"container-min-height","80vh");this.shadowRoot.innerHTML="";var l=document.createElement("style");l.textContent=":host { display:block; width:100%; min-height:"+o+"; box-sizing:border-box; }";var a=document.createElement("iframe");a.src=t,a.title="Vitraun VTO",a.style.cssText="width:100%;height:100%;border:0;min-height:"+o,a.setAttribute("allow","camera"),a.setAttribute("referrerpolicy","strict-origin-when-cross-origin"),a.onload=function(){e===n._renderVersion&&[0,50,150,400,1e3].forEach(function(r){setTimeout(function(){e===n._renderVersion&&n._sendConfigToIframe()},r)})},this.shadowRoot.appendChild(l),this.shadowRoot.appendChild(a)}}_render(){if(this.shadowRoot){this._renderVersion+=1;var t=this._renderVersion;if(!this._state.merchantId){this._setStatus("error",{message:"The merchant-id attribute is required."}),this._renderError("The merchant-id attribute is required.");return}if(!this._state.widgetId){this._setStatus("error",{message:"The widget-id attribute is required."}),this._renderError("The widget-id attribute is required.");return}if(!this._state.env){this._setStatus("error",{message:"The env attribute is required."}),this._renderError("The env attribute is required.");return}this._renderLoading(),this._initializeSessionIfNeeded().then(function(e){t===this._renderVersion&&this._renderIframe(e.iframeUrl,t)}.bind(this)).catch(function(e){t===this._renderVersion&&this._renderError(e&&e.message?e.message:"Failed to load Vitraun.")}.bind(this))}}open(){return this.style.display="",Promise.resolve()}close(){return this.style.display="none",Promise.resolve()}}typeof customElements!="undefined"&&!customElements.get("vitraun-vto")&&customElements.define("vitraun-vto",N)})();})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitraun/webar",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Vitraun WebAR widget as Web Component",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "publishConfig": {