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
|
// 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 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 isScrolledToBottom(div) {
return Math.abs(div.scrollTop - (div.scrollHeight - div.offsetHeight)) <= 3;
}
function scrollToBottom(div) {
div.scrollTop = div.scrollHeight;
}
function refresh() {
var onSuccess = function(json) {
if (json.messages.length > 0) {
var shouldScroll = isScrolledToBottom($('#messageList')[0]);
// Ignore our own messages
var filterFunc = function(m) { return m.nick != Nick };
var msgStr = $.map($.grep(json.messages, filterFunc),
buildMessageDiv).join('');
$('#messageList').append(msgStr);
if (shouldScroll) {
scrollToBottom($('#messageList')[0]);
}
}
$("#userList").html($.map(json.users, buildUserDiv).join(''));
};
var onError = function(resp, textStatus, errorThrown) {};
$.ajax({
type: 'GET',
timeout: 5000,
url: 'refresh',
cache: false,
dataType: 'json',
success: onSuccess,
error: onError
});
}
function init() {
var onSuccess = function(json) {
$('#loadingbox').hide();
Nick = json.nick;
$('#nickspan').text(Nick);
$('#welcomebar').show();
var msgStr = $.map(json.messages, buildMessageDiv).join('');
$('#messageList').append(msgStr);
$("#userList").html($.map(json.users, buildUserDiv).join(''));
$('#nickInput, #nickSubmit, #msgInput, #msgSubmit').removeAttr('disabled');
// Delay scrolling by .5 seconds so images can start loading.
setTimeout(scrollToBottom, 500, $('#messageList')[0]);
setInterval(refresh, 1000);
};
var onError = function(resp, textStatus, errorThrown) {
alert("Error connecting to chat server!");
};
$.ajax({
type: 'GET',
timeout: 5000,
url: 'init',
cache: false,
dataType: 'json',
success: onSuccess,
error: onError
});
$('#msgInput').keyup(ifEnter(submitMessage));
$('#msgSubmit').click(submitMessage);
}
|