This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
/
skatroom.js
executable file
·214 lines (172 loc) · 5.69 KB
/
skatroom.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
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
window.onload = function() {
var userInfo,
messageContainer,
submitButton,
activeUsers,
textInput,
socket,
banter;
var copy = {
enteredTheRoom: 'has entered the bathroom in stall',
disconnected: 'You have been disconnected.',
};
var loginForm = document.forms[0];
/*** INIT SOCKETS AND CHAT ***/
var initSkatroom = function() {
/*** scatroom and its events ***/
document.getElementById('skatroom').style.display = 'block';
messageContainer = document.getElementById('skatroom-messages');
submitButton = document.getElementById('submit-message');
activeUsersList = document.getElementById('active-users');
textInput = document.forms[0].message;
banter = [];
activeUsers = [];
// add button event
submitButton.onclick = function(e){
sendMessage( userInfo.nickname, userInfo.stall, textInput.value );
textInput.value = "";
};
// submit form event
document.forms[0].onsubmit = function() {
return false;
};
};
/*** initialize socket and its events ***/
var initSocket = function() {
socket = io.connect(system.io);
socket.on('connect',function() {
initSkatroom();
// send user to room list
socket.emit('send', { activeUser: userInfo.nickname , userStall: userInfo.stall});
// send message that userInfo.nickname has entered bathroom stall
system.message = userInfo.nickname + ' ' + copy.enteredTheRoom + ' ' + userInfo.stall;
sendMessage(system.name, system.color, system.message);
});
socket.on('history', function(data) {
data.forEach(function(message) {
updateMessageWindow(message);
});
});
socket.on('message',function(data) {
if ( data.chat ) {
updateMessageWindow(data.chat);
}
else if ( data.updateRoom ) {
// get all users in array
activeUsers = [];
for (var key in data.updateRoom) {
var obj = data.updateRoom[key];
if ( activeUsers.indexOf(obj.nickname) == -1 ) {
activeUsers.push(obj.nickname);
}
}
// print active users list
activeUsersList.innerHTML = '';
for ( var i = 0; i < activeUsers.length; i++) {
activeUsersList.innerHTML += '<li>' + activeUsers[i] + '</li>';
}
}
});
socket.on('disconnect',function() {
textInput.style.display = 'none';
window.open('/logoff','_self');
});
};
/*** HELPERS ***/
var updateMessageWindow = function(chatData) {
banter.push(chatData);
var banterHTML = '';
if ( chatData.nick == system.name ) {
banterHTML += '<li class="system"><span class="text">' + processMessage(chatData.text) + '</span></li>';
}
else {
banterHTML += '<li><span class="nick" style="color:' +
stripHTML(chatData.color) + '">' +
stripHTML(chatData.nick) +
' (' + getTheTime(chatData.timestamp) + '):</span> <span class="text">' +
processMessage(chatData.text) + '</span></li>';
}
messageContainer.innerHTML += banterHTML;
messageContainer.scrollTop = messageContainer.scrollHeight;
};
var getCookies = function() {
var cookies = {};
var documentCookies = document.cookie;
if (documentCookies === "")
return cookies;
var cookiesArray = documentCookies.split("; ");
for(var i = 0; i < cookiesArray.length; i++) {
var cookie = cookiesArray[i];
var endOfName = cookie.indexOf("=");
var name = cookie.substring(0, endOfName);
var value = cookie.substring(endOfName + 1);
value = decodeURIComponent(value);
cookies[name] = value;
}
return cookies;
};
var sendMessage = function( nick, stall, messageText ) {
var message = {
nick: nick,
stall: stall,
text: messageText,
timestamp: Date.now()
};
socket.emit('send', { chat: message });
};
var stripHTML = function(html) {
if ( html ) {
return html.replace(/<\/?([a-z][a-z0-9]*)\b[^>]*>?/gi, '');
} else {
return false;
}
};
var getTheTime = function(unix) {
var date = new Date(unix);
var hours = date.getHours();
var minutes = date.getMinutes();
var amOrPm = "am";
if ( hours >= 12 ) {
amOrPm = "pm";
if ( hours != 12 ) {
hours = hours - 12;
}
}
if ( hours === 0 ) {
hours = 12;
}
if ( minutes < 10 ) {
minutes = '0' + minutes;
}
return hours + ':' + minutes + ' ' + amOrPm;
};
var processMessage = function(message) {
// urls within text
var regExURL = /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/g;
message = stripHTML(message);
message = message.replace(regExURL, function(url) {
return '<a href="' + url + '">' + url + '</a>';
});
// :emoji: within text - add emoji gif files in /public/assets/emoji
var regExEmoji = /\:[a-zA-Z0-9\-\.]+\:?/g;
message = message.replace(regExEmoji, function(emojiName) {
return '<img class="emoji" src="/assets/emoji/' + emojiName.replace(/:/g, '') + '.gif" alt=":' + emojiName + '" + title="' + emojiName + '" />';
});
return message;
};
/*** INIT ***/
var init = (function() {
// check for skatroom cookies
var cookies = getCookies();
// get stall name from url
var stallName = window.location.search.replace('?','');
if ( cookies.skatroom) {
initSocket();
userInfo = JSON.parse( cookies.skatroom );
}
else {
// if no cookies, redirect to login
document.location = '/login/?' + stallName;
}
}());
};