Commit c2ee59e3 authored by Sergey Lyubka's avatar Sergey Lyubka

showing server messages. refreshing messages on timer. removing old messages.

parent cbe56b55
// This file is part of Mongoose project, http://code.google.com/p/mongoose // This file is part of Mongoose project, http://code.google.com/p/mongoose
// $Id: main.js 514 2010-05-03 11:06:27Z valenok $
var chat = { var chat = {
// Backend URL, string. // Backend URL, string.
...@@ -9,25 +8,38 @@ var chat = { ...@@ -9,25 +8,38 @@ var chat = {
errorMessageFadeOutTimeoutMs: 2000, errorMessageFadeOutTimeoutMs: 2000,
errorMessageFadeOutTimer: null, errorMessageFadeOutTimer: null,
lastMessageId: 0, lastMessageId: 0,
getMessagesIntervalMs: 1000,
};
chat.normalizeText = function(text) {
return text.replace('<', '&lt;').replace('>', '&gt;');
}; };
chat.refresh = function(data) { chat.refresh = function(data) {
$.each(data, function(index, entry) { $.each(data, function(index, entry) {
var row = $('<div>').addClass('message-row').appendTo('#mml'); var row = $('<div>').addClass('message-row').appendTo('#mml');
var timestamp = (new Date(entry.timestamp * 1000)).toLocaleTimeString(); var timestamp = (new Date(entry.timestamp * 1000)).toLocaleTimeString();
$('<span>').addClass('message-timestamp').html( $('<span>')
'[' + timestamp + ']').prependTo(row); .addClass('message-timestamp')
$('<span>').addClass('message-user').html(entry.user + ':').appendTo(row); .html('[' + timestamp + ']')
$('<span>').addClass('message-text').html(entry.text).appendTo(row); .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; chat.lastMessageId = Math.max(chat.lastMessageId, entry.id) + 1;
}); });
// TODO(lsm): keep only chat.maxVisibleMessages, delete older ones. // Keep only chat.maxVisibleMessages, delete older ones.
/* while ($('#mml').children().length > chat.maxVisibleMessages) {
while ($('#mml').children().length < chat.maxVisibleMessages) { $('#mml div:first-child').remove();
$('#mml').children()[0].remove();
} }
*/
}; };
chat.getMessages = function() { chat.getMessages = function() {
...@@ -39,6 +51,7 @@ chat.getMessages = function() { ...@@ -39,6 +51,7 @@ chat.getMessages = function() {
error: function() { error: function() {
}, },
}); });
window.setTimeout(chat.getMessages, chat.getMessagesIntervalMs);
}; };
chat.handleMenuItemClick = function(ev) { chat.handleMenuItemClick = function(ev) {
...@@ -60,7 +73,7 @@ chat.handleMessageInput = function(ev) { ...@@ -60,7 +73,7 @@ chat.handleMessageInput = function(ev) {
var input = ev.target; var input = ev.target;
if (ev.keyCode != 13 || !input.value) if (ev.keyCode != 13 || !input.value)
return; return;
input.disabled = true; //input.disabled = true;
$.ajax({ $.ajax({
dataType: 'jsonp', dataType: 'jsonp',
url: chat.backendUrl + '/ajax/send_message', url: chat.backendUrl + '/ajax/send_message',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment