#69 : Code cleanup
This commit is contained in:
@@ -21,11 +21,11 @@
|
||||
getCurrentPageIndexWithoutClones,
|
||||
getPagesCountWithoutClones,
|
||||
getClonesCount,
|
||||
getPartialPageSize,
|
||||
} from '../../utils/page'
|
||||
import { get } from '../../utils/object'
|
||||
import { ProgressManager } from '../../utils/ProgressManager'
|
||||
import { wait } from '../../utils/interval'
|
||||
import { getIsOdd, getPartialPageSize } from '../../utils/math'
|
||||
|
||||
const dispatch = createEventDispatcher()
|
||||
|
||||
@@ -210,7 +210,6 @@
|
||||
}
|
||||
|
||||
function addClones() {
|
||||
console.log('addClones', clonesCount)
|
||||
const {
|
||||
clonesToAppend,
|
||||
clonesToPrepend,
|
||||
@@ -250,7 +249,6 @@
|
||||
await tick()
|
||||
cleanupFns.push(store.subscribe(value => {
|
||||
currentPageIndex = value.currentPageIndex
|
||||
console.log('currentPageIndex', currentPageIndex)
|
||||
}))
|
||||
cleanupFns.push(() => progressManager.reset())
|
||||
if (pagesContainer && pageWindowElement) {
|
||||
|
||||
@@ -4,52 +4,3 @@ export const getDistance = (p1, p2) => {
|
||||
|
||||
return Math.sqrt((xDist * xDist) + (yDist * yDist));
|
||||
}
|
||||
|
||||
// TODO: determine when partial offset is needed and apply partial offset
|
||||
// TODO: determine how many clones should be from both sides
|
||||
|
||||
|
||||
// TODO: refactor pagesToShow <= pagesToScroll
|
||||
// TODO: think about case if pagesCount < pagesToShow and pagesCount < pagesToScroll
|
||||
|
||||
// TODO: math to page
|
||||
export function getPartialPageSize({
|
||||
pagesToScroll,
|
||||
pagesToShow,
|
||||
pagesCountWithoutClones
|
||||
}) {
|
||||
const overlap = pagesToScroll - pagesToShow
|
||||
let _pages = pagesToShow
|
||||
|
||||
while(true) {
|
||||
const diff = pagesCountWithoutClones - _pages - overlap
|
||||
if (diff < pagesToShow) {
|
||||
return diff
|
||||
}
|
||||
_pages += pagesToShow + overlap
|
||||
}
|
||||
|
||||
|
||||
// if (pagesToShow <= pagesToScroll) {
|
||||
// const overlap = pagesToScroll - pagesToShow
|
||||
// let _pages = pagesToShow
|
||||
|
||||
// while(true) {
|
||||
// const diff = pagesCountWithoutClones - _pages - overlap
|
||||
// if (diff < pagesToShow) {
|
||||
// return diff
|
||||
// }
|
||||
// _pages += pagesToShow + overlap
|
||||
// }
|
||||
// } else {
|
||||
// const overlap = pagesToShow - pagesToScroll
|
||||
// let _pages = pagesToShow
|
||||
// while(true) {
|
||||
// const diff = pagesCountWithoutClones - _pages + overlap
|
||||
// if (diff < pagesToShow) {
|
||||
// return diff
|
||||
// }
|
||||
// _pages += pagesToShow - overlap
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import {
|
||||
getDistance,
|
||||
getPartialPageSize
|
||||
} from './math.js'
|
||||
|
||||
describe('getDistance', () => {
|
||||
@@ -18,114 +17,3 @@ describe('getDistance', () => {
|
||||
expect(getDistance(p1, p2)).toBe(5)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPartialPageSize', () => {
|
||||
|
||||
it('getPartialPageSize', () => {
|
||||
// ==== pagesToShow <= pagesToScroll
|
||||
const r0 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 9,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 3,
|
||||
})
|
||||
expect(r0).toBe(0)
|
||||
|
||||
const r1 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 15,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 5,
|
||||
})
|
||||
expect(r1).toBe(0)
|
||||
|
||||
const r2 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 16,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 5,
|
||||
})
|
||||
expect(r2).toBe(1)
|
||||
|
||||
const r3 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 17,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 5,
|
||||
})
|
||||
expect(r3).toBe(2)
|
||||
|
||||
const r4 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 18,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 5,
|
||||
})
|
||||
expect(r4).toBe(3)
|
||||
|
||||
const r5 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
})
|
||||
expect(r5).toBe(0)
|
||||
|
||||
// ====== pagesToScroll < pagesToShow
|
||||
|
||||
const r6 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 2,
|
||||
})
|
||||
expect(r6).toBe(2)
|
||||
|
||||
const r7 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 7,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 3,
|
||||
})
|
||||
expect(r7).toBe(1)
|
||||
|
||||
const r8 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 3,
|
||||
})
|
||||
expect(r8).toBe(2)
|
||||
|
||||
const r9 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
})
|
||||
expect(r9).toBe(0)
|
||||
|
||||
const r10 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 9,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 3,
|
||||
})
|
||||
expect(r10).toBe(3)
|
||||
|
||||
const r11 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 3,
|
||||
pagesToScroll: 2,
|
||||
})
|
||||
expect(r11).toBe(2)
|
||||
|
||||
|
||||
const r12 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 6,
|
||||
pagesToShow: 3,
|
||||
pagesToScroll: 1,
|
||||
})
|
||||
expect(r12).toBe(2)
|
||||
|
||||
|
||||
const r13 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 7,
|
||||
pagesToShow: 3,
|
||||
pagesToScroll: 1,
|
||||
})
|
||||
expect(r13).toBe(2)
|
||||
|
||||
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
@@ -104,8 +104,6 @@ export function applyClones({
|
||||
clonesToAppend,
|
||||
clonesToPrepend,
|
||||
}) {
|
||||
console.log('clonesToPrepend', clonesToPrepend.length)
|
||||
console.log('clonesToAppend', clonesToAppend.length)
|
||||
for (let i=0; i<clonesToAppend.length; i++) {
|
||||
pagesContainer.append(clonesToAppend[i])
|
||||
}
|
||||
@@ -173,7 +171,6 @@ export function getClonesCount({
|
||||
pagesToShow,
|
||||
partialPageSize,
|
||||
}) {
|
||||
console.log('partialPageSize', partialPageSize)
|
||||
// Math.max(pagesToScroll, pagesToShow) // max - show 4, scroll 3, pages 7
|
||||
const clonesCount = infinite
|
||||
? {
|
||||
@@ -184,10 +181,26 @@ export function getClonesCount({
|
||||
tail: 0,
|
||||
}
|
||||
|
||||
console.log('partialPageSize', partialPageSize)
|
||||
|
||||
return {
|
||||
...clonesCount,
|
||||
total: clonesCount.head + clonesCount.tail,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: think about case if pagesCount < pagesToShow and pagesCount < pagesToScroll
|
||||
export function getPartialPageSize({
|
||||
pagesToScroll,
|
||||
pagesToShow,
|
||||
pagesCountWithoutClones
|
||||
}) {
|
||||
const overlap = pagesToScroll - pagesToShow
|
||||
let _pages = pagesToShow
|
||||
|
||||
while(true) {
|
||||
const diff = pagesCountWithoutClones - _pages - overlap
|
||||
if (diff < pagesToShow) {
|
||||
return diff
|
||||
}
|
||||
_pages += pagesToShow + overlap
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,3 +223,109 @@ describe('getAdjacentIndexes', () => {
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPartialPageSize', () => {
|
||||
it('getPartialPageSize', () => {
|
||||
// ==== pagesToShow <= pagesToScroll
|
||||
const r0 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 9,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 3,
|
||||
})
|
||||
expect(r0).toBe(0)
|
||||
|
||||
const r1 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 15,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 5,
|
||||
})
|
||||
expect(r1).toBe(0)
|
||||
|
||||
const r2 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 16,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 5,
|
||||
})
|
||||
expect(r2).toBe(1)
|
||||
|
||||
const r3 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 17,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 5,
|
||||
})
|
||||
expect(r3).toBe(2)
|
||||
|
||||
const r4 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 18,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 5,
|
||||
})
|
||||
expect(r4).toBe(3)
|
||||
|
||||
const r5 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
})
|
||||
expect(r5).toBe(0)
|
||||
|
||||
// ====== pagesToScroll < pagesToShow
|
||||
|
||||
const r6 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 2,
|
||||
})
|
||||
expect(r6).toBe(2)
|
||||
|
||||
const r7 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 7,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 3,
|
||||
})
|
||||
expect(r7).toBe(1)
|
||||
|
||||
const r8 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 3,
|
||||
})
|
||||
expect(r8).toBe(2)
|
||||
|
||||
const r9 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
})
|
||||
expect(r9).toBe(0)
|
||||
|
||||
const r10 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 9,
|
||||
pagesToShow: 4,
|
||||
pagesToScroll: 3,
|
||||
})
|
||||
expect(r10).toBe(3)
|
||||
|
||||
const r11 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 3,
|
||||
pagesToScroll: 2,
|
||||
})
|
||||
expect(r11).toBe(2)
|
||||
|
||||
const r12 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 6,
|
||||
pagesToShow: 3,
|
||||
pagesToScroll: 1,
|
||||
})
|
||||
expect(r12).toBe(2)
|
||||
|
||||
const r13 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 7,
|
||||
pagesToShow: 3,
|
||||
pagesToScroll: 1,
|
||||
})
|
||||
expect(r13).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user