2014-04-13 12:30:47 +00:00
|
|
|
/* global $, Util, connection, nickname:true, getVideoSize, getVideoPosition, showToolbar, processReplacements */
|
2014-02-18 19:11:27 +00:00
|
|
|
/**
|
|
|
|
* Chat related user interface.
|
|
|
|
*/
|
|
|
|
var Chat = (function (my) {
|
|
|
|
var notificationInterval = false;
|
|
|
|
var unreadMessages = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes chat related interface.
|
|
|
|
*/
|
|
|
|
my.init = function () {
|
|
|
|
var storedDisplayName = window.localStorage.displayname;
|
|
|
|
if (storedDisplayName) {
|
|
|
|
nickname = storedDisplayName;
|
|
|
|
|
|
|
|
Chat.setChatConversationMode(true);
|
|
|
|
}
|
|
|
|
|
2014-04-13 12:30:47 +00:00
|
|
|
$('#nickinput').keydown(function (event) {
|
2014-03-01 07:39:39 +00:00
|
|
|
if (event.keyCode === 13) {
|
2014-02-18 19:11:27 +00:00
|
|
|
event.preventDefault();
|
2014-02-26 15:19:39 +00:00
|
|
|
var val = Util.escapeHtml(this.value);
|
2014-02-18 19:11:27 +00:00
|
|
|
this.value = '';
|
|
|
|
if (!nickname) {
|
|
|
|
nickname = val;
|
|
|
|
window.localStorage.displayname = nickname;
|
|
|
|
|
|
|
|
connection.emuc.addDisplayNameToPresence(nickname);
|
|
|
|
connection.emuc.sendPresence();
|
|
|
|
|
|
|
|
Chat.setChatConversationMode(true);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-04-13 12:30:47 +00:00
|
|
|
$('#usermsg').keydown(function (event) {
|
2014-03-01 07:39:39 +00:00
|
|
|
if (event.keyCode === 13) {
|
2014-02-18 19:11:27 +00:00
|
|
|
event.preventDefault();
|
2014-06-20 09:00:49 +00:00
|
|
|
var value = this.value;
|
2014-02-18 19:11:27 +00:00
|
|
|
$('#usermsg').val('').trigger('autosize.resize');
|
|
|
|
this.focus();
|
2014-06-20 09:00:49 +00:00
|
|
|
var command = new CommandsProcessor(value);
|
|
|
|
if(command.isCommand())
|
|
|
|
{
|
|
|
|
command.processCommand();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var message = Util.escapeHtml(value);
|
|
|
|
connection.emuc.sendMessage(message, nickname);
|
|
|
|
}
|
2014-02-18 19:11:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-04-13 12:30:47 +00:00
|
|
|
var onTextAreaResize = function () {
|
2014-02-18 19:11:27 +00:00
|
|
|
resizeChatConversation();
|
|
|
|
scrollChatToBottom();
|
|
|
|
};
|
|
|
|
$('#usermsg').autosize({callback: onTextAreaResize});
|
|
|
|
|
|
|
|
$("#chatspace").bind("shown",
|
2014-04-13 12:30:47 +00:00
|
|
|
function () {
|
2014-02-18 19:11:27 +00:00
|
|
|
unreadMessages = 0;
|
|
|
|
setVisualNotification(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends the given message to the chat conversation.
|
|
|
|
*/
|
2014-02-19 14:32:22 +00:00
|
|
|
my.updateChatConversation = function (from, displayName, message) {
|
2014-02-18 19:11:27 +00:00
|
|
|
var divClassName = '';
|
|
|
|
|
2014-03-01 07:39:39 +00:00
|
|
|
if (connection.emuc.myroomjid === from) {
|
2014-02-18 19:11:27 +00:00
|
|
|
divClassName = "localuser";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
divClassName = "remoteuser";
|
|
|
|
|
|
|
|
if (!$('#chatspace').is(":visible")) {
|
|
|
|
unreadMessages++;
|
|
|
|
Util.playSoundNotification('chatNotification');
|
|
|
|
setVisualNotification(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//replace links and smileys
|
2014-02-26 15:19:39 +00:00
|
|
|
var escMessage = Util.escapeHtml(message);
|
|
|
|
var escDisplayName = Util.escapeHtml(displayName);
|
|
|
|
message = processReplacements(escMessage);
|
2014-02-18 19:11:27 +00:00
|
|
|
|
2014-04-13 12:30:47 +00:00
|
|
|
$('#chatconversation').append('<div class="' + divClassName + '"><b>' +
|
|
|
|
escDisplayName + ': </b>' +
|
|
|
|
message + '</div>');
|
2014-02-18 19:11:27 +00:00
|
|
|
$('#chatconversation').animate(
|
|
|
|
{ scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
|
|
|
|
};
|
|
|
|
|
2014-06-20 09:00:49 +00:00
|
|
|
/**
|
|
|
|
* Appends error message to the conversation
|
|
|
|
* @param errorMessage the received error message.
|
|
|
|
* @param originalText the original message.
|
|
|
|
*/
|
|
|
|
my.chatAddError = function(errorMessage, originalText)
|
|
|
|
{
|
|
|
|
errorMessage = Util.escapeHtml(errorMessage);
|
|
|
|
originalText = Util.escapeHtml(originalText);
|
|
|
|
|
|
|
|
$('#chatconversation').append('<div class="errorMessage"><b>Error: </b>'
|
|
|
|
+ 'Your message' + (originalText? (' \"'+ originalText + '\"') : "")
|
|
|
|
+ ' was not sent.' + (errorMessage? (' Reason: ' + errorMessage) : '')
|
|
|
|
+ '</div>');
|
|
|
|
$('#chatconversation').animate(
|
|
|
|
{ scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
|
2014-07-14 10:33:57 +00:00
|
|
|
};
|
2014-06-20 09:00:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the subject to the UI
|
|
|
|
* @param subject the subject
|
|
|
|
*/
|
|
|
|
my.chatSetSubject = function(subject)
|
|
|
|
{
|
|
|
|
if(subject)
|
|
|
|
subject = subject.trim();
|
|
|
|
$('#subject').html(linkify(Util.escapeHtml(subject)));
|
|
|
|
if(subject == "")
|
|
|
|
{
|
|
|
|
$("#subject").css({display: "none"});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$("#subject").css({display: "block"});
|
|
|
|
}
|
2014-07-13 18:12:38 +00:00
|
|
|
};
|
2014-06-20 09:00:49 +00:00
|
|
|
|
2014-02-18 19:11:27 +00:00
|
|
|
/**
|
|
|
|
* Opens / closes the chat area.
|
|
|
|
*/
|
|
|
|
my.toggleChat = function () {
|
|
|
|
var chatspace = $('#chatspace');
|
|
|
|
var videospace = $('#videospace');
|
|
|
|
|
2014-03-26 10:33:46 +00:00
|
|
|
var chatSize = (chatspace.is(":visible")) ? [0, 0] : Chat.getChatSize();
|
|
|
|
var videospaceWidth = window.innerWidth - chatSize[0];
|
|
|
|
var videospaceHeight = window.innerHeight;
|
|
|
|
var videoSize
|
|
|
|
= getVideoSize(null, null, videospaceWidth, videospaceHeight);
|
|
|
|
var videoWidth = videoSize[0];
|
|
|
|
var videoHeight = videoSize[1];
|
2014-04-13 12:30:47 +00:00
|
|
|
var videoPosition = getVideoPosition(videoWidth,
|
|
|
|
videoHeight,
|
|
|
|
videospaceWidth,
|
|
|
|
videospaceHeight);
|
2014-03-26 10:33:46 +00:00
|
|
|
var horizontalIndent = videoPosition[0];
|
|
|
|
var verticalIndent = videoPosition[1];
|
2014-02-18 19:11:27 +00:00
|
|
|
|
2014-07-14 10:33:57 +00:00
|
|
|
var thumbnailSize = VideoLayout.calculateThumbnailSize(videospaceWidth);
|
|
|
|
var thumbnailsWidth = thumbnailSize[0];
|
|
|
|
var thumbnailsHeight = thumbnailSize[1];
|
|
|
|
|
2014-02-18 19:11:27 +00:00
|
|
|
if (chatspace.is(":visible")) {
|
2014-04-13 12:30:47 +00:00
|
|
|
videospace.animate({right: chatSize[0],
|
2014-03-26 10:33:46 +00:00
|
|
|
width: videospaceWidth,
|
|
|
|
height: videospaceHeight},
|
2014-02-18 19:11:27 +00:00
|
|
|
{queue: false,
|
2014-03-26 10:33:46 +00:00
|
|
|
duration: 500});
|
|
|
|
|
2014-07-14 10:33:57 +00:00
|
|
|
$('#remoteVideos').animate({height: thumbnailsHeight},
|
|
|
|
{queue: false,
|
|
|
|
duration: 500});
|
|
|
|
|
|
|
|
$('#remoteVideos>span').animate({height: thumbnailsHeight,
|
|
|
|
width: thumbnailsWidth},
|
|
|
|
{queue: false,
|
2014-07-22 13:12:48 +00:00
|
|
|
duration: 500,
|
|
|
|
complete: function() {
|
|
|
|
$(document).trigger(
|
|
|
|
"remotevideo.resized",
|
|
|
|
[thumbnailsWidth,
|
|
|
|
thumbnailsHeight]);
|
|
|
|
}});
|
2014-07-14 10:33:57 +00:00
|
|
|
|
2014-03-26 10:33:46 +00:00
|
|
|
$('#largeVideoContainer').animate({ width: videospaceWidth,
|
|
|
|
height: videospaceHeight},
|
|
|
|
{queue: false,
|
|
|
|
duration: 500
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#largeVideo').animate({ width: videoWidth,
|
|
|
|
height: videoHeight,
|
|
|
|
top: verticalIndent,
|
|
|
|
bottom: verticalIndent,
|
|
|
|
left: horizontalIndent,
|
|
|
|
right: horizontalIndent},
|
|
|
|
{ queue: false,
|
|
|
|
duration: 500
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#chatspace').hide("slide", { direction: "right",
|
|
|
|
queue: false,
|
|
|
|
duration: 500});
|
2014-02-18 19:11:27 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-07-24 11:39:46 +00:00
|
|
|
// Undock the toolbar when the chat is shown and if we're in a
|
|
|
|
// video mode.
|
|
|
|
if (VideoLayout.isLargeVideoVisible())
|
|
|
|
Toolbar.dockToolbar(false);
|
2014-07-13 18:12:38 +00:00
|
|
|
|
2014-03-26 10:33:46 +00:00
|
|
|
videospace.animate({right: chatSize[0],
|
|
|
|
width: videospaceWidth,
|
|
|
|
height: videospaceHeight},
|
2014-02-18 19:11:27 +00:00
|
|
|
{queue: false,
|
|
|
|
duration: 500,
|
2014-04-13 12:30:47 +00:00
|
|
|
complete: function () {
|
2014-02-18 19:11:27 +00:00
|
|
|
scrollChatToBottom();
|
|
|
|
chatspace.trigger('shown');
|
|
|
|
}
|
|
|
|
});
|
2014-03-26 10:33:46 +00:00
|
|
|
|
2014-07-14 10:33:57 +00:00
|
|
|
$('#remoteVideos').animate({height: thumbnailsHeight},
|
|
|
|
{queue: false,
|
|
|
|
duration: 500});
|
|
|
|
|
|
|
|
$('#remoteVideos>span').animate({height: thumbnailsHeight,
|
|
|
|
width: thumbnailsWidth},
|
|
|
|
{queue: false,
|
2014-07-22 13:12:48 +00:00
|
|
|
duration: 500,
|
|
|
|
complete: function() {
|
|
|
|
$(document).trigger(
|
|
|
|
"remotevideo.resized",
|
|
|
|
[thumbnailsWidth, thumbnailsHeight]);
|
|
|
|
}});
|
2014-07-14 10:33:57 +00:00
|
|
|
|
2014-03-26 10:33:46 +00:00
|
|
|
$('#largeVideoContainer').animate({ width: videospaceWidth,
|
|
|
|
height: videospaceHeight},
|
|
|
|
{queue: false,
|
|
|
|
duration: 500
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#largeVideo').animate({ width: videoWidth,
|
|
|
|
height: videoHeight,
|
|
|
|
top: verticalIndent,
|
|
|
|
bottom: verticalIndent,
|
|
|
|
left: horizontalIndent,
|
|
|
|
right: horizontalIndent},
|
|
|
|
{queue: false,
|
|
|
|
duration: 500
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#chatspace').show("slide", { direction: "right",
|
|
|
|
queue: false,
|
|
|
|
duration: 500});
|
2014-02-18 19:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request the focus in the nickname field or the chat input field.
|
2014-03-01 07:39:39 +00:00
|
|
|
if ($('#nickname').css('visibility') === 'visible')
|
2014-02-18 19:11:27 +00:00
|
|
|
$('#nickinput').focus();
|
|
|
|
else {
|
|
|
|
$('#usermsg').focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the chat conversation mode.
|
|
|
|
*/
|
|
|
|
my.setChatConversationMode = function (isConversationMode) {
|
|
|
|
if (isConversationMode) {
|
2014-04-13 12:30:47 +00:00
|
|
|
$('#nickname').css({visibility: 'hidden'});
|
|
|
|
$('#chatconversation').css({visibility: 'visible'});
|
|
|
|
$('#usermsg').css({visibility: 'visible'});
|
2014-02-18 19:11:27 +00:00
|
|
|
$('#usermsg').focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes the chat area.
|
|
|
|
*/
|
|
|
|
my.resizeChat = function () {
|
2014-03-26 10:33:46 +00:00
|
|
|
var chatSize = Chat.getChatSize();
|
|
|
|
|
|
|
|
$('#chatspace').width(chatSize[0]);
|
|
|
|
$('#chatspace').height(chatSize[1]);
|
|
|
|
|
|
|
|
resizeChatConversation();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the size of the chat.
|
|
|
|
*/
|
2014-04-13 12:30:47 +00:00
|
|
|
my.getChatSize = function () {
|
2014-02-18 19:11:27 +00:00
|
|
|
var availableHeight = window.innerHeight;
|
|
|
|
var availableWidth = window.innerWidth;
|
|
|
|
|
|
|
|
var chatWidth = 200;
|
2014-04-13 12:30:47 +00:00
|
|
|
if (availableWidth * 0.2 < 200)
|
|
|
|
chatWidth = availableWidth * 0.2;
|
2014-02-18 19:11:27 +00:00
|
|
|
|
2014-03-26 10:33:46 +00:00
|
|
|
return [chatWidth, availableHeight];
|
2014-02-18 19:11:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes the chat conversation.
|
|
|
|
*/
|
|
|
|
function resizeChatConversation() {
|
|
|
|
var usermsgStyleHeight = document.getElementById("usermsg").style.height;
|
|
|
|
var usermsgHeight = usermsgStyleHeight
|
|
|
|
.substring(0, usermsgStyleHeight.indexOf('px'));
|
|
|
|
|
2014-03-26 10:33:46 +00:00
|
|
|
$('#usermsg').width($('#chatspace').width() - 10);
|
2014-02-18 19:11:27 +00:00
|
|
|
$('#chatconversation').width($('#chatspace').width() - 10);
|
|
|
|
$('#chatconversation')
|
2014-03-26 10:33:46 +00:00
|
|
|
.height(window.innerHeight - 10 - parseInt(usermsgHeight));
|
2014-04-13 12:30:47 +00:00
|
|
|
}
|
2014-02-18 19:11:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows/hides a visual notification, indicating that a message has arrived.
|
|
|
|
*/
|
|
|
|
function setVisualNotification(show) {
|
|
|
|
var unreadMsgElement = document.getElementById('unreadMessages');
|
|
|
|
|
2014-03-26 10:33:46 +00:00
|
|
|
var glower = $('#chatButton');
|
|
|
|
|
2014-02-18 19:11:27 +00:00
|
|
|
if (unreadMessages) {
|
|
|
|
unreadMsgElement.innerHTML = unreadMessages.toString();
|
|
|
|
|
2014-07-13 18:12:38 +00:00
|
|
|
Toolbar.dockToolbar(true);
|
2014-03-26 10:33:46 +00:00
|
|
|
|
2014-02-18 19:11:27 +00:00
|
|
|
var chatButtonElement
|
2014-03-26 10:33:46 +00:00
|
|
|
= document.getElementById('chatButton').parentNode;
|
2014-04-13 12:30:47 +00:00
|
|
|
var leftIndent = (Util.getTextWidth(chatButtonElement) -
|
|
|
|
Util.getTextWidth(unreadMsgElement)) / 2;
|
|
|
|
var topIndent = (Util.getTextHeight(chatButtonElement) -
|
|
|
|
Util.getTextHeight(unreadMsgElement)) / 2 - 3;
|
2014-02-18 19:11:27 +00:00
|
|
|
|
|
|
|
unreadMsgElement.setAttribute(
|
|
|
|
'style',
|
2014-04-13 12:30:47 +00:00
|
|
|
'top:' + topIndent +
|
|
|
|
'; left:' + leftIndent + ';');
|
2014-03-26 10:33:46 +00:00
|
|
|
|
|
|
|
if (!glower.hasClass('icon-chat-simple')) {
|
|
|
|
glower.removeClass('icon-chat');
|
|
|
|
glower.addClass('icon-chat-simple');
|
|
|
|
}
|
2014-02-18 19:11:27 +00:00
|
|
|
}
|
2014-03-26 10:33:46 +00:00
|
|
|
else {
|
2014-02-18 19:11:27 +00:00
|
|
|
unreadMsgElement.innerHTML = '';
|
2014-03-26 10:33:46 +00:00
|
|
|
glower.removeClass('icon-chat-simple');
|
|
|
|
glower.addClass('icon-chat');
|
|
|
|
}
|
2014-02-18 19:11:27 +00:00
|
|
|
|
|
|
|
if (show && !notificationInterval) {
|
2014-04-13 12:30:47 +00:00
|
|
|
notificationInterval = window.setInterval(function () {
|
2014-02-18 19:11:27 +00:00
|
|
|
glower.toggleClass('active');
|
|
|
|
}, 800);
|
|
|
|
}
|
|
|
|
else if (!show && notificationInterval) {
|
|
|
|
window.clearInterval(notificationInterval);
|
|
|
|
notificationInterval = false;
|
|
|
|
glower.removeClass('active');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrolls chat to the bottom.
|
|
|
|
*/
|
|
|
|
function scrollChatToBottom() {
|
2014-04-13 12:30:47 +00:00
|
|
|
setTimeout(function () {
|
2014-02-18 19:11:27 +00:00
|
|
|
$('#chatconversation').scrollTop(
|
|
|
|
$('#chatconversation')[0].scrollHeight);
|
|
|
|
}, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
return my;
|
2014-03-01 07:39:39 +00:00
|
|
|
}(Chat || {}));
|