forked from Ericsson/c3-web-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupportChat.html
More file actions
208 lines (191 loc) · 6.16 KB
/
supportChat.html
File metadata and controls
208 lines (191 loc) · 6.16 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!--
* Copyright (C) 2016 Ericsson AB. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
<title>Support Chat</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="../resources/main.css">
<link rel="icon" href="../resources/favicon.ico">
<script type="text/javascript" src="https://get.cct.ericsson.net/latest/cct.js"></script>
<script type="text/javascript" src="../exampleUtils.js"></script>
<style>
.chat-container {
max-width: 600px;
}
#chat-window {
height: 300px;
font-size: 1.0em;
background-color: rgba(255,255,255,0.9);
box-shadow: 1px 1px 1px 1px rgba(0,0,0,0.2);
overflow-y: scroll;
word-wrap: break-word;
position: relative;
padding: 10px;
}
#typing-indicator {
visibility: hidden;
bottom: 0px;
text-align: center;
padding: 5px 0;
width: 100%;
background-color: #ff3;
margin-bottom: 5px;
}
#message-preview {
float: left;
background-color: #ff3;
}
#chat-messages {
display: flex;
flex-flow: column nowrap;
}
.chat-message {
margin-top: 6px;
padding: 2px 5px;
border: 1px solid rgba(145,145,145,0.8);
-webkit-box-shadow: 2px 2px 0px 0px rgba(0,0,0,0.1);
-moz-box-shadow: 2px 2px 0px 0px rgba(0,0,0,0.1);
box-shadow: 2px 2px 0px 0px rgba(0,0,0,0.1);
border-radius: 4px;
}
.self-message {
align-self: flex-end;
background-color: #bdf;
}
.remote-message {
align-self: flex-start;
background-color: #ddd;
}
#chat-form > * {
font-size: 0.9em;
}
</style>
</head>
<body>
<header></header>
<main>
<section>
<div class="about">
<h2>Support Chat example</h2>
<p>
This example shows how to connect two <a href="https://get.cct.ericsson.net/latest/docs/reference/Client.html">clients</a> together through a <a href="https://get.cct.ericsson.net/latest/docs/reference/Room.html">room</a>. A random id for the room is generated and used to create a unique URL.
</p>
Once both have joined the room, a chat and a data channel is set up to send custumer input field live to support person.
</div>
<div class="peer-link">
<h3 class="peer-link-title"></h3>
<input class="peer-link-input" type="url" value="" size="55">
<div class="peer-link-connection-state"></div>
</div>
</section>
<div class="chat-container margins">
<h2 id="role-display"></h2>
<div id="chat-window">
<h3>Chat window</h3>
<div id="chat-messages"></div>
<div id="message-preview" class="chat-message" style="display: none;"></div>
</div>
<div id="typing-indicator">Other person is writing</div>
<form id="chat-form">
<input type="text" id="chat-message-input" placeholder="Type here">
<button type="submit">Send</button>
</form>
</div>
<footer></footer>
</main>
<script>
'use strict'
cct.log.setLogLevel('example', cct.log.ALL)
cct.log.color = true
var el = {
chatMessageInput: document.getElementById('chat-message-input'),
chatForm: document.getElementById('chat-form'),
chatWindow: document.getElementById('chat-window'),
chatMessages: document.getElementById('chat-messages'),
messagePreview: document.getElementById('message-preview'),
roleDisplay: document.getElementById('role-display'),
typingIndicator: document.getElementById('typing-indicator'),
}
var client = new cct.Client()
PeerConnecter.clientInCall(client).then(function (connecter) {
var room = connecter.room
var call = connecter.call
var isSupport = connecter.room.creator === client.user
if (isSupport) {
el.roleDisplay.textContent = 'Showing as: Support person'
} else {
el.roleDisplay.textContent = 'Showing as: Customer'
}
el.chatForm.addEventListener('submit', function (event) {
event.preventDefault()
if (el.chatMessageInput.value) {
room.send(el.chatMessageInput.value)
el.chatMessageInput.value = ''
updatePreviewData()
}
})
room.on('event:m.room.message', function (event) {
var scrollPosition = el.chatWindow.scrollTop + el.chatWindow.clientHeight
var wasAtBottom = scrollPosition === el.chatWindow.scrollHeight
var message = document.createElement('div')
message.classList.add('chat-message')
message.textContent = event.content.body
if (event.isOwnEvent) {
message.classList.add('self-message')
} else {
message.classList.add('remote-message')
}
el.chatMessages.appendChild(message)
if (wasAtBottom) {
el.chatWindow.scrollTop = el.chatWindow.scrollHeight
}
})
var dataShare = new cct.DataShare()
call.attach('data', dataShare)
el.chatForm.addEventListener('input', updatePreviewData)
dataShare.on('update:' + !isSupport, function (value) {
if (value) {
el.typingIndicator.style.visibility = 'visible'
} else {
el.typingIndicator.style.visibility = 'hidden'
}
if (isSupport) {
if (value) {
var wasAtBottom = el.chatWindow.scrollTop + 303 + el.chatWindow.scrollHeight
el.messagePreview.textContent = value
el.messagePreview.style.display = 'block'
if (wasAtBottom) {
el.chatWindow.scrollTop = el.chatWindow.scrollHeight
}
} else {
el.messagePreview.style.display = 'none'
}
}
})
function updatePreviewData() {
var value = el.chatMessageInput.value
if (isSupport) {
// don't send text to clients
value = !!value
}
dataShare.set('' + isSupport, value)
}
updatePreviewData()
})
</script>
</body>
</html>