Commit d4673b94 authored by JC Brand's avatar JC Brand

Move the remaining functions into the module. Renamed the module to xmppchat.

parent 790b9451
var babblexmpp = (function ($, console) {
var xmppchat = (function ($, console) {
var obj = {};
obj.chats = [];
obj.chat_focus = [];
obj.sanitizePath = function (call) {
return babblexmpp.base_url + call;
return xmppchat.base_url + call;
};
obj.hash =function (str) {
......@@ -26,7 +26,7 @@ var babblexmpp = (function ($, console) {
};
obj.getMinimizedChats = function () {
var cookie = $.cookie('chats_minimized_'+babblexmpp.username);
var cookie = $.cookie('chats_minimized_'+xmppchat.username);
if (cookie) {
return cookie.split(/\|/);
}
......@@ -35,8 +35,8 @@ var babblexmpp = (function ($, console) {
obj.positionNewChat = function (chatbox) {
var open_chats = 0;
for (var i=0; i<babblexmpp.chats.length; i++) {
if ($("#"+babblexmpp.hash(babblexmpp.chats[i])).css('display') != 'none') {
for (var i=0; i<xmppchat.chats.length; i++) {
if ($("#"+xmppchat.hash(xmppchat.chats[i])).css('display') != 'none') {
open_chats++;
}
}
......@@ -50,7 +50,7 @@ var babblexmpp = (function ($, console) {
};
obj.handleChatEvents = function (chat_id) {
var chat_area = $("#"+babblexmpp.hash(chat_id)+" .chat-textarea"),
var chat_area = $("#"+xmppchat.hash(chat_id)+" .chat-textarea"),
chat_type = chat_id.split('_')[0],
that = this;
......@@ -62,7 +62,7 @@ var babblexmpp = (function ($, console) {
that.chat_focus[chat_id] = true;
chat_area.addClass('chat-textarea-'+chat_type+'-selected');
});
var chatbox = $("#"+babblexmpp.hash(chat_id));
var chatbox = $("#"+xmppchat.hash(chat_id));
chatbox.click(function() {
if (chatbox.find('.chat-content').css('display') != 'none') {
chatbox.find('.chat-textarea').focus();
......@@ -71,14 +71,14 @@ var babblexmpp = (function ($, console) {
};
obj.createChatBox = function (chat_id, jid) {
var path = babblexmpp.sanitizePath('/@@render_chat_box');
var path = xmppchat.sanitizePath('/@@render_chat_box');
$.ajax({
url: path,
cache: false,
async: false,
data: {
chat_id: chat_id,
box_id: babblexmpp.hash(chat_id),
box_id: xmppchat.hash(chat_id),
jid: jid,
tzoffset: -(new Date().getTimezoneOffset())
},
......@@ -105,27 +105,16 @@ var babblexmpp = (function ($, console) {
}
}
});
return $('#'+babblexmpp.hash(chat_id));
return $('#'+xmppchat.hash(chat_id));
};
obj.createChat = function (chat_id, minimize, jid) {
var cookie = $.cookie('chats-open-'+this.username),
open_chats = [],
new_cookie,
chat_content;
console.log('createChat: chat_id is ' + chat_id);
if (cookie) {
open_chats = cookie.split('|');
}
if (!(chat_id in this.oc(open_chats))) {
// Update the cookie if this new chat is not yet in it.
open_chats.push(chat_id);
new_cookie = open_chats.join('|');
$.cookie('chats-open-'+this.username, new_cookie, {path: '/'});
console.log('createChat: updated cookie = ' + new_cookie + '\n');
}
chat_content, chatbox;
this.addChatToCookie(chat_id);
var chatbox = $("#"+this.hash(chat_id));
chatbox = $("#"+this.hash(chat_id));
if (chatbox.length > 0) {
// The chatbox exists, merely hidden
if (chatbox.css('display') == 'none') {
......@@ -158,9 +147,6 @@ var babblexmpp = (function ($, console) {
if (chat_content.length) {
chat_content.scrollTop(chat_content[0].scrollHeight);
}
else {
console.log('createChat: could not get .chat-content');
}
return chatbox;
};
......@@ -186,6 +172,24 @@ var babblexmpp = (function ($, console) {
}
};
obj.addChatToCookie = function (chat_id) {
var cookie = jQuery.cookie('chats-open-'+xmppchat.username),
new_cookie,
open_chats = [];
if (cookie) {
open_chats = cookie.split('|');
}
if (!(chat_id in this.oc(open_chats))) {
// Update the cookie if this new chat is not yet in it.
open_chats.push(chat_id);
new_cookie = open_chats.join('|');
jQuery.cookie('chats-open-'+xmppchat.username, new_cookie, {path: '/'});
console.log('updated cookie = ' + new_cookie + '\n');
}
this.chats.push(chat_id);
};
obj.receiveMessage = function (event) {
var user_id = Strophe.getNodeFromJid(event.from),
jid = Strophe.getBareJidFromJid(event.from),
......@@ -228,11 +232,63 @@ var babblexmpp = (function ($, console) {
jarnxmpp.UI.updateMsgCounter();
});
};
return obj;
})(jQuery, console || {log: function(){}} );
function keypressed(event, textarea, audience, hashed_id, chat_type) {
obj.closeChat = function (chat_id, audience) {
jQuery('#'+xmppchat.hash(chat_id)).css('display','none');
xmppchat.reorderChats();
var cookie = jQuery.cookie('chats-open-'+xmppchat.username),
open_chats = [],
new_chats = [];
if (cookie) {
open_chats = cookie.split('|');
}
for (var i=0; i < open_chats.length; i++) {
if (open_chats[i] != chat_id) {
new_chats.push(open_chats[i]);
}
}
if (new_chats.length) {
jQuery.cookie('chats-open-'+xmppchat.username, new_chats.join('|'), {path: '/'});
}
else {
jQuery.cookie('chats-open-'+xmppchat.username, null, {path: '/'});
}
this.chats.pop(chat_id);
};
obj.toggleChat = function (chat_id) {
var minimized_chats = xmppchat.getMinimizedChats();
var hashed_id = xmppchat.hash(chat_id);
var new_cookie;
if (jQuery('#'+hashed_id+' .chat-content').css('display') == 'none') {
// Chat will be maximized
new_cookie = [];
for (var i=0; i < minimized_chats.length; i++) {
if (minimized_chats[i] != chat_id) {
new_cookie.push(minimized_chats[i]);
}
}
jQuery.cookie('chats_minimized_'+xmppchat.username, new_cookie.join('|'));
var chat_content = jQuery('#'+hashed_id+' .chat-content');
chat_content.css('display','block');
chat_content.scrollTop(chat_content[0].scrollHeight);
jQuery('#'+hashed_id+' .chat-head').removeClass('chat-head-minimized-with-messages');
jQuery('#'+hashed_id+' .chat-input').css('display','block');
}
else {
// Chat will be minimized
if (!(chat_id in xmppchat.oc(minimized_chats))) {
new_cookie = chat_id;
new_cookie += '|'+minimized_chats.join('|');
jQuery.cookie('chats_minimized_'+xmppchat.username, new_cookie);
}
jQuery('#'+hashed_id+' .chat-content').css('display','none');
jQuery('#'+hashed_id+' .chat-input').css('display','none');
}
};
obj.keyPressed = function (event, textarea, audience, hashed_id, chat_type) {
if(event.keyCode == 13 && !event.shiftKey) {
var textbox = jQuery(textarea);
var message = textbox.val();
......@@ -275,115 +331,50 @@ function keypressed(event, textarea, audience, hashed_id, chat_type) {
else {
jQuery(textarea).css('overflow','auto');
}
}
function closeChat(chat_id, audience) {
jQuery('#'+babblexmpp.hash(chat_id)).css('display','none');
babblexmpp.reorderChats();
var cookie = jQuery.cookie('chats-open-'+babblexmpp.username);
var open_chats = [];
if (cookie) {
open_chats = cookie.split('|');
}
new_chats = [];
for (var i=0; i < open_chats.length; i++) {
if (open_chats[i] != chat_id) {
new_chats.push(open_chats[i]);
}
}
if (new_chats.length) {
jQuery.cookie('chats-open-'+babblexmpp.username, new_chats.join('|'), {path: '/'});
}
else {
jQuery.cookie('chats-open-'+babblexmpp.username, null, {path: '/'});
}
}
};
function toggleChat(chat_id) {
var minimized_chats = babblexmpp.getMinimizedChats();
var hashed_id = babblexmpp.hash(chat_id);
var new_cookie;
if (jQuery('#'+hashed_id+' .chat-content').css('display') == 'none') {
// Chat will be maximized
new_cookie = [];
for (var i=0; i < minimized_chats.length; i++) {
if (minimized_chats[i] != chat_id) {
new_cookie.push(minimized_chats[i]);
}
}
jQuery.cookie('chats_minimized_'+babblexmpp.username, new_cookie.join('|'));
var chat_content = jQuery('#'+hashed_id+' .chat-content');
chat_content.css('display','block');
chat_content.scrollTop(chat_content[0].scrollHeight);
jQuery('#'+hashed_id+' .chat-head').removeClass('chat-head-minimized-with-messages');
jQuery('#'+hashed_id+' .chat-input').css('display','block');
}
else {
// Chat will be minimized
if (!(chat_id in babblexmpp.oc(minimized_chats))) {
new_cookie = chat_id;
new_cookie += '|'+minimized_chats.join('|');
jQuery.cookie('chats_minimized_'+babblexmpp.username, new_cookie);
}
jQuery('#'+hashed_id+' .chat-content').css('display','none');
jQuery('#'+hashed_id+' .chat-input').css('display','none');
}
}
return obj;
})(jQuery, console || {log: function(){}} );
$(document).unbind('jarnxmpp.message');
$(document).bind('jarnxmpp.message', function (event) {
babblexmpp.receiveMessage(event);
xmppchat.receiveMessage(event);
});
$(document).ready(function () {
var chatdata = jQuery('span#babble-client-chatdata');
babblexmpp.username = chatdata.attr('username');
babblexmpp.base_url = chatdata.attr('base_url');
var cookie = jQuery.cookie('chats-open-'+chatdata.attr('username'));
var open_chats = [], chat_id;
xmppchat.username = chatdata.attr('username');
xmppchat.base_url = chatdata.attr('base_url');
$.hook(['show', 'hide']);
chat_id = 'online-users-container';
$("div#"+chat_id)
.bind('onbeforeshow', function (e) { })
.bind('onshow', function (e) { })
.bind('onaftershow', function (e) {
var cookie = jQuery.cookie('chats-open-'+babblexmpp.username);
var open_chats = [];
if (cookie) {
open_chats = cookie.split('|');
}
if (!(chat_id in babblexmpp.oc(open_chats))) {
// Update the cookie if this new chat is not yet in it.
open_chats.push(chat_id);
var new_cookie = open_chats.join('|');
jQuery.cookie('chats-open-'+babblexmpp.username, new_cookie, {path: '/'});
console.log('updated cookie = ' + new_cookie + '\n');
}
babblexmpp.chats.push(chat_id);
})
.bind('onafterhide', function (e) {
var cookie = jQuery.cookie('chats-open-'+babblexmpp.username);
if (cookie) {
open_chats = cookie.split('|');
}
new_chats = [];
for (var i=0; i < open_chats.length; i++) {
if (open_chats[i] != chat_id) {
new_chats.push(open_chats[i]);
}
}
if (new_chats.length) {
jQuery.cookie('chats-open-'+babblexmpp.username, new_chats.join('|'), {path: '/'});
}
else {
jQuery.cookie('chats-open-'+babblexmpp.username, null, {path: '/'});
}
xmppchat.addChatToCookie(chat_id);
xmppchat.chats.push(chat_id);
});
$('a.user-details-toggle').live('click', function (e) {
var userid = $(this).parent().attr('data-userid'),
$field = $('[name="message"]:input', $(this).parent()[0]),
recipient = $field.attr('data-recipient');
babblexmpp.startChat('chatbox_'+userid, recipient);
xmppchat.startChat('chatbox_'+userid, recipient);
e.preventDefault();
});
/*
jQuery.cookie('chats-open-'+username, null, {path: '/'});
if (cookie) {
open_chats = cookie.split('|');
for (var i=0; i<open_chats.length; i++) {
chat_id = open_chats[i];
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