@saasquatch/squatch-js 2.8.3 → 2.8.4
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.
- package/CHANGELOG.md +8 -1
- package/dist/squatch.cjs.js +1 -1
- package/dist/squatch.cjs.js.map +1 -1
- package/dist/squatch.esm.js +1 -1
- package/dist/squatch.esm.js.map +1 -1
- package/dist/squatch.js +1 -1
- package/dist/squatch.js.map +1 -1
- package/dist/squatch.min.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [2.8.4] - 2026-04-01
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- Loading skeleton was showing inside \<impact-popup> container on initial loads
|
|
15
|
+
|
|
10
16
|
## [2.8.3] - 2026-04-01
|
|
11
17
|
|
|
12
18
|
### Added
|
|
@@ -412,7 +418,8 @@ No release notes.
|
|
|
412
418
|
|
|
413
419
|
No release notes.
|
|
414
420
|
|
|
415
|
-
[unreleased]: https://github.com/saasquatch/squatch-js/compare/@saasquatch%2Fsquatch-js@2.8.
|
|
421
|
+
[unreleased]: https://github.com/saasquatch/squatch-js/compare/@saasquatch%2Fsquatch-js@2.8.4...HEAD
|
|
422
|
+
[2.8.4]: https://github.com/saasquatch/squatch-js/compare/@saasquatch%2Fsquatch-js@2.8.2...@saasquatch%2Fsquatch-js@2.8.4
|
|
416
423
|
[2.8.3]: https://github.com/saasquatch/squatch-js/compare/@saasquatch%2Fsquatch-js@2.8.2...@saasquatch%2Fsquatch-js@2.8.3
|
|
417
424
|
[2.8.2]: https://github.com/saasquatch/squatch-js/compare/@saasquatch%2Fsquatch-js@2.8.1...@saasquatch%2Fsquatch-js@2.8.2
|
|
418
425
|
[2.8.1]: https://github.com/saasquatch/squatch-js/compare/@saasquatch%2Fsquatch-js@2.8.0...@saasquatch%2Fsquatch-js@2.8.1
|
package/dist/squatch.cjs.js
CHANGED
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
</style>`:""}
|
|
49
49
|
${await this._getSkeletonPreloadHTML(i,(W=e==null?void 0:e.color)==null?void 0:W.backgroundColor)}
|
|
50
50
|
${await this._getContent()}
|
|
51
|
-
`),h.close(),I("Popup template loaded into iframe"),await this._setupResizeHandler(s)}async _setupResizeHandler(e){const{contentWindow:n}=e;if(!n)throw new Error("Frame needs a content window");const i=n.document;$(i,async()=>{i.body.style.overflowY="hidden";let s=!0;new n.ResizeObserver(a=>{for(const c of a){const{top:d,bottom:h}=c.contentRect,u=h+d;u<=0||(s?(s=!1,e.style.height="0",e.height=i.body.scrollHeight+"",e.style.height=""):e.height=u+"",c.target.style="")}}).observe(await this._findInnerContainer(e))})}open(){const e=this.container?this._findElement():document.body,i=(e.shadowRoot||e).querySelector(`#${this.id}`);if(!i)throw new Error("Could not determine container div");i.showModal();const s=this._findFrame();if(!s)throw new Error("Could not find iframe");const{contentWindow:r}=s;if(!r)throw new Error("Squatch.js has an empty iframe");const a=r.document;$(a,()=>{var d;const c=r.squatch||r.widgetIdent;(d=s.contentDocument)==null||d.dispatchEvent(new CustomEvent("sq:refresh")),this.context.user?(this._loadEvent(c),I("Popup opened")):this._attachLoadEventListener(a,c)})}close(){const e=this._findFrame();e!=null&&e.contentDocument&&this._detachLoadEventListener(e.contentDocument);const n=this.container?this._findElement():document.body,s=(n.shadowRoot||n).querySelector(`#${this.id}`);if(!s)throw new Error("Could not determine container div");s.close(),I("Popup closed")}_getErrorStyle(){return"body { margin: 0; } .modal { box-shadow: none; border: 0; }"}}const E=C("squatch-js:widgets");class M{constructor(t){l(this,"api");l(this,"tenantAlias");l(this,"domain");l(this,"npmCdn");const e=P(t);this.tenantAlias=e.tenantAlias,this.domain=e.domain,this.npmCdn=e.npmCdn,this.api=new z(e)}async upsertUser(t){const n=he(t);try{const i=await this.api.upsertUser(n);return{widget:this._renderWidget(i,n,{type:"upsert",user:n.user,engagementMedium:t.engagementMedium,container:t.container,trigger:t.trigger,widgetConfig:{values:{brandingConfig:i==null?void 0:i.brandingConfig}}}),user:i.user}}catch(i){throw E(i),i.apiErrorCode&&this._renderErrorWidget(i,t.engagementMedium),new Error(i)}}async render(t){const n=ue(t);try{const i=await this.api.render(n);return{widget:this._renderWidget(i,n,{type:"passwordless",engagementMedium:n.engagementMedium,container:n.container,trigger:n.trigger,widgetConfig:i==null?void 0:i.widgetConfig}),user:i.user}}catch(i){throw i.apiErrorCode&&this._renderErrorWidget(i,n.engagementMedium),new Error(i)}}async autofill(t){const e=t;if(typeof e=="function"){try{const s=await this.api.squatchReferralCookie();e(s)}catch(s){throw E("Autofill error",s),new Error(s)}return}if(typeof e!="string")throw new Error("Autofill accepts a string or function");let n=document.querySelectorAll(e),i;if(n.length>0)i=n[0];else throw E("Element id/class or function missing"),new Error("Element id/class or function missing");try{const s=await this.api.squatchReferralCookie();i.value=s.codes[0]}catch(s){throw new Error(s)}}_renderWidget(t,e,n){var c;if(E("Rendering Widget..."),!t)throw new Error("Unable to get a response");let i,s=!!e.displayOnLoad;const r=t.jsOptions||{},a={content:t.template,type:e.widgetType||((c=r.widget)==null?void 0:c.defaultWidgetType),api:this.api,domain:this.domain,npmCdn:this.npmCdn,context:n};return r.widgetUrlMappings&&r.widgetUrlMappings.forEach(d=>{var h,u;M._matchesUrl(d.url)&&(d.widgetType!=="CONVERSION_WIDGET"||(u=(h=t.user)==null?void 0:h.referredBy)!=null&&u.code?(s=d.displayOnLoad,E(`Display ${d.widgetType} on ${d.url}`)):E(`Don't display ${d.widgetType} when no referral on widget rule match ${d.url}`))}),r.fuelTankAutofillUrls&&(E("We found a fuel tank autofill!"),r.fuelTankAutofillUrls.forEach(({url:d,formSelector:h})=>{var u,m,p;if(M._matchesUrl(d)&&(E("Fuel Tank URL matches"),(m=(u=t.user)==null?void 0:u.referredBy)!=null&&m.code)){const g=document.querySelector(h);g?g.value=((p=t.user.referredBy.referredReward)==null?void 0:p.fuelTankCode)||"":E(new Error(`Element with id/class ${h} was not found.`))}})),e.engagementMedium==="EMBED"?i=this._renderEmbedWidget(a):(i=this._renderPopupWidget(a),s&&i.open()),i}_renderPopupWidget(t){const e=new x(t,t.context.trigger);return e.load(),e}_renderEmbedWidget(t){const e=new q(t,t.context.container);return e.load(),e}_renderErrorWidget(t,e="POPUP"){const{apiErrorCode:n,rsCode:i,statusCode:s,message:r}=t;E(new Error(`${n} (${i}) ${r}`));const a={content:"error",rsCode:i,apiErrorCode:n,statusCode:s,errorMessage:r,api:this.api,domain:this.domain,npmCdn:this.npmCdn,type:"ERROR_WIDGET",context:{type:"error"}};let c;e==="EMBED"?(c=new q(a),c.load()):e==="POPUP"&&(c=new x(a),c.load())}static _matchesUrl(t){return!!window.location.href.match(new RegExp(t))}}class De{constructor(t){l(this,"tenantAlias");l(this,"domain");const n=P(t);this.tenantAlias=n.tenantAlias,this.domain=n.domain}track(t,e){const n=t,i=e,s=je(n),{jwt:r}=Ne(i),a=encodeURIComponent(this.tenantAlias),c=encodeURIComponent(s.userId),d=encodeURIComponent(s.accountId),h=`/api/v1/${a}/open/account/${d}/user/${c}/events`,u=this.domain+h;return Z(u,JSON.stringify(s),r)}}function je(o){if(!S(o))throw new Error("tracking parameter must be an object");if(!(o!=null&&o.accountId))throw new Error("accountId field is required");if(!(o!=null&&o.events))throw new Error("events field is required");if(!(o!=null&&o.userId))throw new Error("userId field is required");const t=o;if(!Array.isArray(t.events))throw new Error("'events' should be an array");return t}function Ne(o){if(!S(o))throw new Error("'options' should be an object");return o}function Fe(){var i;const o=window[X]?X:L,t=((i=window["_"+o])==null?void 0:i.ready)||[],e=window.impactOnReady||window.squatchOnReady,n=[...t,e].filter(s=>!!s);setTimeout(()=>{window[L]&&(window[X]=window[L],n.forEach(s=>s()),window[L]._auto(),window["_"+o]=void 0,delete window["_"+o])},0)}const D=C("squatch-js");function He(){var u;const o=window.location.search,e=new URLSearchParams(o).get("_saasquatchExtra")||"";if(!e){D("No _saasquatchExtra param");return}const n=P({tenantAlias:"UNKNOWN"});if(!n.domain){D("domain must be provided in config to use _saasquatchExtra");return}let i;try{i=JSON.parse(N(e))}catch{D("Unable to decode _saasquatchExtra config");return}function s(m){return m.replace(/^https?:\/\//,"")}const r=s(n.domain),a=Object.keys((i==null?void 0:i[r])||{})[0],c=(u=i==null?void 0:i[r])==null?void 0:u[a];if(!c){D("_saasquatchExtra did not have an expected structure");return}const{autoPopupWidgetType:d,...h}=c;return{widgetConfig:{widgetType:d,displayOnLoad:!0,...h},squatchConfig:{...n,tenantAlias:a}}}const Je=C("squatch-js:decodeUserJwt");function Be(o){var t;try{const e=o.split(".")[1];if(e===void 0)return null;const n=N(e);return(t=JSON.parse(n))==null?void 0:t.user}catch(e){return Je(e),null}}const j=C("squatch-js:DeclarativeWidget");class we extends HTMLElement{constructor(){super();l(this,"config");l(this,"token");l(this,"tenant");l(this,"widgetType");l(this,"locale");l(this,"widgetApi");l(this,"analyticsApi");l(this,"widgetInstance");l(this,"type");l(this,"container");l(this,"element");l(this,"loaded");l(this,"_setWidget",(e,n)=>{var s;const i={api:this.widgetApi,content:e.template,context:{type:n.type,user:n.user,container:this.container||void 0,engagementMedium:this.type,widgetConfig:e.widgetConfig},type:this.widgetType,domain:((s=this.config)==null?void 0:s.domain)||k,npmCdn:Y,container:this};if(this.type==="EMBED")return new q(i);{const r=this.firstChild?null:void 0;return new x(i,r)}});l(this,"setErrorWidget",e=>{var c;const n=e instanceof Error?e.message:e==null?void 0:e.message,i=e==null?void 0:e.apiErrorCode,s=e==null?void 0:e.rsCode,r=e==null?void 0:e.statusCode,a={api:this.widgetApi,content:"error",context:{type:"error",container:this.container||void 0},type:"ERROR_WIDGET",domain:((c=this.config)==null?void 0:c.domain)||k,npmCdn:Y,container:this,apiErrorCode:i,rsCode:s,statusCode:r,errorMessage:n};if(this.type==="EMBED")return new q(a);{const d=this.firstChild?null:void 0;return new x(a,d)}});l(this,"reload",this.renderWidget);l(this,"show",this.open);l(this,"hide",this.close);this.attachShadow({mode:"open"}).innerHTML="<style>:host { display: block; }</style><slot></slot>",this.config=K(),this.token=U(),this.tenant=window.squatchTenant,this.container=this}_setupApis(e){var n,i;if(!this.tenant)throw new Error("tenantAlias not provided");this.widgetApi=new z({tenantAlias:(e==null?void 0:e.tenantAlias)||this.tenant,domain:(e==null?void 0:e.domain)||((n=this.config)==null?void 0:n.domain)||k}),this.analyticsApi=new ge({domain:(e==null?void 0:e.domain)||((i=this.config)==null?void 0:i.domain)||k})}getWidgetType(e){return e&&e.includes("friendWidget")?"instant-access":"verified-access"}async renderPasswordlessVariant(){return this._setupApis(),j("Rendering as an Instant Access widget"),await this.widgetApi.render({engagementMedium:this.type,widgetType:this.widgetType,locale:this.locale}).then(e=>this._setWidget(e,{type:"passwordless"})).catch(this.setErrorWidget)}async renderUserUpsertVariant(){this._setupApis();const e=Be(this.token);if(!e)return this.setErrorWidget(Error("No user object in token."));!this.locale&&e.locale&&(this.locale=e.locale),j("Rendering as a Verified widget");try{await this.widgetApi.upsertUser({user:e,locale:this.locale,engagementMedium:this.type,widgetType:this.widgetType,jwt:this.token})}catch(i){return this.setErrorWidget(i)}return await this.widgetApi.render({locale:this.locale,engagementMedium:this.type,widgetType:this.widgetType}).then(i=>this._setWidget(i,{type:"upsert",user:e})).catch(this.setErrorWidget)}async getWidgetInstance(){let e;this.widgetType=this.getAttribute("widget")||void 0,this.locale=this.getAttribute("locale")||this.locale;const n=this.getWidgetType(this.widgetType);if(!this.widgetType)throw new Error("No widget has been specified");return!this.token&&n==="verified-access"&&j("[SquatchJS] Authentication token is required for this widget type."),this.token?e=await this.renderUserUpsertVariant():e=await this.renderPasswordlessVariant(),this.widgetInstance=e,this.widgetInstance&&this.dispatchEvent(new CustomEvent("sq:widget-loaded")),e}async renderWidget(){await this.getWidgetInstance(),await this.widgetInstance.load()}open(){if(!this.widgetInstance)throw new Error("Widget has not loaded yet");this.widgetInstance.open()}close(){if(!this.widgetInstance)throw new Error("Widget has not loaded yet");this.widgetInstance.close()}static get observedAttributes(){return["widget","locale"]}attributeChangedCallback(e,n,i){if(!(n===i||!this.loaded))switch(e){case"locale":case"widget":this.connectedCallback();break}}async connectedCallback(){this.loaded=!0,this.container=this.getAttribute("container"),this.widgetType=this.getAttribute("widget")||void 0;const e=this.getWidgetType(this.widgetType),{getSkeleton:n}=await Promise.resolve().then(()=>require("./SkeletonTemplate-B3Bk4NFu.cjs")),i=n({type:e}),s=document.createElement("div");s.id="loading-skeleton",s.innerHTML=i;const r=this.shadowRoot||this.attachShadow({mode:"open"});if(this.type==="POPUP"){const a=r.getElementById("squatchModal");a?(a.innerHTML="",a.appendChild(s)):r.appendChild(s)}else r.innerHTML="",r.appendChild(s);try{await this.renderWidget()}catch(a){j("Failed to render widget",a)}finally{const a=r.getElementById("loading-skeleton");a&&a.remove()}this.getAttribute("open")!==null&&this.open()}}class ee extends we{constructor(){super(),this.type="EMBED",this.loaded=!1}}class te extends we{constructor(){super(),this.type="POPUP",this.loaded=!1,this.addEventListener("click",t=>{t.stopPropagation(),this.open()})}}class Ge extends ee{}class ze extends te{}class Ve extends ee{}class Xe extends te{}window.customElements.get("squatch-embed")||window.customElements.define("squatch-embed",Ge);window.customElements.get("impact-embed")||window.customElements.define("impact-embed",Ve);window.customElements.get("squatch-popup")||window.customElements.define("squatch-popup",ze);window.customElements.get("impact-popup")||window.customElements.define("impact-popup",Xe);function Qe(){console.log("Having trouble using Squatch.js? Go to https://docs.referralsaasquatch.com/developer/ for tutorials, references and error codes.")}const T=C("squatch-js");let H=null,J=null,B=null;function Ye(){return H||R({}),H}function V(){return J||R({}),J}function Ze(){return B||R({}),B}function Ke(o){var t;return(t=V())==null?void 0:t.render(o)}function et(){var t;const o=He();if(o){const{squatchConfig:e,widgetConfig:n}=o;return R(e),(t=V())==null?void 0:t.render(n)}}function R(o){const e=P(o);e.tenantAlias.match("^test")||e.debug?_e("squatch-js*"):ke(),T("initializing ..."),H=new z(e),J=new M(e),B=new De(e),T("Widget API instance",H),T("Widgets instance",J),T("Events API instance",B)}function tt(o){o()}function nt(o){V().autofill(o)}function fe(){Te()}typeof document<"u"&&!window.SaaSquatchDoNotAutoDrop&&fe();var de;(de=window.squatch)!=null&&de.init&&T("Squatchjs is being loaded more than once. This may lead to multiple load events being sent, duplicated widgets, and inaccurate analytics.");typeof document<"u"&&Fe();exports.DeclarativeEmbedWidget=ee;exports.DeclarativePopupWidget=te;exports.EmbedWidget=q;exports.PopupWidget=x;exports.WidgetApi=z;exports.Widgets=M;exports._auto=et;exports.api=Ye;exports.autofill=nt;exports.events=Ze;exports.help=Qe;exports.init=R;exports.pushCookie=fe;exports.ready=tt;exports.widget=Ke;exports.widgets=V;
|
|
51
|
+
`),h.close(),I("Popup template loaded into iframe"),await this._setupResizeHandler(s)}async _setupResizeHandler(e){const{contentWindow:n}=e;if(!n)throw new Error("Frame needs a content window");const i=n.document;$(i,async()=>{i.body.style.overflowY="hidden";let s=!0;new n.ResizeObserver(a=>{for(const c of a){const{top:d,bottom:h}=c.contentRect,u=h+d;u<=0||(s?(s=!1,e.style.height="0",e.height=i.body.scrollHeight+"",e.style.height=""):e.height=u+"",c.target.style="")}}).observe(await this._findInnerContainer(e))})}open(){const e=this.container?this._findElement():document.body,i=(e.shadowRoot||e).querySelector(`#${this.id}`);if(!i)throw new Error("Could not determine container div");i.showModal();const s=this._findFrame();if(!s)throw new Error("Could not find iframe");const{contentWindow:r}=s;if(!r)throw new Error("Squatch.js has an empty iframe");const a=r.document;$(a,()=>{var d;const c=r.squatch||r.widgetIdent;(d=s.contentDocument)==null||d.dispatchEvent(new CustomEvent("sq:refresh")),this.context.user?(this._loadEvent(c),I("Popup opened")):this._attachLoadEventListener(a,c)})}close(){const e=this._findFrame();e!=null&&e.contentDocument&&this._detachLoadEventListener(e.contentDocument);const n=this.container?this._findElement():document.body,s=(n.shadowRoot||n).querySelector(`#${this.id}`);if(!s)throw new Error("Could not determine container div");s.close(),I("Popup closed")}_getErrorStyle(){return"body { margin: 0; } .modal { box-shadow: none; border: 0; }"}}const E=C("squatch-js:widgets");class M{constructor(t){l(this,"api");l(this,"tenantAlias");l(this,"domain");l(this,"npmCdn");const e=P(t);this.tenantAlias=e.tenantAlias,this.domain=e.domain,this.npmCdn=e.npmCdn,this.api=new z(e)}async upsertUser(t){const n=he(t);try{const i=await this.api.upsertUser(n);return{widget:this._renderWidget(i,n,{type:"upsert",user:n.user,engagementMedium:t.engagementMedium,container:t.container,trigger:t.trigger,widgetConfig:{values:{brandingConfig:i==null?void 0:i.brandingConfig}}}),user:i.user}}catch(i){throw E(i),i.apiErrorCode&&this._renderErrorWidget(i,t.engagementMedium),new Error(i)}}async render(t){const n=ue(t);try{const i=await this.api.render(n);return{widget:this._renderWidget(i,n,{type:"passwordless",engagementMedium:n.engagementMedium,container:n.container,trigger:n.trigger,widgetConfig:i==null?void 0:i.widgetConfig}),user:i.user}}catch(i){throw i.apiErrorCode&&this._renderErrorWidget(i,n.engagementMedium),new Error(i)}}async autofill(t){const e=t;if(typeof e=="function"){try{const s=await this.api.squatchReferralCookie();e(s)}catch(s){throw E("Autofill error",s),new Error(s)}return}if(typeof e!="string")throw new Error("Autofill accepts a string or function");let n=document.querySelectorAll(e),i;if(n.length>0)i=n[0];else throw E("Element id/class or function missing"),new Error("Element id/class or function missing");try{const s=await this.api.squatchReferralCookie();i.value=s.codes[0]}catch(s){throw new Error(s)}}_renderWidget(t,e,n){var c;if(E("Rendering Widget..."),!t)throw new Error("Unable to get a response");let i,s=!!e.displayOnLoad;const r=t.jsOptions||{},a={content:t.template,type:e.widgetType||((c=r.widget)==null?void 0:c.defaultWidgetType),api:this.api,domain:this.domain,npmCdn:this.npmCdn,context:n};return r.widgetUrlMappings&&r.widgetUrlMappings.forEach(d=>{var h,u;M._matchesUrl(d.url)&&(d.widgetType!=="CONVERSION_WIDGET"||(u=(h=t.user)==null?void 0:h.referredBy)!=null&&u.code?(s=d.displayOnLoad,E(`Display ${d.widgetType} on ${d.url}`)):E(`Don't display ${d.widgetType} when no referral on widget rule match ${d.url}`))}),r.fuelTankAutofillUrls&&(E("We found a fuel tank autofill!"),r.fuelTankAutofillUrls.forEach(({url:d,formSelector:h})=>{var u,m,p;if(M._matchesUrl(d)&&(E("Fuel Tank URL matches"),(m=(u=t.user)==null?void 0:u.referredBy)!=null&&m.code)){const g=document.querySelector(h);g?g.value=((p=t.user.referredBy.referredReward)==null?void 0:p.fuelTankCode)||"":E(new Error(`Element with id/class ${h} was not found.`))}})),e.engagementMedium==="EMBED"?i=this._renderEmbedWidget(a):(i=this._renderPopupWidget(a),s&&i.open()),i}_renderPopupWidget(t){const e=new x(t,t.context.trigger);return e.load(),e}_renderEmbedWidget(t){const e=new q(t,t.context.container);return e.load(),e}_renderErrorWidget(t,e="POPUP"){const{apiErrorCode:n,rsCode:i,statusCode:s,message:r}=t;E(new Error(`${n} (${i}) ${r}`));const a={content:"error",rsCode:i,apiErrorCode:n,statusCode:s,errorMessage:r,api:this.api,domain:this.domain,npmCdn:this.npmCdn,type:"ERROR_WIDGET",context:{type:"error"}};let c;e==="EMBED"?(c=new q(a),c.load()):e==="POPUP"&&(c=new x(a),c.load())}static _matchesUrl(t){return!!window.location.href.match(new RegExp(t))}}class De{constructor(t){l(this,"tenantAlias");l(this,"domain");const n=P(t);this.tenantAlias=n.tenantAlias,this.domain=n.domain}track(t,e){const n=t,i=e,s=je(n),{jwt:r}=Ne(i),a=encodeURIComponent(this.tenantAlias),c=encodeURIComponent(s.userId),d=encodeURIComponent(s.accountId),h=`/api/v1/${a}/open/account/${d}/user/${c}/events`,u=this.domain+h;return Z(u,JSON.stringify(s),r)}}function je(o){if(!S(o))throw new Error("tracking parameter must be an object");if(!(o!=null&&o.accountId))throw new Error("accountId field is required");if(!(o!=null&&o.events))throw new Error("events field is required");if(!(o!=null&&o.userId))throw new Error("userId field is required");const t=o;if(!Array.isArray(t.events))throw new Error("'events' should be an array");return t}function Ne(o){if(!S(o))throw new Error("'options' should be an object");return o}function Fe(){var i;const o=window[X]?X:L,t=((i=window["_"+o])==null?void 0:i.ready)||[],e=window.impactOnReady||window.squatchOnReady,n=[...t,e].filter(s=>!!s);setTimeout(()=>{window[L]&&(window[X]=window[L],n.forEach(s=>s()),window[L]._auto(),window["_"+o]=void 0,delete window["_"+o])},0)}const D=C("squatch-js");function He(){var u;const o=window.location.search,e=new URLSearchParams(o).get("_saasquatchExtra")||"";if(!e){D("No _saasquatchExtra param");return}const n=P({tenantAlias:"UNKNOWN"});if(!n.domain){D("domain must be provided in config to use _saasquatchExtra");return}let i;try{i=JSON.parse(N(e))}catch{D("Unable to decode _saasquatchExtra config");return}function s(m){return m.replace(/^https?:\/\//,"")}const r=s(n.domain),a=Object.keys((i==null?void 0:i[r])||{})[0],c=(u=i==null?void 0:i[r])==null?void 0:u[a];if(!c){D("_saasquatchExtra did not have an expected structure");return}const{autoPopupWidgetType:d,...h}=c;return{widgetConfig:{widgetType:d,displayOnLoad:!0,...h},squatchConfig:{...n,tenantAlias:a}}}const Je=C("squatch-js:decodeUserJwt");function Be(o){var t;try{const e=o.split(".")[1];if(e===void 0)return null;const n=N(e);return(t=JSON.parse(n))==null?void 0:t.user}catch(e){return Je(e),null}}const j=C("squatch-js:DeclarativeWidget");class we extends HTMLElement{constructor(){super();l(this,"config");l(this,"token");l(this,"tenant");l(this,"widgetType");l(this,"locale");l(this,"widgetApi");l(this,"analyticsApi");l(this,"widgetInstance");l(this,"type");l(this,"container");l(this,"element");l(this,"loaded");l(this,"_setWidget",(e,n)=>{var s;const i={api:this.widgetApi,content:e.template,context:{type:n.type,user:n.user,container:this.container||void 0,engagementMedium:this.type,widgetConfig:e.widgetConfig},type:this.widgetType,domain:((s=this.config)==null?void 0:s.domain)||k,npmCdn:Y,container:this};if(this.type==="EMBED")return new q(i);{const r=this.firstChild?null:void 0;return new x(i,r)}});l(this,"setErrorWidget",e=>{var c;const n=e instanceof Error?e.message:e==null?void 0:e.message,i=e==null?void 0:e.apiErrorCode,s=e==null?void 0:e.rsCode,r=e==null?void 0:e.statusCode,a={api:this.widgetApi,content:"error",context:{type:"error",container:this.container||void 0},type:"ERROR_WIDGET",domain:((c=this.config)==null?void 0:c.domain)||k,npmCdn:Y,container:this,apiErrorCode:i,rsCode:s,statusCode:r,errorMessage:n};if(this.type==="EMBED")return new q(a);{const d=this.firstChild?null:void 0;return new x(a,d)}});l(this,"reload",this.renderWidget);l(this,"show",this.open);l(this,"hide",this.close);this.attachShadow({mode:"open"}).innerHTML="<style>:host { display: block; }</style><slot></slot>",this.config=K(),this.token=U(),this.tenant=window.squatchTenant,this.container=this}_setupApis(e){var n,i;if(!this.tenant)throw new Error("tenantAlias not provided");this.widgetApi=new z({tenantAlias:(e==null?void 0:e.tenantAlias)||this.tenant,domain:(e==null?void 0:e.domain)||((n=this.config)==null?void 0:n.domain)||k}),this.analyticsApi=new ge({domain:(e==null?void 0:e.domain)||((i=this.config)==null?void 0:i.domain)||k})}getWidgetType(e){return e&&e.includes("friendWidget")?"instant-access":"verified-access"}async renderPasswordlessVariant(){return this._setupApis(),j("Rendering as an Instant Access widget"),await this.widgetApi.render({engagementMedium:this.type,widgetType:this.widgetType,locale:this.locale}).then(e=>this._setWidget(e,{type:"passwordless"})).catch(this.setErrorWidget)}async renderUserUpsertVariant(){this._setupApis();const e=Be(this.token);if(!e)return this.setErrorWidget(Error("No user object in token."));!this.locale&&e.locale&&(this.locale=e.locale),j("Rendering as a Verified widget");try{await this.widgetApi.upsertUser({user:e,locale:this.locale,engagementMedium:this.type,widgetType:this.widgetType,jwt:this.token})}catch(i){return this.setErrorWidget(i)}return await this.widgetApi.render({locale:this.locale,engagementMedium:this.type,widgetType:this.widgetType}).then(i=>this._setWidget(i,{type:"upsert",user:e})).catch(this.setErrorWidget)}async getWidgetInstance(){let e;this.widgetType=this.getAttribute("widget")||void 0,this.locale=this.getAttribute("locale")||this.locale;const n=this.getWidgetType(this.widgetType);if(!this.widgetType)throw new Error("No widget has been specified");return!this.token&&n==="verified-access"&&j("[SquatchJS] Authentication token is required for this widget type."),this.token?e=await this.renderUserUpsertVariant():e=await this.renderPasswordlessVariant(),this.widgetInstance=e,this.widgetInstance&&this.dispatchEvent(new CustomEvent("sq:widget-loaded")),e}async renderWidget(){await this.getWidgetInstance(),await this.widgetInstance.load()}open(){if(!this.widgetInstance)throw new Error("Widget has not loaded yet");this.widgetInstance.open()}close(){if(!this.widgetInstance)throw new Error("Widget has not loaded yet");this.widgetInstance.close()}static get observedAttributes(){return["widget","locale"]}attributeChangedCallback(e,n,i){if(!(n===i||!this.loaded))switch(e){case"locale":case"widget":this.connectedCallback();break}}async connectedCallback(){this.loaded=!0,this.container=this.getAttribute("container"),this.widgetType=this.getAttribute("widget")||void 0;const e=this.getWidgetType(this.widgetType),{getSkeleton:n}=await Promise.resolve().then(()=>require("./SkeletonTemplate-B3Bk4NFu.cjs")),i=n({type:e}),s=document.createElement("div");s.id="loading-skeleton",s.innerHTML=i;const r=this.shadowRoot||this.attachShadow({mode:"open"});if(this.type==="POPUP"){const a=r.getElementById("squatchModal");a&&(a.innerHTML="",a.appendChild(s))}else r.innerHTML="",r.appendChild(s);try{await this.renderWidget()}catch(a){j("Failed to render widget",a)}finally{const a=r.getElementById("loading-skeleton");a&&a.remove()}this.getAttribute("open")!==null&&this.open()}}class ee extends we{constructor(){super(),this.type="EMBED",this.loaded=!1}}class te extends we{constructor(){super(),this.type="POPUP",this.loaded=!1,this.addEventListener("click",t=>{t.stopPropagation(),this.open()})}}class Ge extends ee{}class ze extends te{}class Ve extends ee{}class Xe extends te{}window.customElements.get("squatch-embed")||window.customElements.define("squatch-embed",Ge);window.customElements.get("impact-embed")||window.customElements.define("impact-embed",Ve);window.customElements.get("squatch-popup")||window.customElements.define("squatch-popup",ze);window.customElements.get("impact-popup")||window.customElements.define("impact-popup",Xe);function Qe(){console.log("Having trouble using Squatch.js? Go to https://docs.referralsaasquatch.com/developer/ for tutorials, references and error codes.")}const T=C("squatch-js");let H=null,J=null,B=null;function Ye(){return H||R({}),H}function V(){return J||R({}),J}function Ze(){return B||R({}),B}function Ke(o){var t;return(t=V())==null?void 0:t.render(o)}function et(){var t;const o=He();if(o){const{squatchConfig:e,widgetConfig:n}=o;return R(e),(t=V())==null?void 0:t.render(n)}}function R(o){const e=P(o);e.tenantAlias.match("^test")||e.debug?_e("squatch-js*"):ke(),T("initializing ..."),H=new z(e),J=new M(e),B=new De(e),T("Widget API instance",H),T("Widgets instance",J),T("Events API instance",B)}function tt(o){o()}function nt(o){V().autofill(o)}function fe(){Te()}typeof document<"u"&&!window.SaaSquatchDoNotAutoDrop&&fe();var de;(de=window.squatch)!=null&&de.init&&T("Squatchjs is being loaded more than once. This may lead to multiple load events being sent, duplicated widgets, and inaccurate analytics.");typeof document<"u"&&Fe();exports.DeclarativeEmbedWidget=ee;exports.DeclarativePopupWidget=te;exports.EmbedWidget=q;exports.PopupWidget=x;exports.WidgetApi=z;exports.Widgets=M;exports._auto=et;exports.api=Ye;exports.autofill=nt;exports.events=Ze;exports.help=Qe;exports.init=R;exports.pushCookie=fe;exports.ready=tt;exports.widget=Ke;exports.widgets=V;
|
|
52
52
|
//# sourceMappingURL=squatch.cjs.js.map
|