/
tabs.js
384 lines (377 loc) · 9.62 KB
/
tabs.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
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
import { observeDom, KeyCodes } from '../../utils'
import { idMixin } from '../../mixins'
// Helper component
const bTabButtonHelper = {
props: {
content: { type: String, default: '' },
href: { type: String, default: '#' },
posInSet: { type: Number, default: null },
setSize: { type: Number, default: null },
controls: { type: String, default: null },
id: { type: String, default: null },
active: { type: Boolean, default: false },
disabled: { type: Boolean, default: false },
linkClass: { default: null },
itemClass: { default: null }
},
render (h) {
const t = this
const link = h(
'a',
{
class: [ 'nav-link', { active: t.active, disabled: t.disabled }, t.linkClass ],
attrs: {
role: 'tab',
tabindex: '-1',
href: t.href,
id: t.id,
disabled: t.disabled,
'aria-selected': t.active ? 'true' : 'false',
'aria-setsize': t.setSize,
'aria-posinset': t.posInSet,
'aria-controls': t.controls
},
domProps: { innerHTML: t.content },
on: {
click: t.handleClick,
keydown: t.handleClick
}
}
)
return h('li', { class: [ 'nav-item', t.itemClass ], attrs: { role: 'presentation' } }, [ link ])
},
methods: {
handleClick (evt) {
function stop () {
evt.preventDefault()
evt.stopPropagation()
}
if (this.disabled) {
stop()
return
}
if (evt.type === 'click' || evt.keyCode === KeyCodes.ENTER || evt.keyCode === KeyCodes.SPACE) {
stop()
this.$emit('click', evt)
}
}
}
}
export default {
mixins: [idMixin],
render (h) {
const t = this
const tabs = this.tabs
// Navigation 'buttons'
const buttons = tabs.map((tab, index) => {
return h(
bTabButtonHelper,
{
key: index,
props: {
content: tab.headHtml || tab.title,
href: tab.href,
id: tab.controlledBy || t.safeId(`_BV_tab_${index + 1}_`),
active: tab.localActive,
disabled: tab.disabled,
setSize: tabs.length,
posInSet: index + 1,
controls: t.safeId('_BV_tab_container_'),
linkClass: tab.titleLinkClass,
itemClass: tab.titleItemClass
},
on: { click: (evt) => { t.setTab(index) } }
}
)
})
// Nav 'button' wrapper
let navs = h(
'ul',
{
class: [
'nav',
`nav-${t.navStyle}`,
{
[`card-header-${t.navStyle}`]: (t.card && !t.vertical),
'card-header': (t.card && t.vertical),
'h-100': (t.card && t.vertical),
'flex-column': t.vertical,
'border-bottom-0': t.vertical,
'rounded-0': t.vertical,
small: t.small
},
t.navClass
],
attrs: { role: 'tablist', tabindex: '0', id: t.safeId('_BV_tab_controls_') },
on: { keydown: t.onKeynav }
},
[ buttons, t.$slots.tabs ]
)
navs = h(
'div',
{
class: [
{
'card-header': (t.card && !t.vertical && !(t.end || t.bottom)),
'card-footer': (t.card && !t.vertical && (t.end || t.bottom)),
'col-auto': t.vertical
},
t.navWrapperClass
]
},
[ navs ]
)
let empty
if (tabs && tabs.length) {
empty = h(false)
} else {
empty = h(
'div',
{ class: [ 'tab-pane', 'active', { 'card-body': t.card } ] },
t.$slots.empty
)
}
// Main content section
const content = h(
'div',
{
ref: 'tabsContainer',
class: [ 'tab-content', { 'col': t.vertical }, t.contentClass ],
attrs: { id: t.safeId('_BV_tab_container_') }
},
[ t.$slots.default, empty ]
)
// Render final output
return h(
t.tag,
{
class: [ 'tabs', { 'row': t.vertical, 'no-gutters': (t.vertical && t.card) } ],
attrs: { id: t.safeId() }
},
[
(t.end || t.bottom) ? content : h(false),
[ navs ],
(t.end || t.bottom) ? h(false) : content
]
)
},
data () {
return {
currentTab: this.value,
tabs: []
}
},
props: {
tag: {
type: String,
default: 'div'
},
card: {
type: Boolean,
default: false
},
small: {
type: Boolean,
default: false
},
value: {
type: Number,
default: null
},
pills: {
type: Boolean,
default: false
},
vertical: {
type: Boolean,
default: false
},
bottom: {
type: Boolean,
default: false
},
end: {
// Synonym for 'bottom'
type: Boolean,
default: false
},
noFade: {
type: Boolean,
default: false
},
lazy: {
// This prop is sniffed by the tab child
type: Boolean,
default: false
},
contentClass: {
type: [String, Array, Object],
default: null
},
navClass: {
type: [String, Array, Object],
default: null
},
navWrapperClass: {
type: [String, Array, Object],
default: null
}
},
watch: {
currentTab (val, old) {
if (val === old) {
return
}
this.$root.$emit('changed::tab', this, val, this.tabs[val])
this.$emit('input', val)
this.tabs[val].$emit('click')
},
value (val, old) {
if (val === old) {
return
}
if (typeof old !== 'number') {
old = 0
}
// Moving left or right?
const direction = val < old ? -1 : 1
this.setTab(val, false, direction)
}
},
computed: {
fade () {
// This computed prop is sniffed by the tab child
return !this.noFade
},
navStyle () {
return this.pills ? 'pills' : 'tabs'
}
},
methods: {
/**
* Util: Return the sign of a number (as -1, 0, or 1)
*/
sign (x) {
return (x === 0) ? 0 : (x > 0 ? 1 : -1)
},
/*
* handle keyboard navigation
*/
onKeynav (evt) {
const key = evt.keyCode
const shift = evt.shiftKey
function stop () {
evt.preventDefault()
evt.stopPropagation()
}
if (key === KeyCodes.UP || key === KeyCodes.LEFT) {
stop()
if (shift) {
this.setTab(0, false, 1)
} else {
this.previousTab()
}
} else if (key === KeyCodes.DWON || key === KeyCodes.RIGHT) {
stop()
if (shift) {
this.setTab(this.tabs.length - 1, false, -1)
} else {
this.nextTab()
}
}
},
/**
* Move to next tab
*/
nextTab () {
this.setTab(this.currentTab + 1, false, 1)
},
/**
* Move to previous tab
*/
previousTab () {
this.setTab(this.currentTab - 1, false, -1)
},
/**
* Set active tab on the tabs collection and the child 'tab' component
* Index is the tab we want to activate. Direction is the direction we are moving
* so if the tab we requested is disabled, we can skip over it.
* Force is used by updateTabs to ensure we have cleared any previous active tabs.
*/
setTab (index, force, direction) {
direction = this.sign(direction || 0)
index = index || 0
// Prevent setting same tab and infinite loops!
if (!force && index === this.currentTab) {
return
}
const tab = this.tabs[index]
// Don't go beyond indexes!
if (!tab) {
// Reset the v-model to the current Tab
this.$emit('input', this.currentTab)
return
}
// Ignore or Skip disabled
if (tab.disabled) {
if (direction) {
// Skip to next non disabled tab in specified direction (recursive)
this.setTab(index + direction, force, direction)
}
return
}
// Activate requested current tab, and deactivte any old tabs
this.tabs.forEach(t => {
if (t === tab) {
// Set new tab as active
this.$set(t, 'localActive', true)
} else {
// Ensure non current tabs are not active
this.$set(t, 'localActive', false)
}
})
// Update currentTab
this.currentTab = index
},
/**
* Dynamically update tabs list
*/
updateTabs () {
// Probe tabs
this.tabs = this.$children.filter(child => child._isTab)
// Set initial active tab
let tabIndex = null
// Find *last* active non-dsabled tab in current tabs
// We trust tab state over currentTab
this.tabs.forEach((tab, index) => {
if (tab.localActive && !tab.disabled) {
tabIndex = index
}
})
// Else try setting to currentTab
if (tabIndex === null) {
if (this.currentTab >= this.tabs.length) {
// Handle last tab being removed
this.setTab(this.tabs.length - 1, true, -1)
return
} else if (this.tabs[this.currentTab] && !this.tabs[this.currentTab].disabled) {
tabIndex = this.currentTab
}
}
// Else find *first* non-disabled tab in current tabs
if (tabIndex === null) {
this.tabs.forEach((tab, index) => {
if (!tab.disabled && tabIndex === null) {
tabIndex = index
}
})
}
this.setTab(tabIndex || 0, true, 0)
}
},
mounted () {
this.updateTabs()
// Observe Child changes so we can notify tabs change
observeDom(this.$refs.tabsContainer, this.updateTabs.bind(this), {subtree: false})
}
}