From c97e4a77666b02e851de409bedc5991241c8cb47 Mon Sep 17 00:00:00 2001 From: Krausus Date: Thu, 22 Sep 2016 20:27:26 -0400 Subject: [PATCH] Fixes Goonchat scrolling when pruning messages The scroll position is now adjusted to compensate for the elements that get removed. To cut down on the subpixel drift caused by their removal, message pruning is now only done every 2 seconds. --- goon/browserassets/js/browserOutput.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/goon/browserassets/js/browserOutput.js b/goon/browserassets/js/browserOutput.js index 0192959f92a..271d7872d46 100644 --- a/goon/browserassets/js/browserOutput.js +++ b/goon/browserassets/js/browserOutput.js @@ -215,13 +215,7 @@ function output(message, flag) { message = linkify(message); } - opts.messageCount++; - - //Pop the top message off if history limit reached - if (opts.messageCount >= opts.messageLimit) { - $messages.children('div.entry:first-child').remove(); - opts.messageCount--; //I guess the count should only ever equal the limit - } + opts.messageCount++; //Actually append the message var entry = document.createElement('div'); @@ -463,6 +457,20 @@ $(function() { $('.connectionClosed[data-count="'+opts.noResponseCount+'"]:not(.restored)').addClass('restored').text('Your connection has been restored (probably)!'); opts.noResponse = false; } + if (opts.messageCount > opts.messageLimit) { // Prune old messages beyond the message limit + var bodyHeight = $('body').height(); + var messagesHeight = $messages.outerHeight(); + var scrollPos = $(window).scrollTop(); + var atBottom = (bodyHeight + scrollPos >= messagesHeight - opts.scrollSnapTolerance) + + $messages.children().slice(0, opts.messageCount - opts.messageLimit).remove(); + opts.messageCount = opts.messageLimit; + if (!atBottom) { + // If we weren't at the bottom, adjust scroll position to compensate for removed elements + var newPos = scrollPos - (messagesHeight - $messages.outerHeight()) + $('body,html').scrollTop(newPos); + } + } }, 2000); //2 seconds