-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
207 lines (186 loc) · 5.61 KB
/
index.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
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
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Live chat</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/normalize.css">
<link rel="stylesheet" type="text/css" href="image-picker.css">
<script src="js/prefixfree.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script src="/socketHandler.js"></script>
<script src="/htmlMsgs.js"></script>
<script src="/image-picker.js"></script>
</head>
<script>
function initialize() {
const sidebarBox = document.querySelector('#box'),
sidebarBtn = document.querySelector('#btn'),
pageWrapper = document.querySelector('#page-wrapper');
sidebarBtn.addEventListener('click', event => {
sidebarBtn.classList.toggle('active');
sidebarBox.classList.toggle('active');
});
window.addEventListener('keydown', event => {
if (sidebarBox.classList.contains('active') && event.keyCode === 27) {
sidebarBtn.classList.remove('active');
sidebarBox.classList.remove('active');
}
});
}
var person = {};
function login() {
document.getElementById('nickname-input').focus();
var modal = document.getElementById('myModal');
document.getElementById('login-form').addEventListener('submit', function(event){
event.preventDefault();
person.nickname = document.getElementById('nickname-input').value;
document.getElementById('nickname-display').innerHTML = person.nickname;
modal.style.display = "none";
start();
});
}
function start() {
document.getElementById('m').focus();
var msgDiv = document.getElementById('chat');
msgDiv.scrollTop = msgDiv.scrollHeight;
$(function() {
socket = io();
socketHandler = handler(socket);
var typingTimer;
var doneTypingInterval = 2000;
window.onbeforeunload = function () {
socket.emit('user-disconnect', person);
};
socket.on('connect', function() {
socketHandler.onConnect(person);
});
socket.on('new-msg', function(p, msg) {
var msgHtml;
if (p.nickname == person.nickname)
msgHtml = htmlNewMsgUser(p, msg);
else
msgHtml = htmlNewMsgOther(p, msg);
$('#messages').append(msgHtml);
msgDiv.scrollTop = msgDiv.scrollHeight;
})
socket.on('new-person', function(person) {
var msgHtml = htmlNewPerson(person);
$('#messages').append(msgHtml);
});
socket.on('online', function(online) {
$('#online-table-body tr').remove();
for (let e of online) {
let markup = '<tr><td>' + e +
'</td></tr>';
$('#online-table-body').append(markup);
}
});
socket.on('is-typing', function(typers) {
var typersStr = parseTypers(typers);
document.getElementById('typers').innerHTML = typersStr;
});
socket.on('disconnect', function(reason) {
socketHandler.disconnect(person);
})
document.getElementById('m').addEventListener('keypress', function(event) {
if (event.keyCode == 13) {
event.preventDefault();
socketHandler.newMsg(person, $('#m').val());
this.value="";
socketHandler.stopTyping(person);
} else {
socketHandler.isTyping(person);
clearTimeout(typingTimer);
typingTimer = setTimeout(() => {
socketHandler.stopTyping(person);
}, doneTypingInterval);
}
});
document.getElementById("m").addEventListener("keydown", function() {
clearTimeout(typingTimer);
}, false);
});
}
function parseTypers(typers) {
if (typers.length == 0)
return "";
else if (typers.length == 1)
return typers[0] + " is typing...";
else {
return typers.toString() + " are typing...";
}
}
</script>
<body onload="login(); initialize();">
<div id="myModal" class="modal">
<div class="modal-content">
<div class="form" id="form">
<form class="login-form" id="login-form" action="">
<p class="message">Type your nickname please:</p>
<br>
<input type="text" placeholder="nickname" id="nickname-input" autocomplete="off" />
<button>Enter</button>
</form>
</div>
</div>
</div>
<div id="btn">
<div id='top'></div>
<div id='middle'></div>
<div id='bottom'></div>
</div>
<div id="box">
<div id="items">
<div class="item" style="text-align: center;">
Welcome <span id="nickname-display"></span>.
</div>
<div id="online">
<table id="online-table">
<thead>
<tr>
<th>Online</th>
</tr>
</thead>
<tbody id="online-table-body">
<tr>
<td>sdfsdf</td>
</tr>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<section>
<div class="chat" id="chat">
<ul id = "messages">
<li class="you">
<a class="user" href="#"><img alt="" src="https://s3.amazonaws.com/uifaces/faces/twitter/igorgarybaldi/128.jpg" /></a>
<div class="date">
Live chat
</div>
<div class="message">
<div class="hider">
<span>Click to read</span>
</div>
<p>
Welcome to live chat.
</p>
</div>
</li>
</ul>
</div>
<span id="typers"></span>
<div id="inputMsg">
<form action="">
<input class="textarea" id="m" type="text"
placeholder="Type here!" autocomplete="off" />
</form>
</div>
</section>
</body>
</html>