From cc1f6683080070fe4965c31a9251435eddc495d0 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Wed, 28 Aug 2013 22:08:46 -0400 Subject: [PATCH 01/20] changed chat online offline messages --- public/src/forum/account.js | 12 ++-- public/src/forum/footer.js | 23 +++----- public/src/forum/topic.js | 10 +++- public/src/modules/chat.js | 115 ++++++++++++++++++++++-------------- public/templates/footer.tpl | 2 +- src/websockets.js | 39 +----------- 6 files changed, 98 insertions(+), 103 deletions(-) diff --git a/public/src/forum/account.js b/public/src/forum/account.js index aba3eb33a1..5818cae7b1 100644 --- a/public/src/forum/account.js +++ b/public/src/forum/account.js @@ -54,17 +54,19 @@ var onlineStatus = $('.account-online-status'); - socket.on('api:user.isOnline', function(online) { - if(online) { + function handleUserOnline(data) { + if(data.online) { onlineStatus.find('span span').text('online'); onlineStatus.find('i').attr('class', 'icon-circle'); } else { onlineStatus.find('span span').text('offline'); onlineStatus.find('i').attr('class', 'icon-circle-blank'); } - }); - - socket.emit('api:user.isOnline', theirid); + } + + socket.on('api:user.isOnline', handleUserOnline); + + socket.emit('api:user.isOnline', theirid, handleUserOnline); }); diff --git a/public/src/forum/footer.js b/public/src/forum/footer.js index c892b300eb..4a1b52fdd5 100644 --- a/public/src/forum/footer.js +++ b/public/src/forum/footer.js @@ -162,25 +162,18 @@ socket.on('chatMessage', function(data) { require(['chat'], function(chat) { - var chatModal = chat.createModalIfDoesntExist(data.username, data.fromuid, function(created, modal) { - if(!created) - chat.appendChatMessage(modal, data.message, data.timestamp); - }); + var modal = null; + if(chat.modalExists(data.fromuid)) { + modal = chat.getModal(data.fromuid); + chat.appendChatMessage(modal, data.message, data.timestamp); + } else { + modal = chat.createModal(data.username, data.fromuid); + } - chatModal.show(); - chat.bringModalToTop(chatModal); + chat.load(modal.attr('UUID')); }); }); - socket.on('chatGoOffline', function(data) { - require(['chat'], function(chat) { - if(chat.modalOpen(data.uid)) { - var modal = chat.getModal(data.uid); - chat.appendChatMessage(modal, data.username + ' went offline\n', data.timestamp); - } - }); - }) - require(['mobileMenu'], function(mobileMenu) { mobileMenu.init(); }); diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 500cab3e84..8f07b2cac3 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -288,9 +288,13 @@ return; require(['chat'], function(chat) { - var chatModal = chat.createModalIfDoesntExist(username, touid); - chatModal.modal(); - chat.bringModalToTop(chatModal); // I don't think this is necessary + var chatModal; + if(!chat.modalExists(touid)) { + chatModal = chat.createModal(username, touid); + } else { + chatModal = chat.getModal(touid); + } + chat.load(chatModal.attr('UUID')); }); }); diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index 704bf16b5a..3c67a6e170 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -17,46 +17,74 @@ define(['taskbar'], function(taskbar) { return $('#chat-modal-' + touid); } - module.modalOpen = function(touid) { + module.modalExists = function(touid) { return $('#chat-modal-' + touid).length !== 0; } - module.createModalIfDoesntExist = function(username, touid, callback) { - var chatModal = $('#chat-modal-' + touid); - - if(!chatModal.length) { - var chatModal = $('#chat-modal').clone(); - chatModal.attr('id','chat-modal-' + touid); - var uuid = utils.generateUUID(); - chatModal.attr('UUID', uuid); - chatModal.appendTo($('body')); - chatModal.draggable({ - start:function(){ - module.bringModalToTop(chatModal); + + function checkStatus(chatModal, callback) { + socket.emit('api:user.isOnline', chatModal.touid, function(data) { + if(data.online !== chatModal.online) { + if(data.online) { + module.appendChatMessage(chatModal, chatModal.username + ' has come online.\n', data.timestamp); + } else { + module.appendChatMessage(chatModal, chatModal.username + ' has gone offline.\n', data.timestamp); } - }); - chatModal.find('#chat-with-name').html(username); - - chatModal.find('.close').on('click', function(e) { - chatModal.hide(); - taskbar.discard('chat', uuid); - }); - - chatModal.on('click', function(e) { + chatModal.online = data.online; + } + if(callback) + callback(data.online); + }); + } + + function checkOnlineStatus(chatModal) { + if(chatModal.intervalId === 0) { + chatModal.intervalId = setInterval(function(){ + checkStatus(chatModal); + }, 1000); + } + } + + module.createModal = function(username, touid, callback) { + + var chatModal = $('#chat-modal').clone(), + uuid = utils.generateUUID(); + + chatModal.intervalId = 0; + chatModal.touid = touid; + chatModal.username = username; + + chatModal.attr('id', 'chat-modal-' + touid); + chatModal.attr('UUID', uuid); + chatModal.appendTo($('body')); + chatModal.draggable({ + start:function() { module.bringModalToTop(chatModal); - }); - - addSendHandler(chatModal, touid); - - getChatMessages(chatModal, touid, callback); + } + }); + + chatModal.find('#chat-with-name').html(username); + + chatModal.find('.close').on('click', function(e) { + clearInterval(chatModal.intervalId); + chatModal.intervalId = 0; + chatModal.hide(); + taskbar.discard('chat', uuid); + }); - taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with '+username}); - return chatModal; - } + chatModal.on('click', function(e) { + module.bringModalToTop(chatModal); + }); + + addSendHandler(chatModal); - if(callback) - callback(false, chatModal); + checkStatus(chatModal, function(online) { + chatModal.online = online; + getChatMessages(chatModal, function() { + checkOnlineStatus(chatModal); + }); + }); - taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with '+username}); + taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with ' + username}); return chatModal; } @@ -64,45 +92,46 @@ define(['taskbar'], function(taskbar) { var chatModal = $('div[UUID="'+uuid+'"]'); chatModal.show(); module.bringModalToTop(chatModal); + checkOnlineStatus(chatModal); } module.minimize = function(uuid) { var chatModal = $('div[UUID="'+uuid+'"]'); chatModal.hide(); taskbar.minimize('chat', uuid); + clearInterval(chatModal.intervalId); + chatModal.intervalId = 0; } - function getChatMessages(chatModal, touid, callback) { - socket.emit('getChatMessages', {touid:touid}, function(messages) { + function getChatMessages(chatModal, callback) { + socket.emit('getChatMessages', {touid:chatModal.touid}, function(messages) { for(var i = 0; i - -