|
| 1 | +import Gleap, { GleapConfigManager, GleapFrameManager } from "./Gleap"; |
| 2 | + |
| 3 | +export default class GleapModalManager { |
| 4 | + modalUrl = "https://outboundmedia.gleap.io/modal"; |
| 5 | + modalContainer = null; |
| 6 | + modalData = null; |
| 7 | + modalBackdropClickListener = null; |
| 8 | + |
| 9 | + // singleton |
| 10 | + static instance; |
| 11 | + static getInstance() { |
| 12 | + if (!this.instance) { |
| 13 | + this.instance = new GleapModalManager(); |
| 14 | + } |
| 15 | + return this.instance; |
| 16 | + } |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this._listenForMessages(); |
| 20 | + } |
| 21 | + |
| 22 | + setModalUrl(url) { |
| 23 | + this.modalUrl = url; |
| 24 | + } |
| 25 | + |
| 26 | + _listenForMessages() { |
| 27 | + window.addEventListener("message", (event) => { |
| 28 | + if (!this.modalUrl?.includes(event.origin)) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + const data = JSON.parse(event.data); |
| 34 | + |
| 35 | + if (data.name === "modal-loaded" && this.modalData) { |
| 36 | + const flowConfig = GleapConfigManager.getInstance().getFlowConfig(); |
| 37 | + const primaryColor = flowConfig.color ? flowConfig.color : "#485BFF"; |
| 38 | + |
| 39 | + this._postMessage({ |
| 40 | + name: "modal-data", |
| 41 | + data: { |
| 42 | + ...this.modalData, |
| 43 | + primaryColor: primaryColor, |
| 44 | + }, |
| 45 | + }); |
| 46 | + } |
| 47 | + if (data.name === "modal-height") { |
| 48 | + const height = data?.data?.height; |
| 49 | + if (height) { |
| 50 | + // Set the height of the modal iframe |
| 51 | + const iframe = |
| 52 | + this.modalContainer.querySelector(".gleap-modal-frame"); |
| 53 | + if (iframe) { |
| 54 | + iframe.style.height = `${height}px`; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + if (data.name === "modal-data-set") { |
| 59 | + // TODO: Implement |
| 60 | + } |
| 61 | + if (data.name === "modal-close") { |
| 62 | + this.hideModal(); |
| 63 | + } |
| 64 | + if (data.name === "start-conversation") { |
| 65 | + Gleap.startBot(data.data?.botId); |
| 66 | + } |
| 67 | + if (data.name === "start-custom-action") { |
| 68 | + Gleap.triggerCustomAction(data.data?.action); |
| 69 | + } |
| 70 | + if (data.name === "open-url") { |
| 71 | + const url = data.data; |
| 72 | + const newTab = data.newTab ? true : false; |
| 73 | + GleapFrameManager.getInstance().urlHandler(url, newTab); |
| 74 | + } |
| 75 | + if (data.name === "show-form") { |
| 76 | + Gleap.startFeedbackFlow(data.data?.formId); |
| 77 | + } |
| 78 | + if (data.name === "show-survey") { |
| 79 | + Gleap.showSurvey(data.data?.formId, data.data?.surveyFormat); |
| 80 | + } |
| 81 | + if (data.name === "show-news-article") { |
| 82 | + Gleap.openNewsArticle(data.data?.articleId); |
| 83 | + } |
| 84 | + if (data.name === "show-help-article") { |
| 85 | + Gleap.openHelpCenterArticle(data.data?.articleId); |
| 86 | + } |
| 87 | + if (data.name === "show-checklist") { |
| 88 | + Gleap.startChecklist( |
| 89 | + data.data?.checklistId, |
| 90 | + true, |
| 91 | + data.data?.sharedKey |
| 92 | + ); |
| 93 | + } |
| 94 | + } catch (exp) {} |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + _injectModalUI(modalData) { |
| 99 | + if (this.modalContainer || !document.body) return false; |
| 100 | + |
| 101 | + console.log("modalData", modalData); |
| 102 | + |
| 103 | + this.modalData = modalData; |
| 104 | + |
| 105 | + const wrapper = document.createElement("div"); |
| 106 | + wrapper.className = "gleap-modal-wrapper"; |
| 107 | + wrapper.innerHTML = ` |
| 108 | + <div class="gleap-modal-backdrop"></div> |
| 109 | + <div class="gleap-modal"> |
| 110 | + <iframe |
| 111 | + src="${this.modalUrl}" |
| 112 | + class="gleap-modal-frame" |
| 113 | + scrolling="no" |
| 114 | + title="Gleap Modal" |
| 115 | + role="dialog" |
| 116 | + frameborder="0" |
| 117 | + allow="autoplay; encrypted-media; fullscreen; microphone *;" |
| 118 | + ></iframe> |
| 119 | + </div> |
| 120 | + `; |
| 121 | + document.body.appendChild(wrapper); |
| 122 | + this.modalContainer = wrapper; |
| 123 | + |
| 124 | + // Add on backdrop click listener |
| 125 | + this.modalBackdropClickListener = this.modalContainer |
| 126 | + .querySelector(".gleap-modal-backdrop") |
| 127 | + .addEventListener("click", () => { |
| 128 | + if (this.modalData?.showCloseButton ?? true) { |
| 129 | + this.hideModal(); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + // lock background scroll |
| 134 | + document.body.classList.add("gleap-modal-open"); |
| 135 | + } |
| 136 | + |
| 137 | + _postMessage(message) { |
| 138 | + try { |
| 139 | + const frame = this.modalContainer.querySelector(".gleap-modal-frame"); |
| 140 | + if (frame?.contentWindow) { |
| 141 | + frame.contentWindow.postMessage( |
| 142 | + JSON.stringify({ ...message, type: "modal" }), |
| 143 | + this.modalUrl |
| 144 | + ); |
| 145 | + } |
| 146 | + } catch (err) {} |
| 147 | + } |
| 148 | + |
| 149 | + showModal(modalData) { |
| 150 | + if (modalData && modalData.config) { |
| 151 | + this._injectModalUI(modalData.config); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + hideModal() { |
| 156 | + if (!this.modalContainer) return; |
| 157 | + |
| 158 | + if (this.modalBackdropClickListener) { |
| 159 | + this.modalContainer |
| 160 | + .querySelector(".gleap-modal-backdrop") |
| 161 | + .removeEventListener("click", this.modalBackdropClickListener); |
| 162 | + } |
| 163 | + |
| 164 | + document.body.removeChild(this.modalContainer); |
| 165 | + this.modalContainer = null; |
| 166 | + document.body.classList.remove("gleap-modal-open"); |
| 167 | + } |
| 168 | +} |
0 commit comments