Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should update position and size correctly when direction is horizontal', () => {
// given
indicatorOptions.direction = 'horizontal' as Direction
indicator = new Indicator(bscroll, indicatorOptions)
// when
bscroll.trigger('refresh')
// then
expect(indicator.el.style.width).toBe('50px')
expect(indicator.el.style[style.transform as any]).toBe('translateX(5px)')
})
'refresh',
'enable',
'disable',
'destroy',
// scroller
'beforeScrollStart',
'scrollStart',
'scroll',
'scrollEnd',
'touchEnd',
'flick'
])
const res = {
wrapper: wrapper,
options: options,
hooks: new EventEmitter([
'init',
'refresh',
'enable',
'disable',
'destroy'
]),
scroller: new Scroller(wrapper, options),
// own methods
proxy: jest.fn(),
refresh: jest.fn(),
// proxy methods
scrollTo: jest.fn(),
resetPosition: jest.fn(),
registerType: jest.fn().mockImplementation((names: string[]) => {
names.forEach(name => {
const eventTypes = eventEmitter.eventTypes
refresh() {
const scroller = this.scroll.scroller
const scrollBehaviorY = scroller.scrollBehaviorY
// adjust contentSize
const contentRect = getRect(scroller.content)
scrollBehaviorY.contentSize = contentRect.height
this.items = scroller.content.children
this.checkWheelAllDisabled()
this.itemHeight = this.items.length
? scrollBehaviorY.contentSize / this.items.length
: 0
if (this.selectedIndex === undefined) {
this.selectedIndex = this.options.selectedIndex || 0
}
this.scroll.maxScrollX = 0
this.scroll.maxScrollY = -this.itemHeight * (this.items.length - 1)
this.scroll.minScrollX = 0
private checkClick(e: TouchEvent) {
// when in the process of pulling down, it should not prevent click
const cancelable = {
preventClick: this.animater.forceStopped
}
// we scrolled less than momentumLimitDistance pixels
if (this.hooks.trigger(this.hooks.eventTypes.checkClick)) return true
if (!cancelable.preventClick) {
const _dblclick = this.options.dblclick
let dblclickTrigged = false
if (_dblclick && this.lastClickTime) {
const { delay = 300 } = _dblclick as any
if (getNow() - this.lastClickTime < delay) {
dblclickTrigged = true
dblclick(e)
}
}
if (this.options.tap) {
tap(e, this.options.tap)
}
if (
this.options.click &&
!preventDefaultExceptionFn(
e.target,
this.options.preventDefaultException
)
) {
click(e)
}
private handleStart(e: TouchEvent) {
const timestamp = getNow()
this.moved = false
this.startTime = timestamp
this.directionLockAction.reset()
this.scrollBehaviorX.start()
this.scrollBehaviorY.start()
// force stopping last transition or animation
this.animater.stop()
this.scrollBehaviorX.resetStartPos()
this.scrollBehaviorY.resetStartPos()
this.hooks.trigger(this.hooks.eventTypes.start, e)
private handleMove(deltaX: number, deltaY: number, e: TouchEvent) {
if (this.hooks.trigger(this.hooks.eventTypes.beforeMove, e)) {
return
}
const absDistX = this.scrollBehaviorX.getAbsDist(deltaX)
const absDistY = this.scrollBehaviorY.getAbsDist(deltaY)
const timestamp = getNow()
// We need to move at least momentumLimitDistance pixels
// for the scrolling to initiate
if (this.checkMomentum(absDistX, absDistY, timestamp)) {
return true
}
if (this.directionLockAction.checkMovingDirection(absDistX, absDistY, e)) {
this.actionsHandler.setInitiated()
return true
}
const delta = this.directionLockAction.adjustDelta(deltaX, deltaY)
const newX = this.scrollBehaviorX.move(delta.deltaX)
const newY = this.scrollBehaviorY.move(delta.deltaY)
private getEaseTime() {
const DEFAULT_EASETIME = 300
const SAFE_EASETIME = 100
const easeTime = this.mouseWheelOpt.easeTime || DEFAULT_EASETIME
// scrollEnd event will be triggered in every calling of scrollTo when easeTime is too small
// easeTime needs to be greater than 100
if (easeTime < SAFE_EASETIME) {
warn(`easeTime should be greater than 100.
If mouseWheel easeTime is too small, scrollEnd will be triggered many times.`)
}
return easeTime
}
}
move(delta: number) {
delta = this.hasScroll ? delta : 0
this.movingDirection =
delta > 0
? Direction.Negative
: delta < 0
? Direction.Positive
: Direction.Default
let newPos = this.currentPos + delta
// Slow down or stop if outside of the boundaries
if (newPos > this.minScrollPos || newPos < this.maxScrollPos) {
if (
(newPos > this.minScrollPos && this.options.bounces[0]) ||
(newPos < this.maxScrollPos && this.options.bounces[1])
) {
newPos = this.currentPos + delta / 3
} else {
newPos =
newPos > this.minScrollPos ? this.minScrollPos : this.maxScrollPos
let momentumInfo: {
destination?: number
duration?: number
} = {
duration: 0
}
const absDist = Math.abs(this.currentPos - this.startPos)
// start momentum animation if needed
if (
this.options.momentum &&
duration < this.options.momentumLimitTime &&
absDist > this.options.momentumLimitDistance
) {
const wrapperSize =
(this.direction === Direction.Negative && this.options.bounces[0]) ||
(this.direction === Direction.Positive && this.options.bounces[1])
? this.wrapperSize
: 0
momentumInfo = this.hasScroll
? this.momentum(
this.currentPos,
this.startPos,
duration,
this.maxScrollPos,
this.minScrollPos,
wrapperSize,
this.options
)
: { destination: this.currentPos, duration: 0 }
} else {
private _checkPullDown() {
if (!this.scroll.options.pullDownRefresh) {
return
}
const { threshold = 90, stop = 40 } = this.scroll.options
.pullDownRefresh as PullDownRefreshConfig
// check if a real pull down action
if (
this.scroll.directionY !== Direction.Negative ||
this.scroll.y < threshold
) {
return false
}
if (!this.pulling) {
this.pulling = true
this.scroll.trigger('pullingDown')
this.originalMinScrollY = this.scroll.minScrollY
this.scroll.minScrollY = stop
}
this.scroll.scrollTo(
this.scroll.x,
stop,