871ed4d8
梁灏
init Tabs component
|
1
|
<template>
|
17f52abf
梁灏
update Tabs
|
2
3
4
5
6
|
<div :class="classes">
<div :class="[prefixCls + '-bar']">
<div :class="[prefixCls + '-nav-container']">
<div :class="[prefixCls + '-nav-wrap']">
<div :class="[prefixCls + '-nav-scroll']">
|
30510c3d
梁灏
support Tabs
|
7
|
<div :class="[prefixCls + '-nav']" ref="nav">
|
17f52abf
梁灏
update Tabs
|
8
|
<div :class="barClasses" :style="barStyle"></div>
|
30510c3d
梁灏
support Tabs
|
9
|
<div :class="tabCls(item)" v-for="(item, index) in navList" @click="handleChange(index)">
|
17f52abf
梁灏
update Tabs
|
10
11
|
<Icon v-if="item.icon !== ''" :type="item.icon"></Icon>
{{ item.label }}
|
30510c3d
梁灏
support Tabs
|
12
|
<Icon v-if="showClose(item)" type="ios-close-empty" @click.native.stop="handleRemove(index)"></Icon>
|
17f52abf
梁灏
update Tabs
|
13
14
15
16
17
18
19
20
|
</div>
</div>
</div>
</div>
</div>
</div>
<div :class="contentClasses" :style="contentStyle"><slot></slot></div>
</div>
|
871ed4d8
梁灏
init Tabs component
|
21
22
|
</template>
<script>
|
17f52abf
梁灏
update Tabs
|
23
24
25
26
27
|
import Icon from '../icon/icon.vue';
import { oneOf, getStyle } from '../../utils/assist';
const prefixCls = 'ivu-tabs';
|
871ed4d8
梁灏
init Tabs component
|
28
|
export default {
|
30510c3d
梁灏
support Tabs
|
29
|
name: 'Tabs',
|
17f52abf
梁灏
update Tabs
|
30
31
|
components: { Icon },
props: {
|
30510c3d
梁灏
support Tabs
|
32
|
value: {
|
17f52abf
梁灏
update Tabs
|
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
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
},
closable: {
type: Boolean,
default: false
}
},
|
871ed4d8
梁灏
init Tabs component
|
56
|
data () {
|
17f52abf
梁灏
update Tabs
|
57
58
59
60
|
return {
prefixCls: prefixCls,
navList: [],
barWidth: 0,
|
30510c3d
梁灏
support Tabs
|
61
62
|
barOffset: 0,
activeKey: this.value
|
b0893113
jingsam
add eslint
|
63
|
};
|
17f52abf
梁灏
update Tabs
|
64
65
66
67
68
69
70
71
72
73
|
},
computed: {
classes () {
return [
`${prefixCls}`,
{
[`${prefixCls}-card`]: this.type === 'card',
[`${prefixCls}-mini`]: this.size === 'small' && this.type === 'line',
[`${prefixCls}-no-animation`]: !this.animated
}
|
b0893113
jingsam
add eslint
|
74
|
];
|
17f52abf
梁灏
update Tabs
|
75
76
77
78
|
},
contentClasses () {
return [
`${prefixCls}-content`,
|
77bafb31
梁灏
update Tabs
|
79
80
81
|
{
[`${prefixCls}-content-animated`]: this.animated
}
|
b0893113
jingsam
add eslint
|
82
|
];
|
17f52abf
梁灏
update Tabs
|
83
84
85
86
|
},
barClasses () {
return [
`${prefixCls}-ink-bar`,
|
77bafb31
梁灏
update Tabs
|
87
88
89
|
{
[`${prefixCls}-ink-bar-animated`]: this.animated
}
|
b0893113
jingsam
add eslint
|
90
|
];
|
17f52abf
梁灏
update Tabs
|
91
92
|
},
contentStyle () {
|
30510c3d
梁灏
support Tabs
|
93
|
const x = this.navList.findIndex((nav) => nav.name === this.activeKey);
|
17f52abf
梁灏
update Tabs
|
94
95
96
97
98
99
|
const p = x === 0 ? '0%' : `-${x}00%`;
let style = {};
if (x > -1) {
style = {
transform: `translateX(${p}) translateZ(0px)`
|
b0893113
jingsam
add eslint
|
100
|
};
|
17f52abf
梁灏
update Tabs
|
101
102
103
104
105
106
|
}
return style;
},
barStyle () {
let style = {
display: 'none',
|
77bafb31
梁灏
update Tabs
|
107
|
width: `${this.barWidth}px`
|
17f52abf
梁灏
update Tabs
|
108
109
|
};
if (this.type === 'line') style.display = 'block';
|
77bafb31
梁灏
update Tabs
|
110
111
112
113
114
|
if (this.animated) {
style.transform = `translate3d(${this.barOffset}px, 0px, 0px)`;
} else {
style.left = `${this.barOffset}px`;
}
|
17f52abf
梁灏
update Tabs
|
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
return style;
}
},
methods: {
getTabs () {
return this.$children.filter(item => item.$options.name === 'TabPane');
},
updateNav () {
this.navList = [];
this.getTabs().forEach((pane, index) => {
this.navList.push({
label: pane.label,
icon: pane.icon || '',
|
30510c3d
梁灏
support Tabs
|
129
|
name: pane.currentName || index,
|
7a737482
梁灏
fixed #206
|
130
131
|
disabled: pane.disabled,
closable: pane.closable
|
17f52abf
梁灏
update Tabs
|
132
|
});
|
30510c3d
梁灏
support Tabs
|
133
|
if (!pane.currentName) pane.currentName = index;
|
17f52abf
梁灏
update Tabs
|
134
|
if (index === 0) {
|
30510c3d
梁灏
support Tabs
|
135
|
if (!this.activeKey) this.activeKey = pane.currentName || index;
|
17f52abf
梁灏
update Tabs
|
136
137
|
}
});
|
77bafb31
梁灏
update Tabs
|
138
|
this.updateStatus();
|
17f52abf
梁灏
update Tabs
|
139
140
141
142
|
this.updateBar();
},
updateBar () {
this.$nextTick(() => {
|
30510c3d
梁灏
support Tabs
|
143
144
|
const index = this.navList.findIndex((nav) => nav.name === this.activeKey);
const prevTabs = this.$refs.nav.querySelectorAll(`.${prefixCls}-tab`);
|
17f52abf
梁灏
update Tabs
|
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
const tab = prevTabs[index];
this.barWidth = parseFloat(getStyle(tab, 'width'));
if (index > 0) {
let offset = 0;
const gutter = this.size === 'small' ? 0 : 16;
for (let i = 0; i < index; i++) {
offset += parseFloat(getStyle(prevTabs[i], 'width')) + gutter;
}
this.barOffset = offset;
} else {
this.barOffset = 0;
}
});
},
|
77bafb31
梁灏
update Tabs
|
161
162
|
updateStatus () {
const tabs = this.getTabs();
|
30510c3d
梁灏
support Tabs
|
163
|
tabs.forEach(tab => tab.show = (tab.currentName === this.activeKey) || this.animated);
|
77bafb31
梁灏
update Tabs
|
164
|
},
|
17f52abf
梁灏
update Tabs
|
165
166
167
168
169
|
tabCls (item) {
return [
`${prefixCls}-tab`,
{
[`${prefixCls}-tab-disabled`]: item.disabled,
|
30510c3d
梁灏
support Tabs
|
170
|
[`${prefixCls}-tab-active`]: item.name === this.activeKey
|
17f52abf
梁灏
update Tabs
|
171
|
}
|
b0893113
jingsam
add eslint
|
172
|
];
|
17f52abf
梁灏
update Tabs
|
173
174
175
176
|
},
handleChange (index) {
const nav = this.navList[index];
if (nav.disabled) return;
|
30510c3d
梁灏
support Tabs
|
177
178
179
|
this.activeKey = nav.name;
this.$emit('input', nav.name);
this.$emit('on-click', nav.name);
|
17f52abf
梁灏
update Tabs
|
180
181
182
183
|
},
handleRemove (index) {
const tabs = this.getTabs();
const tab = tabs[index];
|
087ad37d
梁灏
update Tabs
|
184
|
tab.$destroy();
|
17f52abf
梁灏
update Tabs
|
185
|
|
30510c3d
梁灏
support Tabs
|
186
|
if (tab.currentName === this.activeKey) {
|
17f52abf
梁灏
update Tabs
|
187
188
189
190
191
192
193
194
|
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) {
|
30510c3d
梁灏
support Tabs
|
195
|
activeKey = rightNoDisabledTabs[0].currentName;
|
17f52abf
梁灏
update Tabs
|
196
|
} else if (leftNoDisabledTabs.length) {
|
30510c3d
梁灏
support Tabs
|
197
|
activeKey = leftNoDisabledTabs[leftNoDisabledTabs.length - 1].currentName;
|
17f52abf
梁灏
update Tabs
|
198
|
} else {
|
30510c3d
梁灏
support Tabs
|
199
|
activeKey = newTabs[0].currentName;
|
17f52abf
梁灏
update Tabs
|
200
201
202
|
}
}
this.activeKey = activeKey;
|
087ad37d
梁灏
update Tabs
|
203
|
this.$emit('input', activeKey);
|
17f52abf
梁灏
update Tabs
|
204
|
}
|
30510c3d
梁灏
support Tabs
|
205
|
this.$emit('on-tab-remove', tab.currentName);
|
17f52abf
梁灏
update Tabs
|
206
|
this.updateNav();
|
7a737482
梁灏
fixed #206
|
207
208
209
210
211
212
213
214
215
216
217
|
},
showClose (item) {
if (this.type === 'card') {
if (item.closable !== null) {
return item.closable;
} else {
return this.closable;
}
} else {
return false;
}
|
17f52abf
梁灏
update Tabs
|
218
219
|
}
},
|
17f52abf
梁灏
update Tabs
|
220
|
watch: {
|
30510c3d
梁灏
support Tabs
|
221
222
223
|
value (val) {
this.activeKey = val;
},
|
17f52abf
梁灏
update Tabs
|
224
225
|
activeKey () {
this.updateBar();
|
0c5e01f1
梁灏
fixed #185
|
226
|
this.updateStatus();
|
17f52abf
梁灏
update Tabs
|
227
228
|
}
}
|
b0893113
jingsam
add eslint
|
229
230
|
};
</script>
|