primevue-mirror/components/lib/paginator/Paginator.vue

313 lines
12 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 23:48:09 +00:00
<nav v-if="alwaysShow ? true : pageLinks && pageLinks.length > 1" v-bind="ptmi('paginatorWrapper')">
<div v-for="(value, key) in templateItems" :key="key" ref="paginator" :class="cx('paginator', { key })" v-bind="ptm('root')">
<div v-if="$slots.start" :class="cx('contentStart')" v-bind="ptm('contentStart')">
2022-12-08 11:04:25 +00:00
<slot name="start" :state="currentState"></slot>
</div>
<template v-for="item in value" :key="item">
2023-09-06 07:15:58 +00:00
<FirstPageLink v-if="item === 'FirstPageLink'" :aria-label="getAriaLabel('firstPageLabel')" :template="$slots.firstpagelinkicon" @click="changePageToFirst($event)" :disabled="isFirstPage || empty" :unstyled="unstyled" :pt="pt" />
<PrevPageLink v-else-if="item === 'PrevPageLink'" :aria-label="getAriaLabel('prevPageLabel')" :template="$slots.prevpagelinkicon" @click="changePageToPrev($event)" :disabled="isFirstPage || empty" :unstyled="unstyled" :pt="pt" />
<NextPageLink v-else-if="item === 'NextPageLink'" :aria-label="getAriaLabel('nextPageLabel')" :template="$slots.nextpagelinkicon" @click="changePageToNext($event)" :disabled="isLastPage || empty" :unstyled="unstyled" :pt="pt" />
<LastPageLink v-else-if="item === 'LastPageLink'" :aria-label="getAriaLabel('lastPageLabel')" :template="$slots.lastpagelinkicon" @click="changePageToLast($event)" :disabled="isLastPage || empty" :unstyled="unstyled" :pt="pt" />
2023-05-08 09:35:25 +00:00
<PageLinks v-else-if="item === 'PageLinks'" :aria-label="getAriaLabel('pageLabel')" :value="pageLinks" :page="page" @click="changePageLink($event)" :pt="pt" />
2022-12-08 11:04:25 +00:00
<CurrentPageReport
v-else-if="item === 'CurrentPageReport'"
aria-live="polite"
:template="currentPageReportTemplate"
:currentPage="currentPage"
:page="page"
:pageCount="pageCount"
:first="d_first"
:rows="d_rows"
:totalRecords="totalRecords"
2023-09-06 07:15:58 +00:00
:unstyled="unstyled"
2023-05-08 09:35:25 +00:00
:pt="pt"
2022-12-08 11:04:25 +00:00
/>
2023-05-08 09:35:25 +00:00
<RowsPerPageDropdown
v-else-if="item === 'RowsPerPageDropdown' && rowsPerPageOptions"
:aria-label="getAriaLabel('rowsPerPageLabel')"
:rows="d_rows"
:options="rowsPerPageOptions"
@rows-change="onRowChange($event)"
:disabled="empty"
:templates="$slots"
:unstyled="unstyled"
2023-05-08 09:35:25 +00:00
:pt="pt"
/>
<JumpToPageDropdown
v-else-if="item === 'JumpToPageDropdown'"
:aria-label="getAriaLabel('jumpToPageDropdownLabel')"
:page="page"
:pageCount="pageCount"
@page-change="changePage($event)"
:disabled="empty"
:templates="$slots"
:unstyled="unstyled"
:pt="pt"
/>
<JumpToPageInput v-else-if="item === 'JumpToPageInput'" :page="currentPage" @page-change="changePage($event)" :disabled="empty" :unstyled="unstyled" :pt="pt" />
2022-12-08 11:04:25 +00:00
</template>
<div v-if="$slots.end" :class="cx('contentEnd')" v-bind="ptm('contentEnd')">
2022-12-08 11:04:25 +00:00
<slot name="end" :state="currentState"></slot>
</div>
2022-09-06 12:03:37 +00:00
</div>
2022-12-08 11:04:25 +00:00
</nav>
2022-09-06 12:03:37 +00:00
</template>
<script>
import { DomHandler, UniqueComponentId } from 'primevue/utils';
2023-05-30 13:39:08 +00:00
import BasePaginator from './BasePaginator.vue';
2022-09-06 12:03:37 +00:00
import CurrrentPageReport from './CurrentPageReport.vue';
import FirstPageLink from './FirstPageLink.vue';
2022-12-08 11:04:25 +00:00
import JumpToPageDropdown from './JumpToPageDropdown.vue';
import JumpToPageInput from './JumpToPageInput.vue';
2022-09-06 12:03:37 +00:00
import LastPageLink from './LastPageLink.vue';
import NextPageLink from './NextPageLink.vue';
import PageLinks from './PageLinks.vue';
import PrevPageLink from './PrevPageLink.vue';
import RowsPerPageDropdown from './RowsPerPageDropdown.vue';
export default {
name: 'Paginator',
2023-05-30 13:39:08 +00:00
extends: BasePaginator,
2024-02-11 23:48:09 +00:00
inheritAttrs: false,
2022-09-06 12:03:37 +00:00
emits: ['update:first', 'update:rows', 'page'],
data() {
return {
d_first: this.first,
d_rows: this.rows
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
first(newValue) {
this.d_first = newValue;
},
rows(newValue) {
this.d_rows = newValue;
},
totalRecords(newValue) {
2022-09-14 11:26:01 +00:00
if (this.page > 0 && newValue && this.d_first >= newValue) {
2022-09-06 12:03:37 +00:00
this.changePage(this.pageCount - 1);
}
}
},
2022-12-08 11:04:25 +00:00
mounted() {
this.setPaginatorAttribute();
this.createStyle();
},
2022-09-06 12:03:37 +00:00
methods: {
changePage(p) {
const pc = this.pageCount;
if (p >= 0 && p < pc) {
this.d_first = this.d_rows * p;
const state = {
page: p,
first: this.d_first,
rows: this.d_rows,
pageCount: pc
};
2022-09-14 11:26:01 +00:00
this.$emit('update:first', this.d_first);
2022-09-06 12:03:37 +00:00
this.$emit('update:rows', this.d_rows);
this.$emit('page', state);
}
},
2022-12-08 11:04:25 +00:00
2022-09-06 12:03:37 +00:00
changePageToFirst(event) {
2022-09-14 11:26:01 +00:00
if (!this.isFirstPage) {
2022-09-06 12:03:37 +00:00
this.changePage(0);
}
event.preventDefault();
},
changePageToPrev(event) {
this.changePage(this.page - 1);
event.preventDefault();
},
changePageLink(event) {
this.changePage(event.value - 1);
event.originalEvent.preventDefault();
},
changePageToNext(event) {
2022-09-14 11:26:01 +00:00
this.changePage(this.page + 1);
2022-09-06 12:03:37 +00:00
event.preventDefault();
},
changePageToLast(event) {
2022-09-14 11:26:01 +00:00
if (!this.isLastPage) {
2022-09-06 12:03:37 +00:00
this.changePage(this.pageCount - 1);
}
event.preventDefault();
},
onRowChange(value) {
this.d_rows = value;
this.changePage(this.page);
2022-12-08 11:04:25 +00:00
},
createStyle() {
if (this.hasBreakpoints() && !this.isUnstyled) {
2022-12-08 11:04:25 +00:00
this.styleElement = document.createElement('style');
this.styleElement.type = 'text/css';
DomHandler.setAttribute(this.styleElement, 'nonce', this.$primevue?.config?.csp?.nonce);
2022-12-08 11:04:25 +00:00
document.head.appendChild(this.styleElement);
let innerHTML = '';
const keys = Object.keys(this.template);
const sortedBreakpoints = {};
keys.sort((a, b) => parseInt(a) - parseInt(b)).forEach((key) => {
sortedBreakpoints[key] = this.template[key];
});
for (const [index, [key]] of Object.entries(Object.entries(sortedBreakpoints))) {
let minValue, calculatedMinValue;
if (key !== 'default' && typeof Object.keys(sortedBreakpoints)[index - 1] === 'string') {
calculatedMinValue = Number(Object.keys(sortedBreakpoints)[index - 1].slice(0, -2)) + 1 + 'px';
} else {
calculatedMinValue = Object.keys(sortedBreakpoints)[index - 1];
}
minValue = Object.entries(sortedBreakpoints)[index - 1] ? `and (min-width:${calculatedMinValue})` : '';
2022-12-08 11:04:25 +00:00
if (key === 'default') {
innerHTML += `
@media screen ${minValue} {
.paginator[${this.attributeSelector}],
.p-paginator-default{
2023-10-17 19:27:27 +00:00
display: flex;
2022-12-08 11:04:25 +00:00
}
}
`;
} else {
innerHTML += `
.paginator[${this.attributeSelector}], .p-paginator-${key} {
2023-11-17 22:40:39 +00:00
display: none;
}
@media screen ${minValue} and (max-width: ${key}) {
.paginator[${this.attributeSelector}], .p-paginator-${key} {
2023-11-17 22:40:39 +00:00
display: flex;
}
.paginator[${this.attributeSelector}],
.p-paginator-default{
2023-11-17 22:40:39 +00:00
display: none;
}
}
2022-12-08 11:04:25 +00:00
`;
}
}
this.styleElement.innerHTML = innerHTML;
}
},
hasBreakpoints() {
return typeof this.template === 'object';
},
setPaginatorAttribute() {
if (this.$refs.paginator && this.$refs.paginator.length >= 0) {
[...this.$refs.paginator].forEach((el) => {
el.setAttribute(this.attributeSelector, '');
});
}
},
getAriaLabel(labelType) {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria[labelType] : undefined;
2022-09-06 12:03:37 +00:00
}
},
computed: {
templateItems() {
2022-12-08 11:04:25 +00:00
let keys = {};
if (this.hasBreakpoints()) {
keys = this.template;
if (!keys.default) {
keys.default = 'FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink RowsPerPageDropdown';
}
2022-09-14 11:26:01 +00:00
2022-12-08 11:04:25 +00:00
for (const item in keys) {
keys[item] = this.template[item].split(' ').map((value) => {
return value.trim();
});
}
return keys;
}
keys['default'] = this.template.split(' ').map((value) => {
return value.trim();
2022-09-14 11:26:01 +00:00
});
2022-09-06 12:03:37 +00:00
return keys;
},
page() {
return Math.floor(this.d_first / this.d_rows);
},
pageCount() {
return Math.ceil(this.totalRecords / this.d_rows);
},
isFirstPage() {
return this.page === 0;
},
isLastPage() {
return this.page === this.pageCount - 1;
},
calculatePageLinkBoundaries() {
const numberOfPages = this.pageCount;
const visiblePages = Math.min(this.pageLinkSize, numberOfPages);
//calculate range, keep current in middle if necessary
2022-09-14 11:26:01 +00:00
let start = Math.max(0, Math.ceil(this.page - visiblePages / 2));
2022-09-06 12:03:37 +00:00
let end = Math.min(numberOfPages - 1, start + visiblePages - 1);
//check when approaching to last page
const delta = this.pageLinkSize - (end - start + 1);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
start = Math.max(0, start - delta);
return [start, end];
},
pageLinks() {
let pageLinks = [];
let boundaries = this.calculatePageLinkBoundaries;
let start = boundaries[0];
let end = boundaries[1];
2022-09-14 11:26:01 +00:00
for (var i = start; i <= end; i++) {
2022-09-06 12:03:37 +00:00
pageLinks.push(i + 1);
}
return pageLinks;
},
currentState() {
return {
page: this.page,
first: this.d_first,
rows: this.d_rows
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
empty() {
return this.pageCount === 0;
},
currentPage() {
return this.pageCount > 0 ? this.page + 1 : 0;
2022-12-08 11:04:25 +00:00
},
attributeSelector() {
return UniqueComponentId();
2022-09-06 12:03:37 +00:00
}
},
components: {
2022-09-14 11:26:01 +00:00
CurrentPageReport: CurrrentPageReport,
FirstPageLink: FirstPageLink,
LastPageLink: LastPageLink,
NextPageLink: NextPageLink,
PageLinks: PageLinks,
PrevPageLink: PrevPageLink,
RowsPerPageDropdown: RowsPerPageDropdown,
JumpToPageDropdown: JumpToPageDropdown,
JumpToPageInput: JumpToPageInput
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>