|
|
|
|
@@ -13,30 +13,19 @@
|
|
|
|
|
} from '../../utils/event'
|
|
|
|
|
import { getAdjacentIndexes } from '../../utils/page'
|
|
|
|
|
import { get } from '../../utils/object'
|
|
|
|
|
import { ProgressManager } from '../../utils/ProgressManager.js'
|
|
|
|
|
import { ProgressManager } from '../../utils/ProgressManager'
|
|
|
|
|
import { wait } from '../../utils/interval'
|
|
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher()
|
|
|
|
|
|
|
|
|
|
const autoplayDirectionFnDescription = {
|
|
|
|
|
[NEXT]: () => {
|
|
|
|
|
progressManager.start(() => {
|
|
|
|
|
showNextPage()
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
[PREV]: () => {
|
|
|
|
|
progressManager.start(() => {
|
|
|
|
|
showPrevPage()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
[NEXT]: async () => await progressManager.start(showNextPage),
|
|
|
|
|
[PREV]: async () => await progressManager.start(showPrevPage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const directionFnDescription = {
|
|
|
|
|
[NEXT]: () => {
|
|
|
|
|
showNextPage()
|
|
|
|
|
},
|
|
|
|
|
[PREV]: () => {
|
|
|
|
|
showPrevPage()
|
|
|
|
|
}
|
|
|
|
|
[NEXT]: showNextPage,
|
|
|
|
|
[PREV]: showPrevPage
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -70,6 +59,9 @@
|
|
|
|
|
* Enables autoplay of pages
|
|
|
|
|
*/
|
|
|
|
|
export let autoplay = false
|
|
|
|
|
$: {
|
|
|
|
|
applyAutoplayIfNeeded(autoplay)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Autoplay change interval (ms)
|
|
|
|
|
@@ -96,31 +88,42 @@
|
|
|
|
|
*/
|
|
|
|
|
export let dots = true
|
|
|
|
|
|
|
|
|
|
export function goTo(pageIndex, options) {
|
|
|
|
|
export async function goTo(pageIndex, options) {
|
|
|
|
|
const animated = get(options, 'animated', true)
|
|
|
|
|
if (typeof pageIndex !== 'number') {
|
|
|
|
|
throw new Error('pageIndex should be a number')
|
|
|
|
|
}
|
|
|
|
|
showPage(pageIndex + Number(infinite), { animated })
|
|
|
|
|
await showPage(pageIndex + Number(infinite), { animated })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function goToPrev(options) {
|
|
|
|
|
export async function goToPrev(options) {
|
|
|
|
|
const animated = get(options, 'animated', true)
|
|
|
|
|
showPrevPage({ animated })
|
|
|
|
|
await showPrevPage({ animated })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function goToNext(options) {
|
|
|
|
|
export async function goToNext(options) {
|
|
|
|
|
const animated = get(options, 'animated', true)
|
|
|
|
|
showNextPage({ animated })
|
|
|
|
|
await showNextPage({ animated })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let store = createStore()
|
|
|
|
|
let currentPageIndex = 0
|
|
|
|
|
$: originalCurrentPageIndex = currentPageIndex - Number(infinite);
|
|
|
|
|
$: originalCurrentPageIndex = getOriginalCurrentPageIndex(currentPageIndex, pagesCount, infinite) // index without cloenes
|
|
|
|
|
$: dispatch('pageChange', originalCurrentPageIndex)
|
|
|
|
|
|
|
|
|
|
let pagesCount = 0
|
|
|
|
|
$: originalPagesCount = Math.max(pagesCount - (infinite ? 2 : 0), 1) // without clones
|
|
|
|
|
|
|
|
|
|
function getOriginalCurrentPageIndex(currentPageIndex, pagesCount, infinite) {
|
|
|
|
|
if (infinite) {
|
|
|
|
|
const CLONES_COUNT = 2
|
|
|
|
|
if (currentPageIndex === pagesCount - 1) return 0
|
|
|
|
|
if (currentPageIndex === 0) return (pagesCount - CLONES_COUNT) - 1
|
|
|
|
|
return currentPageIndex - 1
|
|
|
|
|
}
|
|
|
|
|
return currentPageIndex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let pageWidth = 0
|
|
|
|
|
let offset = 0
|
|
|
|
|
let pageWindowElement
|
|
|
|
|
@@ -169,7 +172,7 @@
|
|
|
|
|
pagesElement.append(first.cloneNode(true))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applyAutoplayIfNeeded(options) {
|
|
|
|
|
async function applyAutoplayIfNeeded(autoplay) {
|
|
|
|
|
// prevent progress change if not infinite for first and last page
|
|
|
|
|
if (
|
|
|
|
|
!infinite && (
|
|
|
|
|
@@ -180,16 +183,8 @@
|
|
|
|
|
progressManager.reset()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (autoplay) {
|
|
|
|
|
const delayMs = get(options, 'delayMs', 0)
|
|
|
|
|
if (delayMs) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
autoplayDirectionFnDescription[autoplayDirection]()
|
|
|
|
|
}, delayMs)
|
|
|
|
|
} else {
|
|
|
|
|
autoplayDirectionFnDescription[autoplayDirection]()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
autoplay && await autoplayDirectionFnDescription[autoplayDirection]()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cleanupFns = []
|
|
|
|
|
@@ -211,8 +206,6 @@
|
|
|
|
|
applyPageSizes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
applyAutoplayIfNeeded()
|
|
|
|
|
|
|
|
|
|
addResizeEventListener(applyPageSizes)
|
|
|
|
|
})()
|
|
|
|
|
})
|
|
|
|
|
@@ -222,26 +215,30 @@
|
|
|
|
|
cleanupFns.filter(fn => fn && typeof fn === 'function').forEach(fn => fn())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function handlePageChange(pageIndex) {
|
|
|
|
|
showPage(pageIndex + Number(infinite))
|
|
|
|
|
async function handlePageChange(pageIndex) {
|
|
|
|
|
await showPage(pageIndex + Number(infinite))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function offsetPage(animated) {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
// _duration is an offset animation time
|
|
|
|
|
_duration = animated ? duration : 0
|
|
|
|
|
offset = -currentPageIndex * pageWidth
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
resolve()
|
|
|
|
|
}, _duration)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// makes delayed jump to 1st or last element
|
|
|
|
|
function jumpIfNeeded() {
|
|
|
|
|
async function jumpIfNeeded() {
|
|
|
|
|
let jumped = false
|
|
|
|
|
if (infinite) {
|
|
|
|
|
if (currentPageIndex === 0) {
|
|
|
|
|
// offsetDelayMs should depend on _duration, as it wait when offset finishes
|
|
|
|
|
showPage(pagesCount - 2, { offsetDelayMs: _duration, animated: false })
|
|
|
|
|
await showPage(pagesCount - 2, { animated: false })
|
|
|
|
|
jumped = true
|
|
|
|
|
} else if (currentPageIndex === pagesCount - 1) {
|
|
|
|
|
showPage(1, { offsetDelayMs: _duration, animated: false })
|
|
|
|
|
await showPage(1, { animated: false })
|
|
|
|
|
jumped = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -250,54 +247,43 @@
|
|
|
|
|
|
|
|
|
|
// Disable page change while animation is in progress
|
|
|
|
|
let disabled = false
|
|
|
|
|
function safeChangePage(cb, options) {
|
|
|
|
|
const animated = get(options, 'animated', true)
|
|
|
|
|
async function changePage(updateStoreFn, options) {
|
|
|
|
|
if (disabled) return
|
|
|
|
|
cb()
|
|
|
|
|
disabled = true
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
|
|
|
|
updateStoreFn()
|
|
|
|
|
await offsetPage(get(options, 'animated', true))
|
|
|
|
|
disabled = false
|
|
|
|
|
}, animated ? duration : 0)
|
|
|
|
|
|
|
|
|
|
const jumped = await jumpIfNeeded()
|
|
|
|
|
!jumped && applyAutoplayIfNeeded(autoplay) // no need to wait it finishes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showPage(pageIndex, options) {
|
|
|
|
|
const animated = get(options, 'animated', true)
|
|
|
|
|
const offsetDelayMs = get(options, 'offsetDelayMs', 0)
|
|
|
|
|
safeChangePage(() => {
|
|
|
|
|
store.moveToPage({ pageIndex, pagesCount })
|
|
|
|
|
// delayed page transition, used for infinite autoplay to jump to real page
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
offsetPage(animated)
|
|
|
|
|
const jumped = jumpIfNeeded()
|
|
|
|
|
!jumped && applyAutoplayIfNeeded({ delayMs: _duration }) // while offset animation is in progress (delayMs = _duration ms) wait for it
|
|
|
|
|
}, offsetDelayMs)
|
|
|
|
|
}, { animated })
|
|
|
|
|
async function showPage(pageIndex, options) {
|
|
|
|
|
await changePage(
|
|
|
|
|
() => store.moveToPage({ pageIndex, pagesCount }),
|
|
|
|
|
options
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
function showPrevPage(options) {
|
|
|
|
|
const animated = get(options, 'animated', true)
|
|
|
|
|
safeChangePage(() => {
|
|
|
|
|
store.prev({ infinite, pagesCount })
|
|
|
|
|
offsetPage(animated)
|
|
|
|
|
const jumped = jumpIfNeeded()
|
|
|
|
|
!jumped && applyAutoplayIfNeeded({ delayMs: _duration })
|
|
|
|
|
}, { animated })
|
|
|
|
|
async function showPrevPage(options) {
|
|
|
|
|
await changePage(
|
|
|
|
|
() => store.prev({ infinite, pagesCount }),
|
|
|
|
|
options
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
function showNextPage(options) {
|
|
|
|
|
const animated = get(options, 'animated', true)
|
|
|
|
|
safeChangePage(() => {
|
|
|
|
|
store.next({ infinite, pagesCount })
|
|
|
|
|
offsetPage(animated)
|
|
|
|
|
const jumped = jumpIfNeeded()
|
|
|
|
|
!jumped && applyAutoplayIfNeeded({ delayMs: _duration })
|
|
|
|
|
}, { animated })
|
|
|
|
|
async function showNextPage(options) {
|
|
|
|
|
await changePage(
|
|
|
|
|
() => store.next({ infinite, pagesCount }),
|
|
|
|
|
options
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// gestures
|
|
|
|
|
function handleSwipeStart() {
|
|
|
|
|
_duration = 0
|
|
|
|
|
}
|
|
|
|
|
function handleThreshold(event) {
|
|
|
|
|
directionFnDescription[event.detail.direction]()
|
|
|
|
|
async function handleThreshold(event) {
|
|
|
|
|
await directionFnDescription[event.detail.direction]()
|
|
|
|
|
}
|
|
|
|
|
function handleSwipeMove(event) {
|
|
|
|
|
offset += event.detail.dx
|
|
|
|
|
|