open-nomad/ui/app/components/list-pagination.js
Buck Doyle 89136cbf6a Add massaged results of class codemod
Manual interventions:
• decorators on the same line for service and controller
  injections and most computed property macros
• preserving import order when possible, both per-line
  and intra-line
• moving new imports to the bottom
• removal of classic decorator for trivial cases
• conversion of init to constructor when appropriate
2020-06-10 16:18:42 -05:00

54 lines
1.3 KiB
JavaScript

import Component from '@ember/component';
import { computed } from '@ember/object';
import { computed as overridable } from 'ember-overridable-computed';
import classic from 'ember-classic-decorator';
@classic
export default class ListPagination extends Component {
@overridable(() => []) source;
size = 25;
page = 1;
spread = 2;
@computed('size', 'page')
get startsAt() {
return (this.page - 1) * this.size + 1;
}
@computed('source.[]', 'size', 'page')
get endsAt() {
return Math.min(this.page * this.size, this.get('source.length'));
}
@computed('source.[]', 'size')
get lastPage() {
return Math.ceil(this.get('source.length') / this.size);
}
@computed('source.[]', 'page', 'spread')
get pageLinks() {
const { spread, page, lastPage } = this;
// 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,
}));
}
@computed('source.[]', 'page', 'size')
get list() {
const size = this.size;
const start = (this.page - 1) * size;
return this.source.slice(start, start + size);
}
}