#69 : Apply clones number

This commit is contained in:
Vadim
2021-09-04 19:12:03 +03:00
parent 835e793f39
commit a27cd3bfdf

View File

@@ -99,19 +99,20 @@
/** /**
* Number of pages to show * Number of pages to show
*/ */
export let pagesToShow = 2 export let pagesToShow = 3
/** /**
* Number of pages to scroll * Number of pages to scroll
*/ */
export let pagesToScroll = 2 export let pagesToScroll = 1
export async function goTo(pageIndex, options) { export async function goTo(pageIndex, options) {
const animated = get(options, 'animated', true) const animated = get(options, 'animated', true)
if (typeof pageIndex !== 'number') { if (typeof pageIndex !== 'number') {
throw new Error('pageIndex should be a number') throw new Error('pageIndex should be a number')
} }
await showPage(pageIndex + Number(infinite), { animated }) await showPage(pageIndex + clonesCount, { animated })
} }
export async function goToPrev(options) { export async function goToPrev(options) {
@@ -129,17 +130,25 @@
$: originalCurrentPageIndex = getOriginalCurrentPageIndex(currentPageIndex, pagesCount, infinite) // index without cloenes $: originalCurrentPageIndex = getOriginalCurrentPageIndex(currentPageIndex, pagesCount, infinite) // index without cloenes
$: dispatch('pageChange', originalCurrentPageIndex) $: dispatch('pageChange', originalCurrentPageIndex)
const PAGE_CLONES_COUNT = 2 let clonesCount = Math.max(pagesToScroll, pagesToShow) // TODO: check
$: pagesItemsClonesCount = pagesToShow * 2 $: pagesItemsClonesCount = clonesCount * 2
let pagesCount = 0 let pagesCount = 0
let pagesItemsCount = 0
$: originalPagesCount = Math.max(pagesCount - (infinite ? PAGE_CLONES_COUNT : 0), 1) // without clones $: originalPagesCount = Math.max(
Math.ceil(
(
pagesCount - (infinite ? pagesItemsClonesCount : 0)
) / pagesToScroll
),
1) // without clones
function getOriginalCurrentPageIndex(currentPageIndex, pagesCount, infinite) { function getOriginalCurrentPageIndex(currentPageIndex, pagesCount, infinite) {
console.log('getOriginalCurrentPageIndex', currentPageIndex, pagesCount, infinite)
console.log('pagesItemsClonesCount', pagesItemsClonesCount)
if (infinite) { if (infinite) {
if (currentPageIndex === pagesCount - 1) return 0 if (currentPageIndex === pagesCount - clonesCount) return 0
if (currentPageIndex === 0) return (pagesCount - PAGE_CLONES_COUNT) - 1 if (currentPageIndex === 0) return (pagesCount - clonesCount)
return currentPageIndex - 1 return currentPageIndex - clonesCount
} }
return currentPageIndex return currentPageIndex
} }
@@ -178,10 +187,9 @@
pagesItemWidth = pagesWindowWidth / pagesToShow pagesItemWidth = pagesWindowWidth / pagesToShow
pagesItemsCount = children.length pagesCount = children.length
pagesCount = (pagesItemsCount / pagesToScroll) // Math ceil
for (let pageIndex=0; pageIndex<pagesItemsCount; pageIndex++) { for (let pageIndex=0; pageIndex<children.length; pageIndex++) {
children[pageIndex].style.minWidth = `${pagesItemWidth}px` children[pageIndex].style.minWidth = `${pagesItemWidth}px`
children[pageIndex].style.maxWidth = `${pagesItemWidth}px` children[pageIndex].style.maxWidth = `${pagesItemWidth}px`
} }
@@ -193,12 +201,12 @@
// TODO: move to utils // TODO: move to utils
// TODO: add fns to remove clones // TODO: add fns to remove clones
const toAppend = [] const toAppend = []
for (let i=0; i<pagesToShow; i++) { for (let i=0; i<clonesCount; i++) {
toAppend.push(pagesElement.children[i].cloneNode(true)) toAppend.push(pagesElement.children[i].cloneNode(true))
} }
const toPrepend = [] const toPrepend = []
const len = pagesElement.children.length const len = pagesElement.children.length
for (let i=len - 1; i>len - 1 - pagesToShow; i--) { for (let i=len - 1; i>len - 1 - clonesCount; i--) {
toPrepend.push(pagesElement.children[i].cloneNode(true)) toPrepend.push(pagesElement.children[i].cloneNode(true))
} }
@@ -234,15 +242,17 @@
await tick() await tick()
cleanupFns.push(store.subscribe(value => { cleanupFns.push(store.subscribe(value => {
currentPageIndex = value.currentPageIndex currentPageIndex = value.currentPageIndex
console.log('currentPageIndex', currentPageIndex)
})) }))
cleanupFns.push(() => progressManager.reset()) cleanupFns.push(() => progressManager.reset())
if (pagesElement && pageWindowElement) { if (pagesElement && pageWindowElement) {
// load first and last child to clone them // load first and last child to clone them
// TODO: update
loaded = [0, pagesElement.children.length - 1] loaded = [0, pagesElement.children.length - 1]
await tick() await tick()
infinite && addClones() infinite && addClones()
store.init(initialPageIndex + Number(infinite)) store.init(initialPageIndex + clonesCount)
applyPageSizes() applyPageSizes()
} }
@@ -256,7 +266,7 @@
}) })
async function handlePageChange(pageIndex) { async function handlePageChange(pageIndex) {
await showPage(pageIndex + Number(infinite)) await showPage(pageIndex + clonesCount)
} }
function offsetPage(options) { function offsetPage(options) {
@@ -264,7 +274,10 @@
return new Promise((resolve) => { return new Promise((resolve) => {
// _duration is an offset animation time // _duration is an offset animation time
_duration = animated ? duration : 0 _duration = animated ? duration : 0
console.log(currentPageIndex, pagesItemWidth, pagesToScroll)
offset = -currentPageIndex * (pagesItemWidth * pagesToScroll) offset = -currentPageIndex * (pagesItemWidth * pagesToScroll)
console.log('offset', offset)
setTimeout(() => { setTimeout(() => {
resolve() resolve()
}, _duration) }, _duration)
@@ -276,10 +289,10 @@
let jumped = false let jumped = false
if (infinite) { if (infinite) {
if (currentPageIndex === 0) { if (currentPageIndex === 0) {
await showPage(pagesCount - PAGE_CLONES_COUNT, { animated: false }) await showPage(pagesCount - clonesCount, { animated: false })
jumped = true jumped = true
} else if (currentPageIndex === pagesCount - 1) { } else if (currentPageIndex === pagesCount - clonesCount) {
await showPage(1, { animated: false }) await showPage(clonesCount, { animated: false })
jumped = true jumped = true
} }
} }
@@ -415,6 +428,7 @@
pagesCount={originalPagesCount} pagesCount={originalPagesCount}
showPage={handlePageChange} showPage={handlePageChange}
> >
{originalCurrentPageIndex}/{originalPagesCount}
<Dots <Dots
pagesCount={originalPagesCount} pagesCount={originalPagesCount}
currentPageIndex={originalCurrentPageIndex} currentPageIndex={originalCurrentPageIndex}