Skip to content

Commit

Permalink
Merge pull request #1205 from ustbhuangyi/feat-indicator-pulldown
Browse files Browse the repository at this point in the history
Feat indicator pulldown
  • Loading branch information
theniceangel authored Jun 23, 2021
2 parents aef6dab + 514df8c commit a9c5440
Show file tree
Hide file tree
Showing 16 changed files with 604 additions and 41 deletions.
157 changes: 157 additions & 0 deletions packages/examples/vue/components/pulldown/sina-weibo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<template>
<div class="pulldown-sina">
<div
class="pulldown-bswrapper"
ref="bsWrapper"
>
<div class="pulldown-scroller">
<div class="pulldown-wrapper">
<div v-html="tipText"></div>
</div>
<ul class="pulldown-list">
<li
:key="i"
class="pulldown-list-item"
v-for="i of dataList"
>{{ `I am item ${i} ` }}</li>
</ul>
</div>
</div>
</div>
</template>

<script>
import BScroll from '@better-scroll/core'
import PullDown from '@better-scroll/pull-down'
BScroll.use(PullDown)
function generateData() {
const BASE = 30
const begin = BASE * STEP
const end = BASE * (STEP + 1)
let ret = []
for (let i = end; i > begin; i--) {
ret.push(i)
}
return ret
}
// pulldownRefresh state
const PHASE = {
moving: {
enter: 'enter',
leave: 'leave'
},
fetching: 'fetching',
succeed: 'succeed'
}
const TIME_BOUNCE = 800
const REQUEST_TIME = 2000
const THRESHOLD = 70
const STOP = 56
let STEP = 0
const ARROW_BOTTOM = '<svg width="16" height="16" viewBox="0 0 512 512"><path fill="currentColor" d="M367.997 338.75l-95.998 95.997V17.503h-32v417.242l-95.996-95.995l-22.627 22.627L256 496l134.624-134.623l-22.627-22.627z"></path></svg>'
const ARROW_UP = '<svg width="16" height="16" viewBox="0 0 512 512"><path fill="currentColor" d="M390.624 150.625L256 16L121.376 150.625l22.628 22.627l95.997-95.998v417.982h32V77.257l95.995 95.995l22.628-22.627z"></path></svg>'
export default {
data() {
return {
tipText: '',
isPullingDown: false,
dataList: generateData()
}
},
mounted() {
this.initBscroll()
},
methods: {
initBscroll() {
this.bscroll = new BScroll(this.$refs.bsWrapper, {
scrollY: true,
bounceTime: TIME_BOUNCE,
useTransition: false,
pullDownRefresh: {
threshold: THRESHOLD,
stop: STOP
}
})
this.bscroll.on('pullingDown', this.pullingDownHandler)
this.bscroll.on('scrollEnd', e => {
console.log('scrollEnd')
})
// v2.4.0 supported
this.bscroll.on('enterThreshold', () => {
this.setTipText(PHASE.moving.enter)
})
this.bscroll.on('leaveThreshold', () => {
this.setTipText(PHASE.moving.leave)
})
},
async pullingDownHandler() {
this.setTipText(PHASE.fetching)
STEP += 1
await this.getData()
this.setTipText(PHASE.succeed)
// tell BetterScroll to finish pull down
this.bscroll.finishPullDown()
// waiting for BetterScroll's bounceAnimation then refresh size
setTimeout(() => {
this.bscroll.refresh()
}, TIME_BOUNCE + 50)
},
async getData() {
const newData = await this.mockFetchData()
this.dataList = newData.concat(this.dataList)
},
mockFetchData() {
return new Promise(resolve => {
setTimeout(() => {
const dataList = generateData()
resolve(dataList)
}, REQUEST_TIME)
})
},
setTipText(phase = PHASE.default) {
const TEXTS_MAP = {
'enter': `${ARROW_BOTTOM} Pull down`,
'leave': `${ARROW_UP} Release`,
'fetching': 'Loading...',
'succeed': 'Refresh succeed'
}
this.tipText = TEXTS_MAP[phase]
}
}
}
</script>

