-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
160 lines (138 loc) · 4.84 KB
/
main.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { CLOSE_ICON, MESSAGE_ICON, styles } from "./assets.js";
class MessageWidget {
constructor(position = "bottom-right") {
this.position = this.getPosition(position);
this.open = false;
this.initialize();
this.injectStyles();
}
position = "";
open = false;
widgetContainer = null;
getPosition(position) {
const [vertical, horizontal] = position.split("-");
return {
[vertical]: "30px",
[horizontal]: "30px",
};
}
async initialize() {
/**
* Create and append a div element to the document body
*/
const container = document.createElement("div");
container.style.position = "fixed";
Object.keys(this.position).forEach(
(key) => (container.style[key] = this.position[key])
);
document.body.appendChild(container);
/**
* Create a button element and give it a class of button__container
*/
const buttonContainer = document.createElement("button");
buttonContainer.classList.add("button__container");
/**
* Create a span element for the widget icon, give it a class of 'widget__icon', update it's innerHTML property to an icon which would serve as the widget icon.
*/
const widgetIconElement = document.createElement("span");
widgetIconElement.innerHTML = MESSAGE_ICON;
widgetIconElement.classList.add("widget__icon");
this.widgetIcon = widgetIconElement;
/**
* Create a span element for the close icon, give it a class of 'widget__icon' and 'widget__hidden' which would be removed whenever the widget is closed, update it's innerHTML property to an icon which would serve as the widget icon during that state.
*/
const closeIconElement = document.createElement("span");
closeIconElement.innerHTML = CLOSE_ICON;
closeIconElement.classList.add("widget__icon", "widget__hidden");
this.closeIcon = closeIconElement;
/**
* Append both icons created to the button element and add a `click` event listener on the button to toggle the widget open and close.
*/
buttonContainer.appendChild(this.widgetIcon);
buttonContainer.appendChild(this.closeIcon);
buttonContainer.addEventListener("click", this.toggleOpen.bind(this));
/**
* Create a container for the widget and add the following classes:- "widget__hidden", "widget__container"
*/
this.widgetContainer = document.createElement("div");
this.widgetContainer.classList.add("widget__hidden", "widget__container");
/**
* Invoke the `createWidget()` method
*/
this.createWidgetContent();
/**
* Append the widget's content and the button to the container
*/
container.appendChild(this.widgetContainer);
container.appendChild(buttonContainer);
}
createWidgetContent() {
this.widgetContainer.innerHTML = `
<header class="widget__header">
<h3>Start a conversation</h3>
<p>We usually respond within a few hours</p>
</header>
<form>
<div class="form__field">
<label for="name">Name</label>
<input
type="text"
id="name"
name="name"
placeholder="Enter your name"
/>
</div>
<div class="form__field">
<label for="email">Email</label>
<input
type="email"
id="email"
name="email"
placeholder="Enter your email"
/>
</div>
<div class="form__field">
<label for="subject">Subject</label>
<input
type="text"
id="subject"
name="subject"
placeholder="Enter Message Subject"
/>
</div>
<div class="form__field">
<label for="message">Message</label>
<textarea
id="message"
name="message"
placeholder="Enter your message"
rows="6"
></textarea>
</div>
<button>Send Message</button>
</form>
`;
}
injectStyles() {
const styleTag = document.createElement("style");
styleTag.innerHTML = styles.replace(/^\s+|\n/gm, "");
document.head.appendChild(styleTag);
}
toggleOpen() {
this.open = !this.open;
if (this.open) {
this.widgetIcon.classList.add("widget__hidden");
this.closeIcon.classList.remove("widget__hidden");
this.widgetContainer.classList.remove("widget__hidden");
} else {
this.createWidgetContent();
this.widgetIcon.classList.remove("widget__hidden");
this.closeIcon.classList.add("widget__hidden");
this.widgetContainer.classList.add("widget__hidden");
}
}
}
function initializeWidget() {
return new MessageWidget();
}
initializeWidget();