Merge pull request #4542 from tgxworld/scroll_to_bottom_When_new_messages_come_in_for_pm

FEATURE: Scroll to new posts when user is near bottom of PM.
This commit is contained in:
Guo Xiang Tan 2016-11-09 14:21:09 +08:00 committed by GitHub
commit 8a0f02fd23
1 changed files with 22 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import Composer from 'discourse/models/composer';
import DiscourseURL from 'discourse/lib/url'; import DiscourseURL from 'discourse/lib/url';
import { categoryBadgeHTML } from 'discourse/helpers/category-link'; import { categoryBadgeHTML } from 'discourse/helpers/category-link';
import Post from 'discourse/models/post'; import Post from 'discourse/models/post';
import debounce from 'discourse/lib/debounce';
export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, { export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
composer: Ember.inject.controller(), composer: Ember.inject.controller(),
@ -668,6 +669,7 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
const topic = this.get('model'); const topic = this.get('model');
const postStream = topic.get('postStream'); const postStream = topic.get('postStream');
const post = postStream.findLoadedPost(postId); const post = postStream.findLoadedPost(postId);
if (post) { if (post) {
DiscourseURL.routeTo(topic.urlForPostNumber(post.get('post_number'))); DiscourseURL.routeTo(topic.urlForPostNumber(post.get('post_number')));
} else { } else {
@ -838,10 +840,30 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
topic.reload().then(() => { topic.reload().then(() => {
this.send('postChangedRoute', topic.get('post_number') || 1); this.send('postChangedRoute', topic.get('post_number') || 1);
}); });
} else {
const postNumber = data.post_number;
const notInPostStream = topic.get('highest_post_number') <= postNumber;
const postNumberDifference = postNumber - topic.get('currentPost');
if (notInPostStream &&
topic.get('isPrivateMessage') &&
postNumberDifference > 0 &&
postNumberDifference < 7) {
this._scrollToPost(data.post_number);
}
} }
}); });
}, },
_scrollToPost: debounce(function(postNumber) {
const $post = $(`.topic-post article#post_${postNumber}`);
if ($post.length === 0) return;
$('body').animate({ scrollTop: $post.offset().top }, 1000);
}, 500),
unsubscribe() { unsubscribe() {
const topicId = this.get('content.id'); const topicId = this.get('content.id');
if (!topicId) return; if (!topicId) return;