You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
11 KiB
JavaScript

"use strict";
/* globals app, config, define, socket, templates, utils, ajaxify */
11 years ago
10 years ago
define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'translator'], function(components, taskbar, S, sounds, Chats, translator) {
12 years ago
var module = {};
var newMessage = false;
module.prepareDOM = function() {
var chatsToggleEl = components.get('chat/dropdown'),
10 years ago
chatsListEl = components.get('chat/list');
11 years ago
chatsToggleEl.on('click', function() {
11 years ago
if (chatsToggleEl.parent().hasClass('open')) {
return;
}
module.loadChatsDropdown(chatsListEl);
});
socket.on('event:chats.receive', function(data) {
11 years ago
var username = data.message.fromUser.username;
10 years ago
var isSelf = parseInt(data.message.fromUser.uid, 10) === parseInt(app.user.uid, 10);
11 years ago
data.message.self = data.self;
11 years ago
if (isSelf) {
11 years ago
username = data.message.toUser.username;
}
newMessage = data.self === 0;
11 years ago
if (module.modalExists(data.withUid)) {
var modal = module.getModal(data.withUid);
Chats.appendChatMessage(modal.find('.chat-content'), data.message);
11 years ago
if (modal.is(":visible")) {
taskbar.updateActive(modal.attr('UUID'));
Chats.scrollToBottom(modal.find('.chat-content'));
} else {
10 years ago
module.toggleNew(modal.attr('UUID'), true, true);
}
11 years ago
11 years ago
if (!isSelf && (!modal.is(":visible") || !app.isFocused)) {
11 years ago
app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + username + ']]');
sounds.play('chat-incoming');
taskbar.push('chat', modal.attr('UUID'), {
title: username,
touid: data.message.fromUser.uid
});
11 years ago
}
} else {
10 years ago
module.createModal({
username: username,
10 years ago
touid: data.withUid,
silent: true
10 years ago
}, function(modal) {
10 years ago
module.toggleNew(modal.attr('UUID'), true, true);
11 years ago
if (!isSelf) {
11 years ago
app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + username + ']]');
sounds.play('chat-incoming');
11 years ago
}
11 years ago
});
}
});
11 years ago
socket.on('event:chats.userStartTyping', function(withUid) {
var modal = module.getModal(withUid);
var chatContent = modal.find('.chat-content');
11 years ago
if (!chatContent.length) {
return;
}
11 years ago
var atBottom = chatContent[0].scrollHeight - chatContent.scrollTop() === chatContent.innerHeight();
modal.find('.user-typing').removeClass('hide');
11 years ago
if (atBottom) {
Chats.scrollToBottom(chatContent);
11 years ago
}
11 years ago
});
socket.on('event:chats.userStopTyping', function(withUid) {
var modal = module.getModal(withUid);
modal.find('.user-typing').addClass('hide');
});
socket.on('event:user_status_change', function(data) {
10 years ago
var modal = module.getModal(data.uid);
app.updateUserStatus(modal.find('[component="user/status"]'), data.status);
});
11 years ago
};
module.loadChatsDropdown = function(chatsListEl) {
10 years ago
var dropdownEl;
socket.emit('modules.chats.getRecentChats', {after: 0}, function(err, chats) {
if (err) {
return app.alertError(err.message);
}
chats = chats.users;
chatsListEl.empty();
if (!chats.length) {
translator.translate('[[modules:chat.no_active]]', function(str) {
$('<li />')
.addClass('no_active')
.html('<a href="#">' + str + '</a>')
.appendTo(chatsListEl);
});
return;
}
chats.forEach(function(userObj) {
10 years ago
dropdownEl = $('<li class="' + (userObj.unread ? 'unread' : '') + '"/>')
.attr('data-uid', userObj.uid)
.html('<a data-ajaxify="false">'+
10 years ago
(userObj.picture ?
'<img src="' + userObj.picture + '" title="' + userObj.username +'" />' :
'<div class="user-icon" style="background-color: ' + userObj['icon:bgColor'] + '">' + userObj['icon:text'] + '</div>') +
10 years ago
'<i class="fa fa-circle status ' + userObj.status + '"></i> ' +
userObj.username + '</a>')
.appendTo(chatsListEl);
dropdownEl.click(function() {
if (!ajaxify.currentPage.match(/^chats\//)) {
app.openChat(userObj.username, userObj.uid);
} else {
ajaxify.go('chats/' + utils.slugify(userObj.username));
}
});
});
10 years ago
});
};
module.bringModalToTop = function(chatModal) {
var topZ = 0;
10 years ago
taskbar.updateActive(chatModal.attr('UUID'));
11 years ago
if ($('.chat-modal').length === 1) {
return;
}
11 years ago
$('.chat-modal').each(function() {
var thisZ = parseInt($(this).css('zIndex'), 10);
if (thisZ > topZ) {
topZ = thisZ;
}
});
11 years ago
12 years ago
chatModal.css('zIndex', topZ + 1);
};
12 years ago
module.getModal = function(touid) {
return $('#chat-modal-' + touid);
};
12 years ago
module.modalExists = function(touid) {
12 years ago
return $('#chat-modal-' + touid).length !== 0;
};
12 years ago
function checkStatus(chatModal) {
socket.emit('user.checkStatus', chatModal.attr('touid'), function(err, status) {
if (err) {
return app.alertError(err.message);
}
12 years ago
app.updateUserStatus(chatModal.find('[component="user/status"]'), status);
});
}
12 years ago
10 years ago
module.createModal = function(data, callback) {
11 years ago
templates.parse('chat', {}, function(chatTpl) {
translator.translate(chatTpl, function (chatTpl) {
var chatModal = $(chatTpl),
10 years ago
uuid = utils.generateUUID(),
dragged = false;
10 years ago
chatModal.attr('id', 'chat-modal-' + data.touid);
chatModal.attr('touid', data.touid);
chatModal.attr('intervalId', 0);
chatModal.attr('UUID', uuid);
11 years ago
chatModal.css('position', 'fixed');
chatModal.css('zIndex', 100);
chatModal.appendTo($('body'));
10 years ago
module.center(chatModal);
10 years ago
app.loadJQueryUI(function() {
chatModal.find('.modal-content').resizable({
minHeight: 250,
minWidth: 400
});
11 years ago
10 years ago
chatModal.find('.modal-content').on('resize', function(event, ui) {
if (ui.originalSize.height === ui.size.height) {
return;
}
11 years ago
chatModal.find('.chat-content').css('height', module.calculateChatListHeight(chatModal));
10 years ago
});
10 years ago
10 years ago
chatModal.draggable({
start:function() {
module.bringModalToTop(chatModal);
},
stop:function() {
chatModal.find('#chat-message-input').focus();
},
distance: 10,
handle: '.modal-header'
});
11 years ago
});
10 years ago
chatModal.find('#chat-with-name').html(data.username);
11 years ago
chatModal.find('#chat-close-btn').on('click', function() {
module.close(chatModal);
});
function gotoChats() {
10 years ago
var text = components.get('chat/input').val();
$(window).one('action:ajaxify.end', function() {
components.get('chat/input').val(text);
});
10 years ago
10 years ago
ajaxify.go('chats/' + utils.slugify(data.username));
11 years ago
module.close(chatModal);
}
chatModal.find('.modal-header').on('dblclick', gotoChats);
chatModal.find('button[data-action="maximize"]').on('click', gotoChats);
11 years ago
chatModal.on('click', function(e) {
module.bringModalToTop(chatModal);
10 years ago
if (!dragged) {
chatModal.find('#chat-message-input').focus();
} else {
dragged = false;
}
});
chatModal.on('mousemove', function(e) {
if (e.which === 1) {
dragged = true;
}
});
chatModal.on('mousemove keypress click', function() {
if (newMessage) {
10 years ago
socket.emit('modules.chats.markRead', data.touid);
newMessage = false;
}
});
11 years ago
Chats.addSinceHandler(chatModal.attr('touid'), chatModal.find('.chat-content'), chatModal.find('[data-since]'));
11 years ago
Chats.addSendHandlers(chatModal.attr('touid'), chatModal.find('#chat-message-input'), chatModal.find('#chat-message-send-btn'));
10 years ago
Chats.loadChatSince(chatModal.attr('touid'), chatModal.find('.chat-content'), 'recent');
checkStatus(chatModal);
10 years ago
module.canMessage(data.touid, function(err) {
if (err) {
// Disable the text input
chatModal.find('input[type="text"]').attr('disabled', true);
}
});
taskbar.push('chat', chatModal.attr('UUID'), {
10 years ago
title: data.username,
touid: data.touid,
11 years ago
icon: 'fa-comment',
state: ''
});
$(window).trigger('action:chat.loaded', chatModal);
10 years ago
if (typeof callback === 'function') {
callback(chatModal);
}
});
});
};
10 years ago
module.focusInput = function(chatModal) {
chatModal.find('#chat-message-input').focus();
};
10 years ago
module.close = function(chatModal, silent) {
clearInterval(chatModal.attr('intervalId'));
chatModal.attr('intervalId', 0);
11 years ago
chatModal.remove();
chatModal.data('modal', null);
taskbar.discard('chat', chatModal.attr('UUID'));
Chats.notifyTyping(chatModal.attr('touid'), false);
10 years ago
if (chatModal.attr('data-mobile')) {
module.disableMobileBehaviour(chatModal);
}
11 years ago
};
11 years ago
module.center = function(chatModal) {
10 years ago
var hideAfter = false;
if (chatModal.hasClass('hide')) {
chatModal.removeClass('hide');
hideAfter = true;
}
chatModal.css('left', Math.max(0, (($(window).width() - $(chatModal).outerWidth()) / 2) + $(window).scrollLeft()) + 'px');
chatModal.css('top', Math.max(0, $(window).height() / 2 - $(chatModal).outerHeight() / 2) + 'px');
10 years ago
10 years ago
if (hideAfter) {
chatModal.addClass('hide');
}
12 years ago
return chatModal;
};
12 years ago
module.load = function(uuid) {
var chatModal = $('div[UUID="'+uuid+'"]');
chatModal.removeClass('hide');
checkStatus(chatModal);
taskbar.updateActive(uuid);
Chats.scrollToBottom(chatModal.find('.chat-content'));
11 years ago
module.bringModalToTop(chatModal);
10 years ago
module.focusInput(chatModal);
socket.emit('modules.chats.markRead', chatModal.attr('touid'));
10 years ago
var env = utils.findBootstrapEnvironment();
if (env === 'xs' || env === 'sm') {
module.enableMobileBehaviour(chatModal);
}
};
module.enableMobileBehaviour = function(modalEl) {
app.toggleNavbar(false);
modalEl.attr('data-mobile', '1');
var messagesEl = modalEl.find('.chat-content');
10 years ago
messagesEl.css('height', module.calculateChatListHeight(modalEl));
$(window).on('resize', function() {
messagesEl.css('height', module.calculateChatListHeight(modalEl));
});
};
module.disableMobileBehaviour = function(modalEl) {
app.toggleNavbar(true);
};
module.calculateChatListHeight = function(modalEl) {
var totalHeight = modalEl.find('.modal-content').outerHeight() - modalEl.find('.modal-header').outerHeight(),
padding = parseInt(modalEl.find('.modal-body').css('padding-top'), 10) + parseInt(modalEl.find('.modal-body').css('padding-bottom'), 10),
contentMargin = parseInt(modalEl.find('.chat-content').css('margin-top'), 10) + parseInt(modalEl.find('.chat-content').css('margin-bottom'), 10),
10 years ago
sinceHeight = modalEl.find('.since-bar').outerHeight(true),
inputGroupHeight = modalEl.find('.input-group').outerHeight();
return totalHeight - padding - contentMargin - sinceHeight - inputGroupHeight;
};
module.minimize = function(uuid) {
11 years ago
var chatModal = $('div[UUID="' + uuid + '"]');
chatModal.addClass('hide');
taskbar.minimize('chat', uuid);
clearInterval(chatModal.attr('intervalId'));
chatModal.attr('intervalId', 0);
Chats.notifyTyping(chatModal.attr('touid'), false);
};
10 years ago
module.toggleNew = taskbar.toggleNew;
10 years ago
module.canMessage = function(toUid, callback) {
socket.emit('modules.chats.canMessage', toUid, callback);
};
10 years ago
return module;
});