<style lang="stylus" scoped>
.pulldown-sina
height 100%
.pulldown-bswrapper
position relative
height 100%
padding 0 10px
border 1px solid #ccc
overflow hidden
.pulldown-list
padding 0
.pulldown-list-item
padding 10px 0
list-style none
border-bottom 1px solid #ccc
.pulldown-wrapper
position absolute
width 100%
padding 20px
box-sizing border-box
transform translateY(-100%) translateZ(0)
text-align center
color #999
</style>
26 changes: 17 additions & 9 deletions packages/examples/vue/pages/pulldown-entry.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<template>
<pull-down />
<div class="container">
<ul class="example-list">
<li class="example-item" @click="goPage('/pulldown/default')">
<span>Default</span>
</li>
<li class="example-item" @click="goPage('/pulldown/sina')">
<span>Sina-Weibo(v2.4.0)</span>
</li>
</ul>
<transition name="move">
<router-view class="view"></router-view>
</transition>
</div>
</template>

<script>
import PullDown from 'vue-example/components/pulldown/default.vue'
export default {
components: {
PullDown
methods: {
goPage(path) {
this.$router.push(path)
}
}
}
</script>

<style lang="stylus">
</style>
13 changes: 13 additions & 0 deletions packages/examples/vue/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ import ComposeSlideNested from 'vue-example/components/compose/slide-nested'

import IndicatorsMinimap from 'vue-example/components/indicators/minimap'
import IndicatorsParallaxScroll from 'vue-example/components/indicators/parallax-scroll'

import PulldownDefault from 'vue-example/components/pulldown/default'
import PulldownSinaWeibo from 'vue-example/components/pulldown/sina-weibo'
Vue.use(Router)

export default new Router({
Expand Down Expand Up @@ -199,6 +202,16 @@ export default new Router({
{
path: '/pulldown',
component: PullDownEntry,
children: [
{
path: 'default',
component: PulldownDefault
},
{
path: 'sina',
component: PulldownSinaWeibo
}
]
},
{
path: '/scrollbar',
Expand Down
27 changes: 24 additions & 3 deletions packages/pull-down/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ describe('pull down tests', () => {
const mockFn = jest.fn()
scroll.on(scroll.eventTypes.pullingDown, mockFn)

scroll.trigger(scroll.eventTypes.scrollStart)
// simulate pullUp action
scroll.y = -100
scroll.scroller.hooks.trigger(scroll.scroller.hooks.eventTypes.end)
Expand All @@ -112,11 +113,31 @@ describe('pull down tests', () => {
expect(mockFn).toHaveBeenCalledTimes(1)
})

it('should checkLocationOfThresholdBoundary', () => {
const enterThresholdFn = jest.fn()
const leaveThresholdFn = jest.fn()
scroll.on(scroll.eventTypes.enterThreshold, enterThresholdFn)
scroll.on(scroll.eventTypes.leaveThreshold, leaveThresholdFn)
scroll.trigger(scroll.eventTypes.scrollStart)

// enter threshold boundary
scroll.y = 20
scroll.trigger(scroll.eventTypes.scroll)

// leave threshold boundary
scroll.y = 100
scroll.trigger(scroll.eventTypes.scroll)

expect(enterThresholdFn).toHaveBeenCalledTimes(1)
expect(leaveThresholdFn).toHaveBeenCalledTimes(1)
})

it('should trigger pullingDown once', () => {
const mockFn = jest.fn()
scroll.on(scroll.eventTypes.pullingDown, mockFn)
// when
scroll.y = 100
scroll.trigger(scroll.eventTypes.scrollStart)
scroll.scroller.hooks.trigger('end')
scroll.scroller.hooks.trigger('end')
// then
Expand All @@ -136,10 +157,10 @@ describe('pull down tests', () => {
})

it('should work well when call finishPullDown()', () => {
pullDown.pulling = true
pullDown.pulling = 2
pullDown.finishPullDown()

expect(pullDown.pulling).toBe(false)
expect(pullDown.pulling).toBe(0)
expect(scroll.scroller.scrollBehaviorY.computeBoundary).toBeCalled()
expect(scroll.resetPosition).toBeCalled()
})
Expand Down Expand Up @@ -196,7 +217,7 @@ describe('pull down tests', () => {

it('should call finishPullDown when content DOM changed', () => {
// simulate pullDown action
pullDown.pulling = true
pullDown.pulling = 2

scroll.hooks.trigger(scroll.hooks.eventTypes.contentChanged)
expect(scroll.scroller.scrollBehaviorY.computeBoundary).toBeCalled()
Expand Down
Loading

0 comments on commit a9c5440

Please sign in to comment.