178 lines
6.5 KiB
JavaScript
178 lines
6.5 KiB
JavaScript
/**
|
|||
|
|
* Shared Pagination Core
|
||
|
|
* Exposes:
|
||
|
|
* - window.NCPagination.render(container, state, onPageChange, variant)
|
||
|
|
* - window.NCPaginationTop.render(container, state, onPageChange)
|
||
|
|
* - window.NCPaginationBottom.render(container, state, onPageChange)
|
||
|
|
* onPageChange receives (page, pageSize), where pageSize is one of 12, 24, 48, or 96.
|
||
|
|
*/
|
||
|
|
(() => {
|
||
|
|
if (window.NCPagination && typeof window.NCPagination.render === 'function') {
|
||
|
|
window.NCPaginationTop = {
|
||
|
|
render(container, state, onPageChange) {
|
||
|
|
window.NCPagination.render(container, state, onPageChange, 'top');
|
||
|
|
},
|
||
|
|
};
|
||
|
|
window.NCPaginationBottom = {
|
||
|
|
render(container, state, onPageChange) {
|
||
|
|
window.NCPagination.render(container, state, onPageChange, 'bottom');
|
||
|
|
},
|
||
|
|
};
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function fmtNum(n) {
|
||
|
|
return String(Number(n) || 0).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||
|
|
}
|
||
|
|
|
||
|
|
const PAGE_SIZE_OPTIONS = [12, 24, 48, 96];
|
||
|
|
const DEFAULT_PAGE_SIZE = 24;
|
||
|
|
|
||
|
|
const icons = {
|
||
|
|
first:
|
||
|
|
'<svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M11 19l-7-7 7-7m8 14l-7-7 7-7" /></svg>',
|
||
|
|
prev: '<svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7" /></svg>',
|
||
|
|
next: '<svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7" /></svg>',
|
||
|
|
last: '<svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M13 5l7 7-7 7M5 5l7 7-7 7" /></svg>',
|
||
|
|
};
|
||
|
|
|
||
|
|
function render(container, state = {}, onPageChange, variant = 'top') {
|
||
|
|
if (!container) return;
|
||
|
|
|
||
|
|
const page = Math.max(1, Number(state.page) || 1);
|
||
|
|
const pages = Math.max(1, Number(state.pages) || 1);
|
||
|
|
const total = Math.max(0, Number(state.total) || 0);
|
||
|
|
const requestedPageSize = Number(state.pageSize);
|
||
|
|
const pageSize = PAGE_SIZE_OPTIONS.includes(requestedPageSize)
|
||
|
|
? requestedPageSize
|
||
|
|
: DEFAULT_PAGE_SIZE;
|
||
|
|
const visible = pages > 1 || (state.total !== undefined && total > 0);
|
||
|
|
|
||
|
|
container.hidden = !visible;
|
||
|
|
container.style.display = visible ? '' : 'none';
|
||
|
|
container.classList.remove('hidden');
|
||
|
|
container.classList.add('nc-pagination-container');
|
||
|
|
container.classList.remove('nc-pagination-top', 'nc-pagination-bottom');
|
||
|
|
container.classList.add(variant === 'bottom' ? 'nc-pagination-bottom' : 'nc-pagination-top');
|
||
|
|
|
||
|
|
if (!visible) {
|
||
|
|
container.innerHTML = '';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
container.innerHTML = '';
|
||
|
|
|
||
|
|
const meta = document.createElement('div');
|
||
|
|
meta.className = 'nc-pagination-meta';
|
||
|
|
|
||
|
|
const infoSpan = document.createElement('span');
|
||
|
|
infoSpan.className = 'nc-pagination-info';
|
||
|
|
if (state.infoText !== undefined) {
|
||
|
|
infoSpan.textContent = state.infoText;
|
||
|
|
} else {
|
||
|
|
infoSpan.textContent = `Page ${fmtNum(page)} of ${fmtNum(pages)} (${fmtNum(total)} total)`;
|
||
|
|
}
|
||
|
|
|
||
|
|
const pageSizeLabel = document.createElement('label');
|
||
|
|
pageSizeLabel.className = 'nc-pagination-page-size-label';
|
||
|
|
pageSizeLabel.textContent = 'Entries per page';
|
||
|
|
|
||
|
|
const pageSizeSelect = document.createElement('select');
|
||
|
|
pageSizeSelect.className = 'nc-pagination-page-size';
|
||
|
|
pageSizeSelect.setAttribute('aria-label', 'Entries per page');
|
||
|
|
PAGE_SIZE_OPTIONS.forEach(size => {
|
||
|
|
const option = document.createElement('option');
|
||
|
|
option.value = String(size);
|
||
|
|
option.textContent = String(size);
|
||
|
|
option.selected = size === pageSize;
|
||
|
|
pageSizeSelect.appendChild(option);
|
||
|
|
});
|
||
|
|
pageSizeSelect.addEventListener('change', () => {
|
||
|
|
if (typeof onPageChange === 'function') onPageChange(1, Number(pageSizeSelect.value));
|
||
|
|
});
|
||
|
|
pageSizeLabel.appendChild(pageSizeSelect);
|
||
|
|
meta.appendChild(infoSpan);
|
||
|
|
meta.appendChild(pageSizeLabel);
|
||
|
|
|
||
|
|
const centerWrap = document.createElement('div');
|
||
|
|
centerWrap.className = 'nc-pagination-center';
|
||
|
|
const centerSlot = document.createElement('div');
|
||
|
|
centerSlot.className = 'nc-pagination-center-slot';
|
||
|
|
centerSlot.dataset.ncPaginationCenterSlot = '1';
|
||
|
|
centerWrap.appendChild(centerSlot);
|
||
|
|
|
||
|
|
const btnContainer = document.createElement('div');
|
||
|
|
btnContainer.className = 'nc-pagination-btns';
|
||
|
|
|
||
|
|
const buttonClass = 'nc-pagination-btn';
|
||
|
|
const activeClass = 'nc-pagination-btn nc-pagination-btn-active';
|
||
|
|
|
||
|
|
const addButton = (labelHtml, targetPage, isActive, ariaLabel) => {
|
||
|
|
const btn = document.createElement('button');
|
||
|
|
btn.type = 'button';
|
||
|
|
btn.innerHTML = labelHtml;
|
||
|
|
btn.className = isActive ? activeClass : buttonClass;
|
||
|
|
if (ariaLabel) btn.setAttribute('aria-label', ariaLabel);
|
||
|
|
if (isActive) btn.setAttribute('aria-current', 'page');
|
||
|
|
|
||
|
|
const canGo = !isActive && targetPage >= 1 && targetPage <= pages && targetPage !== page;
|
||
|
|
if (canGo) {
|
||
|
|
btn.addEventListener('click', () => {
|
||
|
|
if (typeof onPageChange === 'function') onPageChange(targetPage, pageSize);
|
||
|
|
});
|
||
|
|
} else if (!isActive) {
|
||
|
|
btn.disabled = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
btnContainer.appendChild(btn);
|
||
|
|
};
|
||
|
|
|
||
|
|
addButton(icons.first, 1, false, 'First page');
|
||
|
|
addButton(icons.prev, page - 1, false, 'Previous page');
|
||
|
|
|
||
|
|
let start = Math.max(1, page - 2);
|
||
|
|
let end = start + 4;
|
||
|
|
if (end > pages) {
|
||
|
|
end = Math.max(1, pages);
|
||
|
|
start = Math.max(1, end - 4);
|
||
|
|
}
|
||
|
|
|
||
|
|
const pageButtonCount = Math.min(5, pages);
|
||
|
|
for (let i = 0; i < pageButtonCount; i += 1) {
|
||
|
|
const targetPage = start + i;
|
||
|
|
addButton(String(targetPage), targetPage, targetPage === page, `Page ${targetPage}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
addButton(icons.next, page + 1, false, 'Next page');
|
||
|
|
addButton(icons.last, pages, false, 'Last page');
|
||
|
|
|
||
|
|
container.appendChild(meta);
|
||
|
|
container.appendChild(centerWrap);
|
||
|
|
container.appendChild(btnContainer);
|
||
|
|
|
||
|
|
container.dispatchEvent(
|
||
|
|
new CustomEvent('nc:pagination-rendered', {
|
||
|
|
bubbles: true,
|
||
|
|
detail: {
|
||
|
|
variant,
|
||
|
|
page,
|
||
|
|
pages,
|
||
|
|
total,
|
||
|
|
pageSize,
|
||
|
|
},
|
||
|
|
})
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
window.NCPagination = { render };
|
||
|
|
window.NCPaginationTop = {
|
||
|
|
render(container, state, onPageChange) {
|
||
|
|
render(container, state, onPageChange, 'top');
|
||
|
|
},
|
||
|
|
};
|
||
|
|
window.NCPaginationBottom = {
|
||
|
|
render(container, state, onPageChange) {
|
||
|
|
render(container, state, onPageChange, 'bottom');
|
||
|
|
},
|
||
|
|
};
|
||
|
|
})();
|