From 07a82ec12c1bb4f706e851819bb9eeb94155c505 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 7 Jul 2014 12:26:17 -0400 Subject: [PATCH] more WIP --- public/language/en_GB/global.json | 1 + public/src/forum/chats.js | 45 ++++++++++++++++++++----------- public/src/modules/chat.js | 27 +++++-------------- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/public/language/en_GB/global.json b/public/language/en_GB/global.json index 83dd0517f5..dab7fd1df6 100644 --- a/public/language/en_GB/global.json +++ b/public/language/en_GB/global.json @@ -32,6 +32,7 @@ "header.tags": "Tags", "header.popular": "Popular", "header.users": "Users", + "header.chats": "Chats", "header.notifications": "Notifications", "header.search": "Search", "header.profile": "Profile", diff --git a/public/src/forum/chats.js b/public/src/forum/chats.js index 6d48158cad..1341be1c14 100644 --- a/public/src/forum/chats.js +++ b/public/src/forum/chats.js @@ -2,7 +2,7 @@ /* globals define, app*/ -define('forum/chats', function() { +define('forum/chats', ['string','sounds'], function(S, sounds) { var Chats = {}; Chats.init = function() { @@ -24,31 +24,32 @@ define('forum/chats', function() { }; Chats.addEventListeners = function() { - var inputEl = $('.chat-input'); + var inputEl = $('.chat-input'), + sendEl = $('.expanded-chat button[data-action="send"]'); $('.chats-list').on('click', 'li', function(e) { // app.openChat($(this).attr('data-username'), $(this).attr('data-uid')); ajaxify.go('chats/' + utils.slugify($(this).attr('data-username'))); }); - // inputEl.off('keypress').on('keypress', function(e) { - // if(e.which === 13) { - // Chat.sendMessage(chatModal); - // } - // }); + inputEl.off('keypress').on('keypress', function(e) { + if(e.which === 13) { + Chats.sendMessage(Chats.getRecipientUid(), inputEl); + } + }); inputEl.off('keyup').on('keyup', function() { if ($(this).val()) { - Chats.notifyTyping(true); + Chats.notifyTyping(Chats.getRecipientUid(), true); } else { - Chats.notifyTyping(false); + Chats.notifyTyping(Chats.getRecipientUid(), false); } }); - // chatModal.find('#chat-message-send-btn').off('click').on('click', function(e){ - // sendMessage(chatModal); - // return false; - // }); + sendEl.off('click').on('click', function(e) { + Chats.sendMessage(Chats.getRecipientUid(), inputEl); + return false; + }); }; Chats.addSocketListeners = function() { @@ -71,12 +72,26 @@ define('forum/chats', function() { }); }; - Chats.notifyTyping = function(typing) { + Chats.notifyTyping = function(toUid, typing) { socket.emit('modules.chats.user' + (typing ? 'Start' : 'Stop') + 'Typing', { - touid: Chats.getRecipientUid(), + touid: toUid, fromUid: app.uid }); }; + Chats.sendMessage = function(toUid, inputEl) { + var msg = S(inputEl.val()).stripTags().s; + if (msg.length) { + msg = msg +'\n'; + socket.emit('modules.chats.send', { + touid:toUid, + message:msg + }); + inputEl.val(''); + sounds.play('chat-outgoing'); + Chats.notifyTyping(toUid, false); + } + }; + return Chats; }); diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index 5143179a6d..97339eb882 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -1,7 +1,7 @@ "use strict"; /* globals app, config, define, socket, translator, templates, utils */ -define('chat', ['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) { +define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar, S, sounds, Chats) { var module = {}; @@ -231,7 +231,7 @@ define('chat', ['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) { chatModal.remove(); chatModal.data('modal', null); taskbar.discard('chat', chatModal.attr('UUID')); - notifyStopTyping(chatModal.touid); + Chats.notifyTyping(chatModal.touid, false); }; module.center = function(chatModal) { @@ -258,13 +258,9 @@ define('chat', ['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) { taskbar.minimize('chat', uuid); clearInterval(chatModal.intervalId); chatModal.intervalId = 0; - notifyStopTyping(chatModal.touid); + Chats.notifyTyping(chatModal.touid, false); }; - function notifyStopTyping(touid) { - socket.emit('modules.chats.userStopTyping', {touid:touid, fromUid: app.uid}); - } - function getChatMessages(chatModal, callback) { socket.emit('modules.chats.get', {touid:chatModal.touid}, function(err, messages) { for(var i = 0; i