@debox-pro/chat-widget-html
Version:
Use `DeBoxChatWidget` with `HTML`
3 lines (2 loc) • 2.94 kB
JavaScript
var l=Object.defineProperty,s=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,c=Object.prototype.propertyIsEnumerable,a=(o,t,e)=>t in o?l(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e,r=(o,t)=>{for(var e in t||(t={}))h.call(t,e)&&a(o,e,t[e]);if(s)for(var e of s(t))c.call(t,e)&&a(o,e,t[e]);return o};class n{constructor(){this.tag=null,this.config={zIndex:"999",projectId:"",conversationId:"",containerDomId:void 0,defaultOpen:!1,destroyOnClose:!1},this.onClose=t=>{var e,i;(i=(e=this.config).onClose)==null||i.call(e),this.config.destroyOnClose&&(t.preventDefault(),t.stopPropagation(),this.destroy())}}static getInstance(){return n.instance||(n.instance=new n),n.instance}initMain(t){this.config=r(r({},this.config),t),this.tag=document.createElement("debox-chat-widget"),this.tag.style.zIndex=this.config.zIndex,this.tag.projectId=this.config.projectId,this.tag.conversationId=this.config.conversationId,this.tag.setAttribute("defaultOpen",`${this.config.defaultOpen}`);let e=document.body;if(this.config.containerDomId){const i=document.getElementById(this.config.containerDomId);i&&(e=i)}e.appendChild(this.tag),this.addCloseEvent(),this.addToggleEvent()}async init(t){if(this.tag)throw new Error("DeBoxChatWidget is already initialized");this.initMain(t),typeof window<"u"&&await import("@debox-pro/chat-widget-ui")}destroy(){var t,e;this.tag&&(this.removeToggleEvent(),this.removeCloseEvent(),this.callbackFn&&this.removeEventListener(this.callbackFn),this.tag.remove(),this.tag=null),(e=(t=this.config).onDestroy)==null||e.call(t),this.config={zIndex:"999",projectId:"",conversationId:"",containerDomId:void 0,defaultOpen:!1,destroyOnClose:!1}}setConversation(t,e){if(!this.tag)throw new Error("DeBoxChatWidget is not initialized");this.config.conversationId=t,this.tag.conversationId=t,typeof e=="boolean"&&this.tag.toggleChat(e)}toggleChat(t){if(!this.tag)throw new Error("DeBoxChatWidget is not initialized");this.tag.toggleChat(t)}addEventListener(t){if(!this.tag)throw new Error("DeBoxChatWidget is not initialized");this.tag.addEventListener("debox-message-pushed",t),this.callbackFn=t}removeEventListener(t){if(!this.tag)throw new Error("DeBoxChatWidget is not initialized");this.tag.removeEventListener("debox-message-pushed",t),this.callbackFn=void 0}addToggleEvent(){const t=document.querySelector("debox-chat-widget");t&&this.config.onToggle&&(this.observer=new MutationObserver(()=>{var e,i;const d=t.hasAttribute("open");(i=(e=this.config).onToggle)==null||i.call(e,d)}),this.observer.observe(t,{attributes:!0,attributeFilter:["open"]}))}removeToggleEvent(){var t;(t=this.observer)==null||t.disconnect()}addCloseEvent(){var t;(t=this.tag)==null||t.addEventListener("debox-before-close",this.onClose)}removeCloseEvent(){var t;(t=this.tag)==null||t.removeEventListener("debox-before-close",this.onClose)}}const g=n.getInstance();export{g as DeBoxChatWidget};
//# sourceMappingURL=index.js.map