@residently/embed-sdk
Version:
Embed Residently listing collections and detail views within your website
4 lines (3 loc) • 1.34 kB
JavaScript
/* Residently SDK v0.0.1-rc.0 */
var r="0.0.1-rc.0";var o=`${r}`;var d=class l{iframe;events={};static version=o;constructor(){console.log(`Residently SDK version: ${l.version}`)}embedView(i){let{container:e,view:s,collectionToken:n,listingId:t}=i;if(s==="collection"&&!n)throw new Error("A collectionToken is required for the 'collection' view.");if(s==="listing"&&!t)throw new Error("A listingId is required for the 'listing' view.");let a=this.buildIframeUrl(s,n,t);this.iframe=document.createElement("iframe"),this.iframe.src=a,this.iframe.style.width="100%",this.iframe.style.height="100%",this.iframe.style.border="none",e.innerHTML="",e.appendChild(this.iframe),window.addEventListener("message",c=>this.handleMessage(c))}on(i,e){return(this.events[i]||=[]).push(e),()=>{this.events[i]=this.events[i]?.filter(s=>e!==s)}}emit(i,e){(this.events[i]||[]).forEach(n=>n(e))}buildIframeUrl(i,e,s){let n="https://api.residently.com/embed",t=new URLSearchParams;return t.set("view",i),e&&t.set("collection_token",e),s&&t.set("listing_id",s),`${n}?${t.toString()}`}handleMessage(i){if(i.origin!=="https://api.residently.com"){console.warn("Untrusted message origin:",i.origin);return}let e=i.data;e?.type==="listingClicked"&&e?.listingId&&this.emit("listingClicked",e.listingId)}};export{d as Residently};
//# sourceMappingURL=index.js.map