Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const configLocation = "./config.json";
let isIframeLoaded = false;
const sizeCollection = {
"desktop": {
"account-setup": { height: "160px", width: "360px" },
"timeline": {height: "600px", width: "400px"}
},
"mobile": {
"account-setup": { height: "100vh", width: "100vw" },
"timeline": {height: "100vh", width: "100vw"}
}
}
window.addEventListener("message", event => {
const iframeElement = document.querySelector(".chatterbox-iframe") as HTMLIFrameElement;
const { action } = event.data;
switch (action) {
case "resize-iframe":
const { view } = event.data;
const size = sizeCollection.desktop[view];
if (!size) { return; }
const { height, width } = size;
if (height) { iframeElement.style.height = height; }
if (width) { iframeElement.style.width = width; }
break;
case "minimize":
minimizeIframe();
break;
}
});
function renderStartButton() {
const container = document.createElement("div");
container.className = "start";
const button = document.createElement("button");
button.className = "StartChat";
button.onclick = () => isIframeLoaded? minimizeIframe() : loadChatterboxIframe();
container.appendChild(button);
document.body.appendChild(container);
}
function loadChatterboxIframe() {
const iframe = document.createElement("iframe");
iframe.src = `./chatterbox.html?config=${configLocation}`;
iframe.className = "chatterbox-iframe";
document.body.appendChild(iframe);
isIframeLoaded = true;
}
function minimizeIframe() {
const iframeElement = document.querySelector(".chatterbox-iframe") as HTMLIFrameElement;
if (iframeElement.style.display !== "none") {
iframeElement.style.display = "none";
}
else {
iframeElement.style.display = "block";
}
}
renderStartButton();