2014-03-13 17:15:07 -04:00
|
|
|
"use strict";
|
2014-03-12 01:40:31 -04:00
|
|
|
/* globals app, config, define, socket, translator, templates, utils */
|
|
|
|
|
|
2014-03-13 17:15:07 -04:00
|
|
|
define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) {
|
2013-08-26 13:18:20 -04:00
|
|
|
|
2013-06-18 11:26:57 -04:00
|
|
|
var module = {};
|
|
|
|
|
|
2014-01-13 10:06:00 -05:00
|
|
|
module.prepareDOM = function() {
|
|
|
|
|
// Chats Dropdown
|
|
|
|
|
var chatsToggleEl = $('#chat_dropdown'),
|
2014-03-12 01:40:31 -04:00
|
|
|
chatsListEl = $('#chat-list');
|
2014-01-13 13:50:33 -05:00
|
|
|
|
2014-01-13 10:06:00 -05:00
|
|
|
chatsToggleEl.on('click', function() {
|
2014-03-12 01:40:31 -04:00
|
|
|
if (chatsToggleEl.parent().hasClass('open')) {
|
2014-01-13 10:06:00 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-16 20:14:09 -05:00
|
|
|
socket.emit('modules.chats.list', function(err, chats) {
|
2014-03-12 01:40:31 -04:00
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userObj;
|
|
|
|
|
|
2014-02-26 21:55:29 -05:00
|
|
|
chatsListEl.empty();
|
2014-03-12 01:40:31 -04:00
|
|
|
|
|
|
|
|
if (!chats.length) {
|
2014-04-13 22:16:43 -04:00
|
|
|
translator.translate('[[modules:chat.no_active]]', function(str) {
|
2014-03-12 01:40:31 -04:00
|
|
|
$('<li />')
|
2014-02-26 21:55:29 -05:00
|
|
|
.addClass('no_active')
|
2014-03-12 01:40:31 -04:00
|
|
|
.html('<a href="#">' + str + '</a>')
|
|
|
|
|
.appendTo(chatsListEl);
|
2014-01-13 10:06:00 -05:00
|
|
|
});
|
2014-03-12 01:40:31 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(var x = 0; x<chats.length; ++x) {
|
|
|
|
|
userObj = chats[x];
|
|
|
|
|
$('<li />')
|
|
|
|
|
.attr('data-uid', userObj.uid)
|
|
|
|
|
.html('<a href="javascript:app.openChat(\'' +
|
|
|
|
|
userObj.username +
|
|
|
|
|
'\', ' + userObj.uid +
|
2014-03-17 16:27:56 -04:00
|
|
|
');">'+
|
|
|
|
|
'<img src="' + userObj.picture + '" title="' + userObj.username +'" />' +
|
2014-04-03 16:11:34 -04:00
|
|
|
'<i class="fa fa-circle status ' + userObj.status + '"></i> ' +
|
2014-03-17 16:27:56 -04:00
|
|
|
userObj.username + '</a>')
|
2014-03-12 01:40:31 -04:00
|
|
|
.appendTo(chatsListEl);
|
2014-01-13 10:06:00 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('event:chats.receive', function(data) {
|
2014-04-14 18:04:12 -04:00
|
|
|
|
|
|
|
|
var username = data.message.fromUser.username;
|
|
|
|
|
if(parseInt(data.message.fromUser.uid, 10) === parseInt(app.uid, 10)) {
|
|
|
|
|
username = data.message.toUser.username;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 12:13:01 -04:00
|
|
|
if (module.modalExists(data.withUid)) {
|
|
|
|
|
var modal = module.getModal(data.withUid);
|
|
|
|
|
module.appendChatMessage(modal, data.message);
|
2014-01-13 13:50:33 -05:00
|
|
|
|
|
|
|
|
if (modal.is(":visible")) {
|
|
|
|
|
module.bringModalToTop(modal);
|
|
|
|
|
checkOnlineStatus(modal);
|
|
|
|
|
taskbar.updateActive(modal.attr('UUID'));
|
|
|
|
|
scrollToBottom(modal.find('#chat-content'));
|
2014-01-13 10:06:00 -05:00
|
|
|
} else {
|
2014-01-13 13:50:33 -05:00
|
|
|
module.toggleNew(modal.attr('UUID'), true);
|
2014-01-13 10:06:00 -05:00
|
|
|
}
|
2014-01-13 13:50:33 -05:00
|
|
|
|
|
|
|
|
if (!modal.is(":visible") || !app.isFocused) {
|
2014-04-14 18:04:12 -04:00
|
|
|
app.alternatingTitle(username + ' has messaged you');
|
2014-01-13 13:50:33 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2014-04-14 18:04:12 -04:00
|
|
|
module.createModal(username, data.withUid, function(modal) {
|
2014-01-13 13:50:33 -05:00
|
|
|
module.toggleNew(modal.attr('UUID'), true);
|
2014-04-14 18:04:12 -04:00
|
|
|
app.alternatingTitle(username + ' has messaged you');
|
2014-01-13 13:50:33 -05:00
|
|
|
});
|
|
|
|
|
}
|
2014-03-11 17:47:13 -04:00
|
|
|
|
2014-03-28 12:13:01 -04:00
|
|
|
if (parseInt(app.uid, 10) !== parseInt(data.message.fromuid, 10)) {
|
2014-03-13 17:15:07 -04:00
|
|
|
sounds.play('chat-incoming');
|
|
|
|
|
}
|
2014-01-13 10:06:00 -05:00
|
|
|
});
|
2014-04-15 12:48:28 -04:00
|
|
|
|
|
|
|
|
socket.on('event:chats.userStartTyping', function(withUid) {
|
|
|
|
|
var modal = module.getModal(withUid);
|
|
|
|
|
var chatContent = modal.find('#chat-content');
|
2014-05-02 16:02:22 -04:00
|
|
|
var atBottom = chatContent[0].scrollHeight - chatContent.scrollTop() === chatContent.innerHeight();
|
|
|
|
|
|
|
|
|
|
modal.find('.user-typing').removeClass('hide').appendTo(chatContent);
|
|
|
|
|
if (atBottom) {
|
|
|
|
|
scrollToBottom(chatContent);
|
|
|
|
|
}
|
2014-04-15 12:48:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('event:chats.userStopTyping', function(withUid) {
|
|
|
|
|
var modal = module.getModal(withUid);
|
|
|
|
|
modal.find('.user-typing').addClass('hide');
|
|
|
|
|
});
|
2014-03-12 01:40:31 -04:00
|
|
|
};
|
2014-01-13 10:06:00 -05:00
|
|
|
|
2013-06-18 11:26:57 -04:00
|
|
|
module.bringModalToTop = function(chatModal) {
|
|
|
|
|
var topZ = 0;
|
2014-01-06 17:37:42 -05:00
|
|
|
$('.chat-modal').each(function() {
|
|
|
|
|
var thisZ = parseInt($(this).css('zIndex'), 10);
|
|
|
|
|
if (thisZ > topZ) {
|
|
|
|
|
topZ = thisZ;
|
|
|
|
|
}
|
2013-06-18 11:26:57 -04:00
|
|
|
});
|
2013-08-26 13:18:20 -04:00
|
|
|
chatModal.css('zIndex', topZ + 1);
|
2014-02-26 21:55:29 -05:00
|
|
|
};
|
2013-06-18 11:26:57 -04:00
|
|
|
|
2013-08-27 15:55:44 -04:00
|
|
|
module.getModal = function(touid) {
|
|
|
|
|
return $('#chat-modal-' + touid);
|
2014-02-26 21:55:29 -05:00
|
|
|
};
|
2013-08-27 15:55:44 -04:00
|
|
|
|
2013-08-28 22:08:46 -04:00
|
|
|
module.modalExists = function(touid) {
|
2013-08-27 15:55:44 -04:00
|
|
|
return $('#chat-modal-' + touid).length !== 0;
|
2014-02-26 21:55:29 -05:00
|
|
|
};
|
2013-10-11 13:06:21 -04:00
|
|
|
|
2014-01-31 15:13:52 -05:00
|
|
|
function checkStatus(chatModal) {
|
2014-01-16 17:52:46 -05:00
|
|
|
socket.emit('user.isOnline', chatModal.touid, function(err, data) {
|
2014-02-23 21:50:02 -05:00
|
|
|
translator.translate('[[global:' + data.status + ']]', function(translated) {
|
|
|
|
|
$('#chat-user-status').attr('class', 'fa fa-circle status ' + data.status)
|
|
|
|
|
.attr('title', translated)
|
|
|
|
|
.attr('data-original-title', translated);
|
|
|
|
|
});
|
2013-08-28 22:08:46 -04:00
|
|
|
});
|
|
|
|
|
}
|
2013-10-11 13:06:21 -04:00
|
|
|
|
2013-08-28 22:08:46 -04:00
|
|
|
function checkOnlineStatus(chatModal) {
|
|
|
|
|
if(chatModal.intervalId === 0) {
|
2013-09-14 22:04:03 -04:00
|
|
|
chatModal.intervalId = setInterval(function() {
|
2013-08-28 22:08:46 -04:00
|
|
|
checkStatus(chatModal);
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-11 13:06:21 -04:00
|
|
|
|
2013-08-28 22:08:46 -04:00
|
|
|
module.createModal = function(username, touid, callback) {
|
2013-10-11 13:06:21 -04:00
|
|
|
|
2014-03-29 17:13:33 -04:00
|
|
|
templates.parse('chat', {}, function(chatTpl) {
|
|
|
|
|
translator.translate(chatTpl, function (chatTpl) {
|
2014-01-05 22:09:13 -05:00
|
|
|
|
|
|
|
|
var chatModal = $(chatTpl),
|
|
|
|
|
uuid = utils.generateUUID();
|
|
|
|
|
|
|
|
|
|
chatModal.intervalId = 0;
|
|
|
|
|
chatModal.touid = touid;
|
|
|
|
|
chatModal.username = username;
|
|
|
|
|
|
|
|
|
|
chatModal.attr('id', 'chat-modal-' + touid);
|
|
|
|
|
chatModal.attr('UUID', uuid);
|
2014-01-09 15:58:31 -05:00
|
|
|
chatModal.css("position", "fixed");
|
2014-01-05 22:09:13 -05:00
|
|
|
chatModal.appendTo($('body'));
|
|
|
|
|
chatModal.draggable({
|
|
|
|
|
start:function() {
|
|
|
|
|
module.bringModalToTop(chatModal);
|
2014-01-20 15:35:10 -05:00
|
|
|
},
|
2014-01-31 21:13:56 -05:00
|
|
|
stop:function() {
|
|
|
|
|
chatModal.find('#chat-message-input').focus();
|
|
|
|
|
},
|
2014-02-25 17:07:27 -05:00
|
|
|
distance: 10,
|
2014-01-20 15:35:10 -05:00
|
|
|
handle: '.modal-header'
|
2014-01-05 22:09:13 -05:00
|
|
|
});
|
|
|
|
|
|
2014-05-02 18:07:12 -04:00
|
|
|
chatModal.find('.modal-content').resizable({
|
|
|
|
|
minHeight: 250,
|
|
|
|
|
minWidth: 400
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
chatModal.find('.modal-content').on('resize', function(event, ui) {
|
|
|
|
|
var totalHeight = chatModal.find('.modal-content').outerHeight() - chatModal.find('.modal-header').outerHeight();
|
|
|
|
|
var padding = parseInt(chatModal.find('.modal-body').css('padding-top'), 10) + parseInt(chatModal.find('.modal-body').css('padding-bottom'), 10);
|
|
|
|
|
var contentMargin = parseInt(chatModal.find('#chat-content').css('margin-top'), 10) + parseInt(chatModal.find('#chat-content').css('margin-bottom'), 10);
|
|
|
|
|
var inputGroupHeight = chatModal.find('.input-group').outerHeight();
|
|
|
|
|
|
|
|
|
|
chatModal.find('#chat-content').css('height', totalHeight - padding - contentMargin - inputGroupHeight);
|
|
|
|
|
});
|
|
|
|
|
|
2014-01-05 22:09:13 -05:00
|
|
|
chatModal.find('#chat-with-name').html(username);
|
|
|
|
|
|
2014-04-15 12:48:28 -04:00
|
|
|
chatModal.find('#chat-close-btn').on('click', function() {
|
|
|
|
|
module.close(chatModal);
|
2014-01-05 22:09:13 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
chatModal.on('click', function(e) {
|
|
|
|
|
module.bringModalToTop(chatModal);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
addSendHandler(chatModal);
|
|
|
|
|
|
|
|
|
|
getChatMessages(chatModal, function() {
|
|
|
|
|
checkOnlineStatus(chatModal);
|
|
|
|
|
});
|
|
|
|
|
|
2014-04-15 12:48:28 -04:00
|
|
|
translator.translate('[[modules:chat.user_typing, ' + username + ']]', function(translated) {
|
2014-05-02 16:02:22 -04:00
|
|
|
chatModal.find('.user-typing .text').text(translated);
|
2014-04-15 12:48:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2014-01-05 22:09:13 -05:00
|
|
|
taskbar.push('chat', chatModal.attr('UUID'), {
|
|
|
|
|
title:'<i class="fa fa-comment"></i> ' + username,
|
|
|
|
|
state: ''
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
callback(chatModal);
|
|
|
|
|
});
|
2013-08-28 22:08:46 -04:00
|
|
|
});
|
2014-02-26 21:55:29 -05:00
|
|
|
};
|
2013-06-18 11:26:57 -04:00
|
|
|
|
2014-04-15 12:48:28 -04:00
|
|
|
module.close = function(chatModal) {
|
|
|
|
|
clearInterval(chatModal.intervalId);
|
|
|
|
|
chatModal.intervalId = 0;
|
|
|
|
|
chatModal.remove();
|
|
|
|
|
chatModal.data('modal', null);
|
|
|
|
|
taskbar.discard('chat', chatModal.attr('UUID'));
|
|
|
|
|
notifyStopTyping(chatModal.touid);
|
|
|
|
|
};
|
|
|
|
|
|
2013-11-04 12:37:06 -05:00
|
|
|
module.center = function(chatModal) {
|
2013-10-11 13:06:21 -04:00
|
|
|
chatModal.css("left", Math.max(0, (($(window).width() - $(chatModal).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
|
2014-01-05 22:09:13 -05:00
|
|
|
chatModal.css("top", "0px");
|
2014-01-09 15:58:31 -05:00
|
|
|
chatModal.css("zIndex", 2000);
|
|
|
|
|
chatModal.find('#chat-message-input').focus();
|
2013-10-11 13:06:21 -04:00
|
|
|
return chatModal;
|
2014-02-26 21:55:29 -05:00
|
|
|
};
|
2013-10-11 13:06:21 -04:00
|
|
|
|
2013-06-18 12:11:48 -04:00
|
|
|
module.load = function(uuid) {
|
|
|
|
|
var chatModal = $('div[UUID="'+uuid+'"]');
|
2014-01-05 22:09:13 -05:00
|
|
|
chatModal.removeClass('hide');
|
2013-08-28 22:08:46 -04:00
|
|
|
checkOnlineStatus(chatModal);
|
2013-12-05 17:35:44 -05:00
|
|
|
taskbar.updateActive(uuid);
|
2013-12-27 14:09:22 -05:00
|
|
|
scrollToBottom(chatModal.find('#chat-content'));
|
2014-01-09 20:04:50 -05:00
|
|
|
module.center(chatModal);
|
2014-01-30 14:04:20 -05:00
|
|
|
module.bringModalToTop(chatModal);
|
2014-02-26 21:55:29 -05:00
|
|
|
};
|
2013-06-18 12:11:48 -04:00
|
|
|
|
|
|
|
|
module.minimize = function(uuid) {
|
2014-04-15 12:48:28 -04:00
|
|
|
var chatModal = $('div[UUID="' + uuid + '"]');
|
2014-01-05 22:09:13 -05:00
|
|
|
chatModal.addClass('hide');
|
2013-06-18 12:11:48 -04:00
|
|
|
taskbar.minimize('chat', uuid);
|
2013-08-28 22:08:46 -04:00
|
|
|
clearInterval(chatModal.intervalId);
|
|
|
|
|
chatModal.intervalId = 0;
|
2014-04-15 12:48:28 -04:00
|
|
|
notifyStopTyping(chatModal.touid);
|
2014-02-26 21:55:29 -05:00
|
|
|
};
|
2013-06-18 12:11:48 -04:00
|
|
|
|
2014-04-15 12:48:28 -04:00
|
|
|
function notifyStopTyping(touid) {
|
|
|
|
|
socket.emit('modules.chats.userStopTyping', {touid:touid, fromUid: app.uid});
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 22:08:46 -04:00
|
|
|
function getChatMessages(chatModal, callback) {
|
2014-01-16 20:14:09 -05:00
|
|
|
socket.emit('modules.chats.get', {touid:chatModal.touid}, function(err, messages) {
|
2013-08-26 13:18:20 -04:00
|
|
|
for(var i = 0; i<messages.length; ++i) {
|
2014-03-28 12:13:01 -04:00
|
|
|
module.appendChatMessage(chatModal, messages[i]);
|
2013-08-26 13:18:20 -04:00
|
|
|
}
|
2013-08-28 22:08:46 -04:00
|
|
|
callback();
|
2013-08-26 13:18:20 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 22:08:46 -04:00
|
|
|
function addSendHandler(chatModal) {
|
2014-04-15 12:48:28 -04:00
|
|
|
var input = chatModal.find('#chat-message-input');
|
|
|
|
|
input.off('keypress').on('keypress', function(e) {
|
2013-06-18 11:26:57 -04:00
|
|
|
if(e.which === 13) {
|
2013-08-28 22:08:46 -04:00
|
|
|
sendMessage(chatModal);
|
2013-06-18 11:26:57 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2014-04-15 12:48:28 -04:00
|
|
|
input.off('keyup').on('keyup', function() {
|
|
|
|
|
if ($(this).val()) {
|
|
|
|
|
socket.emit('modules.chats.userStartTyping', {touid:chatModal.touid, fromUid: app.uid});
|
|
|
|
|
} else {
|
|
|
|
|
notifyStopTyping(chatModal.touid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2014-01-17 20:10:09 -05:00
|
|
|
chatModal.find('#chat-message-send-btn').off('click').on('click', function(e){
|
2013-08-28 22:08:46 -04:00
|
|
|
sendMessage(chatModal);
|
2013-06-18 11:26:57 -04:00
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 22:08:46 -04:00
|
|
|
function sendMessage(chatModal) {
|
2013-12-26 21:10:26 -05:00
|
|
|
var msg = S(chatModal.find('#chat-message-input').val()).stripTags().s;
|
2014-04-15 12:48:28 -04:00
|
|
|
if (msg.length) {
|
2013-06-18 11:26:57 -04:00
|
|
|
msg = msg +'\n';
|
2014-03-28 12:13:01 -04:00
|
|
|
socket.emit('modules.chats.send', {touid:chatModal.touid, message:msg});
|
2013-06-18 11:26:57 -04:00
|
|
|
chatModal.find('#chat-message-input').val('');
|
2014-03-13 17:15:07 -04:00
|
|
|
sounds.play('chat-outgoing');
|
2014-04-15 12:48:28 -04:00
|
|
|
notifyStopTyping(chatModal.touid);
|
2013-06-18 11:26:57 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 12:13:01 -04:00
|
|
|
module.appendChatMessage = function(chatModal, data) {
|
2013-06-18 11:26:57 -04:00
|
|
|
var chatContent = chatModal.find('#chat-content');
|
2013-08-27 12:10:23 -04:00
|
|
|
|
2014-03-28 12:13:01 -04:00
|
|
|
var isYou = parseInt(app.uid, 10) === parseInt(data.fromuid, 10);
|
2013-08-27 12:10:23 -04:00
|
|
|
|
2014-03-29 19:40:49 -04:00
|
|
|
var message = $('<li class="chat-message clear" data-uid="' + data.fromuid + '"></li>');
|
2014-03-28 12:13:01 -04:00
|
|
|
var time = '<span class="chat-timestamp pull-right timeago" title="' + utils.toISOString(data.timestamp) + '"></span> ';
|
|
|
|
|
|
|
|
|
|
|
2014-05-02 16:02:22 -04:00
|
|
|
if (data.fromuid !== chatContent.children('.chat-message').last().attr('data-uid')) {
|
2014-04-14 18:04:12 -04:00
|
|
|
var userPicture = $('<a href="/user/' + data.fromUser.userslug + '"><img class="chat-user-image" src="' + data.fromUser.picture + '"></a>');
|
|
|
|
|
var userName = $('<strong><span class="chat-user"> '+ data.fromUser.username + '</span></strong>');
|
2014-03-28 12:13:01 -04:00
|
|
|
userName.toggleClass('chat-user-you', isYou);
|
|
|
|
|
|
|
|
|
|
message.append(userPicture)
|
|
|
|
|
.append(userName)
|
|
|
|
|
.append('<br/>');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message.append(S(data.content + time).stripTags('p').s);
|
|
|
|
|
|
|
|
|
|
message.toggleClass('chat-message-them', !isYou);
|
2014-01-30 16:44:36 -05:00
|
|
|
message.find('img:not(".chat-user-image")').addClass('img-responsive');
|
2014-03-24 15:58:02 -04:00
|
|
|
message.find('span.timeago').timeago();
|
|
|
|
|
|
2014-01-15 18:20:05 +01:00
|
|
|
chatContent.append(message);
|
2014-01-30 16:44:36 -05:00
|
|
|
|
2013-12-27 14:09:22 -05:00
|
|
|
scrollToBottom(chatContent);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function scrollToBottom(chatContent) {
|
2014-01-09 15:58:31 -05:00
|
|
|
if(chatContent[0]) {
|
|
|
|
|
chatContent.scrollTop(
|
|
|
|
|
chatContent[0].scrollHeight - chatContent.height()
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-12-27 14:09:22 -05:00
|
|
|
}
|
2013-12-04 21:44:36 -05:00
|
|
|
|
|
|
|
|
module.toggleNew = function(uuid, state) {
|
|
|
|
|
taskbar.toggleNew(uuid, state);
|
|
|
|
|
};
|
2013-06-18 11:26:57 -04:00
|
|
|
|
|
|
|
|
return module;
|
2014-04-10 20:31:57 +01:00
|
|
|
});
|