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.

346 lines
10 KiB
JavaScript

8 years ago
'use strict';
11 years ago
define('chat', [
'components',
'taskbar',
'string',
'sounds',
'forum/chats',
'forum/chats/messages',
'translator',
'scrollStop',
'benchpress',
], function (components, taskbar, S, sounds, Chats, ChatsMessages, translator, scrollStop, Benchpress) {
var module = {};
var newMessage = false;
module.prepareDOM = function () {
9 years ago
var chatsToggleEl = components.get('chat/dropdown');
9 years ago
var chatsListEl = components.get('chat/list');
7 years ago
var chatsDropdownWrapper = chatsToggleEl.parents('.dropdown');
11 years ago
chatsToggleEl.on('click', function () {
11 years ago
if (chatsToggleEl.parent().hasClass('open')) {
return;
}
module.loadChatsDropdown(chatsListEl);
});
7 years ago
if (chatsDropdownWrapper.hasClass('open')) {
module.loadChatsDropdown(chatsListEl);
}
chatsListEl.on('click', '[data-roomid]', function (ev) {
9 years ago
if ($(ev.target).parents('.user-link').length) {
return;
}
var roomId = $(this).attr('data-roomid');
if (!ajaxify.currentPage.match(/^chats\//)) {
app.openChat(roomId);
} else {
ajaxify.go('user/' + app.user.userslug + '/chats/' + roomId);
}
});
$('[component="chats/mark-all-read"]').on('click', function () {
socket.emit('modules.chats.markAllRead', function (err) {
9 years ago
if (err) {
return app.alertError(err);
}
});
});
socket.on('event:chats.receive', function (data) {
11 years ago
var username = data.message.fromUser.username;
9 years ago
var isSelf = data.self === 1;
11 years ago
data.message.self = data.self;
9 years ago
newMessage = data.self === 0;
9 years ago
if (module.modalExists(data.roomId)) {
var modal = module.getModal(data.roomId);
ChatsMessages.appendChatMessage(modal.find('.chat-content'), data.message);
11 years ago
if (modal.is(':visible')) {
taskbar.updateActive(modal.attr('data-uuid'));
ChatsMessages.scrollToBottom(modal.find('.chat-content'));
} else if (!ajaxify.data.template.chats) {
module.toggleNew(modal.attr('data-uuid'), true, true);
}
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', 'chat.incoming:' + data.message.mid);
taskbar.push('chat', modal.attr('data-uuid'), {
title: '[[modules:chat.chatting_with]] ' + (data.roomName || username),
touid: data.message.fromUser.uid,
roomId: data.roomId,
});
11 years ago
}
} else if (!ajaxify.data.template.chats) {
socket.emit('modules.chats.loadRoom', {
roomId: data.roomId,
}, function (err, roomData) {
if (err) {
return app.alertError(err.message);
11 years ago
}
roomData.users = roomData.users.filter(function (user) {
return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10);
});
9 years ago
roomData.silent = true;
9 years ago
roomData.uid = app.user.uid;
module.createModal(roomData, function (modal) {
module.toggleNew(modal.attr('data-uuid'), !isSelf, true);
if (!isSelf) {
app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + username + ']]');
sounds.play('chat-incoming', 'chat.incoming:' + data.message.mid);
}
});
11 years ago
});
}
});
11 years ago
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);
});
9 years ago
socket.on('event:chats.roomRename', function (data) {
var newTitle = $('<div/>').html(data.newName).text();
var modal = module.getModal(data.roomId);
modal.find('[component="chat/room/name"]').text(newTitle);
taskbar.updateTitle('chat', modal.attr('data-uuid'), newTitle);
9 years ago
});
9 years ago
ChatsMessages.onChatMessageEdit();
11 years ago
};
module.loadChatsDropdown = function (chatsListEl) {
socket.emit('modules.chats.getRecentChats', {
uid: app.user.uid,
after: 0,
}, function (err, data) {
10 years ago
if (err) {
return app.alertError(err.message);
}
var rooms = data.rooms.filter(function (room) {
return room.teaser;
});
10 years ago
Benchpress.parse('partials/chats/dropdown', {
rooms: rooms,
}, function (html) {
translator.translate(html, function (translated) {
7 years ago
chatsListEl.find('*').not('.navigation-link').remove();
chatsListEl.prepend(translated);
app.createUserTooltips(chatsListEl, 'right');
});
});
10 years ago
});
};
module.getModal = function (roomId) {
return $('#chat-modal-' + roomId);
};
12 years ago
module.modalExists = function (roomId) {
return $('#chat-modal-' + roomId).length !== 0;
};
12 years ago
module.createModal = function (data, callback) {
app.parseAndTranslate('chat', data, function (chatModal) {
var uuid = utils.generateUUID();
var dragged = false;
chatModal.attr('id', 'chat-modal-' + data.roomId);
chatModal.attr('data-roomid', data.roomId);
chatModal.attr('intervalId', 0);
chatModal.attr('data-uuid', uuid);
chatModal.css('position', 'fixed');
chatModal.appendTo($('body'));
chatModal.find('.timeago').timeago();
module.center(chatModal);
app.loadJQueryUI(function () {
chatModal.find('.modal-content').resizable({
handles: 'n, e, s, w, se',
minHeight: 250,
minWidth: 400,
});
11 years ago
chatModal.find('.modal-content').on('resize', function (event, ui) {
if (ui.originalSize.height === ui.size.height) {
return;
}
10 years ago
chatModal.find('.modal-body').css('height', module.calculateChatListHeight(chatModal));
11 years ago
});
chatModal.draggable({
start: function () {
taskbar.updateActive(uuid);
},
stop: function () {
chatModal.find('#chat-message-input').focus();
},
distance: 10,
handle: '.modal-header',
});
});
scrollStop.apply(chatModal.find('[component="chat/messages"]'));
chatModal.find('#chat-close-btn').on('click', function () {
module.close(chatModal);
});
10 years ago
function gotoChats() {
var text = components.get('chat/input').val();
$(window).one('action:ajaxify.end', function () {
components.get('chat/input').val(text);
});
ajaxify.go('user/' + app.user.userslug + '/chats/' + chatModal.attr('data-roomid'));
module.close(chatModal);
}
11 years ago
chatModal.find('.modal-header').on('dblclick', gotoChats);
chatModal.find('button[data-action="maximize"]').on('click', gotoChats);
8 years ago
chatModal.find('button[data-action="minimize"]').on('click', function () {
7 years ago
var uuid = chatModal.attr('data-uuid');
module.minimize(uuid);
});
10 years ago
7 years ago
chatModal.on('click', ':not(.close)', function () {
6 years ago
taskbar.updateActive(chatModal.attr('data-uuid'));
10 years ago
if (dragged) {
dragged = false;
}
});
chatModal.on('mousemove', function (e) {
if (e.which === 1) {
dragged = true;
}
});
11 years ago
chatModal.on('mousemove keypress click', function () {
if (newMessage) {
socket.emit('modules.chats.markRead', data.roomId);
newMessage = false;
}
});
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <[email protected]> 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 <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <[email protected]> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <[email protected]> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
9 years ago
Chats.addActionHandlers(chatModal.find('[component="chat/messages"]'), data.roomId);
Chats.addRenameHandler(chatModal.attr('data-roomid'), chatModal.find('[data-action="rename"]'), data.roomName);
Chats.addLeaveHandler(chatModal.attr('data-roomid'), chatModal.find('[data-action="leave"]'));
Chats.addSendHandlers(chatModal.attr('data-roomid'), chatModal.find('.chat-input'), chatModal.find('[data-action="send"]'));
7 years ago
Chats.addMemberHandler(chatModal.attr('data-roomid'), chatModal.find('[data-action="members"]'));
Chats.createAutoComplete(chatModal.find('[component="chat/input"]'));
Chats.addScrollHandler(chatModal.attr('data-roomid'), data.uid, chatModal.find('.chat-content'));
7 years ago
Chats.addCharactersLeftHandler(chatModal);
7 years ago
Chats.addIPHandler(chatModal);
8 years ago
taskbar.push('chat', chatModal.attr('data-uuid'), {
title: '[[modules:chat.chatting_with]] ' + (data.roomName || (data.users.length ? data.users[0].username : '')),
roomId: data.roomId,
icon: 'fa-comment',
state: '',
});
$(window).trigger('action:chat.loaded', chatModal);
if (typeof callback === 'function') {
callback(chatModal);
}
});
};
module.focusInput = function (chatModal) {
7 years ago
chatModal.find('[component="chat/input"]').focus();
10 years ago
};
module.close = function (chatModal) {
clearInterval(chatModal.attr('intervalId'));
chatModal.attr('intervalId', 0);
11 years ago
chatModal.remove();
chatModal.data('modal', null);
taskbar.discard('chat', chatModal.attr('data-uuid'));
10 years ago
if (chatModal.attr('data-mobile')) {
module.disableMobileBehaviour(chatModal);
}
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 = $('.chat-modal[data-uuid="' + uuid + '"]');
chatModal.removeClass('hide');
taskbar.updateActive(uuid);
ChatsMessages.scrollToBottom(chatModal.find('.chat-content'));
10 years ago
module.focusInput(chatModal);
socket.emit('modules.chats.markRead', chatModal.attr('data-roomid'));
10 years ago
var env = utils.findBootstrapEnvironment();
if (env === 'xs' || env === 'sm') {
10 years ago
module.enableMobileBehaviour(chatModal);
}
};
module.enableMobileBehaviour = function (modalEl) {
10 years ago
app.toggleNavbar(false);
modalEl.attr('data-mobile', '1');
var messagesEl = modalEl.find('.modal-body');
10 years ago
messagesEl.css('height', module.calculateChatListHeight(modalEl));
$(window).on('resize', function () {
10 years ago
messagesEl.css('height', module.calculateChatListHeight(modalEl));
});
};
module.disableMobileBehaviour = function () {
10 years ago
app.toggleNavbar(true);
};
module.calculateChatListHeight = function (modalEl) {
// Formula: modal height minus header height. Simple(tm).
return modalEl.find('.modal-content').outerHeight() - modalEl.find('.modal-header').outerHeight();
};
module.minimize = function (uuid) {
var chatModal = $('.chat-modal[data-uuid="' + uuid + '"]');
chatModal.addClass('hide');
taskbar.minimize('chat', uuid);
clearInterval(chatModal.attr('intervalId'));
chatModal.attr('intervalId', 0);
};
10 years ago
module.toggleNew = taskbar.toggleNew;
10 years ago
return module;
});