github.com/hernad/nomad@v1.6.112/ui/app/components/list-pagination.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import Component from '@ember/component'; 7 import { computed } from '@ember/object'; 8 import { computed as overridable } from 'ember-overridable-computed'; 9 import classic from 'ember-classic-decorator'; 10 11 @classic 12 export default class ListPagination extends Component { 13 @overridable(() => []) source; 14 size = 25; 15 page = 1; 16 spread = 2; 17 18 @computed('size', 'page') 19 get startsAt() { 20 return (this.page - 1) * this.size + 1; 21 } 22 23 @computed('source.[]', 'size', 'page') 24 get endsAt() { 25 return Math.min(this.page * this.size, this.get('source.length')); 26 } 27 28 @computed('source.[]', 'size') 29 get lastPage() { 30 return Math.ceil(this.get('source.length') / this.size); 31 } 32 33 @computed('source.[]', 'page', 'spread') 34 get pageLinks() { 35 const { spread, page, lastPage } = this; 36 37 // When there is only one page, don't bother with page links 38 if (lastPage === 1) { 39 return []; 40 } 41 42 const lowerBound = Math.max(1, page - spread); 43 const upperBound = Math.min(lastPage, page + spread) + 1; 44 45 return Array(upperBound - lowerBound) 46 .fill(null) 47 .map((_, index) => ({ 48 pageNumber: lowerBound + index, 49 })); 50 } 51 52 @computed('source.[]', 'page', 'size') 53 get list() { 54 const size = this.size; 55 const start = (this.page - 1) * size; 56 return this.source.slice(start, start + size); 57 } 58 }