/
use-panel.js
282 lines (228 loc) · 6.7 KB
/
use-panel.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { h, ref, computed, watch, nextTick, getCurrentInstance, Transition, KeepAlive } from 'vue'
import TouchSwipe from '../../directives/TouchSwipe.js'
import useCache from '../../composables/private/use-cache.js'
import { hSlot } from '../../utils/private/render.js'
import { getNormalizedVNodes } from '../../utils/private/vm.js'
export const usePanelChildProps = {
name: { required: true },
disable: Boolean
}
const PanelWrapper = {
setup (_, { slots }) {
return () => h('div', {
class: 'q-panel scroll',
role: 'tabpanel'
}, hSlot(slots.default))
}
}
export const usePanelProps = {
modelValue: {
required: true
},
animated: Boolean,
infinite: Boolean,
swipeable: Boolean,
vertical: Boolean,
transitionPrev: String,
transitionNext: String,
transitionDuration: {
type: [ String, Number ],
default: 300
},
keepAlive: Boolean,
keepAliveInclude: [ String, Array, RegExp ],
keepAliveExclude: [ String, Array, RegExp ],
keepAliveMax: Number
}
export const usePanelEmits = [ 'update:modelValue', 'beforeTransition', 'transition' ]
export default function () {
const { props, emit, proxy } = getCurrentInstance()
const { getCacheWithFn } = useCache()
let panels, forcedPanelTransition
const panelIndex = ref(null)
const panelTransition = ref(null)
function onSwipe (evt) {
const dir = props.vertical === true ? 'up' : 'left'
goToPanelByOffset((proxy.$q.lang.rtl === true ? -1 : 1) * (evt.direction === dir ? 1 : -1))
}
const panelDirectives = computed(() => {
// if props.swipeable
return [ [
TouchSwipe,
onSwipe,
void 0,
{
horizontal: props.vertical !== true,
vertical: props.vertical,
mouse: true
}
] ]
})
const transitionPrev = computed(() =>
props.transitionPrev || `slide-${ props.vertical === true ? 'down' : 'right' }`
)
const transitionNext = computed(() =>
props.transitionNext || `slide-${ props.vertical === true ? 'up' : 'left' }`
)
const transitionStyle = computed(
() => `--q-transition-duration: ${ props.transitionDuration }ms`
)
const contentKey = computed(() => (
typeof props.modelValue === 'string' || typeof props.modelValue === 'number'
? props.modelValue
: String(props.modelValue)
))
const keepAliveProps = computed(() => ({
include: props.keepAliveInclude,
exclude: props.keepAliveExclude,
max: props.keepAliveMax
}))
const needsUniqueKeepAliveWrapper = computed(() =>
props.keepAliveInclude !== void 0
|| props.keepAliveExclude !== void 0
)
watch(() => props.modelValue, (newVal, oldVal) => {
const index = isValidPanelName(newVal) === true
? getPanelIndex(newVal)
: -1
if (forcedPanelTransition !== true) {
updatePanelTransition(
index === -1 ? 0 : (index < getPanelIndex(oldVal) ? -1 : 1)
)
}
if (panelIndex.value !== index) {
panelIndex.value = index
emit('beforeTransition', newVal, oldVal)
nextTick(() => {
emit('transition', newVal, oldVal)
})
}
})
function nextPanel () { goToPanelByOffset(1) }
function previousPanel () { goToPanelByOffset(-1) }
function goToPanel (name) {
emit('update:modelValue', name)
}
function isValidPanelName (name) {
return name !== void 0 && name !== null && name !== ''
}
function getPanelIndex (name) {
return panels.findIndex(panel => {
return panel.props.name === name
&& panel.props.disable !== ''
&& panel.props.disable !== true
})
}
function getEnabledPanels () {
return panels.filter(panel => {
return panel.props.disable !== ''
&& panel.props.disable !== true
})
}
function updatePanelTransition (direction) {
const val = direction !== 0 && props.animated === true && panelIndex.value !== -1
? 'q-transition--' + (direction === -1 ? transitionPrev.value : transitionNext.value)
: null
if (panelTransition.value !== val) {
panelTransition.value = val
}
}
function goToPanelByOffset (direction, startIndex = panelIndex.value) {
let index = startIndex + direction
while (index > -1 && index < panels.length) {
const opt = panels[ index ]
if (
opt !== void 0
&& opt.props.disable !== ''
&& opt.props.disable !== true
) {
updatePanelTransition(direction)
forcedPanelTransition = true
emit('update:modelValue', opt.props.name)
setTimeout(() => {
forcedPanelTransition = false
})
return
}
index += direction
}
if (props.infinite === true && panels.length > 0 && startIndex !== -1 && startIndex !== panels.length) {
goToPanelByOffset(direction, direction === -1 ? panels.length : -1)
}
}
function updatePanelIndex () {
const index = getPanelIndex(props.modelValue)
if (panelIndex.value !== index) {
panelIndex.value = index
}
return true
}
function getPanelContentChild () {
const panel = isValidPanelName(props.modelValue) === true
&& updatePanelIndex()
&& panels[ panelIndex.value ]
return props.keepAlive === true
? [
h(KeepAlive, keepAliveProps.value, [
h(
needsUniqueKeepAliveWrapper.value === true
? getCacheWithFn(contentKey.value, () => ({ ...PanelWrapper, name: contentKey.value }))
: PanelWrapper,
{ key: contentKey.value, style: transitionStyle.value },
() => panel
)
])
]
: [
h('div', {
class: 'q-panel scroll',
style: transitionStyle.value,
key: contentKey.value,
role: 'tabpanel'
}, [ panel ])
]
}
function getPanelContent () {
if (panels.length === 0) {
return
}
return props.animated === true
? [ h(Transition, { name: panelTransition.value }, getPanelContentChild) ]
: getPanelContentChild()
}
function updatePanelsList (slots) {
panels = getNormalizedVNodes(
hSlot(slots.default, [])
).filter(
panel => panel.props !== null
&& panel.props.slot === void 0
&& isValidPanelName(panel.props.name) === true
)
return panels.length
}
function getPanels () {
return panels
}
// expose public methods
Object.assign(proxy, {
next: nextPanel,
previous: previousPanel,
goTo: goToPanel
})
return {
panelIndex,
panelDirectives,
updatePanelsList,
updatePanelIndex,
getPanelContent,
getEnabledPanels,
getPanels,
isValidPanelName,
keepAliveProps,
needsUniqueKeepAliveWrapper,
goToPanelByOffset,
goToPanel,
nextPanel,
previousPanel
}
}