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.
This commit is contained in:
Krausus
2016-09-22 20:27:26 -04:00
parent 4a0c9f7e74
commit c97e4a7766
+15 -7
View File
@@ -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