forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQPullToRefresh.vue
More file actions
167 lines (157 loc) · 3.89 KB
/
QPullToRefresh.vue
File metadata and controls
167 lines (157 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<template>
<div class="pull-to-refresh">
<div
class="pull-to-refresh-container"
:style="style"
v-touch-pan.vertical.scroll="__pull"
>
<div class="pull-to-refresh-message row flex-center">
<q-icon v-show="state !== 'refreshing'" :class="{'rotate-180': state === 'pulled'}" name="arrow_downward"></q-icon>
<q-icon v-show="state === 'refreshing'" class="animate-spin" :name="refreshIcon"></q-icon>
<span v-html="message"></span>
</div>
<slot></slot>
</div>
</div>
</template>
<script>
import { getScrollTarget, getScrollPosition } from '../../utils/scroll'
import { cssTransform } from '../../utils/dom'
import { QIcon } from '../icon'
import TouchPan from '../../directives/touch-pan'
export default {
name: 'q-pull-to-refresh',
components: {
QIcon
},
directives: {
TouchPan
},
props: {
handler: {
type: Function,
required: true
},
distance: {
type: Number,
default: 35
},
pullMessage: {
type: String,
default: 'Pull down to refresh'
},
releaseMessage: {
type: String,
default: 'Release to refresh'
},
refreshMessage: {
type: String,
default: 'Refreshing...'
},
refreshIcon: {
type: String,
default: 'refresh'
},
inline: Boolean,
disable: Boolean
},
data () {
let height = 65
return {
state: 'pull',
pullPosition: -height,
height: height,
animating: false,
pulling: false,
scrolling: false
}
},
computed: {
message () {
switch (this.state) {
case 'pulled':
return this.releaseMessage
case 'refreshing':
return this.refreshMessage
case 'pull':
default:
return this.pullMessage
}
},
style () {
return cssTransform(`translateY(${this.pullPosition}px)`)
}
},
methods: {
__pull (event) {
if (this.disable) {
return
}
if (event.isFinal) {
this.scrolling = false
this.pulling = false
if (this.scrolling) {
return
}
if (this.state === 'pulled') {
this.state = 'refreshing'
this.__animateTo(0)
this.trigger()
}
else if (this.state === 'pull') {
this.__animateTo(-this.height)
}
return
}
if (this.animating || this.scrolling || this.state === 'refreshing') {
return true
}
let top = getScrollPosition(this.scrollContainer)
if (top !== 0 || (top === 0 && event.direction !== 'down')) {
this.scrolling = true
if (this.pulling) {
this.pulling = false
this.state = 'pull'
this.__animateTo(-this.height)
}
return true
}
event.evt.preventDefault()
this.pulling = true
this.pullPosition = -this.height + Math.max(0, Math.pow(event.distance.y, 0.85))
this.state = this.pullPosition > this.distance ? 'pulled' : 'pull'
},
__animateTo (target, done, previousCall) {
if (!previousCall && this.animationId) {
cancelAnimationFrame(this.animating)
}
this.pullPosition -= (this.pullPosition - target) / 7
if (this.pullPosition - target > 1) {
this.animating = window.requestAnimationFrame(() => {
this.__animateTo(target, done, true)
})
}
else {
this.animating = window.requestAnimationFrame(() => {
this.pullPosition = target
this.animating = false
done && done()
})
}
},
trigger () {
this.handler(() => {
this.__animateTo(-this.height, () => {
this.state = 'pull'
})
})
}
},
mounted () {
this.$nextTick(() => {
this.scrollContainer = this.inline ? this.$el.parentNode : getScrollTarget(this.$el)
})
}
}
</script>