Merge pull request #74 from vadimkorr/feature/#73_Fix-width-apply
feature/#73 Apply width on resize
This commit is contained in:
@@ -8,19 +8,15 @@
|
|||||||
import { swipeable } from '../../actions/swipeable'
|
import { swipeable } from '../../actions/swipeable'
|
||||||
import { hoverable } from '../../actions/hoverable'
|
import { hoverable } from '../../actions/hoverable'
|
||||||
import { tappable } from '../../actions/tappable'
|
import { tappable } from '../../actions/tappable'
|
||||||
import {
|
|
||||||
addResizeEventListener,
|
|
||||||
removeResizeEventListener
|
|
||||||
} from '../../utils/event'
|
|
||||||
import {
|
import {
|
||||||
getAdjacentIndexes,
|
getAdjacentIndexes,
|
||||||
getClones,
|
getClones,
|
||||||
applyClones,
|
applyClones,
|
||||||
getPageSizes,
|
|
||||||
applyPageSizes,
|
applyPageSizes,
|
||||||
getCurrentPageIndexWithoutClones,
|
getCurrentPageIndexWithoutClones,
|
||||||
getPagesCountWithoutClones,
|
getPagesCountWithoutClones,
|
||||||
getOneSideClonesCount,
|
getOneSideClonesCount,
|
||||||
|
createResizeObserver,
|
||||||
} from '../../utils/page'
|
} from '../../utils/page'
|
||||||
import { get } from '../../utils/object'
|
import { get } from '../../utils/object'
|
||||||
import { ProgressManager } from '../../utils/ProgressManager'
|
import { ProgressManager } from '../../utils/ProgressManager'
|
||||||
@@ -145,6 +141,21 @@
|
|||||||
let pageWidth = 0
|
let pageWidth = 0
|
||||||
let offset = 0
|
let offset = 0
|
||||||
let pageWindowElement
|
let pageWindowElement
|
||||||
|
|
||||||
|
const pageWindowElementResizeObserver = createResizeObserver(({ width }) => {
|
||||||
|
pagesWindowWidth = width
|
||||||
|
pageWidth = width
|
||||||
|
|
||||||
|
applyPageSizes({
|
||||||
|
pagesContainerChildren: pagesContainer.children,
|
||||||
|
pageWidth,
|
||||||
|
})
|
||||||
|
|
||||||
|
offsetPage({
|
||||||
|
animated: false,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
let pagesContainer
|
let pagesContainer
|
||||||
let focused = false
|
let focused = false
|
||||||
|
|
||||||
@@ -173,25 +184,6 @@
|
|||||||
infinite,
|
infinite,
|
||||||
})
|
})
|
||||||
|
|
||||||
function initPageSizes() {
|
|
||||||
const sizes = getPageSizes({
|
|
||||||
pageWindowElement,
|
|
||||||
pagesContainerChildren: pagesContainer.children,
|
|
||||||
})
|
|
||||||
applyPageSizes({
|
|
||||||
pagesContainerChildren: pagesContainer.children,
|
|
||||||
pageWidth: sizes.pageWidth,
|
|
||||||
})
|
|
||||||
|
|
||||||
pagesWindowWidth = sizes.pagesWindowWidth
|
|
||||||
pageWidth = sizes.pageWidth
|
|
||||||
pagesCount = sizes.pagesCount
|
|
||||||
|
|
||||||
offsetPage({
|
|
||||||
animated: false,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function addClones() {
|
function addClones() {
|
||||||
const {
|
const {
|
||||||
clonesToAppend,
|
clonesToAppend,
|
||||||
@@ -240,16 +232,16 @@
|
|||||||
await tick()
|
await tick()
|
||||||
infinite && addClones()
|
infinite && addClones()
|
||||||
|
|
||||||
|
pagesCount = pagesContainer.children.length
|
||||||
store.init(initialPageIndex + oneSideClonesCount)
|
store.init(initialPageIndex + oneSideClonesCount)
|
||||||
initPageSizes()
|
|
||||||
}
|
|
||||||
|
|
||||||
addResizeEventListener(initPageSizes)
|
pageWindowElementResizeObserver.observe(pageWindowElement);
|
||||||
|
}
|
||||||
})()
|
})()
|
||||||
})
|
})
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
removeResizeEventListener(initPageSizes)
|
pageWindowElementResizeObserver.disconnect()
|
||||||
cleanupFns.filter(fn => fn && typeof fn === 'function').forEach(fn => fn())
|
cleanupFns.filter(fn => fn && typeof fn === 'function').forEach(fn => fn())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,3 @@
|
|||||||
// resize event
|
|
||||||
export function addResizeEventListener(cb) {
|
|
||||||
window.addEventListener('resize', cb)
|
|
||||||
}
|
|
||||||
export function removeResizeEventListener(cb) {
|
|
||||||
window.removeEventListener('resize', cb)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createDispatcher(source) {
|
export function createDispatcher(source) {
|
||||||
return function (event, data) {
|
return function (event, data) {
|
||||||
source.dispatchEvent(
|
source.dispatchEvent(
|
||||||
|
|||||||
@@ -105,21 +105,6 @@ export function applyClones({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPageSizes({
|
|
||||||
pageWindowElement,
|
|
||||||
pagesContainerChildren,
|
|
||||||
}) {
|
|
||||||
const pagesWindowWidth = pageWindowElement.clientWidth
|
|
||||||
const pageWidth = pagesWindowWidth
|
|
||||||
const pagesCount = pagesContainerChildren.length
|
|
||||||
|
|
||||||
return {
|
|
||||||
pagesWindowWidth,
|
|
||||||
pageWidth,
|
|
||||||
pagesCount,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function applyPageSizes({
|
export function applyPageSizes({
|
||||||
pagesContainerChildren,
|
pagesContainerChildren,
|
||||||
pageWidth,
|
pageWidth,
|
||||||
@@ -157,3 +142,11 @@ export function getOneSideClonesCount({
|
|||||||
}) {
|
}) {
|
||||||
return infinite ? 1 : 0
|
return infinite ? 1 : 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createResizeObserver(onResize) {
|
||||||
|
return new ResizeObserver(entries => {
|
||||||
|
onResize({
|
||||||
|
width: entries[0].contentRect.width,
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user