2017-12-15 21:39:18 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { computed } from '@ember/object';
|
2019-10-15 18:32:58 +00:00
|
|
|
import { computed as overridable } from 'ember-overridable-computed';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
export default class ListPagination extends Component {
|
|
|
|
@overridable(() => []) source;
|
|
|
|
size = 25;
|
|
|
|
page = 1;
|
|
|
|
spread = 2;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('size', 'page')
|
|
|
|
get startsAt() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return (this.page - 1) * this.size + 1;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('source.[]', 'size', 'page')
|
|
|
|
get endsAt() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return Math.min(this.page * this.size, this.get('source.length'));
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('source.[]', 'size')
|
|
|
|
get lastPage() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return Math.ceil(this.get('source.length') / this.size);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('source.[]', 'page', 'spread')
|
|
|
|
get pageLinks() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const { spread, page, lastPage } = this;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
// When there is only one page, don't bother with page links
|
|
|
|
if (lastPage === 1) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const lowerBound = Math.max(1, page - spread);
|
|
|
|
const upperBound = Math.min(lastPage, page + spread) + 1;
|
|
|
|
|
2017-12-15 21:39:18 +00:00
|
|
|
return Array(upperBound - lowerBound)
|
|
|
|
.fill(null)
|
|
|
|
.map((_, index) => ({
|
|
|
|
pageNumber: lowerBound + index,
|
|
|
|
}));
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('source.[]', 'page', 'size')
|
|
|
|
get list() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const size = this.size;
|
|
|
|
const start = (this.page - 1) * size;
|
|
|
|
return this.source.slice(start, start + size);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|