-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathclient.js
162 lines (129 loc) · 2.92 KB
/
client.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
window.log = function() {
try {
console.log.apply(console, arguments);
} catch(e) {
//alert( Array.prototype.join.call( arguments, " "));
}
};
var App = App || {},
nicks;
App.util = {};
App.util.zeroPad = function(digits, n) {
n = n.toString();
while (n.length < digits) {
n = '0' + n;
}
return n;
};
App.util.timeString = function(date) {
var minutes = date.getMinutes().toString(),
hours = date.getHours().toString();
return App.util.zeroPad(2, hours) + ':' + App.util.zeroPad(2, minutes);
};
App.config = {
nick: null,
id: null
};
App.who = function() {
$.getJSON('/who', function(data, status) {
if (status !== 'success') {
return;
}
nicks = data.nicks;
$('#usersLink').text(nicks.length.toString() + ' user');
});
};
App.userJoin = function(nick, timestamp) {
App.addMessage(nick, 'joined', timestamp, 'join');
};
App.addMessage = function(from, text, time, _class) {
var messageElement, content;
if (!text) {
return;
}
if (!time) {
// if time is not mentioned then use current time
time = new Date();
}
time = App.util.timeString(time);
messageElement = $('<table />', {
className: 'message'
});
if (_class) {
messageElement.addClass(_class);
}
content = '<tr>' + "<td class='date'>" + time + "</td>" + "<td class='nick'>" + from + "</td>" + "<td class='msg-text'>" + text + "</td>" + "</tr>";
messageElement.html(content);
$('#log').append(messageElement);
};
App.onConnect = function(session) {
if (session.error) {
alert(session.error);
showConnect();
return;
}
App.config.nick = session.nick;
App.config.id = session.id;
App.who();
App.userJoin(App.config.nick, new Date());
App.showChat();
};
$('#connectButton').live('click', function(e) {
var nick = $('#nickInput').attr('value');
log(nick);
if (nick.length > 50) {
alert('nick too long. 50 characters max');
return false;
} else if (/[^\w-]/.test(nick)) {
alert('Bad character found. Only letters, numbers, _ and - are allowed');
return false;
} else if ($.trim(nick).length < 1) {
alert('enter nick name');
return false;
}
$.ajax({
cache: false,
type: 'GET',
url: '/join',
data: {
nick: nick
},
error: function() {
alert('Error connecting to server');
},
success: App.onConnect
});
return false;
});
$('#entry').live('keypress', function(e) {
if (e.keyCode != 13) {
return;
}
var msg = $('#entry').attr('value').replace('\n', '');
App.send(msg);
$('#entry').attr('value', '');
});
App.showConnect = function() {
$('#connect').show();
$('#loading').hide();
$('#toolbar').hide();
$('#nickInput').focus();
};
App.showLoad = function() {
$('#connect').hide();
$('#loading').show();
$('#toolbar').hide();
};
App.showChat = function() {
$('#connect').hide();
$('#loading').hide();
$('#toolbar').show();
};
$(function() {
App.showConnect();
//update the clock every second
setInterval(function() {
$('#currentTime').text(App.util.timeString(new Date()));
},
1000);
});