From 58580bd866d01227d1a760759e7e395ae5cd0a42 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Sun, 5 Jan 2014 22:09:13 -0500 Subject: [PATCH] closes #716, also moved chat to its own template --- public/src/app.js | 12 +++-- public/src/forum/footer.js | 10 ++-- public/src/modules/chat.js | 92 ++++++++++++++++++++----------------- public/templates/chat.tpl | 18 ++++++++ public/templates/footer.tpl | 19 -------- src/routes/debug.js | 1 + 6 files changed, 80 insertions(+), 72 deletions(-) create mode 100644 public/templates/chat.tpl diff --git a/public/src/app.js b/public/src/app.js index f379358e7a..415f10ae6d 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -391,14 +391,16 @@ var socket, } require(['chat'], function (chat) { - var chatModal; if (!chat.modalExists(touid)) { - chatModal = chat.createModal(username, touid); + chat.createModal(username, touid, loadAndCenter); } else { - chatModal = chat.getModal(touid); + loadAndCenter(chat.getModal(touid)); + } + + function loadAndCenter(chatModal) { + chat.load(chatModal.attr('UUID')); + chat.center(chatModal); } - chat.load(chatModal.attr('UUID')); - chat.center(chatModal); }); }; diff --git a/public/src/forum/footer.js b/public/src/forum/footer.js index 6e622c4a96..c10d951088 100644 --- a/public/src/forum/footer.js +++ b/public/src/forum/footer.js @@ -227,9 +227,8 @@ socket.on('event:chats.receive', function(data) { require(['chat'], function(chat) { - var modal = null; if (chat.modalExists(data.fromuid)) { - modal = chat.getModal(data.fromuid); + var modal = chat.getModal(data.fromuid); chat.appendChatMessage(modal, data.message, data.timestamp); if (modal.is(":visible")) { @@ -242,9 +241,10 @@ app.alternatingTitle(data.username + ' has messaged you'); } } else { - modal = chat.createModal(data.username, data.fromuid); - chat.toggleNew(modal.attr('UUID'), true); - app.alternatingTitle(data.username + ' has messaged you'); + chat.createModal(data.username, data.fromuid, function(modal) { + chat.toggleNew(modal.attr('UUID'), true); + app.alternatingTitle(data.username + ' has messaged you'); + }); } }); }); diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index cddf3a0c22..ce8bb19ebe 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -46,59 +46,65 @@ define(['taskbar', 'string'], function(taskbar, S) { 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); - } - }); - - chatModal.find('#chat-with-name').html(username); - - chatModal.find('.close').on('click', function(e) { - clearInterval(chatModal.intervalId); - chatModal.intervalId = 0; - chatModal.remove(); - chatModal.data('modal', null); - taskbar.discard('chat', uuid); - }); - - chatModal.on('click', function(e) { - module.bringModalToTop(chatModal); + templates.preload_template('chat', function() { + translator.translate(templates['chat'].parse({}), function (chatTpl) { + + var chatModal = $(chatTpl), + 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); + } + }); + + chatModal.find('#chat-with-name').html(username); + + chatModal.find('.close').on('click', function(e) { + clearInterval(chatModal.intervalId); + chatModal.intervalId = 0; + chatModal.remove(); + chatModal.data('modal', null); + taskbar.discard('chat', uuid); + }); + + chatModal.on('click', function(e) { + module.bringModalToTop(chatModal); + }); + + addSendHandler(chatModal); + + getChatMessages(chatModal, function() { + checkOnlineStatus(chatModal); + }); + + taskbar.push('chat', chatModal.attr('UUID'), { + title:' ' + username, + state: '' + }); + + callback(chatModal); + }); }); - - addSendHandler(chatModal); - - getChatMessages(chatModal, function() { - checkOnlineStatus(chatModal); - }); - - taskbar.push('chat', chatModal.attr('UUID'), { - title:' ' + username, - state: '' - }); - - return chatModal; } module.center = function(chatModal) { chatModal.css("position", "fixed"); chatModal.css("left", Math.max(0, (($(window).width() - $(chatModal).outerWidth()) / 2) + $(window).scrollLeft()) + "px"); + chatModal.css("top", "0px"); return chatModal; } module.load = function(uuid) { var chatModal = $('div[UUID="'+uuid+'"]'); - chatModal.show(); + chatModal.removeClass('hide'); module.bringModalToTop(chatModal); checkOnlineStatus(chatModal); taskbar.updateActive(uuid); @@ -108,7 +114,7 @@ define(['taskbar', 'string'], function(taskbar, S) { module.minimize = function(uuid) { var chatModal = $('div[UUID="'+uuid+'"]'); - chatModal.hide(); + chatModal.addClass('hide'); taskbar.minimize('chat', uuid); clearInterval(chatModal.intervalId); chatModal.intervalId = 0; diff --git a/public/templates/chat.tpl b/public/templates/chat.tpl new file mode 100644 index 0000000000..d32f3b0da5 --- /dev/null +++ b/public/templates/chat.tpl @@ -0,0 +1,18 @@ + + \ No newline at end of file diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl index 9eb77e1cc4..e461beae4f 100644 --- a/public/templates/footer.tpl +++ b/public/templates/footer.tpl @@ -2,25 +2,6 @@ - -