open-nomad/ui/app/components/list-pagination.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.3 KiB
JavaScript
Raw Normal View History

import Component from '@ember/component';
import { computed } from '@ember/object';
import { computed as overridable } from 'ember-overridable-computed';
import classic from 'ember-classic-decorator';
2017-09-19 14:47:10 +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
@computed('size', 'page')
get startsAt() {
2019-03-26 07:46:44 +00:00
return (this.page - 1) * this.size + 1;
}
2017-09-19 14:47:10 +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'));
}
2017-09-19 14:47:10 +00:00
@computed('source.[]', 'size')
get lastPage() {
2019-03-26 07:46:44 +00:00
return Math.ceil(this.get('source.length') / this.size);
}
2017-09-19 14:47:10 +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;
return Array(upperBound - lowerBound)
.fill(null)
.map((_, index) => ({
pageNumber: lowerBound + index,
}));
}
2017-09-19 14:47:10 +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);
}
}