Commit 7d3c9829 authored by JC Brand's avatar JC Brand

Remember open and minimized chat windows upon page reload.

parent d4673b94
...@@ -50,35 +50,36 @@ var xmppchat = (function ($, console) { ...@@ -50,35 +50,36 @@ var xmppchat = (function ($, console) {
}; };
obj.handleChatEvents = function (chat_id) { obj.handleChatEvents = function (chat_id) {
var chat_area = $("#"+xmppchat.hash(chat_id)+" .chat-textarea"), var chat_area = $("#"+chat_id+" .chat-textarea"),
chat_type = chat_id.split('_')[0], chat_type = chat_id.split('_')[0],
that = this; that = this;
that.chat_focus[chat_id] = false; that.chat_focus[chat_id] = false;
chat_area.blur(function(){ chat_area.blur(function () {
that.chat_focus[chat_id] = false; that.chat_focus[chat_id] = false;
chat_area.removeClass('chat-textarea-'+chat_type+'-selected'); chat_area.removeClass('chat-textarea-'+chat_type+'-selected');
}).focus(function(){ }).focus(function (){
that.chat_focus[chat_id] = true; that.chat_focus[chat_id] = true;
chat_area.addClass('chat-textarea-'+chat_type+'-selected'); chat_area.addClass('chat-textarea-'+chat_type+'-selected');
}); });
var chatbox = $("#"+xmppchat.hash(chat_id)); var chatbox = $("#"+chat_id);
chatbox.click(function() { chatbox.click(function () {
if (chatbox.find('.chat-content').css('display') != 'none') { if (chatbox.find('.chat-content').css('display') != 'none') {
chatbox.find('.chat-textarea').focus(); chatbox.find('.chat-textarea').focus();
} }
}); });
}; };
obj.createChatBox = function (chat_id, jid) { obj.createChatBox = function (jid) {
var path = xmppchat.sanitizePath('/@@render_chat_box'); var path = xmppchat.sanitizePath('/@@render_chat_box'),
chat_id = this.hash(jid);
$.ajax({ $.ajax({
url: path, url: path,
cache: false, cache: false,
async: false, async: false,
data: { data: {
chat_id: chat_id, chat_id: 'chatbox_'+jid,
box_id: xmppchat.hash(chat_id), box_id: chat_id,
jid: jid, jid: jid,
tzoffset: -(new Date().getTimezoneOffset()) tzoffset: -(new Date().getTimezoneOffset())
}, },
...@@ -105,16 +106,20 @@ var xmppchat = (function ($, console) { ...@@ -105,16 +106,20 @@ var xmppchat = (function ($, console) {
} }
} }
}); });
return $('#'+xmppchat.hash(chat_id)); return $('#'+chat_id);
}; };
obj.createChat = function (chat_id, minimize, jid) { obj.createChat = function (jid, minimize) {
if (typeof(jid) === undefined) {
return;
}
var cookie = $.cookie('chats-open-'+this.username), var cookie = $.cookie('chats-open-'+this.username),
chat_content, chatbox; chat_content,
chatbox,
this.addChatToCookie(chat_id); chat_id = this.hash(jid);
this.addChatToCookie(jid);
chatbox = $("#"+this.hash(chat_id)); chatbox = $("#"+chat_id);
if (chatbox.length > 0) { if (chatbox.length > 0) {
// The chatbox exists, merely hidden // The chatbox exists, merely hidden
if (chatbox.css('display') == 'none') { if (chatbox.css('display') == 'none') {
...@@ -126,11 +131,7 @@ var xmppchat = (function ($, console) { ...@@ -126,11 +131,7 @@ var xmppchat = (function ($, console) {
chat_content.scrollTop(chat_content[0].scrollHeight); chat_content.scrollTop(chat_content[0].scrollHeight);
return; return;
} }
chatbox = this.createChatBox(chat_id, jid); chatbox = this.createChatBox(jid);
if (chatbox.length === 0) {
console.log('Could not create chatbox with id: ' + chat_id);
return;
}
this.positionNewChat(chatbox); this.positionNewChat(chatbox);
this.chats.push(chat_id); this.chats.push(chat_id);
if (minimize == 1) { if (minimize == 1) {
...@@ -150,9 +151,10 @@ var xmppchat = (function ($, console) { ...@@ -150,9 +151,10 @@ var xmppchat = (function ($, console) {
return chatbox; return chatbox;
}; };
obj.startChat = function (chat_id, jid) { obj.startChat = function (jid) {
this.createChat(chat_id, 0, jid);
$("#"+this.hash(chat_id)+" .chat-textarea").focus(); this.createChat(jid, 0);
$("#"+this.hash(jid)+" .chat-textarea").focus();
}; };
obj.reorderChats = function () { obj.reorderChats = function () {
...@@ -197,10 +199,9 @@ var xmppchat = (function ($, console) { ...@@ -197,10 +199,9 @@ var xmppchat = (function ($, console) {
that = this; that = this;
jarnxmpp.Presence.getUserInfo(user_id, function (data) { jarnxmpp.Presence.getUserInfo(user_id, function (data) {
var chat_id = 'chatbox_'+user_id; var chat = $('#'+that.hash(jid));
var chat = $('#'+that.hash(chat_id));
if (chat.length <= 0) { if (chat.length <= 0) {
chat = that.createChat(chat_id, 0, jid); chat = that.createChat(jid, 0);
} }
if (chat.css('display') == 'none') { if (chat.css('display') == 'none') {
chat.css('display','block'); chat.css('display','block');
...@@ -233,9 +234,9 @@ var xmppchat = (function ($, console) { ...@@ -233,9 +234,9 @@ var xmppchat = (function ($, console) {
}); });
}; };
obj.closeChat = function (jid, audience) {
obj.closeChat = function (chat_id, audience) { var chat_id = this.hash(jid);
jQuery('#'+xmppchat.hash(chat_id)).css('display','none'); jQuery('#'+chat_id).css('display','none');
xmppchat.reorderChats(); xmppchat.reorderChats();
var cookie = jQuery.cookie('chats-open-'+xmppchat.username), var cookie = jQuery.cookie('chats-open-'+xmppchat.username),
open_chats = [], open_chats = [],
...@@ -244,7 +245,7 @@ var xmppchat = (function ($, console) { ...@@ -244,7 +245,7 @@ var xmppchat = (function ($, console) {
open_chats = cookie.split('|'); open_chats = cookie.split('|');
} }
for (var i=0; i < open_chats.length; i++) { for (var i=0; i < open_chats.length; i++) {
if (open_chats[i] != chat_id) { if (open_chats[i] != jid) {
new_chats.push(open_chats[i]); new_chats.push(open_chats[i]);
} }
} }
...@@ -257,11 +258,12 @@ var xmppchat = (function ($, console) { ...@@ -257,11 +258,12 @@ var xmppchat = (function ($, console) {
this.chats.pop(chat_id); this.chats.pop(chat_id);
}; };
obj.toggleChat = function (chat_id) { obj.toggleChat = function (jid) {
var minimized_chats = xmppchat.getMinimizedChats(); var chat_id = this.hash(jid),
var hashed_id = xmppchat.hash(chat_id); minimized_chats = xmppchat.getMinimizedChats(),
var new_cookie; new_cookie;
if (jQuery('#'+hashed_id+' .chat-content').css('display') == 'none') {
if (jQuery('#'+chat_id+' .chat-content').css('display') == 'none') {
// Chat will be maximized // Chat will be maximized
new_cookie = []; new_cookie = [];
for (var i=0; i < minimized_chats.length; i++) { for (var i=0; i < minimized_chats.length; i++) {
...@@ -270,11 +272,11 @@ var xmppchat = (function ($, console) { ...@@ -270,11 +272,11 @@ var xmppchat = (function ($, console) {
} }
} }
jQuery.cookie('chats_minimized_'+xmppchat.username, new_cookie.join('|')); jQuery.cookie('chats_minimized_'+xmppchat.username, new_cookie.join('|'));
var chat_content = jQuery('#'+hashed_id+' .chat-content'); var chat_content = jQuery('#'+chat_id+' .chat-content');
chat_content.css('display','block'); chat_content.css('display','block');
chat_content.scrollTop(chat_content[0].scrollHeight); chat_content.scrollTop(chat_content[0].scrollHeight);
jQuery('#'+hashed_id+' .chat-head').removeClass('chat-head-minimized-with-messages'); jQuery('#'+chat_id+' .chat-head').removeClass('chat-head-minimized-with-messages');
jQuery('#'+hashed_id+' .chat-input').css('display','block'); jQuery('#'+chat_id+' .chat-input').css('display','block');
} }
else { else {
// Chat will be minimized // Chat will be minimized
...@@ -283,12 +285,12 @@ var xmppchat = (function ($, console) { ...@@ -283,12 +285,12 @@ var xmppchat = (function ($, console) {
new_cookie += '|'+minimized_chats.join('|'); new_cookie += '|'+minimized_chats.join('|');
jQuery.cookie('chats_minimized_'+xmppchat.username, new_cookie); jQuery.cookie('chats_minimized_'+xmppchat.username, new_cookie);
} }
jQuery('#'+hashed_id+' .chat-content').css('display','none'); jQuery('#'+chat_id+' .chat-content').css('display','none');
jQuery('#'+hashed_id+' .chat-input').css('display','none'); jQuery('#'+chat_id+' .chat-input').css('display','none');
} }
}; };
obj.keyPressed = function (event, textarea, audience, hashed_id, chat_type) { obj.keyPressed = function (event, textarea, audience, chat_id, chat_type) {
if(event.keyCode == 13 && !event.shiftKey) { if(event.keyCode == 13 && !event.shiftKey) {
var textbox = jQuery(textarea); var textbox = jQuery(textarea);
var message = textbox.val(); var message = textbox.val();
...@@ -308,7 +310,7 @@ var xmppchat = (function ($, console) { ...@@ -308,7 +310,7 @@ var xmppchat = (function ($, console) {
var minutes = now.getMinutes().toString(); var minutes = now.getMinutes().toString();
if (minutes.length==1) {minutes = '0'+minutes;} if (minutes.length==1) {minutes = '0'+minutes;}
var time = now.toLocaleTimeString().substring(0,5); var time = now.toLocaleTimeString().substring(0,5);
var chat_content = jQuery('#'+hashed_id+' .chat-content'); var chat_content = jQuery('#'+chat_id+' .chat-content');
chat_content.append( chat_content.append(
'<div class="chat-message">' + '<div class="chat-message">' +
'<span class="chat-message-me">'+time+' me:&nbsp;&nbsp;</span>' + '<span class="chat-message-me">'+time+' me:&nbsp;&nbsp;</span>' +
...@@ -345,36 +347,29 @@ $(document).ready(function () { ...@@ -345,36 +347,29 @@ $(document).ready(function () {
var chatdata = jQuery('span#babble-client-chatdata'); var chatdata = jQuery('span#babble-client-chatdata');
var cookie = jQuery.cookie('chats-open-'+chatdata.attr('username')); var cookie = jQuery.cookie('chats-open-'+chatdata.attr('username'));
var open_chats = [], chat_id; var open_chats = [], chat_id;
xmppchat.username = chatdata.attr('username'); xmppchat.username = chatdata.attr('username');
xmppchat.base_url = chatdata.attr('base_url'); xmppchat.base_url = chatdata.attr('base_url');
$.hook(['show', 'hide']); $.hook(['show', 'hide']);
chat_id = 'online-users-container'; $("div#online-users-container")
$("div#"+chat_id)
.bind('onaftershow', function (e) { .bind('onaftershow', function (e) {
xmppchat.addChatToCookie(chat_id); xmppchat.addChatToCookie('online-users-container');
xmppchat.chats.push(chat_id); xmppchat.chats.push('online-users-container');
}); });
$('a.user-details-toggle').live('click', function (e) { $('a.user-details-toggle').live('click', function (e) {
var userid = $(this).parent().attr('data-userid'), var userid = $(this).parent().attr('data-userid'),
$field = $('[name="message"]:input', $(this).parent()[0]), $field = $('[name="message"]:input', $(this).parent()[0]),
recipient = $field.attr('data-recipient'); recipient = $field.attr('data-recipient');
xmppchat.startChat('chatbox_'+userid, recipient); xmppchat.startChat(recipient);
e.preventDefault(); e.preventDefault();
}); });
/* jQuery.cookie('chats-open-'+xmppchat.username, null, {path: '/'});
jQuery.cookie('chats-open-'+username, null, {path: '/'});
if (cookie) { if (cookie) {
open_chats = cookie.split('|'); open_chats = cookie.split('|');
for (var i=0; i<open_chats.length; i++) { for (var i=0; i<open_chats.length; i++) {
chat_id = open_chats[i]; xmppchat.createChat(open_chats[i], 1);
if (chat_id) {
createChat(chat_id, 1);
}
} }
} }
*/
}); });
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