Skip to content

Commit 19a9df3

Browse files
committed
v14.6.0
1 parent 1e653da commit 19a9df3

File tree

11 files changed

+274
-7
lines changed

11 files changed

+274
-7
lines changed

build/cjs/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/esm/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export namespace Gleap {
5050
function setWSApiUrl(wsApiUrl: string): void;
5151
function setFrameUrl(frameUrl: string): void;
5252
function closeBanner(): void;
53+
function closeModal(): void;
5354
function setBannerUrl(bannerUrl: string): void;
55+
function setModalUrl(modalUrl: string): void;
5456
function setMaxNetworkRequests(maxRequests: number): void;
5557
function startNetworkLogger(): void;
5658
function setNetworkLogsBlacklist(networkLogBlacklist: string[]): void;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gleap",
3-
"version": "14.5.7",
3+
"version": "14.6.0",
44
"main": "build/cjs/index.js",
55
"module": "build/esm/index.mjs",
66
"exports": {

published/14.6.0/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

published/latest/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Gleap.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import GleapShortcutListener from "./GleapShortcutListener";
2424
import GleapPreFillManager from "./GleapPreFillManager";
2525
import GleapNotificationManager from "./GleapNotificationManager";
2626
import GleapBannerManager from "./GleapBannerManager";
27+
import GleapModalManager from "./GleapModalManager";
2728
import GleapAudioManager from "./GleapAudioManager";
2829
import GleapTagManager from "./GleapTagManager";
2930
import GleapAdminManager from "./GleapAdminManager";
@@ -307,6 +308,13 @@ class Gleap {
307308
GleapBannerManager.getInstance().removeBannerUI();
308309
}
309310

311+
/**
312+
* Close any open modal.
313+
*/
314+
static closeModal() {
315+
GleapModalManager.getInstance().hideModal();
316+
}
317+
310318
/**
311319
* Enable or disable Gleap session tracking through cookies.
312320
* @param {*} useCookies
@@ -535,6 +543,14 @@ class Gleap {
535543
GleapBannerManager.getInstance().setBannerUrl(bannerUrl);
536544
}
537545

546+
/**
547+
* Set a custom modal url.
548+
* @param {string} modalUrl
549+
*/
550+
static setModalUrl(modalUrl) {
551+
GleapModalManager.getInstance().setModalUrl(modalUrl);
552+
}
553+
538554
/**
539555
* Set a custom frame api url.
540556
* @param {string} frameUrl
@@ -1182,6 +1198,8 @@ class Gleap {
11821198
}
11831199
} else if (action.actionType === "banner") {
11841200
Gleap.showBanner(action);
1201+
} else if (action.actionType === "modal") {
1202+
Gleap.showModal(action);
11851203
} else if (action.actionType === "tour") {
11861204
Gleap.startProductTourWithConfig(action.outbound, action.data);
11871205
} else {
@@ -1222,6 +1240,12 @@ class Gleap {
12221240
} catch (e) {}
12231241
}
12241242

1243+
static showModal(data) {
1244+
try {
1245+
GleapModalManager.getInstance().showModal(data);
1246+
} catch (e) {}
1247+
}
1248+
12251249
static showNotification(data) {
12261250
GleapNotificationManager.getInstance().showNotification(data);
12271251
}
@@ -1319,6 +1343,7 @@ export {
13191343
GleapAudioManager,
13201344
GleapNotificationManager,
13211345
GleapBannerManager,
1346+
GleapModalManager,
13221347
GleapPreFillManager,
13231348
GleapShortcutListener,
13241349
GleapMarkerManager,

src/GleapBannerManager.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Gleap from "./Gleap";
1+
import Gleap, { GleapFrameManager } from "./Gleap";
22

33
export default class GleapBannerManager {
44
bannerUrl = "https://outboundmedia.gleap.io";
@@ -25,7 +25,7 @@ export default class GleapBannerManager {
2525
startCommunication() {
2626
// Add window message listener.
2727
window.addEventListener("message", (event) => {
28-
if (event.origin !== this.bannerUrl) {
28+
if (!this.bannerUrl?.includes(event.origin)) {
2929
return;
3030
}
3131

@@ -59,6 +59,11 @@ export default class GleapBannerManager {
5959
if (data.name === "start-custom-action") {
6060
Gleap.triggerCustomAction(data.data?.action);
6161
}
62+
if (data.name === "open-url") {
63+
const url = data.data;
64+
const newTab = data.newTab ? true : false;
65+
GleapFrameManager.getInstance().urlHandler(url, newTab);
66+
}
6267
if (data.name === "show-form") {
6368
Gleap.startFeedbackFlow(data.data?.formId);
6469
}
@@ -72,7 +77,11 @@ export default class GleapBannerManager {
7277
Gleap.openHelpCenterArticle(data.data?.articleId);
7378
}
7479
if (data.name === "show-checklist") {
75-
Gleap.startChecklist(data.data?.checklistId, true, data.data?.sharedKey);
80+
Gleap.startChecklist(
81+
data.data?.checklistId,
82+
true,
83+
data.data?.sharedKey
84+
);
7685
}
7786
} catch (exp) {}
7887
});

src/GleapModalManager.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
}

src/GleapSession.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
GleapBannerManager,
44
GleapEventManager,
55
GleapFrameManager,
6+
GleapModalManager,
67
GleapNotificationManager,
78
GleapStreamedEvent,
89
GleapTranslationManager,
@@ -150,6 +151,7 @@ export default class GleapSession {
150151
GleapNotificationManager.getInstance().clearAllNotifications(false);
151152
GleapNotificationManager.getInstance().setNotificationCount(0);
152153
GleapBannerManager.getInstance().removeBannerUI();
154+
GleapModalManager.getInstance().hideModal();
153155

154156
if (retry) {
155157
if (!isNaN(attemp)) {

0 commit comments

Comments
 (0)