diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 8fa737bb2..509e00343 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -216,25 +216,21 @@ return this.$('.bottom-bar form').submit(); } - var $discussionContainer = this.$('.discussion-container'), - $bottomBar = this.$('.bottom-bar'), - $messageList = this.$('.message-list'); - - var scrollPosition = $messageList.scrollTop() + $messageList.outerHeight(), - scrollHeight = $messageList[0].scrollHeight, - shouldStickToBottom = scrollPosition === scrollHeight; - + this.view.measureScrollPosition(); window.autosize(this.$messageField); + + var $discussionContainer = this.$('.discussion-container'), + $bottomBar = this.$('.bottom-bar'); + $bottomBar.outerHeight(this.$messageField.outerHeight() + 1); var $bottomBarNewHeight = $bottomBar.outerHeight(); $discussionContainer.outerHeight(this.$el.outerHeight() - $bottomBarNewHeight - this.$('#header').outerHeight()); - if (shouldStickToBottom) { - $messageList.scrollTop(scrollHeight); - } + this.view.scrollToBottomIfNeeded(); }, forceUpdateMessageFieldSize: function (event) { + this.view.scrollToBottomIfNeeded(); window.autosize.update(this.$messageField); this.updateMessageFieldSize(event); } diff --git a/js/views/message_list_view.js b/js/views/message_list_view.js index 207814bea..d02b9ea2f 100644 --- a/js/views/message_list_view.js +++ b/js/views/message_list_view.js @@ -17,6 +17,10 @@ 'use strict'; window.Whisper = window.Whisper || {}; + var scrollPosition, + scrollHeight, + shouldStickToBottom; + Whisper.MessageListView = Whisper.ListView.extend({ tagName: 'ul', className: 'message-list', @@ -25,9 +29,20 @@ 'add': 'scrollToBottom', 'update *': 'scrollToBottom' }, + measureScrollPosition: function() { + scrollPosition = this.$el.scrollTop() + this.$el.outerHeight(); + scrollHeight = this.el.scrollHeight; + shouldStickToBottom = scrollPosition === scrollHeight; + }, + scrollToBottomIfNeeded: function() { + if (shouldStickToBottom) { + this.$el.scrollTop(scrollHeight); + } + }, scrollToBottom: function() { // TODO: Avoid scrolling if user has manually scrolled up? this.$el.scrollTop(this.el.scrollHeight); + this.measureScrollPosition(); }, addAll: function() { Whisper.ListView.prototype.addAll.apply(this, arguments); // super()