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

46 lines
1.2 KiB
JavaScript
Raw Normal View History

import Component from '@ember/component';
import { computed } from '@ember/object';
2017-09-19 14:47:10 +00:00
export default Component.extend({
source: computed(() => []),
size: 25,
page: 1,
spread: 2,
startsAt: computed('size', 'page', function() {
2019-03-26 07:46:44 +00:00
return (this.page - 1) * this.size + 1;
2017-09-19 14:47:10 +00:00
}),
endsAt: computed('source.[]', 'size', 'page', function() {
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
}),
lastPage: computed('source.[]', 'size', function() {
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
}),
pageLinks: computed('source.[]', 'page', 'spread', function() {
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
}),
list: computed('source.[]', 'page', 'size', function() {
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);
2017-09-19 14:47:10 +00:00
}),
});