index.js
3.17 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
var t = require("../common/component"), e = require("../common/utils");
(0, t.VantComponent)({
classes: [ "active-class" ],
props: {
valueKey: String,
className: String,
itemHeight: Number,
visibleItemCount: Number,
initialOptions: {
type: Array,
value: []
},
defaultIndex: {
type: Number,
value: 0,
observer: function(t) {
this.setIndex(t);
}
}
},
data: {
startY: 0,
offset: 0,
duration: 0,
startOffset: 0,
options: [],
currentIndex: 0
},
created: function() {
var t = this, e = this.data, n = e.defaultIndex, i = e.initialOptions;
this.set({
currentIndex: n,
options: i
}).then(function() {
t.setIndex(n);
});
},
methods: {
getCount: function() {
return this.data.options.length;
},
onTouchStart: function(t) {
this.setData({
startY: t.touches[0].clientY,
startOffset: this.data.offset,
duration: 0
});
},
onTouchMove: function(t) {
var n = this.data, i = t.touches[0].clientY - n.startY;
this.setData({
offset: (0, e.range)(n.startOffset + i, -this.getCount() * n.itemHeight, n.itemHeight)
});
},
onTouchEnd: function() {
var t = this.data;
if (t.offset !== t.startOffset) {
this.setData({
duration: 200
});
var n = (0, e.range)(Math.round(-t.offset / t.itemHeight), 0, this.getCount() - 1);
this.setIndex(n, !0);
}
},
onClickItem: function(t) {
var e = t.currentTarget.dataset.index;
this.setIndex(e, !0);
},
adjustIndex: function(t) {
for (var n = this.data, i = this.getCount(), s = t = (0, e.range)(t, 0, i); s < i; s++) if (!this.isDisabled(n.options[s])) return s;
for (var a = t - 1; a >= 0; a--) if (!this.isDisabled(n.options[a])) return a;
},
isDisabled: function(t) {
return (0, e.isObj)(t) && t.disabled;
},
getOptionText: function(t) {
var n = this.data;
return (0, e.isObj)(t) && n.valueKey in t ? t[n.valueKey] : t;
},
setIndex: function(t, e) {
var n = this, i = this.data, s = -(t = this.adjustIndex(t) || 0) * i.itemHeight;
return t !== i.currentIndex ? this.set({
offset: s,
currentIndex: t
}).then(function() {
e && n.$emit("change", t);
}) : this.set({
offset: s
});
},
setValue: function(t) {
for (var e = this.data.options, n = 0; n < e.length; n++) if (this.getOptionText(e[n]) === t) return this.setIndex(n);
return Promise.resolve();
},
getValue: function() {
var t = this.data;
return t.options[t.currentIndex];
}
}
});