Files
NodeBB/public/src/modules/chat.js

367 lines
10 KiB
JavaScript
Raw Normal View History

"use strict";
2015-12-15 17:50:30 +02:00
/* globals app, define, socket, templates, utils, ajaxify */
2014-03-12 01:40:31 -04:00
2016-04-30 22:34:36 +03:00
define('chat', [
'components',
'taskbar',
'string',
'sounds',
'forum/chats',
'forum/chats/messages',
'translator'
], function(components, taskbar, S, sounds, Chats, ChatsMessages, translator) {
2013-08-26 13:18:20 -04:00
2013-06-18 11:26:57 -04:00
var module = {};
2014-07-19 10:33:27 -04:00
var newMessage = false;
2013-06-18 11:26:57 -04:00
module.prepareDOM = function() {
2016-03-11 13:41:51 +02:00
var chatsToggleEl = components.get('chat/dropdown');
2016-03-11 13:38:52 +02:00
var chatsListEl = components.get('chat/list');
2014-01-13 13:50:33 -05:00
chatsToggleEl.on('click', function() {
2014-03-12 01:40:31 -04:00
if (chatsToggleEl.parent().hasClass('open')) {
return;
}
2015-09-18 14:30:18 -04:00
module.loadChatsDropdown(chatsListEl);
});
chatsListEl.on('click', '[data-roomid]', function() {
var roomId = this.getAttribute('data-roomid');
if (!ajaxify.currentPage.match(/^chats\//)) {
app.openChat(roomId);
} else {
2016-09-20 14:26:26 +03:00
ajaxify.go('user/' + app.user.userslug + '/chats/' + roomId);
}
});
2016-03-11 13:38:52 +02:00
$('[component="chats/mark-all-read"]').on('click', function() {
socket.emit('modules.chats.markAllRead', function(err) {
if (err) {
return app.alertError(err);
}
});
});
socket.on('event:chats.receive', function(data) {
2014-04-14 18:04:12 -04:00
var username = data.message.fromUser.username;
2015-12-16 15:09:14 +02:00
var isSelf = data.self === 1;
2014-09-02 16:01:45 -04:00
data.message.self = data.self;
2015-12-16 15:09:14 +02:00
2014-09-02 15:50:02 -04:00
newMessage = data.self === 0;
2015-12-16 15:09:14 +02:00
if (module.modalExists(data.roomId)) {
var modal = module.getModal(data.roomId);
2015-09-18 14:30:18 -04:00
2016-04-30 22:34:36 +03:00
ChatsMessages.appendChatMessage(modal.find('.chat-content'), data.message);
2014-01-13 13:50:33 -05:00
if (modal.is(':visible')) {
2014-01-13 13:50:33 -05:00
taskbar.updateActive(modal.attr('UUID'));
2016-04-30 22:34:36 +03:00
ChatsMessages.scrollToBottom(modal.find('.chat-content'));
} else {
2015-07-28 10:24:46 -04:00
module.toggleNew(modal.attr('UUID'), true, true);
}
2014-01-13 13:50:33 -05:00
if (!isSelf && (!modal.is(':visible') || !app.isFocused)) {
2014-05-09 17:57:39 -04:00
app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + username + ']]');
sounds.play('chat-incoming');
taskbar.push('chat', modal.attr('UUID'), {
title: username,
2016-03-21 12:57:23 -04:00
touid: data.message.fromUser.uid,
roomId: data.roomId
});
2014-01-13 13:50:33 -05:00
}
} else {
socket.emit('modules.chats.loadRoom', {roomId: data.roomId}, function(err, roomData) {
if (err) {
return app.alertError(err.message);
2014-05-09 17:46:10 -04:00
}
roomData.users = roomData.users.filter(function(user) {
return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10);
});
2015-12-23 11:27:34 +02:00
roomData.silent = true;
module.createModal(roomData, function(modal) {
2016-01-16 11:01:06 +02:00
module.toggleNew(modal.attr('UUID'), !isSelf, true);
if (!isSelf) {
app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + username + ']]');
sounds.play('chat-incoming');
}
});
2014-01-13 13:50:33 -05:00
});
}
});
2014-04-15 12:48:28 -04:00
socket.on('event:user_status_change', function(data) {
2014-11-04 16:49:02 -05:00
var modal = module.getModal(data.uid);
app.updateUserStatus(modal.find('[component="user/status"]'), data.status);
2014-09-02 05:04:39 -04:00
});
2015-12-23 11:27:34 +02:00
socket.on('event:chats.roomRename', function(data) {
2016-03-03 13:51:42 +02:00
module.getModal(data.roomId).find('[component="chat/room/name"]').val($('<div/>').html(data.newName).text());
2015-12-23 11:27:34 +02:00
});
2016-02-01 21:22:36 +02:00
2016-04-30 22:34:36 +03:00
ChatsMessages.onChatMessageEdit();
2014-03-12 01:40:31 -04:00
};
2015-09-18 14:30:18 -04:00
module.loadChatsDropdown = function(chatsListEl) {
2015-12-15 17:50:30 +02:00
socket.emit('modules.chats.getRecentChats', {after: 0}, function(err, data) {
2015-08-26 16:32:32 -04:00
if (err) {
return app.alertError(err.message);
}
2015-12-15 17:50:30 +02:00
2016-02-29 06:19:43 +00:00
var rooms = data.rooms.filter(function(room) {
return room.teaser;
});
2015-08-26 16:32:32 -04:00
templates.parse('partials/chat_dropdown', {
rooms: rooms
}, function(html) {
translator.translate(html, function(translated) {
chatsListEl.empty().html(translated);
app.createUserTooltips(chatsListEl, 'right');
2015-09-14 13:19:21 -04:00
});
});
2015-08-26 16:32:32 -04:00
});
};
2013-06-18 11:26:57 -04:00
module.bringModalToTop = function(chatModal) {
var topZ = 0;
2014-11-07 18:38:03 -05:00
taskbar.updateActive(chatModal.attr('UUID'));
2014-10-02 20:21:43 -04:00
if ($('.chat-modal').length === 1) {
return;
}
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
});
2014-10-02 20:21:43 -04:00
2013-08-26 13:18:20 -04:00
chatModal.css('zIndex', topZ + 1);
};
2013-06-18 11:26:57 -04:00
2015-12-16 10:09:00 +02:00
module.getModal = function(roomId) {
return $('#chat-modal-' + roomId);
};
2013-08-27 15:55:44 -04:00
2015-12-16 10:09:00 +02:00
module.modalExists = function(roomId) {
return $('#chat-modal-' + roomId).length !== 0;
};
2013-10-11 13:06:21 -04:00
2014-01-31 15:13:52 -05:00
function checkStatus(chatModal) {
socket.emit('user.checkStatus', chatModal.attr('touid'), function(err, status) {
if (err) {
return app.alertError(err.message);
}
2013-10-11 13:06:21 -04:00
app.updateUserStatus(chatModal.find('[component="user/status"]'), status);
2014-09-02 05:04:39 -04:00
});
2013-08-28 22:08:46 -04:00
}
2013-10-11 13:06:21 -04:00
2015-07-24 14:25:23 -04:00
module.createModal = function(data, callback) {
2015-12-23 11:27:34 +02:00
templates.parse('chat', data, function(chatTpl) {
2014-03-29 17:13:33 -04:00
translator.translate(chatTpl, function (chatTpl) {
var chatModal = $(chatTpl),
2014-11-07 18:38:03 -05:00
uuid = utils.generateUUID(),
dragged = false;
2015-12-16 10:09:00 +02:00
chatModal.attr('id', 'chat-modal-' + data.roomId);
chatModal.attr('roomId', data.roomId);
2014-07-19 10:33:27 -04:00
chatModal.attr('intervalId', 0);
chatModal.attr('UUID', uuid);
2014-10-02 20:21:43 -04:00
chatModal.css('position', 'fixed');
chatModal.css('zIndex', 100);
chatModal.appendTo($('body'));
2014-11-04 16:49:02 -05:00
module.center(chatModal);
2015-06-08 16:58:53 -04:00
app.loadJQueryUI(function() {
chatModal.find('.modal-content').resizable({
handles: 'n, e, s, w, se',
2015-06-08 16:58:53 -04:00
minHeight: 250,
minWidth: 400
});
2014-05-02 18:07:12 -04:00
2015-06-08 16:58:53 -04:00
chatModal.find('.modal-content').on('resize', function(event, ui) {
if (ui.originalSize.height === ui.size.height) {
return;
}
2014-05-02 18:07:12 -04:00
chatModal.find('.chat-content').css('height', module.calculateChatListHeight(chatModal));
2015-06-08 16:58:53 -04:00
});
2015-09-10 17:13:36 -04:00
2015-06-08 16:58:53 -04:00
chatModal.draggable({
start:function() {
module.bringModalToTop(chatModal);
},
stop:function() {
chatModal.find('#chat-message-input').focus();
},
distance: 10,
handle: '.modal-header'
});
2014-05-02 18:07:12 -04:00
});
2014-04-15 12:48:28 -04:00
chatModal.find('#chat-close-btn').on('click', function() {
module.close(chatModal);
});
function gotoChats() {
2015-04-17 13:44:47 -04:00
var text = components.get('chat/input').val();
$(window).one('action:ajaxify.end', function() {
components.get('chat/input').val(text);
});
2015-09-10 17:13:36 -04:00
2016-09-20 14:26:26 +03:00
ajaxify.go('user/' + app.user.userslug + '/chats/' + chatModal.attr('roomId'));
2014-07-09 11:03:32 -04:00
module.close(chatModal);
}
chatModal.find('.modal-header').on('dblclick', gotoChats);
chatModal.find('button[data-action="maximize"]').on('click', gotoChats);
2014-07-09 11:03:32 -04:00
2015-12-15 17:50:30 +02:00
chatModal.on('click', function() {
module.bringModalToTop(chatModal);
2014-11-07 18:38:03 -05:00
2015-12-17 11:56:28 +02:00
if (dragged) {
2014-11-07 18:38:03 -05:00
dragged = false;
}
});
chatModal.on('mousemove', function(e) {
if (e.which === 1) {
dragged = true;
}
});
2014-07-19 10:33:27 -04:00
chatModal.on('mousemove keypress click', function() {
if (newMessage) {
2015-12-16 10:09:00 +02:00
socket.emit('modules.chats.markRead', data.roomId);
2014-07-19 10:33:27 -04:00
newMessage = false;
}
});
2014-07-09 11:03:32 -04:00
2016-04-30 22:34:36 +03:00
Chats.addEditDeleteHandler(chatModal.find('[component="chat/messages"]'), data.roomId);
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
chatModal.find('[component="chat/controlsToggle"]').on('click', function() {
var messagesEl = chatModal.find('[component="chat/messages"]');
chatModal.find('[component="chat/controls"]').toggle();
messagesEl.css('height', module.calculateChatListHeight(chatModal));
});
2015-12-16 10:09:00 +02:00
Chats.addSinceHandler(chatModal.attr('roomId'), chatModal.find('.chat-content'), chatModal.find('[data-since]'));
2015-12-23 11:27:34 +02:00
Chats.addRenameHandler(chatModal.attr('roomId'), chatModal.find('[component="chat/room/name"]'));
2014-09-17 16:29:03 -04:00
2015-12-16 10:09:00 +02:00
Chats.addSendHandlers(chatModal.attr('roomId'), chatModal.find('#chat-message-input'), chatModal.find('#chat-message-send-btn'));
Chats.createTagsInput(chatModal.find('.users-tag-input'), data);
2016-01-12 16:37:34 +02:00
Chats.createAutoComplete(chatModal.find('[component="chat/input"]'));
2015-12-16 10:09:00 +02:00
Chats.loadChatSince(chatModal.attr('roomId'), chatModal.find('.chat-content'), 'recent');
2015-09-18 14:30:18 -04:00
checkStatus(chatModal);
taskbar.push('chat', chatModal.attr('UUID'), {
2015-12-17 11:56:28 +02:00
title: data.users.length ? data.users[0].username : '',
2015-12-16 10:09:00 +02:00
roomId: data.roomId,
2014-06-13 13:57:38 -04:00
icon: 'fa-comment',
state: ''
});
2014-07-01 15:05:07 -04:00
$(window).trigger('action:chat.loaded', chatModal);
2015-07-24 14:25:23 -04:00
if (typeof callback === 'function') {
callback(chatModal);
}
});
2013-08-28 22:08:46 -04:00
});
};
2013-06-18 11:26:57 -04:00
2014-12-21 00:08:01 -05:00
module.focusInput = function(chatModal) {
chatModal.find('#chat-message-input').focus();
};
2016-04-30 22:34:36 +03:00
module.close = function(chatModal) {
2014-07-19 10:33:27 -04:00
clearInterval(chatModal.attr('intervalId'));
chatModal.attr('intervalId', 0);
2014-04-15 12:48:28 -04:00
chatModal.remove();
chatModal.data('modal', null);
taskbar.discard('chat', chatModal.attr('UUID'));
2015-02-26 11:03:15 -05:00
if (chatModal.attr('data-mobile')) {
module.disableMobileBehaviour(chatModal);
}
2014-04-15 12:48:28 -04:00
};
2013-11-04 12:37:06 -05:00
module.center = function(chatModal) {
2014-11-04 16:49:02 -05:00
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');
2014-12-21 00:08:01 -05:00
2014-11-04 16:49:02 -05:00
if (hideAfter) {
chatModal.addClass('hide');
}
2013-10-11 13:06:21 -04:00
return chatModal;
};
2013-10-11 13:06:21 -04:00
2013-06-18 12:11:48 -04:00
module.load = function(uuid) {
2015-12-16 10:09:00 +02:00
var chatModal = $('div[UUID="' + uuid + '"]');
chatModal.removeClass('hide');
2014-09-02 05:04:39 -04:00
checkStatus(chatModal);
taskbar.updateActive(uuid);
2016-04-30 22:34:36 +03:00
ChatsMessages.scrollToBottom(chatModal.find('.chat-content'));
2014-01-30 14:04:20 -05:00
module.bringModalToTop(chatModal);
2014-12-21 00:08:01 -05:00
module.focusInput(chatModal);
2015-12-16 10:09:00 +02:00
socket.emit('modules.chats.markRead', chatModal.attr('roomId'));
2015-02-26 11:03:15 -05:00
var env = utils.findBootstrapEnvironment();
if (env === 'xs' || env === 'sm') {
2015-02-26 11:03:15 -05:00
module.enableMobileBehaviour(chatModal);
}
};
module.enableMobileBehaviour = function(modalEl) {
app.toggleNavbar(false);
modalEl.attr('data-mobile', '1');
var messagesEl = modalEl.find('.chat-content');
2015-02-26 11:03:15 -05:00
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) {
2016-04-30 22:34:36 +03:00
var totalHeight = modalEl.find('.modal-content').outerHeight() - modalEl.find('.modal-header').outerHeight();
var padding = parseInt(modalEl.find('.modal-body').css('padding-top'), 10) + parseInt(modalEl.find('.modal-body').css('padding-bottom'), 10);
var contentMargin = parseInt(modalEl.find('.chat-content').css('margin-top'), 10) + parseInt(modalEl.find('.chat-content').css('margin-bottom'), 10);
var sinceHeight = modalEl.find('.since-bar').outerHeight(true);
var inputGroupHeight = modalEl.find('.input-group').outerHeight();
2015-02-26 11:03:15 -05:00
return totalHeight - padding - contentMargin - inputGroupHeight;
};
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 + '"]');
chatModal.addClass('hide');
2013-06-18 12:11:48 -04:00
taskbar.minimize('chat', uuid);
2014-07-19 10:33:27 -04:00
clearInterval(chatModal.attr('intervalId'));
chatModal.attr('intervalId', 0);
};
2013-06-18 12:11:48 -04:00
2015-07-24 14:25:23 -04:00
module.toggleNew = taskbar.toggleNew;
2013-06-18 11:26:57 -04:00
2015-07-24 14:25:23 -04:00
2013-06-18 11:26:57 -04:00
return module;
2014-04-10 20:31:57 +01:00
});