|
|
|
@ -23,7 +23,7 @@ OTHER DEALINGS IN THE SOFTWARE. |
|
|
|
|
var windowFocus = true; |
|
|
|
|
var username; |
|
|
|
|
var chatHeartbeatCount = 0; |
|
|
|
|
var minChatHeartbeat = 1000; |
|
|
|
|
var minChatHeartbeat = 4000; |
|
|
|
|
var maxChatHeartbeat = 33000; |
|
|
|
|
var chatHeartbeatTime = minChatHeartbeat; |
|
|
|
|
var originalTitle; |
|
|
|
@ -33,10 +33,9 @@ var chatboxFocus = new Array(); |
|
|
|
|
var newMessages = new Array(); |
|
|
|
|
var newMessagesWin = new Array(); |
|
|
|
|
var chatBoxes = new Array(); |
|
|
|
|
|
|
|
|
|
//var ajax_url = 'chat.php'; // This variable is loaded in header.inc.php/ template/layout/head.tpl
|
|
|
|
|
|
|
|
|
|
$(document).ready(function(){ |
|
|
|
|
$(document).ready(function() { |
|
|
|
|
originalTitle = document.title; |
|
|
|
|
startChatSession(); |
|
|
|
|
|
|
|
|
@ -49,7 +48,7 @@ $(document).ready(function(){ |
|
|
|
|
|
|
|
|
|
/* Live conditions */ |
|
|
|
|
|
|
|
|
|
// Header
|
|
|
|
|
// User name header toogle
|
|
|
|
|
$('.chatboxtitle').live('click', function(){ |
|
|
|
|
chatbox = $(this).parents(".chatbox"); |
|
|
|
|
var chat_id = chatbox.attr('id'); |
|
|
|
@ -57,13 +56,13 @@ $(document).ready(function(){ |
|
|
|
|
toggleChatBoxGrowth(chat_id); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
//Toggle
|
|
|
|
|
//Minimize button
|
|
|
|
|
$('.chatboxhead .togglelink').live('click', function(){ |
|
|
|
|
var chat_id = $(this).attr('rel');
|
|
|
|
|
toggleChatBoxGrowth(chat_id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//Close
|
|
|
|
|
//Close button
|
|
|
|
|
$('.chatboxhead .closelink').live('click', function(){
|
|
|
|
|
var chat_id = $(this).attr('rel');
|
|
|
|
|
closeChatBox(chat_id);
|
|
|
|
@ -71,6 +70,9 @@ $(document).ready(function(){ |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Start chat session |
|
|
|
|
*/ |
|
|
|
|
function startChatSession() {
|
|
|
|
|
$.ajax({ |
|
|
|
|
url: ajax_url+"?action=startchatsession", |
|
|
|
@ -78,41 +80,165 @@ function startChatSession() { |
|
|
|
|
dataType: "json", |
|
|
|
|
success: function(data) { |
|
|
|
|
if (data) { |
|
|
|
|
username = data.username;
|
|
|
|
|
|
|
|
|
|
$.each(data.items, function(i,item){ |
|
|
|
|
if (item) { // fix strange ie bug
|
|
|
|
|
my_user_id = item.f; |
|
|
|
|
chatboxtitle = item.username; |
|
|
|
|
|
|
|
|
|
if ($("#chatbox_"+my_user_id).length <= 0) { |
|
|
|
|
createChatBox(my_user_id, chatboxtitle, 1, item.online); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (item.s == 1) { |
|
|
|
|
//item.f = username;
|
|
|
|
|
} |
|
|
|
|
if (item.s == 2) { |
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>'); |
|
|
|
|
} else { |
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage">\n\ |
|
|
|
|
<span class="chatboxmessagefrom">'+item.username+': </span>\n\ |
|
|
|
|
<span class="chatboxmessagecontent">'+item.m+'</span></div>'); |
|
|
|
|
username = data.me; |
|
|
|
|
|
|
|
|
|
$.each(data.items, function(my_user_id, user_items) { |
|
|
|
|
//received_messages[item.f][item.id] = true;
|
|
|
|
|
$.each(user_items, function(i, item) { |
|
|
|
|
|
|
|
|
|
if (item) { // fix strange ie bug
|
|
|
|
|
//my_user_id = item.f;
|
|
|
|
|
if ($("#chatbox_"+my_user_id).length <= 0) { |
|
|
|
|
createChatBox(my_user_id, item.username, 1, item.online); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (item.s == 1) { |
|
|
|
|
//item.f = username;
|
|
|
|
|
} |
|
|
|
|
if (item.s == 2) { |
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>'); |
|
|
|
|
} else { |
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage">\n\ |
|
|
|
|
<span class="chatboxmessagefrom">'+item.username+': </span>\n\ |
|
|
|
|
<span class="chatboxmessagecontent">'+item.m+'</span></div>'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
for (i=0;i<chatBoxes.length;i++) { |
|
|
|
|
my_user_id = chatBoxes[i]; |
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight); |
|
|
|
|
setTimeout('$("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight);', 100); // yet another strange ie bug
|
|
|
|
|
//setTimeout('$("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight);', 100); // yet another strange ie bug
|
|
|
|
|
} |
|
|
|
|
setTimeout('chatHeartbeat();',chatHeartbeatTime); |
|
|
|
|
setTimeout('chatHeartbeat();', chatHeartbeatTime); |
|
|
|
|
}
|
|
|
|
|
}}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* Shows the user messages in all windows |
|
|
|
|
*
|
|
|
|
|
* Item array structure : |
|
|
|
|
*
|
|
|
|
|
* item.s = type of message: 1 = message, 2 = "sent at" string |
|
|
|
|
* item.m = message |
|
|
|
|
* item.f = from_user |
|
|
|
|
* |
|
|
|
|
**/ |
|
|
|
|
function chatHeartbeat() { |
|
|
|
|
var itemsfound = 0; |
|
|
|
|
|
|
|
|
|
if (windowFocus == false) { |
|
|
|
|
|
|
|
|
|
var blinkNumber = 0; |
|
|
|
|
var titleChanged = 0; |
|
|
|
|
|
|
|
|
|
for (x in newMessagesWin) { |
|
|
|
|
if (newMessagesWin[x].status == true) { |
|
|
|
|
++blinkNumber; |
|
|
|
|
if (blinkNumber >= blinkOrder) { |
|
|
|
|
document.title = newMessagesWin[x].username+' says...'; |
|
|
|
|
titleChanged = 1; |
|
|
|
|
break;
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (titleChanged == 0) { |
|
|
|
|
document.title = originalTitle; |
|
|
|
|
blinkOrder = 0; |
|
|
|
|
} else { |
|
|
|
|
++blinkOrder; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
for (x in newMessagesWin) { |
|
|
|
|
newMessagesWin[x].status = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (x in newMessages) { |
|
|
|
|
if (newMessages[x] == true) { |
|
|
|
|
if (chatboxFocus[x] == false) { |
|
|
|
|
//FIXME: add toggle all or none policy, otherwise it looks funny
|
|
|
|
|
$('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$.ajax({ |
|
|
|
|
url: ajax_url+"?action=chatheartbeat", |
|
|
|
|
cache: false, |
|
|
|
|
dataType: "json", |
|
|
|
|
success: function(data) {
|
|
|
|
|
|
|
|
|
|
$.each(data.items, function(my_user_id, user_items) {
|
|
|
|
|
|
|
|
|
|
$.each(user_items, function(i, item) { |
|
|
|
|
if (item) { // fix strange ie bug
|
|
|
|
|
|
|
|
|
|
if ($("#chatbox_"+my_user_id).length <= 0) { |
|
|
|
|
createChatBox(my_user_id, item.username, 0, item.online); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($("#chatbox_"+my_user_id).css('display') == 'none') { |
|
|
|
|
$("#chatbox_"+my_user_id).css('display','block'); |
|
|
|
|
restructureChatBoxes(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (item.s == 1) { |
|
|
|
|
//item.f = username;
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
update_online_user(my_user_id, item.online); |
|
|
|
|
|
|
|
|
|
if (item.s == 2) { |
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>'); |
|
|
|
|
} else { |
|
|
|
|
newMessages[my_user_id] = {'status':true,'username':item.username}; |
|
|
|
|
newMessagesWin[my_user_id] = {'status':true,'username':item.username};
|
|
|
|
|
|
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage">\n\ |
|
|
|
|
<span class="chatboxmessagefrom">'+item.username+': </span>\n\ |
|
|
|
|
<span class="chatboxmessagecontent">'+item.m+'</span></div>'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight); |
|
|
|
|
itemsfound += 1; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
chatHeartbeatCount++; |
|
|
|
|
|
|
|
|
|
if (itemsfound > 0) { |
|
|
|
|
chatHeartbeatTime = minChatHeartbeat; |
|
|
|
|
chatHeartbeatCount = 1; |
|
|
|
|
} else if (chatHeartbeatCount >= 10) { |
|
|
|
|
chatHeartbeatTime *= 2; |
|
|
|
|
chatHeartbeatCount = 1; |
|
|
|
|
if (chatHeartbeatTime > maxChatHeartbeat) { |
|
|
|
|
chatHeartbeatTime = maxChatHeartbeat; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setTimeout('chatHeartbeat();',chatHeartbeatTime); |
|
|
|
|
} |
|
|
|
|
}); //ajax
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function closeChatBox(user_id) { |
|
|
|
|
$('#chatbox_'+user_id).css('display','none'); |
|
|
|
|
restructureChatBoxes();
|
|
|
|
|
$.post(ajax_url+"?action=closechat", {chatbox: user_id} , function(data){
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function restructureChatBoxes() { |
|
|
|
|
align = 0; |
|
|
|
|
for (x in chatBoxes) { |
|
|
|
@ -129,11 +255,20 @@ function restructureChatBoxes() { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Function that fires the chat with an user (creates a chat bloclk) |
|
|
|
|
* @param int user id
|
|
|
|
|
* @param string user's firstname + lastname |
|
|
|
|
* |
|
|
|
|
**/ |
|
|
|
|
function chatWith(user_id, user_name) { |
|
|
|
|
createChatBox(user_id, user_name, 0, 0); |
|
|
|
|
$("#chatbox_"+user_id+" .chatboxtextarea").focus(); |
|
|
|
|
$("#chatbox_"+user_id+" .chatboxtextarea").focus();
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates a div
|
|
|
|
|
*/ |
|
|
|
|
function createChatBox(user_id, chatboxtitle, minimizeChatBox, online) { |
|
|
|
|
if ($("#chatbox_"+user_id).length > 0) { |
|
|
|
|
if ($("#chatbox_"+user_id).css('display') == 'none') { |
|
|
|
@ -218,6 +353,9 @@ function createChatBox(user_id, chatboxtitle, minimizeChatBox, online) { |
|
|
|
|
$("#chatbox_"+user_id).show(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates the div user status (green/gray button next to the user name)
|
|
|
|
|
*/ |
|
|
|
|
function return_online_user(user_id, status) { |
|
|
|
|
var div_wrapper = $("<div />" ); |
|
|
|
|
var new_div = $("<div />" ); |
|
|
|
@ -234,6 +372,9 @@ function return_online_user(user_id, status) { |
|
|
|
|
return div_wrapper.html();
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Updates the user status (green/gray button next to the user name)
|
|
|
|
|
*/ |
|
|
|
|
function update_online_user(user_id, status) { |
|
|
|
|
if ($("#online_" +user_id).length > 0) { |
|
|
|
|
if (status == 1) { |
|
|
|
@ -243,119 +384,7 @@ function update_online_user(user_id, status) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
/* |
|
|
|
|
* Item array structure : |
|
|
|
|
*
|
|
|
|
|
* item.s = type 1= message, 2= sent at string |
|
|
|
|
* item.m = message |
|
|
|
|
* item.f = from user |
|
|
|
|
* |
|
|
|
|
**/ |
|
|
|
|
|
|
|
|
|
function chatHeartbeat() { |
|
|
|
|
var itemsfound = 0; |
|
|
|
|
|
|
|
|
|
if (windowFocus == false) { |
|
|
|
|
|
|
|
|
|
var blinkNumber = 0; |
|
|
|
|
var titleChanged = 0;
|
|
|
|
|
for (x in newMessagesWin) { |
|
|
|
|
if (newMessagesWin[x].status == true) { |
|
|
|
|
++blinkNumber; |
|
|
|
|
if (blinkNumber >= blinkOrder) { |
|
|
|
|
document.title = newMessagesWin[x].username+' says...'; |
|
|
|
|
titleChanged = 1; |
|
|
|
|
break;
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (titleChanged == 0) { |
|
|
|
|
document.title = originalTitle; |
|
|
|
|
blinkOrder = 0; |
|
|
|
|
} else { |
|
|
|
|
++blinkOrder; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
for (x in newMessagesWin) { |
|
|
|
|
newMessagesWin[x].status = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (x in newMessages) { |
|
|
|
|
if (newMessages[x] == true) { |
|
|
|
|
if (chatboxFocus[x] == false) { |
|
|
|
|
//FIXME: add toggle all or none policy, otherwise it looks funny
|
|
|
|
|
$('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$.ajax({ |
|
|
|
|
url: ajax_url+"?action=chatheartbeat", |
|
|
|
|
cache: false, |
|
|
|
|
dataType: "json", |
|
|
|
|
success: function(data) {
|
|
|
|
|
$.each(data.items, function(i, item) { |
|
|
|
|
if (item) { // fix strange ie bug
|
|
|
|
|
|
|
|
|
|
my_user_id = item.f;
|
|
|
|
|
|
|
|
|
|
if ($("#chatbox_"+my_user_id).length <= 0) { |
|
|
|
|
createChatBox(my_user_id, item.username, 0, item.online); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($("#chatbox_"+my_user_id).css('display') == 'none') { |
|
|
|
|
$("#chatbox_"+my_user_id).css('display','block'); |
|
|
|
|
restructureChatBoxes(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (item.s == 1) { |
|
|
|
|
//item.f = username;
|
|
|
|
|
} |
|
|
|
|
update_online_user(my_user_id, item.online); |
|
|
|
|
|
|
|
|
|
if (item.s == 2) { |
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>'); |
|
|
|
|
} else { |
|
|
|
|
newMessages[my_user_id] = {'status':true,'username':item.username}; |
|
|
|
|
newMessagesWin[my_user_id] = {'status':true,'username':item.username};
|
|
|
|
|
|
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage">\n\ |
|
|
|
|
<span class="chatboxmessagefrom">'+item.username+': </span>\n\ |
|
|
|
|
<span class="chatboxmessagecontent">'+item.m+'</span></div>'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$("#chatbox_"+my_user_id+" .chatboxcontent").scrollTop($("#chatbox_"+my_user_id+" .chatboxcontent")[0].scrollHeight); |
|
|
|
|
itemsfound += 1; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
chatHeartbeatCount++; |
|
|
|
|
|
|
|
|
|
if (itemsfound > 0) { |
|
|
|
|
chatHeartbeatTime = minChatHeartbeat; |
|
|
|
|
chatHeartbeatCount = 1; |
|
|
|
|
} else if (chatHeartbeatCount >= 10) { |
|
|
|
|
chatHeartbeatTime *= 2; |
|
|
|
|
chatHeartbeatCount = 1; |
|
|
|
|
if (chatHeartbeatTime > maxChatHeartbeat) { |
|
|
|
|
chatHeartbeatTime = maxChatHeartbeat; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setTimeout('chatHeartbeat();',chatHeartbeatTime); |
|
|
|
|
}}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function closeChatBox(chatboxtitle) { |
|
|
|
|
$('#chatbox_'+chatboxtitle).css('display','none'); |
|
|
|
|
restructureChatBoxes(); |
|
|
|
|
$.post(ajax_url+"?action=closechat", { chatbox: chatboxtitle} , function(data){
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function toggleChatBoxGrowth(user_id) {
|
|
|
|
|
if ($('#chatbox_'+user_id+' .chatboxcontent').css('display') == 'none') {
|
|
|
|
@ -382,18 +411,16 @@ function toggleChatBoxGrowth(user_id) { |
|
|
|
|
$("#chatbox_"+user_id+" .chatboxcontent").scrollTop($("#chatbox_"+user_id+" .chatboxcontent")[0].scrollHeight); |
|
|
|
|
} else {
|
|
|
|
|
var newCookie = user_id; |
|
|
|
|
|
|
|
|
|
if ($.cookie('chatbox_minimized')) { |
|
|
|
|
newCookie += '|'+$.cookie('chatbox_minimized'); |
|
|
|
|
} |
|
|
|
|
$.cookie('chatbox_minimized',newCookie); |
|
|
|
|
$('#chatbox_'+user_id+' .chatboxcontent').css('display','none'); |
|
|
|
|
$('#chatbox_'+user_id+' .chatboxinput').css('display','none'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function checkChatBoxInputKey(event,chatboxtextarea, user_id) { |
|
|
|
|
function checkChatBoxInputKey(event, chatboxtextarea, user_id) { |
|
|
|
|
|
|
|
|
|
if(event.keyCode == 13 && event.shiftKey == 0) { |
|
|
|
|
message = $(chatboxtextarea).val(); |
|
|
|
@ -404,12 +431,15 @@ function checkChatBoxInputKey(event,chatboxtextarea, user_id) { |
|
|
|
|
$(chatboxtextarea).css('height','44px'); |
|
|
|
|
|
|
|
|
|
if (message != '') { |
|
|
|
|
$.post(ajax_url + "?action=sendchat", {to: user_id, message: message} , function(data){ |
|
|
|
|
$.post(ajax_url + "?action=sendchat", {to: user_id, message: message} , function(data) { |
|
|
|
|
message = message.replace(/</g,"<").replace(/>/g,">").replace(/\"/g,"""); |
|
|
|
|
|
|
|
|
|
$("#chatbox_"+user_id+" .chatboxcontent").append('<div class="chatboxmessage">\n\ |
|
|
|
|
<span class="chatboxmessagefrom">'+username+': </span>\n\ |
|
|
|
|
<span class="chatboxmessagecontent">'+message+'</span></div>'); |
|
|
|
|
|
|
|
|
|
$("#chatbox_"+user_id+" .chatboxcontent").scrollTop($("#chatbox_"+user_id+" .chatboxcontent")[0].scrollHeight); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
chatHeartbeatTime = minChatHeartbeat; |
|
|
|
@ -429,8 +459,7 @@ function checkChatBoxInputKey(event,chatboxtextarea, user_id) { |
|
|
|
|
$(chatboxtextarea).css('height',adjustedHeight+8 +'px'); |
|
|
|
|
} else { |
|
|
|
|
$(chatboxtextarea).css('overflow','auto'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|