Merge branch 'main' into feature/#69_Add-pages-to-show-pages-to-scroll

# Conflicts:
#	src/components/Carousel/Carousel.svelte
#	src/store.js
#	src/utils/page.js
This commit is contained in:
Vadim
2021-09-05 21:37:05 +03:00
4 changed files with 194 additions and 57 deletions

View File

@@ -134,20 +134,24 @@
} }
let store = createStore() let store = createStore()
let oneSideClonesCount = getOneSideClonesCount({ pagesToScroll, pagesToShow }) let oneSideClonesCount = getOneSideClonesCount({
infinite,
pagesToScroll,
pagesToShow,
})
let currentPageIndex = 0 let currentPageIndex = 0
$: originalCurrentPageIndex = getCurrentPageIndexWithoutClones({ $: currentPageIndexWithoutClones = getCurrentPageIndexWithoutClones({
currentPageIndex, currentPageIndex,
pagesCount, pagesCount,
oneSideClonesCount, oneSideClonesCount,
infinite, infinite,
pagesToScroll pagesToScroll,
}) })
$: dispatch('pageChange', originalCurrentPageIndex) $: dispatch('pageChange', currentPageIndexWithoutClones)
let pagesCount = 0 let pagesCount = 0
$: originalPagesCount = getPagesCountWithoutClones({ $: pagesCountWithoutClones = getPagesCountWithoutClones({
pagesCount, pagesCount,
infinite, infinite,
oneSideClonesCount, oneSideClonesCount,
@@ -180,7 +184,11 @@
} }
// used for lazy loading images, preloaded only current, adjacent and cloanable images // used for lazy loading images, preloaded only current, adjacent and cloanable images
$: loaded = getAdjacentIndexes(originalCurrentPageIndex, originalPagesCount, infinite) $: loaded = getAdjacentIndexes({
pageIndex: currentPageIndexWithoutClones,
pagesCount: pagesCountWithoutClones,
infinite,
})
function initPageSizes() { function initPageSizes() {
const sizes = getPageSizes({ const sizes = getPageSizes({
@@ -197,7 +205,9 @@
pageWidth = sizes.pageWidth pageWidth = sizes.pageWidth
pagesCount = sizes.pagesCount pagesCount = sizes.pagesCount
offsetPage({ animated: false }) offsetPage({
animated: false,
})
} }
function addClones() { function addClones() {
@@ -206,7 +216,7 @@
clonesToPrepend, clonesToPrepend,
} = getClones({ } = getClones({
oneSideClonesCount, oneSideClonesCount,
pagesContainerChildren: pagesContainer.children pagesContainerChildren: pagesContainer.children,
}) })
applyClones({ applyClones({
pagesContainer, pagesContainer,
@@ -321,14 +331,22 @@
} }
async function showPrevPage(options) { async function showPrevPage(options) {
await changePage( await changePage(
() => store.prev({ infinite, pagesCount, pagesToScroll }), () => store.prev({
options infinite,
pagesCount,
pagesToScroll,
}),
options,
) )
} }
async function showNextPage(options) { async function showNextPage(options) {
await changePage( await changePage(
() => store.next({ infinite, pagesCount, pagesToScroll }), () => store.next({
options infinite,
pagesCount,
pagesToScroll,
}),
options,
) )
} }
@@ -369,7 +387,7 @@
<div class="sc-carousel__arrow-container"> <div class="sc-carousel__arrow-container">
<Arrow <Arrow
direction="prev" direction="prev"
disabled={!infinite && originalCurrentPageIndex === 0} disabled={!infinite && currentPageIndexWithoutClones === 0}
on:click={showPrevPage} on:click={showPrevPage}
/> />
</div> </div>
@@ -413,7 +431,7 @@
<div class="sc-carousel__arrow-container"> <div class="sc-carousel__arrow-container">
<Arrow <Arrow
direction="next" direction="next"
disabled={!infinite && originalCurrentPageIndex === originalPagesCount - 1} disabled={!infinite && currentPageIndexWithoutClones === pagesCountWithoutClones - 1}
on:click={showNextPage} on:click={showNextPage}
/> />
</div> </div>
@@ -423,15 +441,15 @@
{#if dots} {#if dots}
<slot <slot
name="dots" name="dots"
currentPageIndex={originalCurrentPageIndex} currentPageIndex={currentPageIndexWithoutClones}
pagesCount={originalPagesCount} pagesCount={pagesCountWithoutClones}
showPage={handlePageChange} showPage={handlePageChange}
> >
{currentPageIndex}/{pagesCount}; {currentPageIndex}/{pagesCount};
{originalCurrentPageIndex}/{originalPagesCount} {originalCurrentPageIndex}/{originalPagesCount}
<Dots <Dots
pagesCount={originalPagesCount} pagesCount={pagesCountWithoutClones}
currentPageIndex={originalCurrentPageIndex} currentPageIndex={currentPageIndexWithoutClones}
on:pageChange={event => handlePageChange(event.detail)} on:pageChange={event => handlePageChange(event.detail)}
></Dots> ></Dots>
</slot> </slot>

View File

@@ -1,8 +1,10 @@
import { writable } from 'svelte/store'; import {
writable,
} from 'svelte/store';
import { import {
getNextPageIndexFn, getNextPageIndexFn,
getPrevPageIndexFn, getPrevPageIndexFn,
getPageIndex getPageIndex,
} from './utils/page' } from './utils/page'
const initState = { const initState = {
@@ -15,22 +17,36 @@ function createStore() {
function init(initialPageIndex) { function init(initialPageIndex) {
set({ set({
...initState, ...initState,
currentPageIndex: initialPageIndex currentPageIndex: initialPageIndex,
}) })
} }
function moveToPage({ pageIndex, pagesCount }) { function moveToPage({
pageIndex,
pagesCount,
}) {
update(store => { update(store => {
return { return {
...store, ...store,
currentPageIndex: getPageIndex(pageIndex, pagesCount), currentPageIndex: getPageIndex({
pageIndex,
pagesCount,
}),
} }
}) })
} }
function next({ infinite, pagesCount, pagesToScroll }) { function next({
infinite,
pagesCount,
pagesToScroll,
}) {
update(store => { update(store => {
const newCurrentPageIndex = getNextPageIndexFn(infinite)(store.currentPageIndex, pagesCount, pagesToScroll) const newCurrentPageIndex = getNextPageIndexFn(infinite)({
currentPageIndex: store.currentPageIndex,
pagesCount,
pagesToScroll,
})
return { return {
...store, ...store,
currentPageIndex: newCurrentPageIndex, currentPageIndex: newCurrentPageIndex,
@@ -38,9 +54,17 @@ function createStore() {
}) })
} }
function prev({ infinite, pagesCount, pagesToScroll }) { function prev({
infinite,
pagesCount,
pagesToScroll,
}) {
update(store => { update(store => {
const newCurrentPageIndex = getPrevPageIndexFn(infinite)(store.currentPageIndex, pagesCount, pagesToScroll) const newCurrentPageIndex = getPrevPageIndexFn(infinite)({
currentPageIndex: store.currentPageIndex,
pagesCount,
pagesToScroll,
})
return { return {
...store, ...store,
currentPageIndex: newCurrentPageIndex, currentPageIndex: newCurrentPageIndex,

View File

@@ -1,9 +1,17 @@
export function getNextPageIndexLimited(currentPageIndex, pagesCount, pagesToScroll) { export function getNextPageIndexLimited({
currentPageIndex,
pagesCount,
pagesToScroll,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
return Math.min(Math.max(currentPageIndex + pagesToScroll, 0), pagesCount - 1) return Math.min(Math.max(currentPageIndex + pagesToScroll, 0), pagesCount - 1)
} }
export function getNextPageIndexInfinte(currentPageIndex, pagesCount, pagesToScroll) { export function getNextPageIndexInfinte({
currentPageIndex,
pagesCount,
pagesToScroll,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
const newCurrentPageIndex = Math.max(currentPageIndex, 0) + pagesToScroll const newCurrentPageIndex = Math.max(currentPageIndex, 0) + pagesToScroll
return newCurrentPageIndex > pagesCount - 1 ? 0 : Math.max(newCurrentPageIndex, 0) return newCurrentPageIndex > pagesCount - 1 ? 0 : Math.max(newCurrentPageIndex, 0)
@@ -13,12 +21,20 @@ export function getNextPageIndexFn(infinite) {
return infinite ? getNextPageIndexInfinte : getNextPageIndexLimited return infinite ? getNextPageIndexInfinte : getNextPageIndexLimited
} }
export function getPrevPageIndexLimited(currentPageIndex, pagesCount, pagesToScroll) { export function getPrevPageIndexLimited({
currentPageIndex,
pagesCount,
pagesToScroll,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
return Math.max(Math.min(currentPageIndex - pagesToScroll, pagesCount - 1), 0) return Math.max(Math.min(currentPageIndex - pagesToScroll, pagesCount - 1), 0)
} }
export function getPrevPageIndexInfinte(currentPageIndex, pagesCount, pagesToScroll) { export function getPrevPageIndexInfinte({
currentPageIndex,
pagesCount,
pagesToScroll,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - pagesToScroll const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - pagesToScroll
return newCurrentPageIndex >= 0 ? Math.min(newCurrentPageIndex, pagesCount - 1) : pagesCount - 1 return newCurrentPageIndex >= 0 ? Math.min(newCurrentPageIndex, pagesCount - 1) : pagesCount - 1
@@ -28,12 +44,19 @@ export function getPrevPageIndexFn(infinite) {
return infinite ? getPrevPageIndexInfinte : getPrevPageIndexLimited return infinite ? getPrevPageIndexInfinte : getPrevPageIndexLimited
} }
export function getPageIndex(pageIndex, pagesCount) { export function getPageIndex({
pageIndex,
pagesCount,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
return pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1) return pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1)
} }
export function getAdjacentIndexes(pageIndex, pagesCount, infinite) { export function getAdjacentIndexes({
pageIndex,
pagesCount,
infinite,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
const _pageIndex = Math.max(0, Math.min(pageIndex, pagesCount - 1)) const _pageIndex = Math.max(0, Math.min(pageIndex, pagesCount - 1))
let rangeStart = _pageIndex - 1; let rangeStart = _pageIndex - 1;
@@ -53,9 +76,9 @@ export function getAdjacentIndexes(pageIndex, pagesCount, infinite) {
export function getClones({ export function getClones({
oneSideClonesCount, oneSideClonesCount,
pagesContainerChildren pagesContainerChildren,
}) { }) {
// TODO: add fns to remove clones // TODO: add fns to remove clones if needed
const clonesToAppend = [] const clonesToAppend = []
for (let i=0; i<oneSideClonesCount; i++) { for (let i=0; i<oneSideClonesCount; i++) {
clonesToAppend.push(pagesContainerChildren[i].cloneNode(true)) clonesToAppend.push(pagesContainerChildren[i].cloneNode(true))
@@ -141,10 +164,12 @@ export function getPagesCountWithoutClones({
1) 1)
} }
// TODO: check
export function getOneSideClonesCount({ export function getOneSideClonesCount({
infinite,
pagesToScroll, pagesToScroll,
pagesToShow, pagesToShow,
}) { }) {
return Math.max(pagesToScroll, pagesToShow) // max - show 4, scroll 3, pages 7 return infinite
? Math.max(pagesToScroll, pagesToShow) // max - show 4, scroll 3, pages 7
: 0
} }

View File

@@ -16,15 +16,25 @@ describe('getNextPageIndexLimited', () => {
{ currentPageIndex: 2, pagesCount: 3, expected: 2 }, { currentPageIndex: 2, pagesCount: 3, expected: 2 },
{ currentPageIndex: 7, pagesCount: 3, expected: 2 }, { currentPageIndex: 7, pagesCount: 3, expected: 2 },
] ]
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => { testCases.forEach(({
expect(getNextPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected) currentPageIndex,
pagesCount,
expected,
}) => {
expect(getNextPageIndexLimited({
currentPageIndex,
pagesCount,
})).toBe(expected)
}) })
}) })
it('throws error if pagesCount is less than 1', () => { it('throws error if pagesCount is less than 1', () => {
const currentPageIndex = 5 const currentPageIndex = 5
const pagesCount = 0 const pagesCount = 0
expect( expect(
() => getNextPageIndexLimited(currentPageIndex, pagesCount) () => getNextPageIndexLimited({
currentPageIndex,
pagesCount,
})
).toThrowError('pagesCount must be at least 1') ).toThrowError('pagesCount must be at least 1')
}) })
}) })
@@ -38,15 +48,25 @@ describe('getNextPageIndexInfinte', () => {
{ currentPageIndex: 2, pagesCount: 3, expected: 0 }, { currentPageIndex: 2, pagesCount: 3, expected: 0 },
{ currentPageIndex: 7, pagesCount: 3, expected: 0 }, { currentPageIndex: 7, pagesCount: 3, expected: 0 },
] ]
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => { testCases.forEach(({
expect(getNextPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected) currentPageIndex,
pagesCount,
expected,
}) => {
expect(getNextPageIndexInfinte({
currentPageIndex,
pagesCount,
})).toBe(expected)
}) })
}) })
it('throws error if pagesCount is less than 1', () => { it('throws error if pagesCount is less than 1', () => {
const currentPageIndex = 5 const currentPageIndex = 5
const pagesCount = 0 const pagesCount = 0
expect( expect(
() => getNextPageIndexInfinte(currentPageIndex, pagesCount) () => getNextPageIndexInfinte({
currentPageIndex,
pagesCount,
})
).toThrowError('pagesCount must be at least 1') ).toThrowError('pagesCount must be at least 1')
}) })
}) })
@@ -60,15 +80,25 @@ describe('getPrevPageIndexLimited', () => {
{ currentPageIndex: 2, pagesCount: 3, expected: 1 }, { currentPageIndex: 2, pagesCount: 3, expected: 1 },
{ currentPageIndex: 7, pagesCount: 3, expected: 2 }, { currentPageIndex: 7, pagesCount: 3, expected: 2 },
] ]
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => { testCases.forEach(({
expect(getPrevPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected) currentPageIndex,
pagesCount,
expected,
}) => {
expect(getPrevPageIndexLimited({
currentPageIndex,
pagesCount,
})).toBe(expected)
}) })
}) })
it('throws error if pagesCount is less than 1', () => { it('throws error if pagesCount is less than 1', () => {
const currentPageIndex = 5 const currentPageIndex = 5
const pagesCount = 0 const pagesCount = 0
expect( expect(
() => getPrevPageIndexLimited(currentPageIndex, pagesCount) () => getPrevPageIndexLimited({
currentPageIndex,
pagesCount,
})
).toThrowError('pagesCount must be at least 1') ).toThrowError('pagesCount must be at least 1')
}) })
}) })
@@ -82,15 +112,25 @@ describe('getPrevPageIndexInfinte', () => {
{ currentPageIndex: 2, pagesCount: 3, expected: 1 }, { currentPageIndex: 2, pagesCount: 3, expected: 1 },
{ currentPageIndex: 7, pagesCount: 3, expected: 1 }, { currentPageIndex: 7, pagesCount: 3, expected: 1 },
] ]
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => { testCases.forEach(({
expect(getPrevPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected) currentPageIndex,
pagesCount,
expected,
}) => {
expect(getPrevPageIndexInfinte({
currentPageIndex,
pagesCount,
})).toBe(expected)
}) })
}) })
it('throws error if pagesCount is less than 1', () => { it('throws error if pagesCount is less than 1', () => {
const currentPageIndex = 5 const currentPageIndex = 5
const pagesCount = 0 const pagesCount = 0
expect( expect(
() => getPrevPageIndexInfinte(currentPageIndex, pagesCount) () => getPrevPageIndexInfinte({
currentPageIndex,
pagesCount,
})
).toThrowError('pagesCount must be at least 1') ).toThrowError('pagesCount must be at least 1')
}) })
}) })
@@ -104,15 +144,25 @@ describe('getPageIndex', () => {
{ pageIndex: 2, pagesCount: 3, expected: 2 }, { pageIndex: 2, pagesCount: 3, expected: 2 },
{ pageIndex: 7, pagesCount: 3, expected: 2 }, { pageIndex: 7, pagesCount: 3, expected: 2 },
] ]
testCases.forEach(({ pageIndex, pagesCount, expected }) => { testCases.forEach(({
expect(getPageIndex(pageIndex, pagesCount)).toBe(expected) pageIndex,
pagesCount,
expected,
}) => {
expect(getPageIndex({
pageIndex,
pagesCount,
})).toBe(expected)
}) })
}) })
it('throws error if pagesCount is less than 1', () => { it('throws error if pagesCount is less than 1', () => {
const pageIndex = 5 const pageIndex = 5
const pagesCount = 0 const pagesCount = 0
expect( expect(
() => getPageIndex(pageIndex, pagesCount) () => getPageIndex({
pageIndex,
pagesCount,
})
).toThrowError('pagesCount must be at least 1') ).toThrowError('pagesCount must be at least 1')
}) })
}) })
@@ -127,8 +177,16 @@ describe('getAdjacentIndexes', () => {
{ pageIndex: 9, pagesCount: 10, expected: [0, 8, 9] }, { pageIndex: 9, pagesCount: 10, expected: [0, 8, 9] },
{ pageIndex: 15, pagesCount: 10, expected: [0, 8, 9] }, { pageIndex: 15, pagesCount: 10, expected: [0, 8, 9] },
] ]
testCases.forEach(({ pageIndex, pagesCount, expected }) => { testCases.forEach(({
expect(getAdjacentIndexes(pageIndex, pagesCount, true)).toEqual(expected) pageIndex,
pagesCount,
expected,
}) => {
expect(getAdjacentIndexes({
pageIndex,
pagesCount,
infinite: true,
})).toEqual(expected)
}) })
}) })
it('returns indexes as expected if not infinite', () => { it('returns indexes as expected if not infinite', () => {
@@ -140,8 +198,16 @@ describe('getAdjacentIndexes', () => {
{ pageIndex: 9, pagesCount: 10, expected: [8, 9] }, { pageIndex: 9, pagesCount: 10, expected: [8, 9] },
{ pageIndex: 15, pagesCount: 10, expected: [8, 9] }, { pageIndex: 15, pagesCount: 10, expected: [8, 9] },
] ]
testCases.forEach(({ pageIndex, pagesCount, expected }) => { testCases.forEach(({
expect(getAdjacentIndexes(pageIndex, pagesCount, false)).toEqual(expected) pageIndex,
pagesCount,
expected,
}) => {
expect(getAdjacentIndexes({
pageIndex,
pagesCount,
infinite: false,
})).toEqual(expected)
}) })
}) })
it('throws error if pagesCount is less than 1', () => { it('throws error if pagesCount is less than 1', () => {
@@ -149,7 +215,11 @@ describe('getAdjacentIndexes', () => {
const pagesCount = 0 const pagesCount = 0
const infinite = true const infinite = true
expect( expect(
() => getAdjacentIndexes(pageIndex, pagesCount, infinite) () => getAdjacentIndexes({
pageIndex,
pagesCount,
infinite,
})
).toThrowError('pagesCount must be at least 1') ).toThrowError('pagesCount must be at least 1')
}) })
}) })