From 0a9f82e30d0912aa5f8e6e3206874ffe1d43844c Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 18 Sep 2015 14:30:18 -0400 Subject: [PATCH] removed some dupe code from chats --- public/src/client/chats.js | 65 +++++++++++++++++++++++--------------- public/src/modules/chat.js | 57 +++++---------------------------- 2 files changed, 48 insertions(+), 74 deletions(-) diff --git a/public/src/client/chats.js b/public/src/client/chats.js index e350cc983f..333c022658 100644 --- a/public/src/client/chats.js +++ b/public/src/client/chats.js @@ -1,6 +1,6 @@ '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) { var Chats = { @@ -10,8 +10,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', var newMessage = false; Chats.init = function() { - var containerEl = $('.expanded-chat ul'), - env = utils.findBootstrapEnvironment(); + var env = utils.findBootstrapEnvironment(); if (!Chats.initialised) { 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'); - $('.expanded-chat [data-since]').removeClass('selected'); + sinceEl.removeClass('selected'); $(this).addClass('selected'); - loadChatSince(since); + Chats.loadChatSince(toUid, chatContentEl, since); return false; }); }; @@ -111,17 +114,20 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', }); }; - function loadChatSince(since) { - var uid = Chats.getRecipientUid(); - if (!uid) { + Chats.loadChatSince = function(toUid, chatContentEl, since) { + if (!toUid) { return; } - socket.emit('modules.chats.get', {touid: uid, since: since}, function(err, messages) { - var chatContent = $('.expanded-chat .chat-content'); - chatContent.find('.chat-message').remove(); - Chats.parseMessage(messages, onMessagesParsed); + socket.emit('modules.chats.get', {touid: toUid, since: since}, function(err, messages) { + if (err) { + return app.alertError(err.message); + } + + chatContentEl.find('.chat-message').remove(); + + Chats.appendChatMessage(chatContentEl, messages); }); - } + }; Chats.addGlobalEventListeners = function() { $(window).on('resize', Chats.resizeMainWindow); @@ -136,26 +142,34 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', }); }; - function onMessagesParsed(html) { - var newMessage = $(html), - chatContainer = $('.chat-content'); - newMessage.appendTo(chatContainer); + Chats.appendChatMessage = function(chatContentEl, data) { + + var lastSpeaker = parseInt(chatContentEl.find('.chat-message').last().attr('data-uid'), 10); + 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('img:not(.not-responsive)').addClass('img-responsive'); - Chats.scrollToBottom($('.expanded-chat .chat-content')); + Chats.scrollToBottom(chatContentEl); } Chats.addSocketListeners = function() { 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)) { newMessage = data.self === 0; 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 { var contactEl = $('[component="chat/recent"] li[data-uid="' + data.withUid + '"]'), userKey = data.withUid === data.message.fromuid ? 'fromUser' : 'toUser'; @@ -208,6 +222,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', fromTop = messagesList.offset().top; messagesList.height($(window).height() - (fromTop + inputHeight + (margin * 4))); + components.get('chat/recent').height($('.expanded-chat').height()); } Chats.setActive(); diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index cadc3ef113..329621d204 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -18,7 +18,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra return; } - module.loadChats(chatsListEl); + module.loadChatsDropdown(chatsListEl); }); socket.on('event:chats.receive', function(data) { @@ -36,7 +36,8 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra newMessage = data.self === 0; if (module.modalExists(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")) { 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; 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); } chats = chats.users; - var userObj; chatsListEl.empty(); @@ -294,19 +294,13 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra } }); - chatModal.find('[data-since]').on('click', function() { - var since = $(this).attr('data-since'); - chatModal.find('[data-since]').removeClass('selected'); - $(this).addClass('selected'); - loadChatSince(chatModal, since); - return false; - }); + Chats.addSinceHandler(chatModal.attr('touid'), chatModal.find('.chat-content'), chatModal.find('[data-since]')); Chats.addSendHandlers(chatModal.attr('touid'), chatModal.find('#chat-message-input'), chatModal.find('#chat-message-send-btn')); - getChatMessages(chatModal, function() { - checkStatus(chatModal); - }); + Chats.loadChatSince(chatModal.attr('touid'), chatModal.find('.chat-content'), 'recent') + + checkStatus(chatModal); module.canMessage(data.touid, function(err) { if (err) { @@ -421,41 +415,6 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra 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.canMessage = function(toUid, callback) {