From a082039ecbe3bc7c88810a547668ee7ec543706f Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Wed, 23 Mar 2016 12:15:44 +0200 Subject: [PATCH] Keep autocompletion sort whenever an user joins --- client/js/lounge.js | 49 ++++++++++++++++++++++++++----------------- client/views/chat.tpl | 4 +--- src/client.js | 2 +- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index 3106e094..ffbcffbe 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -153,7 +153,7 @@ $(function() { channels: channels }) ); - channels.forEach(renderChannelMessages); + channels.forEach(renderChannel); confirmExit(); } @@ -191,7 +191,7 @@ $(function() { channels: [data.chan] }) ); - renderChannelMessages(data.chan); + renderChannel(data.chan); var chan = sidebar.find(".chan") .sort(function(a, b) { return $(a).data("id") - $(b).data("id"); }) .last(); @@ -260,11 +260,38 @@ $(function() { }, $(document.createDocumentFragment())); } + function renderChannel(data) { + renderChannelMessages(data); + renderChannelUsers(data); + } + function renderChannelMessages(data) { var documentFragment = buildChannelMessages(data.id, data.messages); chat.find("#chan-" + data.id + " .messages").append(documentFragment); } + function renderChannelUsers(data) { + var users = chat.find("#chan-" + data.id).find(".users"); + var nicks = users.data("nicks") || []; + var i, oldSortOrder = {}; + + for (i in nicks) { + oldSortOrder[nicks[i]] = i; + } + + nicks = []; + + for (i in data.users) { + nicks.push(data.users[i].name); + } + + nicks = nicks.sort(function(a, b) { + return (oldSortOrder[a] || Number.MAX_VALUE) - (oldSortOrder[b] || Number.MAX_VALUE); + }); + + users.html(render("user", data)).data("nicks", nicks); + } + socket.on("msg", function(data) { var msg = buildChatMessage(data); var target = "#chan-" + data.chan; @@ -401,14 +428,7 @@ $(function() { } }); - socket.on("names", function(data) { - var users = chat.find("#chan-" + data.chan).find(".users").html(render("user", data)); - var nicks = []; - for (var i in data.users) { - nicks.push(data.users[i].name); - } - users.data("nicks", nicks); - }); + socket.on("names", renderChannelUsers); var userStyles = $("#user-specified-css"); var settings = $("#settings"); @@ -966,15 +986,6 @@ $(function() { var users = chat.find(".active").find(".users"); var nicks = users.data("nicks"); - if (!nicks) { - nicks = []; - users.find(".user").each(function() { - var nick = $(this).text().replace(/[~&@%+]/, ""); - nicks.push(nick); - }); - users.data("nicks", nicks); - } - for (var i in nicks) { words.push(nicks[i]); } diff --git a/client/views/chat.tpl b/client/views/chat.tpl index cf929e92..39e6da0a 100644 --- a/client/views/chat.tpl +++ b/client/views/chat.tpl @@ -18,9 +18,7 @@
{{/each}} diff --git a/src/client.js b/src/client.js index 561492ce..2d0b01f4 100644 --- a/src/client.js +++ b/src/client.js @@ -340,7 +340,7 @@ Client.prototype.names = function(data) { } client.emit("names", { - chan: target.chan.id, + id: target.chan.id, users: target.chan.users }); };