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
|
// This file is part of Civetweb project,
// http://sourceforge.net/projects/civetweb/
var chat = {
// Backend URL, string.
// 'http://backend.address.com' or '' if backend is the same as frontend
backendUrl: '',
maxVisibleMessages: 10,
errorMessageFadeOutTimeoutMs: 2000,
errorMessageFadeOutTimer: null,
lastMessageId: 0,
getMessagesIntervalMs: 1000,
};
chat.normalizeText = function(text) {
return text.replace('<', '<').replace('>', '>');
};
chat.refresh = function(data) {
if (data === undefined) {
return;
}
$.each(data, function(index, entry) {
var row = $('<div>').addClass('message-row').appendTo('#mml');
var timestamp = (new Date(entry.timestamp * 1000)).toLocaleTimeString();
$('<span>')
.addClass('message-timestamp')
.html('[' + timestamp + ']')
.prependTo(row);
$('<span>')
.addClass('message-user')
.addClass(entry.user ? '' : 'message-user-server')
.html(chat.normalizeText((entry.user || '[server]') + ':'))
.appendTo(row);
$('<span>')
.addClass('message-text')
.addClass(entry.user ? '' : 'message-text-server')
.html(chat.normalizeText(entry.text))
.appendTo(row);
chat.lastMessageId = Math.max(chat.lastMessageId, entry.id) + 1;
});
// Keep only chat.maxVisibleMessages, delete older ones.
while ($('#mml').children().length > chat.maxVisibleMessages) {
$('#mml div:first-child').remove();
}
};
chat.getMessages = function(enter_loop) {
$.ajax({
dataType: 'jsonp',
url: chat.backendUrl + '/ajax/get_messages',
data: {last_id: chat.lastMessageId},
success: chat.refresh,
error: function() {
},
});
if (enter_loop) {
window.setTimeout('chat.getMessages(true)', chat.getMessagesIntervalMs);
}
};
chat.handleMenuItemClick = function(ev) {
$('.menu-item').removeClass('menu-item-selected'); // Deselect menu buttons
$(this).addClass('menu-item-selected'); // Select clicked button
$('.main').addClass('hidden'); // Hide all main windows
$('#' + $(this).attr('name')).removeClass('hidden'); // Show main window
};
chat.showError = function(message) {
$('#error').html(message).fadeIn('fast');
window.clearTimeout(chat.errorMessageFadeOutTimer);
chat.errorMessageFadeOutTimer = window.setTimeout(function() {
$('#error').fadeOut('slow');
}, chat.errorMessageFadeOutTimeoutMs);
};
chat.handleMessageInput = function(ev) {
var input = ev.target;
if (ev.keyCode != 13 || !input.value)
return;
//input.disabled = true;
$.ajax({
dataType: 'jsonp',
url: chat.backendUrl + '/ajax/send_message',
data: {text: input.value},
success: function(ev) {
input.value = '';
input.disabled = false;
chat.getMessages(false);
},
error: function(ev) {
chat.showError('Error sending message');
input.disabled = false;
},
});
};
$(document).ready(function() {
$('.menu-item').click(chat.handleMenuItemClick);
$('.message-input').keypress(chat.handleMessageInput);
chat.getMessages(true);
});
// vim:ts=2:sw=2:et
|