@fleetbase/ember-ui
Version:
Fleetbase UI provides all the interface components, helpers, services and utilities for building a Fleetbase extension into the Console.
202 lines (175 loc) • 4.97 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action, computed, defineProperty } from '@ember/object';
import { alias, gt, not } from '@ember/object/computed';
import getWithDefault from '@fleetbase/ember-core/utils/get-with-default';
import PaginationItems from '../utils/pagination/items';
import arrayRange from '../utils/array-range';
export default class PaginationComponent extends Component {
/**
* Whether to truncate pages items
*
* @var {Boolean}
*/
truncatePages = true;
/**
* Whether to show first and last buttons on pagination
*
* @var {Boolean}
*/
showFL = false;
/**
* The maximum number of pages to show in the pagination
*
* @var {Integer}
*/
numPagesToShow = 10;
/**
* The current page of the pagination.
*
* @var {Integer}
*/
currentPage = 1;
/**
* Get the page from, if none use a default of 1
*
* @var {Integer}
*/
get from() {
return this.args.meta.from || 1;
}
/**
* Get the page from, if none use a default of 1
*
* @var {Integer}
*/
get to() {
return this.args.meta.to || 1;
}
/**
* The total pages from the pagination meta
*
* @var {Integer}
*/
totalPages;
/**
* The total number of results
*
* @var {Integer}
*/
totalResults;
/**
* True if the pagination meta has more than one page
*
* @var {Integer}
*/
hasPages;
/**
* Determines if user can step backwards
*
* @var {Boolean}
*/
canStepBackward;
/**
* Determines if user can step forward
*
* @var {Boolean}
*/
get canStepForward() {
return this.args.meta.current_page < this.totalPages;
}
/**
* Alias of canStepBackward inverse
*
* @var {Boolean}
*/
cannotStepBackward;
/**
* Alias of canStepForward inverse
*
* @var {Boolean}
*/
cannotStepForward;
/**
* The pagination page items object
*
* @var {Object}
*/
get pageItemsObj() {
const result = PaginationItems.create({
parent: this,
});
defineProperty(result, 'currentPage', alias('parent.args.currentPage'));
defineProperty(result, 'totalPages', alias('parent.totalPages'));
defineProperty(result, 'truncatePages', alias('parent.truncatePages'));
defineProperty(result, 'numPagesToShow', alias('parent.numPagesToShow'));
defineProperty(result, 'showFL', alias('parent.showFL'));
return result;
}
/**
* The pageItems computed property
*
* @var {Object}
*/
get pageItems() {
return this.pageItemsObj.pageItems;
}
/**
* Generate page numbers from range
*
* @var {Array}
*/
get pageNumbers() {
const pages = arrayRange(this.args.meta.last_page || 0, 1).map((page) => {
return {
page,
dots: page === 12,
};
});
return pages.length < 12 ? pages : pages.slice(0, 12);
}
/**
* Create instance of PaginationComponent
*/
constructor() {
super(...arguments);
this.numPagesToShow = getWithDefault(this.args, 'numPagesToShow', 10);
this.currentPage = getWithDefault(this.args, 'currentPage', 1);
this.showFL = getWithDefault(this.args, 'showFL', false);
this.truncatePages = getWithDefault(this.args, 'truncatePages', true);
}
/**
* Increments the page
*
* @void
*/
incrementPage(step = 1) {
const currentPage = Number(this.currentPage);
const totalPages = Number(this.totalPages);
if (currentPage === totalPages && step === 1) {
return false;
}
if (currentPage <= 1 && step === -1) {
return false;
}
this.currentPage = this.currentPage + step;
if (typeof this.args.onPageChange === 'function') {
this.args.onPageChange(this.currentPage);
}
}
/**
* Increments the page
*
* @void
*/
goToPage(page) {
if (page === this.currentPage) {
return;
}
this.currentPage = page;
if (typeof this.args.onPageChange === 'function') {
this.args.onPageChange(this.currentPage);
}
}
}