/
setupWebExample.js
144 lines (119 loc) · 3.4 KB
/
setupWebExample.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
/**
* initializes and attaches the chat window
*/
function chatWindowSetup(fetchUrl, suppliedUser) {
const wrapper = document.createElement('div');
const content = document.createElement('div');
const inputWrapper = document.createElement('div');
const inputButton = document.createElement('button');
const inputLabel = document.createElement('label');
const input = document.createElement('input');
const styles = document.createElement('style');
/**
* addCss
*/
function addCss() {
const css = `
.chat__hidden {
position: absolute;
left: 9999999px;
opacity: 0;
}
.chat__wrapper {
position: fixed;
bottom: 0;
right: 15px;
height: 400px;
width: 275px;
border: #ff8c00 solid 1px;
background-color: #fff;
}
.chat__content {
padding: 10px;
max-height: calc( 100% - 40px );
font-family: monospace;
font-size: 10px;
overflow-y: scroll;
}
.chat__val, .chat__user {
width: 30px;
display: inline-block;
color: #00a1e4;
}
.chat__user {
color: #00a100;
}
.chat__input-wrapper {
position: absolute;
bottom: 2px;
left: 2px;
width: calc( 100% - 4px );
}
.chat__input {
width: calc( 100% - 65px );
text-indent: 5px;
}
.chat__input-button {
width: 55px;
margin-left: 4px;
}`;
styles.innerHTML = css;
document.head.appendChild( styles );
}
function addResponse(user, res) {
const userClass = user === 'Val' ? 'val' : 'user';
const text = res.text ? res.text : res;
const response = document.createElement('div');
response.innerHTML = `<span class="chat__${userClass}">${user}:</span>
</span class="chat__response">${text}</span>`;
content.appendChild(response);
}
/**
* ## buildWindow
*/
function buildWindow() {
wrapper.classList.add('chat__wrapper');
content.classList.add('chat__content');
inputWrapper.classList.add('chat__input-wrapper');
inputButton.classList.add('chat__input-button');
inputLabel.classList.add('chat__hidden');
input.classList.add('chat__input');
inputLabel.textContent = 'How can I help?'
input.setAttribute('placeholder', 'How can I help?');
inputButton.textContent = 'Submit';
wrapper.appendChild(content);
inputWrapper.appendChild(inputLabel);
inputWrapper.appendChild(input);
inputWrapper.appendChild(inputButton);
wrapper.appendChild(inputWrapper);
document.body.appendChild(wrapper);
}
function bindEvents() {
input.addEventListener('keydown', checkEnter);
inputButton.addEventListener('click', submit);
}
function checkEnter(e) {
if (e.key === 'Enter') {
submit();
}
}
function submit(user = suppliedUser) {
const value = input.value;
if (value !== '') {
input.value = '';
addResponse(user, value);
fetch(`${fetchUrl}${value}`)
.then(r => r.json())
.then(data => {
if (data.status === '200' && data.text && data.text !== '') {
addResponse( 'Val', data.text );
}
});
}
}
buildWindow();
addResponse('Val', 'How can I help you today?');
addCss();
bindEvents();
input.focus();
}