-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.js
More file actions
93 lines (81 loc) · 2.59 KB
/
widget.js
File metadata and controls
93 lines (81 loc) · 2.59 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const iframeId = "callWidgetIFrame";
const overlayId = "callWidgetOverlay";
const iframeUrl = "http://localhost:3000";
class CallWidget
{
isOpen = true;
title = "Title";
buttonText = "Call Us";
/**
* Initializes the call widget by adding an IFrame element to the body
* of the document. An `init` message is sent once the IFrame is loaded.
*
* @param {string} title The title of the widget.
* @param {string} buttonText The text of the call button.
*/
init(title, buttonText)
{
this.title = title;
this.buttonText = buttonText;
var iframe = document.createElement("iframe");
iframe.id = iframeId;
iframe.src = iframeUrl;
iframe.style.border = "None";
iframe.style.width = "300px";
iframe.style.height = "300px";
iframe.style.position = "fixed";
iframe.style.top = "50%";
iframe.style.left = "50%";
iframe.style.transform = "translate(-50%, -50%)";
var overlay = document.createElement("div");
overlay.id = overlayId;
overlay.style.position = "absolute";
overlay.style.top = "0";
overlay.style.left = "0";
overlay.style.right = "0";
overlay.style.bottom = "0";
overlay.style.height = "100%";
overlay.style.width = "100%";
overlay.style.backgroundColor = "rgba(0, 0, 0, .5)";
overlay.onclick = () => this.closeCallWidget();
overlay.appendChild(iframe);
document.body.appendChild(overlay);
window.addEventListener("message", this.onMessage);
}
onMessage(e)
{
if(e.origin !== iframeUrl) return;
if(e.data == "done")
{
let iframe = document.getElementById(iframeId);
iframe.contentWindow.postMessage({
"message": "init",
"contents": {
"title": this.title,
"buttonText": this.buttonText
}
}, "*");
return;
}
else if(e.data["message"] == "openState")
{
this.isOpen = e.data["contents"];
if(!this.isOpen)
{
var overlay = document.getElementById(overlayId);
overlay.style.display = "none";
}
}
}
openCallWidget()
{
var overlay = document.getElementById(overlayId);
overlay.style.display = "block";
}
closeCallWidget()
{
var overlay = document.getElementById(overlayId);
overlay.style.display = "none";
}
}
onCallWidgetReady(new CallWidget())