54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
/**
|
|
* 페이지 값을 유효한 범위로 보정한다.
|
|
* @param {number|string} page - 보정할 페이지
|
|
* @param {number|string} pageCount - 전체 페이지 수
|
|
* @returns {number} 보정된 페이지
|
|
*/
|
|
export const clampAdminPage = (page, pageCount) => {
|
|
const totalPageCount = Math.max(1, Number(pageCount) || 1)
|
|
return Math.min(Math.max(Number(page) || 1, 1), totalPageCount)
|
|
}
|
|
|
|
/**
|
|
* 관리자 번호형 페이지네이션 항목을 생성한다.
|
|
* @param {number|string} page - 현재 페이지
|
|
* @param {number|string} pageCount - 전체 페이지 수
|
|
* @returns {Array<number|string>} 페이지 번호와 ellipsis 항목
|
|
*/
|
|
export const buildAdminPaginationItems = (page, pageCount) => {
|
|
const total = Math.max(1, Number(pageCount) || 1)
|
|
const current = clampAdminPage(page, total)
|
|
|
|
if (total <= 7) {
|
|
return Array.from({ length: total }, (_, index) => index + 1)
|
|
}
|
|
|
|
const pages = new Set([1, total, current - 1, current, current + 1])
|
|
|
|
if (current <= 4) {
|
|
pages.add(2)
|
|
pages.add(3)
|
|
pages.add(4)
|
|
pages.add(5)
|
|
}
|
|
|
|
if (current >= total - 3) {
|
|
pages.add(total - 4)
|
|
pages.add(total - 3)
|
|
pages.add(total - 2)
|
|
pages.add(total - 1)
|
|
}
|
|
|
|
const orderedPages = [...pages].filter((item) => item >= 1 && item <= total).sort((left, right) => left - right)
|
|
return orderedPages.reduce((items, item, index) => {
|
|
const previousPage = orderedPages[index - 1]
|
|
|
|
if (previousPage && item - previousPage > 1) {
|
|
items.push(`ellipsis-${previousPage}-${item}`)
|
|
}
|
|
|
|
items.push(item)
|
|
return items
|
|
}, [])
|
|
}
|