1
0
mirror of https://github.com/flarum/core.git synced 2025-08-07 08:56:38 +02:00

Shrink the diff, e.g. by moving methods around

This commit is contained in:
Franz Liedke
2020-08-01 02:53:03 +02:00
parent 53582ab999
commit 527d93120a
2 changed files with 58 additions and 39 deletions

View File

@@ -15,7 +15,18 @@ import Button from '../../common/components/Button';
*/ */
export default class PostStream extends Component { export default class PostStream extends Component {
init() { init() {
/**
* The discussion to display the post stream for.
*
* @type {Discussion}
*/
this.discussion = this.props.discussion; this.discussion = this.props.discussion;
/**
* The shared state of the post stream.
*
* @type {PostStreamState}
*/
this.stream = this.props.stream; this.stream = this.props.stream;
} }

View File

@@ -99,6 +99,20 @@ export default class PostStreamScrubber extends Component {
); );
} }
/**
* Go to the first post in the discussion.
*/
goToFirst() {
this.navigateTo(0);
}
/**
* Go to the last post in the discussion.
*/
goToLast() {
this.navigateTo(this.props.count - 1);
}
/** /**
* Check whether or not the scrubber should be disabled, i.e. if all of the * Check whether or not the scrubber should be disabled, i.e. if all of the
* posts are visible in the viewport. * posts are visible in the viewport.
@@ -211,20 +225,38 @@ export default class PostStreamScrubber extends Component {
} }
/** /**
* Go to the first post in the discussion. * Get the percentage of the height of the scrubber that should be allocated
* to each post.
*
* @return {Object}
* @property {Number} index The percent per post for posts on either side of
* the visible part of the scrubber.
* @property {Number} visible The percent per post for the visible part of the
* scrubber.
*/ */
goToFirst() { percentPerPost() {
this.navigateTo(0); const count = this.props.count || 1;
} const visible = this.props.visible || 1;
/** // To stop the handle of the scrollbar from getting too small when there
* Go to the last post in the discussion. // are many posts, we define a minimum percentage height for the handle
*/ // calculated from a 50 pixel limit. From this, we can calculate the
goToLast() { // minimum percentage per visible post. If this is greater than the actual
this.navigateTo(this.props.count - 1); // percentage per post, then we need to adjust the 'before' percentage to
// account for it.
const minPercentVisible = (50 / this.$('.Scrubber-scrollbar').outerHeight()) * 100;
const percentPerVisiblePost = Math.max(100 / count, minPercentVisible / visible);
const percentPerPost = count === visible ? 0 : (100 - percentPerVisiblePost * visible) / (count - visible);
return {
index: percentPerPost,
visible: percentPerVisiblePost,
};
} }
onresize() { onresize() {
this.scrollListener.update();
// Adjust the height of the scrollbar so that it fills the height of // Adjust the height of the scrollbar so that it fills the height of
// the sidebar and doesn't overlap the footer. // the sidebar and doesn't overlap the footer.
const scrubber = this.$(); const scrubber = this.$();
@@ -284,11 +316,6 @@ export default class PostStreamScrubber extends Component {
this.dragIndex = null; this.dragIndex = null;
} }
navigateTo(index) {
this.props.onNavigate(Math.floor(index));
this.renderScrollbar({ animate: true });
}
onclick(e) { onclick(e) {
// Calculate the index which we want to jump to based on the click position. // Calculate the index which we want to jump to based on the click position.
@@ -314,32 +341,13 @@ export default class PostStreamScrubber extends Component {
} }
/** /**
* Get the percentage of the height of the scrubber that should be allocated * Trigger post stream navigation, but also animate the scrollbar according
* to each post. * to the expected result.
* *
* @return {Object} * @param {number} index
* @property {Number} index The percent per post for posts on either side of
* the visible part of the scrubber.
* @property {Number} visible The percent per post for the visible part of the
* scrubber.
*/ */
percentPerPost() { navigateTo(index) {
const count = this.props.count || 1; this.props.onNavigate(Math.floor(index));
const visible = this.props.visible || 1; this.renderScrollbar({ animate: true });
// To stop the handle of the scrollbar from getting too small when there
// are many posts, we define a minimum percentage height for the handle
// calculated from a 50 pixel limit. From this, we can calculate the
// minimum percentage per visible post. If this is greater than the actual
// percentage per post, then we need to adjust the 'before' percentage to
// account for it.
const minPercentVisible = (50 / this.$('.Scrubber-scrollbar').outerHeight()) * 100;
const percentPerVisiblePost = Math.max(100 / count, minPercentVisible / visible);
const percentPerPost = count === visible ? 0 : (100 - percentPerVisiblePost * visible) / (count - visible);
return {
index: percentPerPost,
visible: percentPerVisiblePost,
};
} }
} }