export function getNextPageIndexLimited({ currentPageIndex, pagesCount, pagesToScroll, }) { if (pagesCount < 1) throw new Error('pagesCount must be at least 1') console.log('next', pagesCount, currentPageIndex, pagesCount - currentPageIndex) return currentPageIndex + Math.min(pagesCount - (currentPageIndex+1) - pagesToScroll, pagesToScroll) } export function getNextPageIndexInfinte({ currentPageIndex, pagesCount, pagesToScroll, clonesCountTail, }) { if (pagesCount < 1) throw new Error('pagesCount must be at least 1') const newCurrentPageIndex = Math.max(currentPageIndex, 0) + Math.min(pagesCount - clonesCountTail - currentPageIndex, pagesToScroll) return newCurrentPageIndex > pagesCount - 1 ? 0 : Math.max(newCurrentPageIndex, 0) } export function getNextPageIndexFn(infinite) { return infinite ? getNextPageIndexInfinte : getNextPageIndexLimited } export function getPrevPageIndexLimited({ currentPageIndex, pagesCount, pagesToScroll, }) { if (pagesCount < 1) throw new Error('pagesCount must be at least 1') return Math.max( Math.min( currentPageIndex - Math.min(currentPageIndex, pagesToScroll), pagesCount - 1 ), 0) } export function getPrevPageIndexInfinte({ currentPageIndex, pagesCount, pagesToScroll, }) { if (pagesCount < 1) throw new Error('pagesCount must be at least 1') const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - Math.min(currentPageIndex, pagesToScroll) return newCurrentPageIndex >= 0 ? Math.min(newCurrentPageIndex, pagesCount - 1) : pagesCount - 1 } export function getPrevPageIndexFn(infinite) { return infinite ? getPrevPageIndexInfinte : getPrevPageIndexLimited } export function getPageIndex({ pageIndex, pagesCount, }) { if (pagesCount < 1) throw new Error('pagesCount must be at least 1') return pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1) } export function getIndexesOfPagesWithoutClonesInScroll({ scrollIndex, pagesToShow, pagesToScroll, pagesCount, }) { const overlap = scrollIndex === 0 ? 0 : pagesToShow - pagesToScroll const from = scrollIndex * pagesToShow - scrollIndex * overlap const to = from + Math.max(pagesToShow, pagesToScroll) - 1 console.log('=======>', from, to) const indexes = [] for (let i=from; i<=Math.min(pagesCount - 1, to); i++) { indexes.push(i) } return indexes } export function getAdjacentIndexes({ infinite, scrollIndex, scrollsCount, pagesCount, pagesToShow, pagesToScroll, }) { const _scrollIndex = Math.max(0, Math.min(scrollIndex, scrollsCount - 1)) let rangeStart = _scrollIndex - 1 let rangeEnd = _scrollIndex + 1 rangeStart = infinite ? rangeStart < 0 ? scrollsCount - 1 : rangeStart : Math.max(0, rangeStart) rangeEnd = infinite ? rangeEnd > scrollsCount - 1 ? 0 : rangeEnd : Math.min(scrollsCount - 1, rangeEnd) const scrollIndexes = [...new Set([ rangeStart, _scrollIndex, rangeEnd, 0, // needed to clone first scroll pages scrollsCount - 1, // needed to clone last scroll pages ])].sort((a, b) => a - b) const pageIndexes = scrollIndexes.flatMap( scrollIndex => getIndexesOfPagesWithoutClonesInScroll({ scrollIndex, pagesToShow, pagesToScroll, pagesCount, }) ) return { scrollIndexes, pageIndexes: [...new Set(pageIndexes)].sort((a, b) => a - b), } } export function getClones({ headClonesCount, tailClonesCount, pagesContainerChildren, }) { // TODO: add fns to remove clones if needed const clonesToAppend = [] for (let i=0; ilen-1-headClonesCount; i--) { clonesToPrepend.push(pagesContainerChildren[i].cloneNode(true)) } return { clonesToAppend, clonesToPrepend, } } export function applyClones({ pagesContainer, clonesToAppend, clonesToPrepend, }) { for (let i=0; i