index.js
8.23 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
var t = function() {
function t(t, e) {
var n = [], i = !0, r = !1, a = void 0;
try {
for (var s, o = t[Symbol.iterator](); !(i = (s = o.next()).done) && (n.push(s.value),
!e || n.length !== e); i = !0) ;
} catch (t) {
t = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(t);
r = !0, a = t;
} finally {
try {
!i && o.return && o.return();
} finally {
if (r) throw a;
}
}
return n;
}
return function(e, n) {
if (Array.isArray(e)) return e;
if (Symbol.iterator in Object(e)) return t(e, n);
throw new TypeError("Invalid attempt to destructure non-iterable instance");
};
}(), e = require("../common/component"), n = require("../mixins/touch"), i = require("../common/utils");
(0, e.VantComponent)({
mixins: [ n.touch ],
classes: [ "nav-class", "tab-class", "tab-active-class", "line-class" ],
relation: {
name: "tab",
type: "descendant",
current: "tabs",
linked: function(t) {
t.index = this.children.length - 1, this.updateTabs();
},
unlinked: function() {
this.children = this.children.map(function(t, e) {
return t.index = e, t;
}), this.updateTabs();
}
},
props: {
color: {
type: String,
observer: "setLine"
},
sticky: Boolean,
animated: {
type: Boolean,
observer: function() {
var t = this;
this.children.forEach(function(e, n) {
return e.updateRender(n === t.data.currentIndex, t);
});
}
},
swipeable: Boolean,
lineWidth: {
type: [ String, Number ],
value: -1,
observer: "setLine"
},
lineHeight: {
type: [ String, Number ],
value: -1,
observer: "setLine"
},
titleActiveColor: String,
titleInactiveColor: String,
active: {
type: [ String, Number ],
value: 0,
observer: function(t) {
t !== this.getCurrentName() && this.setCurrentIndexByName(t);
}
},
type: {
type: String,
value: "line"
},
border: {
type: Boolean,
value: !0
},
ellipsis: {
type: Boolean,
value: !0
},
duration: {
type: Number,
value: .3
},
zIndex: {
type: Number,
value: 1
},
swipeThreshold: {
type: Number,
value: 4,
observer: function(t) {
this.setData({
scrollable: this.children.length > t || !this.data.ellipsis
});
}
},
offsetTop: {
type: Number,
value: 0
},
lazyRender: {
type: Boolean,
value: !0
}
},
data: {
tabs: [],
lineStyle: "",
scrollLeft: 0,
scrollable: !1,
trackStyle: "",
currentIndex: null,
container: null
},
mounted: function() {
var t = this;
wx.nextTick(function() {
t.setLine(!0), t.scrollIntoView();
});
},
methods: {
updateContainer: function() {
var t = this;
this.setData({
container: function() {
return t.createSelectorQuery().select(".van-tabs");
}
});
},
updateTabs: function() {
var t = this.children, e = void 0 === t ? [] : t, n = this.data;
this.setData({
tabs: e.map(function(t) {
return t.data;
}),
scrollable: this.children.length > n.swipeThreshold || !n.ellipsis
}), this.setCurrentIndexByName(this.getCurrentName() || n.active);
},
trigger: function(t, e) {
var n = this.data.currentIndex, r = e || this.children[n];
(0, i.isDef)(r) && this.$emit(t, {
index: r.index,
name: r.getComputedName(),
title: r.data.title
});
},
onTap: function(t) {
var e = this, n = t.currentTarget.dataset.index, i = this.children[n];
i.data.disabled ? this.trigger("disabled", i) : (this.setCurrentIndex(n), wx.nextTick(function() {
e.trigger("click");
}));
},
setCurrentIndexByName: function(t) {
var e = this.children, n = (void 0 === e ? [] : e).filter(function(e) {
return e.getComputedName() === t;
});
n.length && this.setCurrentIndex(n[0].index);
},
setCurrentIndex: function(t) {
var e = this, n = this.data, r = this.children, a = void 0 === r ? [] : r;
if (!(!(0, i.isDef)(t) || t >= a.length || t < 0) && (a.forEach(function(n, i) {
var r = i === t;
r === n.data.active && n.inited || n.updateRender(r, e);
}), t !== n.currentIndex)) {
var s = null !== n.currentIndex;
this.setData({
currentIndex: t
}), wx.nextTick(function() {
e.setLine(), e.scrollIntoView(), e.updateContainer(), e.trigger("input"), s && e.trigger("change");
});
}
},
getCurrentName: function() {
var t = this.children[this.data.currentIndex];
if (t) return t.getComputedName();
},
setLine: function(t) {
var e = this;
if ("line" === this.data.type) {
var n = this.data, r = n.color, a = n.duration, s = n.currentIndex, o = n.lineWidth, l = n.lineHeight;
this.getRect(".van-tab", !0).then(function() {
var n = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [], u = n[s];
if (null != u) {
var c = -1 !== o ? o : u.width / 2, d = -1 !== l ? "height: " + (0, i.addUnit)(l) + "; border-radius: " + (0,
i.addUnit)(l) + ";" : "", h = n.slice(0, s).reduce(function(t, e) {
return t + e.width;
}, 0);
h += (u.width - c) / 2;
var v = t ? "" : "transition-duration: " + a + "s; -webkit-transition-duration: " + a + "s;";
e.setData({
lineStyle: "\n " + d + "\n width: " + (0, i.addUnit)(c) + ";\n background-color: " + r + ";\n -webkit-transform: translateX(" + h + "px);\n transform: translateX(" + h + "px);\n " + v + "\n "
});
}
});
}
},
scrollIntoView: function() {
var e = this, n = this.data, i = n.currentIndex;
n.scrollable && Promise.all([ this.getRect(".van-tab", !0), this.getRect(".van-tabs__nav") ]).then(function(n) {
var r = t(n, 2), a = r[0], s = r[1], o = a[i], l = a.slice(0, i).reduce(function(t, e) {
return t + e.width;
}, 0);
e.setData({
scrollLeft: l - (s.width - o.width) / 2
});
});
},
onTouchScroll: function(t) {
this.$emit("scroll", t.detail);
},
onTouchStart: function(t) {
this.data.swipeable && this.touchStart(t);
},
onTouchMove: function(t) {
this.data.swipeable && this.touchMove(t);
},
onTouchEnd: function() {
if (this.data.swipeable) {
var t = this.data, e = t.tabs, n = t.currentIndex, i = this.direction, r = this.deltaX, a = this.offsetX;
"horizontal" === i && a >= 50 && (r > 0 && 0 !== n ? this.setCurrentIndex(n - 1) : r < 0 && n !== e.length - 1 && this.setCurrentIndex(n + 1));
}
}
}
});