/**
* 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:
'',
prev: '',
next: '',
last: '',
};
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');
},
};
})();