', {'class':'chat'})
- .data('key', curvePublic).appendTo($msgContainer);
+ var $chat = ui.getChannel(curvePublic);
+ if (!$chat) {
+ $chat = ui.createChat(curvePublic);
createChatBox(common, $chat, curvePublic, messenger);
isNew = true;
}
// Show the correct div
- $msgContainer.find('.chat').hide();
+ ui.hideChat();
$chat.show();
- Msg.active = curvePublic;
+ // TODO set this attr per-messenger
+ messenger.setActive(curvePublic);
// TODO don't mark messages as read unless you have displayed them
refresh(curvePublic);
};
+ // TODO take a callback
var remove = function (curvePublic) {
var data = getFriend(common, curvePublic);
var channel = channels[data.channel];
@@ -555,106 +708,20 @@ define([
};
// Display friend list
- common.getFriendListUI(common, display, remove).appendTo($listContainer);
+ ui.setFriendList(display, remove);
- // Notify on new messages
- var notify = function (curvePublic) {
- //if (Msg.active === curvePublic) { return; }
- var $friend = $listContainer.find('.friend').filter(function (idx, el) {
- return $(el).data('key') === curvePublic;
- });
- $friend.addClass('notify');
- common.notify();
- };
- var unnotify = function (curvePublic) {
- var $friend = $listContainer.find('.friend').filter(function (idx, el) {
- return $(el).data('key') === curvePublic;
- });
- $friend.removeClass('notify');
- };
+ // TODO extract into UI method...
var removeUI = function (curvePublic) {
- var $friend = $listContainer.find('.friend').filter(function (idx, el) {
- return $(el).data('key') === curvePublic;
- });
- var $chat = $msgContainer.find('.chat').filter(function (idx, el) {
- return $(el).data('key') === curvePublic;
- });
+ var $friend = ui.getFriend(curvePublic);
+ var $chat = ui.getChannel(curvePublic);
$friend.remove();
$chat.remove();
- $msgContainer.find('.info').show();
- };
- var updateUI = 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;
- }
- });
- }
- }
- };
- var updateStatus = function (curvePublic) {
- var data = getFriend(common, curvePublic);
- var chan = channels[data.channel];
- 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);
- };
- var getMoreHistory = function (network, chan, hash, count) {
- var msg = [
- 'GET_HISTORY_RANGE',
- chan.id,
- {
- from: hash,
- count: count,
- }
- ];
-
- console.log(msg);
-
- network.sendto(network.historyKeeper, JSON.stringify(msg)).then(function (a, b, c) {
- console.log(a, b, c);
- }, function (err) {
- throw new Error(err);
- });
- };
-
- var getChannelMessagesSince = function (network, chan, data, keys) {
- var cfg = {
- validateKey: keys.validateKey,
- owners: [proxy.edPublic, data.edPublic],
- lastKnownHash: data.lastKnownHash
- };
- var msg = ['GET_HISTORY', chan.id, cfg];
- network.sendto(network.historyKeeper, JSON.stringify(msg))
- .then($.noop, function (err) {
- throw new Error(err);
- });
+ ui.showInfo();
};
// Open the channels
+
+ // TODO extract this into an external function
var openFriendChannel = function (f) {
if (f === "me") { return; }
var data = friends[f];
@@ -668,11 +735,14 @@ define([
encryptor: encryptor,
messages: [],
refresh: function () { refresh(data.curvePublic); },
- notify: function () { notify(data.curvePublic); },
- unnotify: function () { unnotify(data.curvePublic); },
+ notify: function () {
+ ui.notify(data.curvePublic);
+ common.notify();
+ },
+ unnotify: function () { ui.unnotify(data.curvePublic); },
removeUI: function () { removeUI(data.curvePublic); },
- updateUI: function (types) { updateUI(data.curvePublic, types); },
- updateStatus: function () { updateStatus(data.curvePublic); },
+ updateUI: function (types) { ui.update(data.curvePublic, types); },
+ updateStatus: function () { ui.updateStatus(data.curvePublic); },
setLastMessageRead: function (hash) {
data.lastKnownHash = hash;
},
@@ -680,10 +750,10 @@ define([
return data.lastKnownHash;
},
isActive: function () {
- return data.curvePublic === Msg.active;
+ return data.curvePublic === messenger.active;
},
getMessagesSinceDisconnect: function () {
- getChannelMessagesSince(network, chan, data, keys);
+ getChannelMessagesSince(network, proxy, chan, data, keys);
},
wc: chan,
userList: [],
@@ -713,7 +783,6 @@ define([
var msg = [Types.message, proxy.curvePublic, +new Date(), payload];
var msgStr = JSON.stringify(msg);
var cryptMsg = channel.encryptor.encrypt(msgStr);
- channel.sending = true;
channel.wc.bcast(cryptMsg).then(function () {
pushMsg(common, channel, cryptMsg);
@@ -752,7 +821,7 @@ define([
channel.updateStatus();
});
- getChannelMessagesSince(network, chan, data, keys);
+ getChannelMessagesSince(network, proxy, chan, data, keys);
}, function (err) {
console.error(err);
});
@@ -768,6 +837,7 @@ define([
Object.keys(friends).forEach(openFriendChannel);
};
+ // TODO split out into UI
messenger.setEditable = function (bool) {
bool = !bool;
var input = messenger.input;
@@ -790,14 +860,14 @@ define([
openFriendChannels();
+ // TODO split loop innards into ui methods
var checkNewFriends = function () {
Object.keys(friends).forEach(function (f) {
- var $friend = $listContainer.find('.friend').filter(function (idx, el) {
- return $(el).data('key') === f;
- });
+ var $friend = ui.getFriend(f);
+
if (!$friend.length) {
openFriendChannel(f);
- addToFriendListUI(common, $listContainer.find('> div'), display, remove, f);
+ ui.addToFriendList(f, display, remove);
}
});
};
@@ -939,6 +1009,7 @@ define([
if (pendingRequests.indexOf(netfluxId) === -1) {
pendingRequests.push(netfluxId);
var proxy = common.getProxy();
+ // FIXME this name doesn't indicate what it does
common.changeDisplayName(proxy[common.displayNameKey]);
}
network.sendto(netfluxId, msgStr);
diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js
index 437d91de6..c95c61fa6 100644
--- a/www/common/cryptpad-common.js
+++ b/www/common/cryptpad-common.js
@@ -125,10 +125,10 @@ define([
common.createOwnedChannel = Messaging.createOwnedChannel;
common.getFriendList = Messaging.getFriendList;
common.getFriendChannelsList = Messaging.getFriendChannelsList;
- common.getFriendListUI = Messaging.getFriendListUI;
common.createData = Messaging.createData;
common.getPendingInvites = Messaging.getPending;
common.getLatestMessages = Messaging.getLatestMessages;
+ common.initMessagingUI = Messaging.UI.init;
// Userlist
common.createUserList = UserList.create;
diff --git a/www/contacts/main.js b/www/contacts/main.js
index 355e8822a..4803a812a 100644
--- a/www/contacts/main.js
+++ b/www/contacts/main.js
@@ -51,7 +51,8 @@ define([
APP.messenger.setEditable(true);
});
- APP.messenger = Cryptpad.initMessaging(Cryptpad, $list, $messages);
+ var ui = APP.ui = Cryptpad.initMessagingUI(Cryptpad, $list, $messages);
+ APP.messenger = Cryptpad.initMessaging(Cryptpad, ui);
var $infoBlock = $('
', {'class': 'info'}).appendTo($messages);
$('
').text(Messages.contacts_info1).appendTo($infoBlock);