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
|
// pichat.js
var Nick = null;
function handleMsgError(resp) {
var respText = resp.responseText ? resp.responseText.trim() : false;
if (respText == 'UNKNOWN_USER') {
alert("Can't send message! Please login.");
} else if (respText) {
alert("Cannot send message! (" + respText + ")");
} else {
alert("Cannot send message!");
}
}
function escapeHtml(txt) {
if (!txt) {
return ""
} else {
return $("<span>").text(txt).html();
}
}
function buildUserDiv(user) {
return '<div>' + escapeHtml(user) + '</div>';
}
// http://stackoverflow.com/questions/37684/replace-url-with-html-links-javascript
function linkify(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
return text.replace(exp,"<a href='$1'>$1</a>");
}
// http://snippets.dzone.com/posts/show/6995
var URLRegex = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i;
var PicRegex = /\.(jpg|jpeg|png|gif|bmp)$/i;
function buildMessageDiv(msg) {
function buildContent(content) {
var match = URLRegex.exec(content)
if (match && PicRegex.test(match[0])) {
return '<a href="' + match[0] + '" target="_blank">'
+ '<img src="'+ match[0] + '" /></a>';
} else {
return linkify(escapeHtml(msg.content));
}
}
return '<div class="msgDiv"><b>' + escapeHtml(msg.nick) + ': </b>'
+ buildContent(msg.content) + '</div>';
}
function setNick(nick) {
Nick = nick;
$('#nickspan').text(nick);
$('#welcomebar').show();
$('#msgInput, #msgSubmit').removeAttr('disabled');
$('#msgInput').keyup(ifEnter(submitMessage));
$('#msgSubmit').click(submitMessage);
}
function submitMessage() {
var content = $('#msgInput').val();
var msg = { 'nick': Nick, 'content': content, 'timestamp': new Date() };
if (content == '') { return; }
var shouldScroll = isScrolledToBottom($('#messageList')[0]);
$('#messageList').append($(buildMessageDiv(msg)));
$('#msgInput').val('');
if (shouldScroll) {
scrollToBottom($('#messageList')[0]);
}
var onSuccess = function() {};
var onError = function(resp, textStatus, errorThrown) {
handleMsgError(resp);
};
$.ajax({
type: 'GET',
timeout: 5000,
url: 'msg',
data: {'content': content },
cache: false,
dataType: 'json',
success: onSuccess,
error: onError
});
}
function ifEnter(fn) {
return function(e) {
if (e.keyCode == 13) { fn(); }
};
}
function login() {
var nick = $('#nickInput').val();
var password = $('#passwordInput').val();
var hash = hex_sha1(nick + '$' + password + '$dumpfm');
var onSuccess = function(json) {
$('#loginbar').hide();
setNick(nick);
};
var onError = function(resp, textStatus, errorThrown) {
alert("Error logging in!");
};
$.ajax({
type: 'GET',
timeout: 5000,
url: 'login',
data: {'nick': nick, 'hash': hash },
cache: false,
dataType: 'json',
success: onSuccess,
error: onError
});
};
function isScrolledToBottom(div) {
return Math.abs(div.scrollTop - (div.scrollHeight - div.offsetHeight)) <= 3;
}
function scrollToBottom(div) {
div.scrollTop = div.scrollHeight;
}
function updateUI(json, initialUpdate) {
if (json.messages.length > 0) {
if (initialUpdate) {
var messages = json.messages;
} else {
// Our own messages have already been displayed.
var filterFunc = function(m) { return m.nick != Nick };
var messages = $.grep(json.messages, filterFunc);
}
var msgStr = $.map(messages,
buildMessageDiv).join('');
var wasScrolledToBottom = isScrolledToBottom($('#messageList')[0]);
$('#messageList').append(msgStr);
if (initialUpdate || wasScrolledToBottom) {
// Delay scrolling by .5 seconds so images can start loading.
setTimeout(scrollToBottom, 500, $('#messageList')[0]);
}
}
$("#userList").html($.map(json.users, buildUserDiv).join(''));
}
function refresh() {
var onSuccess = function(json) {
updateUI(json, false);
setTimeout(refresh, 1000);
};
var onError = function(resp, textStatus, errorThrown) {
setTimeout(refresh, 1000);
};
$.ajax({
type: 'GET',
timeout: 5000,
url: 'refresh',
cache: false,
dataType: 'json',
success: onSuccess,
error: onError
});
}
function init() {
var onSuccess = function(json) {
if (json.nick) {
setNick(json.nick);
} else {
$('#loginbar').show();
$('#passwordInput').keyup(ifEnter(login));
$('#loginSubmit').click(login);
}
updateUI(json, true);
setTimeout(refresh, 1000);
};
var onError = function(resp, textStatus, errorThrown) {
alert("Error initializing!");
};
$.ajax({
type: 'GET',
timeout: 5000,
url: 'init',
cache: false,
dataType: 'json',
success: onSuccess,
error: onError
});
}
|