-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathQTabs.js
More file actions
705 lines (575 loc) · 20.9 KB
/
QTabs.js
File metadata and controls
705 lines (575 loc) · 20.9 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
import { h, ref, computed, watch, onBeforeUnmount, onActivated, onDeactivated, getCurrentInstance, provide } from 'vue'
import QIcon from '../icon/QIcon.js'
import QResizeObserver from '../resize-observer/QResizeObserver.js'
import useTick from '../../composables/use-tick/use-tick.js'
import useTimeout from '../../composables/use-timeout/use-timeout.js'
import { createComponent } from '../../utils/private.create/create.js'
import { hSlot } from '../../utils/private.render/render.js'
import { tabsKey } from '../../utils/private.symbols/symbols.js'
import { rtlHasScrollBug } from '../../utils/private.rtl/rtl.js'
function getIndicatorClass (color, top, vertical, shape) {
if (shape === 'pill') return `q-tab__indicator--pill${ color ? ` text-${ color }` : '' }`
const pos = vertical === true
? [ 'left', 'right' ]
: [ 'top', 'bottom' ]
return `q-tab__indicator--line absolute-${ top === true ? pos[ 0 ] : pos[ 1 ] }${ color ? ` text-${ color }` : '' }`
}
const alignValues = [ 'left', 'center', 'right', 'justify' ]
const indicatorShapeValues = [ 'line', 'pill' ]
export default createComponent({
name: 'QTabs',
props: {
modelValue: [ Number, String ],
align: {
type: String,
default: 'center',
validator: v => alignValues.includes(v)
},
breakpoint: {
type: [ String, Number ],
default: 600
},
vertical: Boolean,
shrink: Boolean,
stretch: Boolean,
activeClass: String,
activeColor: String,
activeBgColor: String,
indicatorColor: String,
indicatorShape: {
type: String,
default: 'line',
validator: v => indicatorShapeValues.includes(v)
},
leftIcon: String,
rightIcon: String,
outsideArrows: Boolean,
mobileArrows: Boolean,
switchIndicator: Boolean,
narrowIndicator: Boolean,
inlineLabel: Boolean,
noCaps: Boolean,
dense: Boolean,
contentClass: String,
'onUpdate:modelValue': [ Function, Array ]
},
setup (props, { slots, emit }) {
const { proxy } = getCurrentInstance()
const { $q } = proxy
const { registerTick: registerScrollTick } = useTick()
const { registerTick: registerUpdateArrowsTick } = useTick()
const { registerTick: registerAnimateTick } = useTick()
const { registerTimeout: registerFocusTimeout, removeTimeout: removeFocusTimeout } = useTimeout()
const { registerTimeout: registerScrollToTabTimeout, removeTimeout: removeScrollToTabTimeout } = useTimeout()
const rootRef = ref(null)
const contentRef = ref(null)
const currentModel = ref(props.modelValue)
const scrollable = ref(false)
const leftArrow = ref(true)
const rightArrow = ref(false)
const justify = ref(false)
const tabDataList = []
const tabDataListLen = ref(0)
const hasFocus = ref(false)
let animateTimer = null, scrollTimer = null, unwatchRoute
const tabProps = computed(() => ({
activeClass: props.activeClass,
activeColor: props.activeColor,
activeBgColor: props.activeBgColor,
indicatorClass: getIndicatorClass(
props.indicatorColor,
props.switchIndicator,
props.vertical,
props.indicatorShape
),
narrowIndicator: props.narrowIndicator,
inlineLabel: props.inlineLabel,
noCaps: props.noCaps
}))
const hasActiveTab = computed(() => {
const len = tabDataListLen.value
const val = currentModel.value
for (let i = 0; i < len; i++) {
if (tabDataList[ i ].name.value === val) {
return true
}
}
return false
})
const alignClass = computed(() => {
const align = scrollable.value === true
? 'left'
: (justify.value === true ? 'justify' : props.align)
return `q-tabs__content--align-${ align }`
})
const classes = computed(() =>
'q-tabs row no-wrap items-center'
+ ` q-tabs--${ scrollable.value === true ? '' : 'not-' }scrollable`
+ ` q-tabs--${ props.vertical === true ? 'vertical' : 'horizontal' }`
+ ` q-tabs__arrows--${ props.outsideArrows === true ? 'outside' : 'inside' }`
+ ` q-tabs--mobile-with${ props.mobileArrows === true ? '' : 'out' }-arrows`
+ (props.dense === true ? ' q-tabs--dense' : '')
+ (props.shrink === true ? ' col-shrink' : '')
+ (props.stretch === true ? ' self-stretch' : '')
)
const innerClass = computed(() =>
'q-tabs__content scroll--mobile row no-wrap items-center self-stretch hide-scrollbar relative-position '
+ alignClass.value
+ (props.contentClass !== void 0 ? ` ${ props.contentClass }` : '')
)
const domProps = computed(() => (
props.vertical === true
? { container: 'height', content: 'offsetHeight', scroll: 'scrollHeight' }
: { container: 'width', content: 'offsetWidth', scroll: 'scrollWidth' }
))
const isRTL = computed(() => props.vertical !== true && $q.lang.rtl === true)
const rtlPosCorrection = computed(() => rtlHasScrollBug === false && isRTL.value === true)
watch(isRTL, updateArrows)
watch(() => props.modelValue, name => {
updateModel({ name, setCurrent: true, skipEmit: true })
})
watch(() => props.outsideArrows, recalculateScroll)
function updateModel ({ name, setCurrent, skipEmit }) {
if (currentModel.value === name) return
if (skipEmit !== true && props[ 'onUpdate:modelValue' ] !== void 0) {
emit('update:modelValue', name)
}
if (
setCurrent === true
|| props[ 'onUpdate:modelValue' ] === void 0
) {
animate(currentModel.value, name)
currentModel.value = name
}
}
function recalculateScroll () {
registerScrollTick(() => {
rootRef.value && updateContainer({
width: rootRef.value.offsetWidth,
height: rootRef.value.offsetHeight
})
})
}
function updateContainer (domSize) {
// it can be called faster than component being initialized
// so we need to protect against that case
// (one example of such case is the docs release notes page)
if (domProps.value === void 0 || contentRef.value === null) return
const
size = domSize[ domProps.value.container ],
scrollSize = Math.min(
contentRef.value[ domProps.value.scroll ],
Array.prototype.reduce.call(
contentRef.value.children,
(acc, el) => acc + (el[ domProps.value.content ] || 0),
0
)
),
scroll = size > 0 && scrollSize > size // when there is no tab, in Chrome, size === 0 and scrollSize === 1
scrollable.value = scroll
// Arrows need to be updated even if the scroll status was already true
scroll === true && registerUpdateArrowsTick(updateArrows)
justify.value = size < parseInt(props.breakpoint, 10)
}
function animate (oldName, newName) {
const
oldTab = oldName !== void 0 && oldName !== null && oldName !== ''
? tabDataList.find(tab => tab.name.value === oldName)
: null,
newTab = newName !== void 0 && newName !== null && newName !== ''
? tabDataList.find(tab => tab.name.value === newName)
: null
if (hadActivated === true) {
// After the component has been re-activated
// we should not animate the transition.
// Consider it as if the component has just been mounted.
hadActivated = false
}
else if (oldTab && newTab) {
const
oldEl = oldTab.tabIndicatorRef.value,
newEl = newTab.tabIndicatorRef.value
if (animateTimer !== null) {
clearTimeout(animateTimer)
animateTimer = null
}
oldEl.style.transition = 'none'
oldEl.style.transform = 'none'
newEl.style.transition = 'none'
newEl.style.transform = 'none'
const
oldPos = oldEl.getBoundingClientRect(),
newPos = newEl.getBoundingClientRect()
newEl.style.transform = props.vertical === true
? `translate3d(0,${ oldPos.top - newPos.top }px,0) scale3d(1,${ newPos.height ? oldPos.height / newPos.height : 1 },1)`
: `translate3d(${ oldPos.left - newPos.left }px,0,0) scale3d(${ newPos.width ? oldPos.width / newPos.width : 1 },1,1)`
// allow scope updates to kick in (QRouteTab needs more time)
registerAnimateTick(() => {
animateTimer = setTimeout(() => {
animateTimer = null
newEl.style.transition = 'transform .25s cubic-bezier(.4, 0, .2, 1)'
newEl.style.transform = 'none'
}, 70)
})
}
if (newTab && scrollable.value === true) {
scrollToTabEl(newTab.rootRef.value)
}
}
function scrollToTabEl (el) {
const
{ left, width, top, height } = contentRef.value.getBoundingClientRect(),
newPos = el.getBoundingClientRect()
let offset = props.vertical === true ? newPos.top - top : newPos.left - left
if (offset < 0) {
contentRef.value[ props.vertical === true ? 'scrollTop' : 'scrollLeft' ] += Math.floor(offset)
updateArrows()
return
}
offset += props.vertical === true ? newPos.height - height : newPos.width - width
if (offset > 0) {
contentRef.value[ props.vertical === true ? 'scrollTop' : 'scrollLeft' ] += Math.ceil(offset)
updateArrows()
}
}
function updateArrows () {
const content = contentRef.value
if (content === null) return
const
rect = content.getBoundingClientRect(),
pos = props.vertical === true ? content.scrollTop : Math.abs(content.scrollLeft)
if (isRTL.value === true) {
leftArrow.value = Math.ceil(pos + rect.width) < content.scrollWidth - 1
rightArrow.value = pos > 0
}
else {
leftArrow.value = pos > 0
rightArrow.value = props.vertical === true
? Math.ceil(pos + rect.height) < content.scrollHeight
: Math.ceil(pos + rect.width) < content.scrollWidth
}
}
function animScrollTo (value) {
scrollTimer !== null && clearInterval(scrollTimer)
scrollTimer = setInterval(() => {
if (scrollTowards(value) === true) {
stopAnimScroll()
}
}, 5)
}
function scrollToStart () {
animScrollTo(rtlPosCorrection.value === true ? Number.MAX_SAFE_INTEGER : 0)
}
function scrollToEnd () {
animScrollTo(rtlPosCorrection.value === true ? 0 : Number.MAX_SAFE_INTEGER)
}
function stopAnimScroll () {
if (scrollTimer !== null) {
clearInterval(scrollTimer)
scrollTimer = null
}
}
function onKbdNavigate (keyCode, fromEl) {
const tabs = Array.prototype.filter.call(
contentRef.value.children,
el => el === fromEl || (el.matches && el.matches('.q-tab.q-focusable') === true)
)
const len = tabs.length
if (len === 0) return
if (keyCode === 36) { // Home
scrollToTabEl(tabs[ 0 ])
tabs[ 0 ].focus()
return true
}
if (keyCode === 35) { // End
scrollToTabEl(tabs[ len - 1 ])
tabs[ len - 1 ].focus()
return true
}
const dirPrev = keyCode === (props.vertical === true ? 38 /* ArrowUp */ : 37 /* ArrowLeft */)
const dirNext = keyCode === (props.vertical === true ? 40 /* ArrowDown */ : 39 /* ArrowRight */)
const dir = dirPrev === true ? -1 : (dirNext === true ? 1 : void 0)
if (dir !== void 0) {
const rtlDir = isRTL.value === true ? -1 : 1
const index = tabs.indexOf(fromEl) + dir * rtlDir
if (index >= 0 && index < len) {
scrollToTabEl(tabs[ index ])
tabs[ index ].focus({ preventScroll: true })
}
return true
}
}
// let's speed up execution of time-sensitive scrollTowards()
// with a computed variable by directly applying the minimal
// number of instructions on get/set functions
const posFn = computed(() => (
rtlPosCorrection.value === true
? { get: content => Math.abs(content.scrollLeft), set: (content, pos) => { content.scrollLeft = -pos } }
: (
props.vertical === true
? { get: content => content.scrollTop, set: (content, pos) => { content.scrollTop = pos } }
: { get: content => content.scrollLeft, set: (content, pos) => { content.scrollLeft = pos } }
)
))
function scrollTowards (value) {
const
content = contentRef.value,
{ get, set } = posFn.value
let
done = false,
pos = get(content)
const direction = value < pos ? -1 : 1
pos += direction * 5
if (pos < 0) {
done = true
pos = 0
}
else if (
(direction === -1 && pos <= value)
|| (direction === 1 && pos >= value)
) {
done = true
pos = value
}
set(content, pos)
updateArrows()
return done
}
function hasQueryIncluded (targetQuery, matchingQuery) {
for (const key in targetQuery) {
if (targetQuery[ key ] !== matchingQuery[ key ]) {
return false
}
}
return true
}
// 1. Do not use directly; use verifyRouteModel() instead
// 2. Should set hadActivated to false upon exit
function updateActiveRoute () {
let name = null, bestScore = { matchedLen: 0, queryDiff: 9999, hrefLen: 0 }
const list = tabDataList.filter(tab => tab.routeData?.hasRouterLink.value === true)
const { hash: currentHash, query: currentQuery } = proxy.$route
const currentQueryLen = Object.keys(currentQuery).length
// Vue Router does not keep account of hash & query when matching
// so we're doing this as well
for (const tab of list) {
const exact = tab.routeData.exact.value === true
if (tab.routeData[ exact === true ? 'linkIsExactActive' : 'linkIsActive' ].value !== true) {
// it cannot match anything as it's not active nor exact-active
continue
}
const { hash, query, matched, href } = tab.routeData.resolvedLink.value
const queryLen = Object.keys(query).length
if (exact === true) {
if (hash !== currentHash) {
// it's set to exact but it doesn't matches the hash
continue
}
if (
queryLen !== currentQueryLen
|| hasQueryIncluded(currentQuery, query) === false
) {
// it's set to exact but it doesn't matches the query
continue
}
// yey, we found the perfect match (route + hash + query)
name = tab.name.value
break
}
if (hash !== '' && hash !== currentHash) {
// it has hash and it doesn't matches
continue
}
if (
queryLen !== 0
&& hasQueryIncluded(query, currentQuery) === false
) {
// it has query and it doesn't includes the current one
continue
}
const newScore = {
matchedLen: matched.length,
queryDiff: currentQueryLen - queryLen,
hrefLen: href.length - hash.length
}
if (newScore.matchedLen > bestScore.matchedLen) {
// it matches more routes so it's more specific so we set it as current champion
name = tab.name.value
bestScore = newScore
continue
}
else if (newScore.matchedLen !== bestScore.matchedLen) {
// it matches less routes than the current champion so we discard it
continue
}
if (newScore.queryDiff < bestScore.queryDiff) {
// query is closer to the current one so we set it as current champion
name = tab.name.value
bestScore = newScore
}
else if (newScore.queryDiff !== bestScore.queryDiff) {
// it matches less routes than the current champion so we discard it
continue
}
if (newScore.hrefLen > bestScore.hrefLen) {
// href is lengthier so it's more specific so we set it as current champion
name = tab.name.value
bestScore = newScore
}
}
if (
name === null
&& tabDataList.some(tab => tab.routeData === void 0 && tab.name.value === currentModel.value) === true
) {
// we shouldn't interfere if non-route tab is active
hadActivated = false
return
}
updateModel({ name, setCurrent: true })
}
function onFocusin (e) {
removeFocusTimeout()
if (
hasFocus.value !== true
&& rootRef.value !== null
&& e.target
&& typeof e.target.closest === 'function'
) {
const tab = e.target.closest('.q-tab')
// if the target is contained by a QTab/QRouteTab
// (it might be other elements focused, like additional QBtn)
if (tab && rootRef.value.contains(tab) === true) {
hasFocus.value = true
scrollable.value === true && scrollToTabEl(tab)
}
}
}
function onFocusout () {
registerFocusTimeout(() => { hasFocus.value = false }, 30)
}
function verifyRouteModel () {
if ($tabs.avoidRouteWatcher === false) {
registerScrollToTabTimeout(updateActiveRoute)
}
else {
removeScrollToTabTimeout()
}
}
function watchRoute () {
if (unwatchRoute === void 0) {
const unwatch = watch(() => proxy.$route.fullPath, verifyRouteModel)
unwatchRoute = () => {
unwatch()
unwatchRoute = void 0
}
}
}
function registerTab (tabData) {
tabDataList.push(tabData)
tabDataListLen.value++
recalculateScroll()
// if it's a QTab or we don't have Vue Router
if (tabData.routeData === void 0 || proxy.$route === void 0) {
// we should position to the currently active tab (if any)
registerScrollToTabTimeout(() => {
if (scrollable.value === true) {
const value = currentModel.value
const newTab = value !== void 0 && value !== null && value !== ''
? tabDataList.find(tab => tab.name.value === value)
: null
newTab && scrollToTabEl(newTab.rootRef.value)
}
})
}
// else if it's a QRouteTab with a valid link
else {
// start watching route
watchRoute()
if (tabData.routeData.hasRouterLink.value === true) {
verifyRouteModel()
}
}
}
function unregisterTab (tabData) {
tabDataList.splice(tabDataList.indexOf(tabData), 1)
tabDataListLen.value--
recalculateScroll()
if (unwatchRoute !== void 0 && tabData.routeData !== void 0) {
// unwatch route if we don't have any QRouteTabs left
if (tabDataList.every(tab => tab.routeData === void 0) === true) {
unwatchRoute()
}
// then update model
verifyRouteModel()
}
}
const $tabs = {
currentModel,
tabProps,
hasFocus,
hasActiveTab,
registerTab,
unregisterTab,
verifyRouteModel,
updateModel,
onKbdNavigate,
avoidRouteWatcher: false // false | string (uid)
}
provide(tabsKey, $tabs)
function cleanup () {
animateTimer !== null && clearTimeout(animateTimer)
stopAnimScroll()
unwatchRoute?.()
}
let hadRouteWatcher, hadActivated
onBeforeUnmount(cleanup)
onDeactivated(() => {
hadRouteWatcher = unwatchRoute !== void 0
cleanup()
})
onActivated(() => {
if (hadRouteWatcher === true) {
watchRoute()
hadActivated = true
verifyRouteModel()
}
recalculateScroll()
})
return () => {
return h('div', {
ref: rootRef,
class: classes.value,
role: 'tablist',
onFocusin,
onFocusout
}, [
h(QResizeObserver, { onResize: updateContainer }),
h('div', {
ref: contentRef,
class: innerClass.value,
onScroll: updateArrows
}, hSlot(slots.default)),
h(QIcon, {
class: 'q-tabs__arrow q-tabs__arrow--left absolute q-tab__icon'
+ (leftArrow.value === true ? '' : ' q-tabs__arrow--faded'),
name: props.leftIcon || $q.iconSet.tabs[ props.vertical === true ? 'up' : 'left' ],
onMousedownPassive: scrollToStart,
onTouchstartPassive: scrollToStart,
onMouseupPassive: stopAnimScroll,
onMouseleavePassive: stopAnimScroll,
onTouchendPassive: stopAnimScroll
}),
h(QIcon, {
class: 'q-tabs__arrow q-tabs__arrow--right absolute q-tab__icon'
+ (rightArrow.value === true ? '' : ' q-tabs__arrow--faded'),
name: props.rightIcon || $q.iconSet.tabs[ props.vertical === true ? 'down' : 'right' ],
onMousedownPassive: scrollToEnd,
onTouchstartPassive: scrollToEnd,
onMouseupPassive: stopAnimScroll,
onMouseleavePassive: stopAnimScroll,
onTouchendPassive: stopAnimScroll
})
])
}
}
})