From 706943af26dbb55bf99f8ed11b0daf39ebf31e0c Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 12 Sep 2017 15:40:26 +0300 Subject: [PATCH] Move unread marker when loading more history Fixes #835 --- client/js/lounge.js | 1 + client/js/render.js | 22 ++++++++++++++-------- client/js/socket-events/more.js | 24 +++++++++++++++++++++++- client/js/socket-events/msg.js | 1 + 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index 4fececa3..d9998eb0 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -371,6 +371,7 @@ $(function() { lastActiveChan .find(".unread-marker") + .data("unread-id", 0) .appendTo(lastActiveChan.find(".messages")); var chan = $(target) diff --git a/client/js/render.js b/client/js/render.js index 8150f6a1..cb0adc36 100644 --- a/client/js/render.js +++ b/client/js/render.js @@ -120,19 +120,25 @@ function renderChannelMessages(data) { const documentFragment = buildChannelMessages(data.id, data.type, data.messages); const channel = chat.find("#chan-" + data.id + " .messages").append(documentFragment); - if (data.firstUnread > 0) { - const first = channel.find("#msg-" + data.firstUnread); + const template = $(templates.unread_marker()); + + if (data.firstUnread > 0) { + let first = channel.find("#msg-" + data.firstUnread); - // TODO: If the message is far off in the history, we still need to append the marker into DOM if (!first.length) { - channel.prepend(templates.unread_marker()); - } else if (first.parent().hasClass("condensed")) { - first.parent().before(templates.unread_marker()); + template.data("unread-id", data.firstUnread); + channel.prepend(template); } else { - first.before(templates.unread_marker()); + const parent = first.parent(); + + if (parent.hasClass("condensed")) { + first = parent; + } + + first.before(template); } } else { - channel.append(templates.unread_marker()); + channel.append(template); } } diff --git a/client/js/socket-events/more.js b/client/js/socket-events/more.js index 9183bfef..f84dc14c 100644 --- a/client/js/socket-events/more.js +++ b/client/js/socket-events/more.js @@ -34,7 +34,29 @@ socket.on("more", function(data) { // Add the older messages const documentFragment = render.buildChannelMessages(data.chan, type, data.messages); - chan.prepend(documentFragment).end(); + chan.prepend(documentFragment); + + // Move unread marker to correct spot if needed + const unreadMarker = chan.find(".unread-marker"); + const firstUnread = unreadMarker.data("unread-id"); + + if (firstUnread > 0) { + let first = chan.find("#msg-" + firstUnread); + + if (!first.length) { + chan.prepend(unreadMarker); + } else { + const parent = first.parent(); + + if (parent.hasClass("condensed")) { + first = parent; + } + + unreadMarker.data("unread-id", 0); + + first.before(unreadMarker); + } + } // restore scroll position const position = chan.height() - heightOld; diff --git a/client/js/socket-events/msg.js b/client/js/socket-events/msg.js index bf5c0eb9..a576451f 100644 --- a/client/js/socket-events/msg.js +++ b/client/js/socket-events/msg.js @@ -57,6 +57,7 @@ function processReceivedMessage(data) { && lastVisible.prev().hasClass("unread-marker"))) { container .find(".unread-marker") + .data("unread-id", 0) .appendTo(container); }