Add pageCount check, add getAdjacentIndexes
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
addResizeEventListener,
|
||||
removeResizeEventListener
|
||||
} from '../../utils/event'
|
||||
import { getAdjacentIndexes } from '../../utils/page'
|
||||
|
||||
const directionFnDescription = {
|
||||
[NEXT]: showNextPage,
|
||||
@@ -60,19 +61,14 @@
|
||||
let currentPageIndex = 0
|
||||
$: originalCurrentPageIndex = currentPageIndex - Number(infinite);
|
||||
let pagesCount = 0
|
||||
$: originalPagesCount = Math.max(pagesCount - (infinite ? 2 : 0), 0) // without clones
|
||||
$: originalPagesCount = Math.max(pagesCount - (infinite ? 2 : 0), 1) // without clones
|
||||
let pageWidth = 0
|
||||
let offset = 0
|
||||
let pageWindowElement
|
||||
let pagesElement
|
||||
|
||||
// used for lazy loading images, preloaded only current, adjacent and cloanable images
|
||||
$: loaded = [
|
||||
...new Set([
|
||||
0, originalPagesCount-1,
|
||||
originalCurrentPageIndex - 1, originalCurrentPageIndex, originalCurrentPageIndex + 1
|
||||
])
|
||||
].filter(index => index >= 0)
|
||||
$: loaded = getAdjacentIndexes(originalCurrentPageIndex, originalPagesCount)
|
||||
|
||||
function applyPageSizes() {
|
||||
const children = pagesElement.children
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
export function getNextPageIndexLimited(currentPageIndex, pagesCount) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
return Math.min(Math.max(currentPageIndex + 1, 0), pagesCount - 1)
|
||||
}
|
||||
|
||||
export function getNextPageIndexInfinte(currentPageIndex, pagesCount) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
const newCurrentPageIndex = Math.max(currentPageIndex, 0) + 1
|
||||
return newCurrentPageIndex > pagesCount - 1 ? 0 : Math.max(newCurrentPageIndex, 0)
|
||||
}
|
||||
@@ -12,10 +14,12 @@ export function getNextPageIndexFn(infinite) {
|
||||
}
|
||||
|
||||
export function getPrevPageIndexLimited(currentPageIndex, pagesCount) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
return Math.max(Math.min(currentPageIndex - 1, pagesCount - 1), 0)
|
||||
}
|
||||
|
||||
export function getPrevPageIndexInfinte(currentPageIndex, pagesCount) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - 1
|
||||
return newCurrentPageIndex >= 0 ? Math.min(newCurrentPageIndex, pagesCount - 1) : pagesCount - 1
|
||||
}
|
||||
@@ -25,5 +29,24 @@ export function getPrevPageIndexFn(infinite) {
|
||||
}
|
||||
|
||||
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 getAdjacentIndexes(pageIndex, pagesCount, infinite) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
const _pageIndex = Math.max(0, Math.min(pageIndex, pagesCount - 1))
|
||||
let rangeStart = _pageIndex - 1;
|
||||
let rangeEnd = _pageIndex + 1;
|
||||
rangeStart = rangeStart < 0
|
||||
? infinite
|
||||
? pagesCount - 1
|
||||
: 0
|
||||
: rangeStart
|
||||
rangeEnd = rangeEnd > pagesCount - 1
|
||||
? infinite
|
||||
? 0
|
||||
: pagesCount - 1
|
||||
: rangeEnd
|
||||
return [...new Set([rangeStart, rangeEnd, _pageIndex])].sort((a, b) => a - b)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,9 @@ import {
|
||||
getNextPageIndexInfinte,
|
||||
getPrevPageIndexLimited,
|
||||
getPrevPageIndexInfinte,
|
||||
getPageIndex
|
||||
} from './page.js';
|
||||
getPageIndex,
|
||||
getAdjacentIndexes
|
||||
} from './page.js'
|
||||
|
||||
describe('getNextPageIndexLimited', () => {
|
||||
it('returns next page index as expected', () => {
|
||||
@@ -16,10 +17,17 @@ describe('getNextPageIndexLimited', () => {
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getNextPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getNextPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getNextPageIndexLimited(currentPageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getNextPageIndexInfinte', () => {
|
||||
it('returns next page index as expected', () => {
|
||||
@@ -31,10 +39,17 @@ describe('getNextPageIndexInfinte', () => {
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 0 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getNextPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getNextPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getNextPageIndexInfinte(currentPageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPrevPageIndexLimited', () => {
|
||||
it('returns prev page index as expected', () => {
|
||||
@@ -46,10 +61,17 @@ describe('getPrevPageIndexLimited', () => {
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getPrevPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getPrevPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getPrevPageIndexLimited(currentPageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPrevPageIndexInfinte', () => {
|
||||
it('returns prev page index as expected', () => {
|
||||
@@ -61,10 +83,17 @@ describe('getPrevPageIndexInfinte', () => {
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 1 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getPrevPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getPrevPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getPrevPageIndexInfinte(currentPageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPageIndex', () => {
|
||||
it('returns normalized page index as expected', () => {
|
||||
@@ -76,7 +105,51 @@ describe('getPageIndex', () => {
|
||||
{ pageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ pageIndex, pagesCount, expected }) => {
|
||||
expect(getPageIndex(pageIndex, pagesCount)).toBe(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(getPageIndex(pageIndex, pagesCount)).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const pageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getPageIndex(pageIndex, pagesCount)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getAdjacentIndexes', () => {
|
||||
it('returns indexes as expected if infinite', () => {
|
||||
const testCases = [
|
||||
{ pageIndex: 0, pagesCount: 1, expected: [0] },
|
||||
{ pageIndex: -5, pagesCount: 10, expected: [0, 1, 9] },
|
||||
{ pageIndex: 0, pagesCount: 10, expected: [0, 1, 9] },
|
||||
{ pageIndex: 5, pagesCount: 10, expected: [4, 5, 6] },
|
||||
{ pageIndex: 9, pagesCount: 10, expected: [0, 8, 9] },
|
||||
{ pageIndex: 15, pagesCount: 10, expected: [0, 8, 9] },
|
||||
]
|
||||
testCases.forEach(({ pageIndex, pagesCount, expected }) => {
|
||||
expect(getAdjacentIndexes(pageIndex, pagesCount, true)).toEqual(expected)
|
||||
})
|
||||
})
|
||||
it('returns indexes as expected if not infinite', () => {
|
||||
const testCases = [
|
||||
{ pageIndex: 0, pagesCount: 1, expected: [0] },
|
||||
{ pageIndex: -5, pagesCount: 10, expected: [0, 1] },
|
||||
{ pageIndex: 0, pagesCount: 10, expected: [0, 1] },
|
||||
{ pageIndex: 5, pagesCount: 10, expected: [4, 5, 6] },
|
||||
{ pageIndex: 9, pagesCount: 10, expected: [8, 9] },
|
||||
{ pageIndex: 15, pagesCount: 10, expected: [8, 9] },
|
||||
]
|
||||
testCases.forEach(({ pageIndex, pagesCount, expected }) => {
|
||||
expect(getAdjacentIndexes(pageIndex, pagesCount, false)).toEqual(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const pageIndex = 5
|
||||
const pagesCount = 0
|
||||
const infinite = true
|
||||
expect(
|
||||
() => getAdjacentIndexes(pageIndex, pagesCount, infinite)
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user