tabs.vue
20.3 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
<template>
<div :class="classes">
<div :class="[prefixCls + '-bar']">
<div :class="[prefixCls + '-nav-right']" v-if="showSlot"><slot name="extra"></slot></div>
<div
:class="[prefixCls + '-nav-container']"
tabindex="0"
ref="navContainer"
@keydown="handleTabKeyNavigation"
@keydown.space.prevent="handleTabKeyboardSelect(false)"
>
<div ref="navWrap" :class="[prefixCls + '-nav-wrap', scrollable ? prefixCls + '-nav-scrollable' : '']">
<span :class="[prefixCls + '-nav-prev', scrollable ? '' : prefixCls + '-nav-scroll-disabled']" @click="scrollPrev"><Icon type="ios-arrow-back"></Icon></span>
<span :class="[prefixCls + '-nav-next', scrollable ? '' : prefixCls + '-nav-scroll-disabled']" @click="scrollNext"><Icon type="ios-arrow-forward"></Icon></span>
<div ref="navScroll" :class="[prefixCls + '-nav-scroll']">
<div ref="nav" :class="[prefixCls + '-nav']" :style="navStyle">
<div :class="barClasses" :style="barStyle"></div>
<div :class="tabCls(item)" v-for="(item, index) in navList" @click="handleChange(index)">
<Icon v-if="item.icon !== ''" :type="item.icon"></Icon>
<Render v-if="item.labelType === 'function'" :render="item.label"></Render>
<template v-else>{{ item.label }}</template>
<Icon :class="[prefixCls + '-close']" v-if="showClose(item)" :type="arrowType" :custom="customArrowType" :size="arrowSize" @click.native.stop="handleRemove(index)"></Icon>
</div>
</div>
</div>
</div>
</div>
</div>
<div :class="contentClasses" :style="contentStyle" ref="panes"><slot></slot></div>
</div>
</template>
<script>
import Icon from '../icon/icon.vue';
import Render from '../base/render';
import { oneOf, MutationObserver, findComponentsDownward } from '../../utils/assist';
import Emitter from '../../mixins/emitter';
import elementResizeDetectorMaker from 'element-resize-detector';
const prefixCls = 'ivu-tabs';
const transitionTime = 300; // from CSS
const getNextTab = (list, activeKey, direction, countDisabledAlso) => {
const currentIndex = list.findIndex(tab => tab.name === activeKey);
const nextIndex = (currentIndex + direction + list.length) % list.length;
const nextTab = list[nextIndex];
if (nextTab.disabled) return getNextTab(list, nextTab.name, direction, countDisabledAlso);
else return nextTab;
};
const focusFirst = (element, root) => {
try {element.focus();}
catch(err) {} // eslint-disable-line no-empty
if (document.activeElement == element && element !== root) return true;
const candidates = element.children;
for (let candidate of candidates) {
if (focusFirst(candidate, root)) return true;
}
return false;
};
export default {
name: 'Tabs',
mixins: [ Emitter ],
components: { Icon, Render },
provide () {
return { TabsInstance: this };
},
props: {
value: {
type: [String, Number]
},
type: {
validator (value) {
return oneOf(value, ['line', 'card']);
},
default: 'line'
},
size: {
validator (value) {
return oneOf(value, ['small', 'default']);
},
default: 'default'
},
animated: {
type: Boolean,
default: true
},
captureFocus: {
type: Boolean,
default: false
},
closable: {
type: Boolean,
default: false
},
beforeRemove: Function,
// Tabs 嵌套时,用 name 区分层级
name: {
type: String
},
},
data () {
return {
prefixCls: prefixCls,
navList: [],
barWidth: 0,
barOffset: 0,
activeKey: this.value,
focusedKey: this.value,
showSlot: false,
navStyle: {
transform: ''
},
scrollable: false,
transitioning: false,
};
},
computed: {
classes () {
return [
`${prefixCls}`,
{
[`${prefixCls}-card`]: this.type === 'card',
[`${prefixCls}-mini`]: this.size === 'small' && this.type === 'line',
[`${prefixCls}-no-animation`]: !this.animated
}
];
},
contentClasses () {
return [
`${prefixCls}-content`,
{
[`${prefixCls}-content-animated`]: this.animated
}
];
},
barClasses () {
return [
`${prefixCls}-ink-bar`,
{
[`${prefixCls}-ink-bar-animated`]: this.animated
}
];
},
contentStyle () {
const x = this.getTabIndex(this.activeKey);
const p = x === 0 ? '0%' : `-${x}00%`;
let style = {};
if (x > -1) {
style = {
transform: `translateX(${p}) translateZ(0px)`
};
}
return style;
},
barStyle () {
let style = {
visibility: 'hidden',
width: `${this.barWidth}px`
};
if (this.type === 'line') style.visibility = 'visible';
if (this.animated) {
style.transform = `translate3d(${this.barOffset}px, 0px, 0px)`;
} else {
style.left = `${this.barOffset}px`;
}
return style;
},
// 3.4.0, global setting customArrow 有值时,arrow 赋值空
arrowType () {
let type = 'ios-close';
if (this.$IVIEW) {
if (this.$IVIEW.tabs.customCloseIcon) {
type = '';
} else if (this.$IVIEW.tabs.closeIcon) {
type = this.$IVIEW.tabs.closeIcon;
}
}
return type;
},
// 3.4.0, global setting
customArrowType () {
let type = '';
if (this.$IVIEW) {
if (this.$IVIEW.tabs.customCloseIcon) {
type = this.$IVIEW.tabs.customCloseIcon;
}
}
return type;
},
// 3.4.0, global setting
arrowSize () {
let size = '';
if (this.$IVIEW) {
if (this.$IVIEW.tabs.closeIconSize) {
size = this.$IVIEW.tabs.closeIconSize;
}
}
return size;
}
},
methods: {
getTabs () {
// return this.$children.filter(item => item.$options.name === 'TabPane');
const AllTabPanes = findComponentsDownward(this, 'TabPane');
const TabPanes = [];
AllTabPanes.forEach(item => {
if (item.tab && this.name) {
if (item.tab === this.name) {
TabPanes.push(item);
}
} else {
TabPanes.push(item);
}
});
// 在 TabPane 使用 v-if 时,并不会按照预先的顺序渲染,这时可设置 index,并从小到大排序
TabPanes.sort((a, b) => {
if (a.index && b.index) {
return a.index > b.index ? 1 : -1;
}
});
return TabPanes;
},
updateNav () {
this.navList = [];
this.getTabs().forEach((pane, index) => {
this.navList.push({
labelType: typeof pane.label,
label: pane.label,
icon: pane.icon || '',
name: pane.currentName || index,
disabled: pane.disabled,
closable: pane.closable
});
if (!pane.currentName) pane.currentName = index;
if (index === 0) {
if (!this.activeKey) this.activeKey = pane.currentName || index;
}
});
this.updateStatus();
this.updateBar();
},
updateBar () {
this.$nextTick(() => {
const index = this.getTabIndex(this.activeKey);
if (!this.$refs.nav) return; // 页面销毁时,这里会报错,为了解决 #2100
const prevTabs = this.$refs.nav.querySelectorAll(`.${prefixCls}-tab`);
const tab = prevTabs[index];
this.barWidth = tab ? parseFloat(tab.offsetWidth) : 0;
if (index > 0) {
let offset = 0;
const gutter = this.size === 'small' ? 0 : 16;
for (let i = 0; i < index; i++) {
offset += parseFloat(prevTabs[i].offsetWidth) + gutter;
}
this.barOffset = offset;
} else {
this.barOffset = 0;
}
this.updateNavScroll();
});
},
updateStatus () {
const tabs = this.getTabs();
tabs.forEach(tab => tab.show = (tab.currentName === this.activeKey) || this.animated);
},
tabCls (item) {
return [
`${prefixCls}-tab`,
{
[`${prefixCls}-tab-disabled`]: item.disabled,
[`${prefixCls}-tab-active`]: item.name === this.activeKey,
[`${prefixCls}-tab-focused`]: item.name === this.focusedKey,
}
];
},
handleChange (index) {
if (this.transitioning) return;
this.transitioning = true;
setTimeout(() => this.transitioning = false, transitionTime);
const nav = this.navList[index];
if (nav.disabled) return;
this.activeKey = nav.name;
this.$emit('input', nav.name);
this.$emit('on-click', nav.name);
},
handleTabKeyNavigation(e){
if (e.keyCode !== 37 && e.keyCode !== 39) return;
const direction = e.keyCode === 39 ? 1 : -1;
const nextTab = getNextTab(this.navList, this.focusedKey, direction);
this.focusedKey = nextTab.name;
},
handleTabKeyboardSelect(init = false){
if (init) return;
const focused = this.focusedKey || 0;
const index = this.getTabIndex(focused);
this.handleChange(index);
},
handleRemove (index) {
if (!this.beforeRemove) {
return this.handleRemoveTab(index);
}
const before = this.beforeRemove(index);
if (before && before.then) {
before.then(() => {
this.handleRemoveTab(index);
});
} else {
this.handleRemoveTab(index);
}
},
handleRemoveTab (index) {
const tabs = this.getTabs();
const tab = tabs[index];
tab.$destroy();
if (tab.currentName === this.activeKey) {
const newTabs = this.getTabs();
let activeKey = -1;
if (newTabs.length) {
const leftNoDisabledTabs = tabs.filter((item, itemIndex) => !item.disabled && itemIndex < index);
const rightNoDisabledTabs = tabs.filter((item, itemIndex) => !item.disabled && itemIndex > index);
if (rightNoDisabledTabs.length) {
activeKey = rightNoDisabledTabs[0].currentName;
} else if (leftNoDisabledTabs.length) {
activeKey = leftNoDisabledTabs[leftNoDisabledTabs.length - 1].currentName;
} else {
activeKey = newTabs[0].currentName;
}
}
this.activeKey = activeKey;
this.$emit('input', activeKey);
}
this.$emit('on-tab-remove', tab.currentName);
this.updateNav();
},
showClose (item) {
if (this.type === 'card') {
if (item.closable !== null) {
return item.closable;
} else {
return this.closable;
}
} else {
return false;
}
},
scrollPrev() {
const containerWidth = this.$refs.navScroll.offsetWidth;
const currentOffset = this.getCurrentScrollOffset();
if (!currentOffset) return;
let newOffset = currentOffset > containerWidth
? currentOffset - containerWidth
: 0;
this.setOffset(newOffset);
},
scrollNext() {
const navWidth = this.$refs.nav.offsetWidth;
const containerWidth = this.$refs.navScroll.offsetWidth;
const currentOffset = this.getCurrentScrollOffset();
if (navWidth - currentOffset <= containerWidth) return;
let newOffset = navWidth - currentOffset > containerWidth * 2
? currentOffset + containerWidth
: (navWidth - containerWidth);
this.setOffset(newOffset);
},
getCurrentScrollOffset() {
const { navStyle } = this;
return navStyle.transform
? Number(navStyle.transform.match(/translateX\(-(\d+(\.\d+)*)px\)/)[1])
: 0;
},
getTabIndex(name){
return this.navList.findIndex(nav => nav.name === name);
},
setOffset(value) {
this.navStyle.transform = `translateX(-${value}px)`;
},
scrollToActiveTab() {
if (!this.scrollable) return;
const nav = this.$refs.nav;
const activeTab = this.$el.querySelector(`.${prefixCls}-tab-active`);
if(!activeTab) return;
const navScroll = this.$refs.navScroll;
const activeTabBounding = activeTab.getBoundingClientRect();
const navScrollBounding = navScroll.getBoundingClientRect();
const navBounding = nav.getBoundingClientRect();
const currentOffset = this.getCurrentScrollOffset();
let newOffset = currentOffset;
if (navBounding.right < navScrollBounding.right) {
newOffset = nav.offsetWidth - navScrollBounding.width;
}
if (activeTabBounding.left < navScrollBounding.left) {
newOffset = currentOffset - (navScrollBounding.left - activeTabBounding.left);
}else if (activeTabBounding.right > navScrollBounding.right) {
newOffset = currentOffset + activeTabBounding.right - navScrollBounding.right;
}
if(currentOffset !== newOffset){
this.setOffset(Math.max(newOffset, 0));
}
},
updateNavScroll(){
const navWidth = this.$refs.nav.offsetWidth;
const containerWidth = this.$refs.navScroll.offsetWidth;
const currentOffset = this.getCurrentScrollOffset();
if (containerWidth < navWidth) {
this.scrollable = true;
if (navWidth - currentOffset < containerWidth) {
this.setOffset(navWidth - containerWidth);
}
} else {
this.scrollable = false;
if (currentOffset > 0) {
this.setOffset(0);
}
}
},
handleResize(){
this.updateNavScroll();
},
isInsideHiddenElement () {
let parentNode = this.$el.parentNode;
while(parentNode && parentNode !== document.body) {
if (parentNode.style && parentNode.style.display === 'none') {
return parentNode;
}
parentNode = parentNode.parentNode;
}
return false;
},
updateVisibility(index){
[...this.$refs.panes.querySelectorAll(`.${prefixCls}-tabpane`)].forEach((el, i) => {
if (index === i) {
[...el.children].filter(child=> child.classList.contains(`${prefixCls}-tabpane`)).forEach(child => child.style.visibility = 'visible');
if (this.captureFocus) setTimeout(() => focusFirst(el, el), transitionTime);
} else {
setTimeout(() => {
[...el.children].filter(child=> child.classList.contains(`${prefixCls}-tabpane`)).forEach(child => child.style.visibility = 'hidden');
}, transitionTime);
}
});
}
},
watch: {
value (val) {
this.activeKey = val;
this.focusedKey = val;
},
activeKey (val) {
this.focusedKey = val;
this.updateBar();
this.updateStatus();
this.broadcast('Table', 'on-visible-change', true);
this.$nextTick(() => {
this.scrollToActiveTab();
});
// update visibility
const nextIndex = Math.max(this.getTabIndex(this.focusedKey), 0);
this.updateVisibility(nextIndex);
}
},
mounted () {
this.showSlot = this.$slots.extra !== undefined;
this.observer = elementResizeDetectorMaker();
this.observer.listenTo(this.$refs.navWrap, this.handleResize);
const hiddenParentNode = this.isInsideHiddenElement();
if (hiddenParentNode) {
this.mutationObserver = new MutationObserver(() => {
if (hiddenParentNode.style.display !== 'none') {
this.updateBar();
this.mutationObserver.disconnect();
}
});
this.mutationObserver.observe(hiddenParentNode, { attributes: true, childList: true, characterData: true, attributeFilter: ['style'] });
}
this.handleTabKeyboardSelect(true);
this.updateVisibility(this.getTabIndex(this.activeKey));
},
beforeDestroy() {
this.observer.removeListener(this.$refs.navWrap, this.handleResize);
if (this.mutationObserver) this.mutationObserver.disconnect();
}
};
</script>