From 938a95ad3b4c40a5a321ae0a18ef2f38d7237419 Mon Sep 17 00:00:00 2001 From: Vadim Date: Sat, 11 Sep 2021 13:18:11 +0300 Subject: [PATCH] #69 : Remove unnecessary fns --- src/components/Carousel/Carousel.svelte | 11 ++- src/store.js | 33 ++++++--- src/utils/page.js | 97 +++++++++++-------------- 3 files changed, 74 insertions(+), 67 deletions(-) diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index cceb9dd..5355650 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -131,6 +131,7 @@ infinite, pageIndex, clonesCountHead: clonesCount.head, + clonesCountTail: clonesCount.tail, particlesToScroll, particlesCount, particlesToShow, @@ -292,6 +293,7 @@ infinite, pageIndex: initialPageIndex, clonesCountHead: clonesCount.head, + clonesCountTail: clonesCount.tail, particlesToScroll, particlesCount, particlesToShow, @@ -314,6 +316,7 @@ infinite, pageIndex, clonesCountHead: clonesCount.head, + clonesCountTail: clonesCount.tail, particlesToScroll, particlesCount, particlesToShow, @@ -376,8 +379,12 @@ await changePage( () => store.prev({ infinite, - particlesCount, + currentPageIndex, + clonesCountHead: clonesCount.head, + clonesCountTail: clonesCount.tail, particlesToScroll, + particlesCount, + particlesToShow, }), options, ) @@ -387,9 +394,11 @@ await changePage( () => store.next({ infinite, + currentPageIndex, particlesCount, particlesToScroll, particlesToShow, + clonesCountHead: clonesCount.head, clonesCountTail: clonesCount.tail, }), options, diff --git a/src/store.js b/src/store.js index 002f6e2..857cf9a 100644 --- a/src/store.js +++ b/src/store.js @@ -2,8 +2,7 @@ import { writable, } from 'svelte/store'; import { - getNextParticleIndexFn, - getPrevParticleIndexFn, + getParticleIndexByPageIndex, } from './utils/page' import { getValueInRange, @@ -37,18 +36,22 @@ function createStore() { function next({ infinite, + currentPageIndex, particlesCount, particlesToScroll, particlesToShow, + clonesCountHead, clonesCountTail, }) { update(store => { - const newCurrentParticleIndex = getNextParticleIndexFn(infinite)({ - currentParticleIndex: store.currentParticleIndex, - particlesCount, - particlesToScroll, - particlesToShow, + const newCurrentParticleIndex = getParticleIndexByPageIndex({ + infinite, + pageIndex: currentPageIndex + 1, + clonesCountHead, clonesCountTail, + particlesToScroll, + particlesCount, + particlesToShow, }) return { ...store, @@ -59,14 +62,22 @@ function createStore() { function prev({ infinite, - particlesCount, + currentPageIndex, + clonesCountHead, + clonesCountTail, particlesToScroll, + particlesCount, + particlesToShow, }) { update(store => { - const newCurrentParticleIndex = getPrevParticleIndexFn(infinite)({ - currentParticleIndex: store.currentParticleIndex, - particlesCount, + const newCurrentParticleIndex = getParticleIndexByPageIndex({ + infinite, + pageIndex: currentPageIndex - 1, + clonesCountHead, + clonesCountTail, particlesToScroll, + particlesCount, + particlesToShow, }) return { ...store, diff --git a/src/utils/page.js b/src/utils/page.js index b872bc8..534840c 100644 --- a/src/utils/page.js +++ b/src/utils/page.js @@ -1,59 +1,7 @@ import { getValueInRange, } from './math' - -export function getNextParticleIndexLimited({ - currentParticleIndex, - particlesCount, - particlesToScroll, - particlesToShow, -}) { - if (particlesCount < 1) throw new Error('particlesCount must be at least 1') - return getValueInRange(0, currentParticleIndex + particlesToScroll, particlesCount - particlesToShow) -} - -export function getNextParticleIndexInfinte({ - currentParticleIndex, - particlesCount, - particlesToScroll, - clonesCountTail, -}) { - if (particlesCount < 1) throw new Error('particlesCount must be at least 1') - const newCurrentParticleIndex = Math.max(currentParticleIndex, 0) + Math.min(particlesCount - clonesCountTail - currentParticleIndex, particlesToScroll) - return newCurrentParticleIndex > particlesCount - 1 ? 0 : Math.max(newCurrentParticleIndex, 0) -} - -export function getNextParticleIndexFn(infinite) { - return infinite ? getNextParticleIndexInfinte : getNextParticleIndexLimited -} - -export function getPrevParticleIndexLimited({ - currentParticleIndex, - particlesCount, - particlesToScroll, -}) { - if (particlesCount < 1) throw new Error('particlesCount must be at least 1') - return getValueInRange( - 0, - currentParticleIndex - Math.min(currentParticleIndex, particlesToScroll), - particlesCount - 1 - ) -} - -export function getPrevParticleIndexInfinte({ - currentParticleIndex, - particlesCount, - particlesToScroll, -}) { - if (particlesCount < 1) throw new Error('particlesCount must be at least 1') - const newCurrentParticleIndex = Math.min(currentParticleIndex, particlesCount - 1) - Math.min(currentParticleIndex, particlesToScroll) - return newCurrentParticleIndex >= 0 ? Math.min(newCurrentParticleIndex, particlesCount - 1) : particlesCount - 1 -} - -export function getPrevParticleIndexFn(infinite) { - return infinite ? getPrevParticleIndexInfinte : getPrevParticleIndexLimited -} - + export function getSizes({ pageWindowElement, particlesContainerChildren, @@ -123,15 +71,54 @@ export function getPagesCountByParticlesCount({ : Math.round(particlesCountWithoutClones / particlesToScroll) } +export function getParticleIndexByPageIndexInfinite({ + pageIndex, + clonesCountHead, + clonesCountTail, + particlesToScroll, + particlesCount, +}) { + return getValueInRange( + clonesCountHead, + Math.min(clonesCountHead + pageIndex * particlesToScroll, particlesCount - clonesCountTail), + particlesCount - 1 + ) +} + +export function getParticleIndexByPageIndexLimited({ + pageIndex, + particlesToScroll, + particlesCount, + particlesToShow, +}) { + return getValueInRange( + 0, + Math.min(pageIndex * particlesToScroll, particlesCount - particlesToShow), + particlesCount - 1 + ) +} + export function getParticleIndexByPageIndex({ infinite, pageIndex, clonesCountHead, + clonesCountTail, particlesToScroll, particlesCount, particlesToShow, }) { return infinite - ? clonesCountHead + pageIndex * particlesToScroll - : Math.min(pageIndex * particlesToScroll, particlesCount - particlesToShow) + ? getParticleIndexByPageIndexInfinite({ + pageIndex, + clonesCountHead, + clonesCountTail, + particlesToScroll, + particlesCount, + }) + : getParticleIndexByPageIndexLimited({ + pageIndex, + particlesToScroll, + particlesCount, + particlesToShow, + }) }