From 1bdbb75d4cae2c98f3fd9f06b30f4e38bd4568ba Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Wed, 6 Dec 2017 14:07:43 +0200 Subject: [PATCH] Merge condensed messages when loading more history --- client/js/condensed.js | 14 +++++++++++--- client/js/render.js | 7 +++++-- client/js/socket-events/more.js | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/client/js/condensed.js b/client/js/condensed.js index a7c49668..ee9726f8 100644 --- a/client/js/condensed.js +++ b/client/js/condensed.js @@ -1,21 +1,29 @@ "use strict"; +const _ = require("lodash"); const constants = require("./constants"); const templates = require("../views"); module.exports = { updateText, + getStoredTypes, }; -function updateText(condensed, addedTypes) { +function getStoredTypes(condensed) { const obj = {}; constants.condensedTypes.forEach((type) => { obj[type] = condensed.data(type) || 0; }); - addedTypes.forEach((type) => { - obj[type]++; + return obj; +} + +function updateText(condensed, addedTypes) { + const obj = getStoredTypes(condensed); + + _.forOwn(addedTypes, (count, type) => { + obj[type] += count; condensed.data(type, obj[type]); }); diff --git a/client/js/render.js b/client/js/render.js index 121e83f9..3d8461cb 100644 --- a/client/js/render.js +++ b/client/js/render.js @@ -61,18 +61,21 @@ function appendMessage(container, chanId, chanType, msg) { return; } + const obj = {}; + obj[msg.type] = 1; + // If the previous message is already condensed, // we just append to it and update text if (lastChild.hasClass("condensed")) { lastChild.append(renderedMessage); - condensed.updateText(lastChild, [msg.type]); + condensed.updateText(lastChild, obj); return; } // Always create a condensed container const newCondensed = $(templates.msg_condensed({time: msg.time})); - condensed.updateText(newCondensed, [msg.type]); + condensed.updateText(newCondensed, obj); newCondensed.append(renderedMessage); container.append(newCondensed); } diff --git a/client/js/socket-events/more.js b/client/js/socket-events/more.js index feab1362..e8760253 100644 --- a/client/js/socket-events/more.js +++ b/client/js/socket-events/more.js @@ -3,6 +3,7 @@ const $ = require("jquery"); const socket = require("../socket"); const render = require("../render"); +const condensed = require("../condensed"); const chat = $("#chat"); socket.on("more", function(data) { @@ -58,6 +59,21 @@ socket.on("more", function(data) { } } + // Join duplicate condensed messages together + const condensedDuplicate = chan.find(".msg.condensed + .msg.condensed"); + + if (condensedDuplicate) { + const condensedCorrect = condensedDuplicate.prev(); + + condensed.updateText(condensedCorrect, condensed.getStoredTypes(condensedDuplicate)); + + condensedCorrect + .append(condensedDuplicate.find(".msg")) + .toggleClass("closed", condensedDuplicate.hasClass("closed")); + + condensedDuplicate.remove(); + } + // restore scroll position const position = chan.height() - heightOld; scrollable.finish().scrollTop(position);