-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.html
230 lines (190 loc) · 7.79 KB
/
interface.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!DOCTYPE html>
<html>
<head>
<title>Chat room</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/main.css">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript">
angular.module("smApp", [])
.controller("defCtrl", function($scope) {
$scope.addNewRoom = function(roomDetais) {
$scope.room = roomDetais.room;
}
});
</script>
</head>
<body>
<div ng-app="smApp" ng-controller="defCtrl">
<div class="container">
<div class="row">
<form class="form-inline">
<input type="text" class="username" placeholder="User" disabled>
<div class="username-in"></div>
</form>
<div class="row">
<div class="col-md-4">
<input type="text" class="chat-rooms" placeholder="Room names:" disabled>
<div class="chat-room" id="chat-room"></div>
<form class="form-inline" name="roomsForm" novalidate ng-submit="addNewRoom(iRoomName)">
<div class="form-group">
<!-- AngularJS validation -->
<input name="iRoomName" type="text" class="form-control" id="newRoomName" required ng-model="iRoomName.room" placeholder="New room name">
<div class="emproomName" ng-show="roomsForm.iRoomName.$invalid && roomsForm.iRoomName.$dirty">
<span ng-show="roomsForm.iRoomName.$error.required">Room name required</span>
</div>
<button type="button" class="btn btn-primary" ng-disabled="roomsForm.$invalid" onclick="addNewRoom()">Create</button>
<button type="button" class="btn btn-danger" ng-disabled="roomsForm.$invalid" onclick="dropRoom()">Drop</button>
</div>
</form>
</div>
<div class="col-md-4">
<div class="chat">
<input type="text" class="chat-name" placeholder="System and chat messages:" disabled>
<div class="chat-system-messages"></div>
<div class="chat-messages" id="chat-messages"></div>
<textarea id="textarea" placeholder="Type your message"></textarea>
<div class="chat-status">Status:<span>Idle</span></div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script src='http://localhost:8085/socket.io/socket.io.js'></script>
<script>
//Global socket
try {
var socket = io.connect('http://localhost:8085');
}catch(e) {
//Set status to warn user
}
//Create new room on the page
function addNewRoom(){
if (socket !== undefined) {
var room = document.getElementById('newRoomName');
socket.emit('addnewRoom', {currRoom: 'Main', username:$("#username-in-chat").text(), room:room.value});
}
};
//Send to server change room
function switchRoom(room) {
if (socket !== undefined) {
socket.emit('switchRoom', {username:$("#username-in-chat").text(), room:room});
}
};
function dropRoom() {
};
(function() {
var getNode = function(s) {
return document.querySelector(s);
},
//Get required nodes
status = getNode('.chat-status span'),
messages = getNode ('.chat-messages'),
sysmessages = getNode('.chat-system-messages'),
textarea = getNode('.chat textarea'),
chatRooms = getNode('.chat-room'),
chatName = getNode('.chat-name'),
userName = getNode('.username-in');
statusDefault = status.textContent,
setStatus = function (s) {
status.textContent = s;
if(s !== statusDefault) {
var delay = setTimeout(function() {
setStatus(statusDefault);
clearInterval(delay);
}, 3000);
}
};
if (socket !== undefined) {
//Listen for a status
socket.on('status', function(data) {
setStatus((typeof data === 'object') ? data.message : data);
//clear send message
if(data.clear === true) {
textarea.value = '';
}
});
//Add user to chat
socket.on('connect', function () {
var username = prompt("Your name:")
if(username != null) {
socket.emit('addUser', username);
//Return name to page
var user = document.createElement('div');
user.setAttribute('class', 'username-in-chat');
user.setAttribute('id', 'username-in-chat');
user.textContent = username;
userName.appendChild(user);
userName.insertBefore(user, userName.firstChild);
};
});
//Add new room to page
socket.on('addRoomToPage', function(data) {
$('#chat-room').append('<div><a href="#" class="btn-link btn-primary btn-lg" role="button" onclick="switchRoom(\''+ data + '\')">'
+ data
+ '</a></div>');
});
//Update rooms
socket.on('updateRooms', function(rooms, currentRoom) {
$('#chat-room').empty();
$.each(rooms, function(key, value) {
if(value === currentRoom) {
$('#chat-room').append('<div><a href="#" class="btn btn-link btn-default btn-lg disabled" role="button" onclick="switchRoom(\''+ value + '\')">'
+ value
+ '</a></div>');
}else{
$('#chat-room').append('<div><a href="#" class="btn btn-link btn-primary btn-lg" role="button" onclick="switchRoom(\''+ value + '\')">'
+ value
+ '</a></div>');
}
});
});
//When new room joined clear all old messages at the interface
socket.on('clearRoom', function(data) {
$('#chat-messages').empty();
});
//Get and send messages
socket.on('updateChat', function(time, username, data) {
var message = document.createElement('div');
if (username == 'System') {
message.setAttribute('class','chat-system-message');
if(data === undefined) {
message.textContent = time +' '+ username;
}else{
message.textContent = time +' '+ username +' wrote: '+ data;
}
sysmessages.appendChild(message);
sysmessages.insertBefore(message, sysmessages.firstChild);
}else{
message.setAttribute('class','chat-message');
if(data === undefined) {
message.textContent = time +' '+ username;
}else{
message.textContent = time +' '+ username +' wrote: '+ data;
};
messages.appendChild(message);
messages.insertBefore(message, messages.firstChild);
}
});
//Listen for keydown
textarea.addEventListener('keydown', function(event) {
if(event.which === 13 && event.shiftKey === false) {
//Send message to server
socket.emit('sendChat', this.value);
console.log($("#textarea").text());
$('#textarea').empty();
event.preventDefault();
};
});
}
})();
</script>
</html>