lemo-crm/web/app/components/pagination-bar.js
2022-06-23 17:29:32 +08:00

85 lines
2.7 KiB
JavaScript

import BaseComponent from './basic-component';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
export default BaseComponent.extend({
classNames: ['widget-toolbox', 'clearfix'],
total: alias('route.controller.model.total'),
prevPage: computed('currPage', function() {
return this.get('currPage') - 1 || 1;
}),
nextPage: computed('currPage', function() {
const me = this;
const page = me.get('currPage');
return page + 1 <= me.get('totalPage') ? page + 1 : page;
}),
currPage: computed('params.page', function() {
const me = this;
const currPage = parseInt(me.get('params.page'));
console.info('Get current page [' + currPage + ']');
return currPage;
}),
pages: computed('total', function() {
const me = this;
const totalPage = me.get('totalPage');
const currPage = me.get('currPage');
const pages = [];
if (currPage > 4) {
pages.push({page: 1, text: 1});
pages.push({page: currPage - 3, text: '...'});
pages.push({page: currPage - 2, text: currPage - 2});
pages.push({page: currPage - 1, text: currPage - 1});
}
// from page 1
else {
let i = 0;
while (++i < currPage) {
pages.push({page: i, text: i});
}
}
pages.push({page: currPage, text: currPage, active: true});
if (currPage < totalPage - 4) {
pages.push({page: currPage + 1, text: currPage + 1});
pages.push({page: currPage + 2, text: currPage + 2});
pages.push({page: currPage + 3, text: '...'});
pages.push({page: totalPage, text: totalPage});
}
// to the end
else {
let i = currPage;
while (++i <= totalPage) {
pages.push({page: i, text: i});
}
}
return pages;
}),
allPages: computed('total', function() {
const me = this;
const totalPage = me.get('totalPage');
const pages = [];
let i = 0;
while (++i <= totalPage) {
pages.push(i);
}
return pages;
}),
totalPage: computed('total', function() {
const me = this;
const pageSize = me.get('service.pageSize') || 32;
return parseInt((me.get('total') + pageSize - 1) / pageSize);
}),
actions: {
gotoPage(page) {
const me = this;
const router = me.get('router');
const params = me.get('params');
console.info('Go to page [' + page + '], params: ', params);
router.transitionTo(me.get('routeName'), page, {queryParams: params});
}
}
});