From acf67ca416abe23ffd02245cffe1ea655306d50e Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 20 Nov 2015 12:33:56 +1030 Subject: [PATCH] Add a "load more" button to the end of the post stream This is necessary if the page is viewed in a context with no scrolling, i.e. an auto-resizing iframe --- js/forum/src/components/PostStream.js | 112 ++++++++++++++------------ 1 file changed, 62 insertions(+), 50 deletions(-) diff --git a/js/forum/src/components/PostStream.js b/js/forum/src/components/PostStream.js index 38b710976..4ce6b6040 100644 --- a/js/forum/src/components/PostStream.js +++ b/js/forum/src/components/PostStream.js @@ -5,6 +5,7 @@ import anchorScroll from 'flarum/utils/anchorScroll'; import mixin from 'flarum/utils/mixin'; import evented from 'flarum/utils/evented'; import ReplyPlaceholder from 'flarum/components/ReplyPlaceholder'; +import Button from 'flarum/components/Button'; /** * The `PostStream` component displays an infinitely-scrollable wall of posts in @@ -194,60 +195,71 @@ class PostStream extends Component { this.visibleEnd = this.sanitizeIndex(this.visibleEnd); this.viewingEnd = this.visibleEnd === this.count(); + const posts = this.posts(); const postIds = this.discussion.postIds(); + const items = posts.map((post, i) => { + let content; + const attrs = {'data-index': this.visibleStart + i}; + + if (post) { + const time = post.time(); + const PostComponent = app.postComponents[post.contentType()]; + content = PostComponent ? PostComponent.component({post}) : ''; + + attrs.key = 'post' + post.id(); + attrs.config = fadeIn; + attrs['data-time'] = time.toISOString(); + attrs['data-number'] = post.number(); + attrs['data-id'] = post.id(); + + // If the post before this one was more than 4 hours ago, we will + // display a 'time gap' indicating how long it has been in between + // the posts. + const dt = time - lastTime; + + if (dt > 1000 * 60 * 60 * 24 * 4) { + content = [ +
+ {app.translator.trans('core.forum.post_stream.time_lapsed_text', {period: moment.duration(dt).humanize()})} +
, + content + ]; + } + + lastTime = time; + } else { + attrs.key = 'post' + postIds[this.visibleStart + i]; + + content = PostLoading.component(); + } + + return
{content}
; + }); + + if (!this.viewingEnd && posts[this.visibleEnd - this.visibleStart - 1]) { + items.push( +
+ +
+ ); + } + + // If we're viewing the end of the discussion, the user can reply, and + // is not already doing so, then show a 'write a reply' placeholder. + if (this.viewingEnd && (!app.session.user || this.discussion.canReply())) { + items.push( +
+ {ReplyPlaceholder.component({discussion: this.discussion})} +
+ ); + } + return (
- {this.posts().map((post, i) => { - let content; - const attrs = {'data-index': this.visibleStart + i}; - - if (post) { - const time = post.time(); - const PostComponent = app.postComponents[post.contentType()]; - content = PostComponent ? PostComponent.component({post}) : ''; - - attrs.key = 'post' + post.id(); - attrs.config = fadeIn; - attrs['data-time'] = time.toISOString(); - attrs['data-number'] = post.number(); - attrs['data-id'] = post.id(); - - // If the post before this one was more than 4 hours ago, we will - // display a 'time gap' indicating how long it has been in between - // the posts. - const dt = time - lastTime; - - if (dt > 1000 * 60 * 60 * 24 * 4) { - content = [ -
- {app.translator.trans('core.forum.post_stream.time_lapsed_text', {period: moment.duration(dt).humanize()})} -
, - content - ]; - } - - lastTime = time; - } else { - attrs.key = 'post' + postIds[this.visibleStart + i]; - - content = PostLoading.component(); - } - - return
{content}
; - })} - - { - // If we're viewing the end of the discussion, the user can reply, and - // is not already doing so, then show a 'write a reply' placeholder. - this.viewingEnd && - (!app.session.user || this.discussion.canReply()) - ? ( -
- {ReplyPlaceholder.component({discussion: this.discussion})} -
- ) : '' - } + {items}
); }