forked from markwatsonatx/ibm-finish-line
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-http-get-template.html
executable file
·160 lines (157 loc) · 4.18 KB
/
chat-http-get-template.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<style>
html, body {
height: 100%;
padding: 0px;
margin: 0px;
overflow: hidden;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 14px;
color: #333;
}
hr {
margin-top: 20px;
margin-bottom: 20px;
border: 0;
border-top: 1px solid #ccc;
}
#app {
height: 100%;
}
#chat-container {
width: 100%;
max-width: 800px;
position: absolute;
bottom: 0px;
margin: 0 auto;
left: 0px;
right: 0px;
padding: 30px;
}
#chat-message-container {
overflow-y: scroll;
margin-bottom: 10px;
}
.chat-msg-body-bot {
background-color: #eee;
color: black;
border-radius: 5px;
margin: 10px 50px 10px 0px;
padding: 5px 10px 5px 10px;
}
.chat-msg-body-user {
background-color: #34aadc;
color: white;
border-radius: 5px;
margin: 10px 0px 10px 50px;
padding: 5px 10px 5px 10px;
}
#chat-form-input {
width: calc(100% - 140px);
padding: 6px 12px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
}
#chat-form-submit {
width: 100px;
padding: 6px 12px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<div id="chat-container">
<div id="chat-message-container">
<div v-for="msg in messages" v-bind:class="[msg.isBot ? 'chat-msg-body-bot' : 'chat-msg-body-user']">
<b><span v-html="msg.user"></span></b>:
<div v-html="msg.msg"></div>
</div>
</div>
<hr />
<div id="chat-form-container">
<input id="chat-form-input" type="text" autocomplete="off" placeholder="Enter your message..." v-model="message" v-on:keyup.13="submitMessage">
<button id="chat-form-submit" type="submit" v-on:click="submitMessage">Send</button>
</div>
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
botName: 'bot',
userId: null,
message: '',
messages: []
},
methods: {
submitMessage: function () {
if (app.message.length == 0) {
return;
}
app.addMessage({
isBot: false,
user: 'Me',
ts: new Date(),
msg: app.message
});
var xhr = new XMLHttpRequest();
xhr.open('POST', '/chat', true);
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.onreadystatechange = function() {
if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
app.addMessage({
isBot: true,
user: app.botName,
ts: new Date(),
msg: xhr.responseText
});
}
}
xhr.send(app.message);
app.message = '';
},
addMessage(message) {
app.messages.push(message);
setTimeout(function () {
app.scrollMessagesToBottom()
}, 100);
},
scrollMessagesToBottom() {
setTimeout(function () {
app.resizeMessageContainer();
var e = document.getElementById("chat-message-container");
e.scrollTop = e.scrollHeight;
}, 100);
},
resizeMessageContainer() {
var e = document.getElementById("chat-message-container");
var rect = e.getBoundingClientRect();
if (rect.top <= 20) {
e.style.maxHeight = rect.height + rect.top + 'px';
}
else {
e.style.maxHeight = null;
}
},
init() {
// register with window onresize event
window.onresize = function () {
app.resizeMessageContainer();
}
}
}
});
(function () {
// Initialize vue app
app.init();
})();
</script>
</body>
</html>