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 = {}; var obj = {};
obj.chats = []; obj.chats = [];
obj.chat_focus = []; obj.chat_focus = [];
obj.sanitizePath = function (call) { obj.sanitizePath = function (call) {
return babblexmpp.base_url + call; return xmppchat.base_url + call;
}; };
obj.hash =function (str) { obj.hash =function (str) {
...@@ -26,7 +26,7 @@ var babblexmpp = (function ($, console) { ...@@ -26,7 +26,7 @@ var babblexmpp = (function ($, console) {
}; };
obj.getMinimizedChats = function () { obj.getMinimizedChats = function () {
var cookie = $.cookie('chats_minimized_'+babblexmpp.username); var cookie = $.cookie('chats_minimized_'+xmppchat.username);
if (cookie) { if (cookie) {
return cookie.split(/\|/); return cookie.split(/\|/);
} }
...@@ -35,8 +35,8 @@ var babblexmpp = (function ($, console) { ...@@ -35,8 +35,8 @@ var babblexmpp = (function ($, console) {
obj.positionNewChat = function (chatbox) { obj.positionNewChat = function (chatbox) {
var open_chats = 0; var open_chats = 0;
for (var i=0; i<babblexmpp.chats.length; i++) { for (var i=0; i<xmppchat.chats.length; i++) {
if ($("#"+babblexmpp.hash(babblexmpp.chats[i])).css('display') != 'none') { if ($("#"+xmppchat.hash(xmppchat.chats[i])).css('display') != 'none') {
open_chats++; open_chats++;
} }
} }
...@@ -50,7 +50,7 @@ var babblexmpp = (function ($, console) { ...@@ -50,7 +50,7 @@ var babblexmpp = (function ($, console) {
}; };
obj.handleChatEvents = function (chat_id) { 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], chat_type = chat_id.split('_')[0],
that = this; that = this;
...@@ -62,7 +62,7 @@ var babblexmpp = (function ($, console) { ...@@ -62,7 +62,7 @@ var babblexmpp = (function ($, console) {
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 = $("#"+babblexmpp.hash(chat_id)); var chatbox = $("#"+xmppchat.hash(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();
...@@ -71,14 +71,14 @@ var babblexmpp = (function ($, console) { ...@@ -71,14 +71,14 @@ var babblexmpp = (function ($, console) {
}; };
obj.createChatBox = function (chat_id, jid) { obj.createChatBox = function (chat_id, jid) {
var path = babblexmpp.sanitizePath('/@@render_chat_box'); var path = xmppchat.sanitizePath('/@@render_chat_box');
$.ajax({ $.ajax({
url: path, url: path,
cache: false, cache: false,
async: false, async: false,
data: { data: {
chat_id: chat_id, chat_id: chat_id,
box_id: babblexmpp.hash(chat_id), box_id: xmppchat.hash(chat_id),
jid: jid, jid: jid,
tzoffset: -(new Date().getTimezoneOffset()) tzoffset: -(new Date().getTimezoneOffset())
}, },
...@@ -105,27 +105,16 @@ var babblexmpp = (function ($, console) { ...@@ -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) { obj.createChat = function (chat_id, minimize, jid) {
var cookie = $.cookie('chats-open-'+this.username), var cookie = $.cookie('chats-open-'+this.username),
open_chats = [], chat_content, chatbox;
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');
}
var chatbox = $("#"+this.hash(chat_id)); this.addChatToCookie(chat_id);
chatbox = $("#"+this.hash(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') {
...@@ -158,9 +147,6 @@ var babblexmpp = (function ($, console) { ...@@ -158,9 +147,6 @@ var babblexmpp = (function ($, console) {
if (chat_content.length) { if (chat_content.length) {
chat_content.scrollTop(chat_content[0].scrollHeight); chat_content.scrollTop(chat_content[0].scrollHeight);
} }
else {
console.log('createChat: could not get .chat-content');
}
return chatbox; return chatbox;
}; };
...@@ -186,6 +172,24 @@ var babblexmpp = (function ($, console) { ...@@ -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) { obj.receiveMessage = function (event) {
var user_id = Strophe.getNodeFromJid(event.from), var user_id = Strophe.getNodeFromJid(event.from),
jid = Strophe.getBareJidFromJid(event.from), jid = Strophe.getBareJidFromJid(event.from),
...@@ -228,162 +232,149 @@ var babblexmpp = (function ($, console) { ...@@ -228,162 +232,149 @@ var babblexmpp = (function ($, console) {
jarnxmpp.UI.updateMsgCounter(); jarnxmpp.UI.updateMsgCounter();
}); });
}; };
return obj;
})(jQuery, console || {log: function(){}} );
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);
};
function keypressed(event, textarea, audience, hashed_id, chat_type) { obj.toggleChat = function (chat_id) {
if(event.keyCode == 13 && !event.shiftKey) { var minimized_chats = xmppchat.getMinimizedChats();
var textbox = jQuery(textarea); var hashed_id = xmppchat.hash(chat_id);
var message = textbox.val(); var new_cookie;
var form = textbox.parent(); if (jQuery('#'+hashed_id+' .chat-content').css('display') == 'none') {
form.submit(); // Chat will be maximized
message = message.replace(/^\s+|\s+jQuery/g,""); new_cookie = [];
textbox.val('').focus().css('height','44px'); for (var i=0; i < minimized_chats.length; i++) {
if (message !== '') { if (minimized_chats[i] != chat_id) {
message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;"); new_cookie.push(minimized_chats[i]);
list = message.match(/\b(http:\/\/www\.\S+\.\w+|www\.\S+\.\w+|http:\/\/(?=[^w]){3}\S+[\.:]\S+)[^ ]+\b/g);
if (list) {
for (i = 0; i < list.length; i++) {
message = message.replace( list[i], "<a target='_blank' href='" + escape( list[i] ) + "'>"+ list[i] + "</a>" );
} }
} }
var now = new Date(); jQuery.cookie('chats_minimized_'+xmppchat.username, new_cookie.join('|'));
var minutes = now.getMinutes().toString();
if (minutes.length==1) {minutes = '0'+minutes;}
var time = now.toLocaleTimeString().substring(0,5);
var chat_content = jQuery('#'+hashed_id+' .chat-content'); var chat_content = jQuery('#'+hashed_id+' .chat-content');
chat_content.append( chat_content.css('display','block');
'<div class="chat-message">' +
'<span class="chat-message-me">'+time+' me:&nbsp;&nbsp;</span>' +
'<span class="chat-message-content">'+message+'</span>' +
'</div>');
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('#'+hashed_id+' .chat-input').css('display','block');
var adjustedHeight = textarea.clientHeight; }
var maxHeight = 94; else {
if (maxHeight > adjustedHeight) { // Chat will be minimized
adjustedHeight = Math.max(textarea.scrollHeight, adjustedHeight); if (!(chat_id in xmppchat.oc(minimized_chats))) {
if (maxHeight) { new_cookie = chat_id;
adjustedHeight = Math.min(maxHeight, adjustedHeight); new_cookie += '|'+minimized_chats.join('|');
} jQuery.cookie('chats_minimized_'+xmppchat.username, new_cookie);
if (adjustedHeight > textarea.clientHeight) { }
jQuery(textarea).css('height',adjustedHeight+8 +'px'); jQuery('#'+hashed_id+' .chat-content').css('display','none');
} jQuery('#'+hashed_id+' .chat-input').css('display','none');
}
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) { obj.keyPressed = function (event, textarea, audience, hashed_id, chat_type) {
var minimized_chats = babblexmpp.getMinimizedChats(); if(event.keyCode == 13 && !event.shiftKey) {
var hashed_id = babblexmpp.hash(chat_id); var textbox = jQuery(textarea);
var new_cookie; var message = textbox.val();
if (jQuery('#'+hashed_id+' .chat-content').css('display') == 'none') { var form = textbox.parent();
// Chat will be maximized form.submit();
new_cookie = []; message = message.replace(/^\s+|\s+jQuery/g,"");
for (var i=0; i < minimized_chats.length; i++) { textbox.val('').focus().css('height','44px');
if (minimized_chats[i] != chat_id) { if (message !== '') {
new_cookie.push(minimized_chats[i]); message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
list = message.match(/\b(http:\/\/www\.\S+\.\w+|www\.\S+\.\w+|http:\/\/(?=[^w]){3}\S+[\.:]\S+)[^ ]+\b/g);
if (list) {
for (i = 0; i < list.length; i++) {
message = message.replace( list[i], "<a target='_blank' href='" + escape( list[i] ) + "'>"+ list[i] + "</a>" );
}
}
var now = new Date();
var minutes = now.getMinutes().toString();
if (minutes.length==1) {minutes = '0'+minutes;}
var time = now.toLocaleTimeString().substring(0,5);
var chat_content = jQuery('#'+hashed_id+' .chat-content');
chat_content.append(
'<div class="chat-message">' +
'<span class="chat-message-me">'+time+' me:&nbsp;&nbsp;</span>' +
'<span class="chat-message-content">'+message+'</span>' +
'</div>');
chat_content.scrollTop(chat_content[0].scrollHeight);
} }
} }
jQuery.cookie('chats_minimized_'+babblexmpp.username, new_cookie.join('|')); var adjustedHeight = textarea.clientHeight;
var chat_content = jQuery('#'+hashed_id+' .chat-content'); var maxHeight = 94;
chat_content.css('display','block'); if (maxHeight > adjustedHeight) {
chat_content.scrollTop(chat_content[0].scrollHeight); adjustedHeight = Math.max(textarea.scrollHeight, adjustedHeight);
jQuery('#'+hashed_id+' .chat-head').removeClass('chat-head-minimized-with-messages'); if (maxHeight) {
jQuery('#'+hashed_id+' .chat-input').css('display','block'); adjustedHeight = Math.min(maxHeight, adjustedHeight);
} }
else { if (adjustedHeight > textarea.clientHeight) {
// Chat will be minimized jQuery(textarea).css('height',adjustedHeight+8 +'px');
if (!(chat_id in babblexmpp.oc(minimized_chats))) { }
new_cookie = chat_id; }
new_cookie += '|'+minimized_chats.join('|'); else {
jQuery.cookie('chats_minimized_'+babblexmpp.username, new_cookie); jQuery(textarea).css('overflow','auto');
} }
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).unbind('jarnxmpp.message');
$(document).bind('jarnxmpp.message', function (event) { $(document).bind('jarnxmpp.message', function (event) {
babblexmpp.receiveMessage(event); xmppchat.receiveMessage(event);
}); });
$(document).ready(function () { $(document).ready(function () {
var chatdata = jQuery('span#babble-client-chatdata'); var chatdata = jQuery('span#babble-client-chatdata');
babblexmpp.username = chatdata.attr('username'); var cookie = jQuery.cookie('chats-open-'+chatdata.attr('username'));
babblexmpp.base_url = chatdata.attr('base_url'); var open_chats = [], chat_id;
xmppchat.username = chatdata.attr('username');
xmppchat.base_url = chatdata.attr('base_url');
$.hook(['show', 'hide']); $.hook(['show', 'hide']);
chat_id = 'online-users-container'; chat_id = 'online-users-container';
$("div#"+chat_id) $("div#"+chat_id)
.bind('onbeforeshow', function (e) { })
.bind('onshow', function (e) { })
.bind('onaftershow', function (e) { .bind('onaftershow', function (e) {
var cookie = jQuery.cookie('chats-open-'+babblexmpp.username); xmppchat.addChatToCookie(chat_id);
var open_chats = []; xmppchat.chats.push(chat_id);
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: '/'});
}
}); });
$('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');
babblexmpp.startChat('chatbox_'+userid, recipient); xmppchat.startChat('chatbox_'+userid, recipient);
e.preventDefault(); 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