From 58b3b32cc371141f49888d24024bb783cbfe0568 Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 8 Aug 2017 17:13:10 +0200 Subject: [PATCH] big messaging refactor. split UI from logic --- www/common/common-messaging.js | 339 ++++++++++++++++++++------------- www/common/cryptpad-common.js | 2 +- www/contacts/main.js | 3 +- 3 files changed, 208 insertions(+), 136 deletions(-) diff --git a/www/common/common-messaging.js b/www/common/common-messaging.js index f11d17293..01fa6e8df 100644 --- a/www/common/common-messaging.js +++ b/www/common/common-messaging.js @@ -79,7 +79,120 @@ define([ // Messaging tools var avatars = {}; - var addToFriendListUI = function (common, $block, open, remove, f) { + // TODO make this internal to the messenger + var channels = Msg.channels = window.channels = {}; + + var UI = Msg.UI = {}; + UI.init = function (common, $listContainer, $msgContainer) { + var ui = { + containers: { + friendList: $listContainer, + messages: $msgContainer, + }, + }; + + ui.setFriendList = function (display, remove) { + UI.getFriendList(common, display, remove).appendTo($listContainer); + }; + + ui.notify = function (curvePublic) { + var $friend = $listContainer.find('.friend').filter(function (idx, el) { + return $(el).data('key') === curvePublic; + }); + $friend.addClass('notify'); + }; + + ui.unnotify = function (curvePublic) { + var $friend = $listContainer.find('.friend').filter(function (idx, el) { + return $(el).data('key') === curvePublic; + }); + $friend.removeClass('notify'); + }; + + ui.update = function (curvePublic, types) { + var data = getFriend(common, curvePublic); + var chan = channels[data.channel]; + if (!chan.ready) { + chan.updateOnReady = (chan.updateOnReady || []).concat(types); + return; + } + var $friend = $listContainer.find('.friend').filter(function (idx, el) { + return $(el).data('key') === curvePublic; + }); + if (types.indexOf('displayName') >= 0) { + $friend.find('.name').text(data.displayName); + } + if (types.indexOf('avatar') >= 0) { + $friend.find('.default').remove(); + $friend.find('media-tag').remove(); + if (data.avatar && avatars[data.avatar]) { + $friend.prepend(avatars[data.avatar]); + } else { + common.displayAvatar($friend, data.avatar, data.displayName, function ($img) { + if (data.avatar && $img) { + avatars[data.avatar] = $img[0].outerHTML; + } + }); + } + } + }; + + ui.updateStatus = function (curvePublic) { + var data = getFriend(common, curvePublic); + var chan = channels[data.channel]; + + // FIXME mixes logic and presentation + var $friend = $listContainer.find('.friend').filter(function (idx, el) { + return $(el).data('key') === curvePublic; + }); + var status = chan.userList.some(function (nId) { + return chan.mapId[nId] === curvePublic; + }); + var statusText = status ? 'online' : 'offline'; + $friend.find('.status').attr('class', 'status '+statusText); + }; + + ui.getChannel = function (curvePublic) { + // TODO extract into UI method + var $chat = $msgContainer.find('.chat').filter(function (idx, el) { + return $(el).data('key') === curvePublic; + }); + return $chat.length? $chat: null; + }; + + ui.hideInfo = function () { + $msgContainer.find('.info').hide(); + }; + + ui.showInfo = function () { + $msgContainer.find('.info').show(); + }; + + ui.createChat = function (curvePublic) { + return $('
', {'class':'chat'}) + .data('key', curvePublic).appendTo($msgContainer); + }; + + ui.hideChat = function () { + $msgContainer.find('.chat').hide(); + }; + + ui.getFriend = function (curvePublic) { + return $listContainer.find('.friend').filter(function (idx, el) { + return $(el).data('key') === curvePublic; + }); + }; + + ui.addToFriendList = function (curvePublic, display, remove) { + var $block = $listContainer.find('> div'); + UI.addToFriendList(common, $block, display, remove, curvePublic); + }; + + return ui; + }; + + // internal + UI.addToFriendList = function (common, $block, open, remove, f) { var proxy = common.getProxy(); var friends = proxy.friends || {}; if (f === "me") { return; } @@ -120,12 +233,14 @@ define([ } $('', {'class': 'status'}).appendTo($friend); }; - Msg.getFriendListUI = function (common, open, remove) { - var proxy = common.getProxy(); + + // iterate over your friends list and return a dom element containing UI + UI.getFriendList = function (common, open, remove) { + var proxy = common.getProxy(); // throws var $block = $('
'); var friends = proxy.friends || {}; Object.keys(friends).forEach(function (f) { - addToFriendListUI(common, $block, open, remove, f); + UI.addToFriendList(common, $block, open, remove, f); }); return $block; }; @@ -146,7 +261,6 @@ define([ }); }; - var channels = Msg.channels = window.channels = {}; var msgAlreadyKnown = function (channel, sig) { return channel.messages.some(function (message) { @@ -164,7 +278,7 @@ define([ if (parsedMsg[0] === Types.message) { // TODO validate messages here var res = { - type: parsedMsg[0], //Types.message, + type: parsedMsg[0], sig: sig, channel: parsedMsg[1], time: parsedMsg[2], @@ -200,6 +314,8 @@ define([ } }; + /* Broadcast a display name, profile, or avatar change to all contacts + */ var updateMyData = function (common) { var friends = getFriendList(common); var mySyncData = friends.me; @@ -226,7 +342,7 @@ define([ var onChannelReady = function (common, chanId) { if (ready.indexOf(chanId) !== -1) { return; } ready.push(chanId); - channels[chanId].updateStatus(); + channels[chanId].updateStatus(); // c'est quoi? var friends = getFriendList(common); if (ready.length === Object.keys(friends).length) { // All channels are ready @@ -323,6 +439,7 @@ define([ } }; + // TODO extract into UI method var createChatBox = function (common, $container, curvePublic, messenger) { var data = getFriend(common, curvePublic); @@ -390,6 +507,7 @@ define([ var payload = $input.val(); // Send the message + channel.sending = true; channel.send(payload, function (e) { if (e) { channel.sending = false; @@ -401,12 +519,11 @@ define([ channel.sending = false; }); }; - $('