removed some dupe code from chats

v1.18.x
barisusakli 9 years ago
parent 2f94307125
commit 0a9f82e30d

@ -1,6 +1,6 @@
'use strict'; 'use strict';
/* globals define, app, ajaxify, utils, socket, templates */ /* globals define, config, app, ajaxify, utils, socket, templates */
define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', 'translator'], function(components, S, sounds, infinitescroll, translator) { define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', 'translator'], function(components, S, sounds, infinitescroll, translator) {
var Chats = { var Chats = {
@ -10,8 +10,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
var newMessage = false; var newMessage = false;
Chats.init = function() { Chats.init = function() {
var containerEl = $('.expanded-chat ul'), var env = utils.findBootstrapEnvironment();
env = utils.findBootstrapEnvironment();
if (!Chats.initialised) { if (!Chats.initialised) {
Chats.addSocketListeners(); Chats.addSocketListeners();
@ -76,11 +75,15 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
} }
}); });
$('.expanded-chat [data-since]').on('click', function() { Chats.addSinceHandler(Chats.getRecipientUid(), $('.expanded-chat .chat-content'), $('.expanded-chat [data-since]'));
};
Chats.addSinceHandler = function(toUid, chatContentEl, sinceEl) {
sinceEl.on('click', function() {
var since = $(this).attr('data-since'); var since = $(this).attr('data-since');
$('.expanded-chat [data-since]').removeClass('selected'); sinceEl.removeClass('selected');
$(this).addClass('selected'); $(this).addClass('selected');
loadChatSince(since); Chats.loadChatSince(toUid, chatContentEl, since);
return false; return false;
}); });
}; };
@ -111,17 +114,20 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
}); });
}; };
function loadChatSince(since) { Chats.loadChatSince = function(toUid, chatContentEl, since) {
var uid = Chats.getRecipientUid(); if (!toUid) {
if (!uid) {
return; return;
} }
socket.emit('modules.chats.get', {touid: uid, since: since}, function(err, messages) { socket.emit('modules.chats.get', {touid: toUid, since: since}, function(err, messages) {
var chatContent = $('.expanded-chat .chat-content'); if (err) {
chatContent.find('.chat-message').remove(); return app.alertError(err.message);
Chats.parseMessage(messages, onMessagesParsed); }
chatContentEl.find('.chat-message').remove();
Chats.appendChatMessage(chatContentEl, messages);
}); });
} };
Chats.addGlobalEventListeners = function() { Chats.addGlobalEventListeners = function() {
$(window).on('resize', Chats.resizeMainWindow); $(window).on('resize', Chats.resizeMainWindow);
@ -136,26 +142,34 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
}); });
}; };
function onMessagesParsed(html) { Chats.appendChatMessage = function(chatContentEl, data) {
var newMessage = $(html),
chatContainer = $('.chat-content'); var lastSpeaker = parseInt(chatContentEl.find('.chat-message').last().attr('data-uid'), 10);
newMessage.appendTo(chatContainer); if (!Array.isArray(data)) {
data.newSet = lastSpeaker !== data.fromuid;
}
Chats.parseMessage(data, function(html) {
onMessagesParsed(chatContentEl, html);
});
};
function onMessagesParsed(chatContentEl, html) {
var newMessage = $(html);
newMessage.appendTo(chatContentEl);
newMessage.find('.timeago').timeago(); newMessage.find('.timeago').timeago();
newMessage.find('img:not(.not-responsive)').addClass('img-responsive'); newMessage.find('img:not(.not-responsive)').addClass('img-responsive');
Chats.scrollToBottom($('.expanded-chat .chat-content')); Chats.scrollToBottom(chatContentEl);
} }
Chats.addSocketListeners = function() { Chats.addSocketListeners = function() {
socket.on('event:chats.receive', function(data) { socket.on('event:chats.receive', function(data) {
var typingNotifEl = $('.user-typing'),
containerEl = $('.expanded-chat ul'),
lastSpeaker = parseInt(containerEl.find('.chat-message').last().attr('data-uid'), 10);
if (Chats.isCurrentChat(data.withUid)) { if (Chats.isCurrentChat(data.withUid)) {
newMessage = data.self === 0; newMessage = data.self === 0;
data.message.self = data.self; data.message.self = data.self;
data.message.newSet = lastSpeaker !== data.message.fromuid;
Chats.parseMessage(data.message, onMessagesParsed); Chats.appendChatMessage($('.expanded-chat .chat-content'), data.message);
} else { } else {
var contactEl = $('[component="chat/recent"] li[data-uid="' + data.withUid + '"]'), var contactEl = $('[component="chat/recent"] li[data-uid="' + data.withUid + '"]'),
userKey = data.withUid === data.message.fromuid ? 'fromUser' : 'toUser'; userKey = data.withUid === data.message.fromuid ? 'fromUser' : 'toUser';
@ -208,6 +222,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
fromTop = messagesList.offset().top; fromTop = messagesList.offset().top;
messagesList.height($(window).height() - (fromTop + inputHeight + (margin * 4))); messagesList.height($(window).height() - (fromTop + inputHeight + (margin * 4)));
components.get('chat/recent').height($('.expanded-chat').height());
} }
Chats.setActive(); Chats.setActive();

@ -18,7 +18,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
return; return;
} }
module.loadChats(chatsListEl); module.loadChatsDropdown(chatsListEl);
}); });
socket.on('event:chats.receive', function(data) { socket.on('event:chats.receive', function(data) {
@ -36,7 +36,8 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
newMessage = data.self === 0; newMessage = data.self === 0;
if (module.modalExists(data.withUid)) { if (module.modalExists(data.withUid)) {
var modal = module.getModal(data.withUid); var modal = module.getModal(data.withUid);
module.appendChatMessage(modal, data.message);
Chats.appendChatMessage(modal.find('.chat-content'), data.message);
if (modal.is(":visible")) { if (modal.is(":visible")) {
taskbar.updateActive(modal.attr('UUID')); taskbar.updateActive(modal.attr('UUID'));
@ -130,7 +131,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
}); });
}; };
module.loadChats = function(chatsListEl) { module.loadChatsDropdown = function(chatsListEl) {
var dropdownEl; var dropdownEl;
socket.emit('modules.chats.getRecentChats', {after: 0}, function(err, chats) { socket.emit('modules.chats.getRecentChats', {after: 0}, function(err, chats) {
@ -138,7 +139,6 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
return app.alertError(err.message); return app.alertError(err.message);
} }
chats = chats.users; chats = chats.users;
var userObj;
chatsListEl.empty(); chatsListEl.empty();
@ -294,19 +294,13 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
} }
}); });
chatModal.find('[data-since]').on('click', function() { Chats.addSinceHandler(chatModal.attr('touid'), chatModal.find('.chat-content'), chatModal.find('[data-since]'));
var since = $(this).attr('data-since');
chatModal.find('[data-since]').removeClass('selected');
$(this).addClass('selected');
loadChatSince(chatModal, since);
return false;
});
Chats.addSendHandlers(chatModal.attr('touid'), chatModal.find('#chat-message-input'), chatModal.find('#chat-message-send-btn')); Chats.addSendHandlers(chatModal.attr('touid'), chatModal.find('#chat-message-input'), chatModal.find('#chat-message-send-btn'));
getChatMessages(chatModal, function() { Chats.loadChatSince(chatModal.attr('touid'), chatModal.find('.chat-content'), 'recent')
checkStatus(chatModal);
}); checkStatus(chatModal);
module.canMessage(data.touid, function(err) { module.canMessage(data.touid, function(err) {
if (err) { if (err) {
@ -421,41 +415,6 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
Chats.notifyTyping(chatModal.attr('touid'), false); Chats.notifyTyping(chatModal.attr('touid'), false);
}; };
function getChatMessages(chatModal, callback) {
socket.emit('modules.chats.get', {touid: chatModal.attr('touid'), since: 'recent'}, function(err, messages) {
module.appendChatMessage(chatModal, messages, callback);
});
}
function loadChatSince(chatModal, since, callback) {
socket.emit('modules.chats.get', {touid: chatModal.attr('touid'), since: since}, function(err, messages) {
var chatContent = chatModal.find('.chat-content');
chatContent.find('.chat-message').remove();
module.appendChatMessage(chatModal, messages, callback);
});
}
module.appendChatMessage = function(chatModal, data, done) {
var chatContent = chatModal.find('.chat-content'),
lastSpeaker = parseInt(chatContent.find('.chat-message').last().attr('data-uid'), 10);
if (!Array.isArray(data)) {
data.newSet = lastSpeaker !== data.fromuid;
}
Chats.parseMessage(data, function(html) {
var message = $(html);
message.appendTo(chatContent);
message.find('img:not(.not-responsive)').addClass('img-responsive');
message.find('.timeago').timeago();
Chats.scrollToBottom(chatContent);
if (typeof done === 'function') {
done();
}
});
};
module.toggleNew = taskbar.toggleNew; module.toggleNew = taskbar.toggleNew;
module.canMessage = function(toUid, callback) { module.canMessage = function(toUid, callback) {

Loading…
Cancel
Save