index.vue
8.06 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
<template>
<div class="zui-steps" :class="stepsClassRender">
<template v-for="(step, index) of steps">
<div class="step-wrapper" :class="[$_getStepStatusClass(index)]" :key="stepKey(index)">
<div v-if="$scopedSlots.icon" class="icon-wrapper">
<slot name="icon" :index="index" :current-index="currentLength" :step="step"></slot>
</div>
<div v-else class="icon-wrapper">
<template v-if="index < currentLength">
<slot v-if="$scopedSlots.reached || $slots.reached" name="reached" :index="index"></slot>
<div v-else class="step-node-default">
<div class="step-node-default-icon" style="width: 8px; height: 8px; border-radius: 50%;"></div>
</div>
</template>
<template v-else-if="index == currentLength">
<slot v-if="$scopedSlots.current || $slots.current" name="current" :index="index"></slot>
<zui-icon v-else name="checkcircle"></zui-icon>
</template>
<template v-else>
<slot v-if="$scopedSlots.unreached || $slots.unreached" name="unreached" :index="index"></slot>
<div v-else class="step-node-default">
<div class="step-node-default-icon" style="width: 8px; height: 8px; border-radius: 50%;"></div>
</div>
</template>
</div>
<div class="text-wrapper">
<slot v-if="$scopedSlots.content" name="content" :index="index" :step="step"></slot>
<template v-else>
<div class="name">{{ step.name }}</div>
<div class="desc" v-if="step.text">{{ step.text }}</div>
</template>
</div>
</div>
<div class="bar" :class="barClassRender" :style="$_getStepSizeForStyle(index)" :key="barKey(index)">
<i class="bar-inner" v-if="progress[index]" :style="$_barInnerStyle(index)"></i>
</div>
</template>
</div>
</template>
<script>
export default {
name: 'Steps',
props: {
steps: {
type: Array,
default: function () {
/* istanbul ignore next */
return [];
},
},
current: {
type: Number,
default: 0,
validator(val) {
return val >= 0;
},
},
direction: {
type: String,
default: 'horizontal',
},
transition: {
type: Boolean,
default: false,
},
verticalAdaptive: {
type: Boolean,
default: false,
},
},
data: function () {
return {
initialed: false,
progress: [],
stepsSize: [],
currentLength: 0,
duration: 0.3,
timer: null,
};
},
computed: {
stepsClassRender: function () {
return {
'zui-steps-vertical': this.direction == 'vertical',
'zui-steps-horizontal': this.direction == 'horizontal',
'vertical-adaptive': this.direction == 'vertical' && this.verticalAdaptive,
'no-current': this.currentLength % 1 !== 0,
};
},
barClassRender: function () {
return [this.direction == 'horizontal' ? 'horizontal-bar' : 'vertical-bar'];
},
$_barInnerStyle: function () {
var progress = this.progress;
var self = this;
return function (index) {
var transform = self.direction == 'horizontal' ? '(' + (progress[index]['len'] - 1) * 100 + '%, 0, 0)' : '(0, ' + (progress[index]['len'] - 1) * 100 + '%, 0)';
return {
transform: 'translate3d' + transform,
transition: 'all ' + progress[index]['time'] + 's linear',
};
};
},
},
watch: {
current: function (val, oldVal) {
var currentStep = this.$_formatValue(val);
var newProgress = this.$_sliceProgress(currentStep);
if (this.transition) {
var isAdd = currentStep >= oldVal;
var self = this;
this.timer && clearTimeout(this.timer);
this.timer = setTimeout(function () {
self.$_doTransition(function (newProgress, isAdd, len) {
if ((isAdd && len > self.currentLength) || (!isAdd && len < self.currentLength)) {
self.currentLength = len;
}
});
}, 100);
} else {
this.progress = newProgress;
this.currentLength = currentStep;
}
},
},
created: function () {
var currentStep = this.$_formatValue(this.current);
this.currentLength = currentStep;
this.progress = this.$_sliceProgress(currentStep);
},
mounted: function () {
this.$_initStepSize();
},
updated: function () {
this.$nextTick(function () {
this.$_initStepSize();
});
},
methods: {
stepKey: function (index) {
return 'steps-' + index;
},
barKey: function (index) {
return 'bar-' + index;
},
toArray: function (list, start) {
start = start || 0;
var i = list.length - start;
var ret = [];
while (i--) {
ret.unshift(list[i + start]);
}
return ret;
},
// MARK: private methods
$_initStepSize: function () {
if (this.direction != 'vertical' || this.verticalAdaptive) {
return;
}
var iconWrappers = this.$el.querySelectorAll('.icon-wrapper');
var textWrappers = this.$el.querySelectorAll('.text-wrapper');
var self = this;
var stepsSize = this.toArray(textWrappers).map(function (wrapper, index) {
var stepHeight = wrapper.clientHeight;
var iconHeight = iconWrappers[index].clientHeight;
if (index == textWrappers.length - 1) {
// The last step needs to subtract floated height
stepHeight -= iconHeight;
} else {
// Add spacing between steps to prevent distance too close
// stepHeight += 40
// 这里减小空白间隙,使其看起来更紧凑
stepHeight += self.stepHeightFix == 0 ? self.stepHeightFix : self.stepHeightFix || -15;
}
return stepHeight > 0 ? stepHeight : 0;
});
if (stepsSize.toString() != this.stepsSize.toString()) {
this.stepsSize = stepsSize;
}
},
$_getStepSizeForStyle: function (index) {
var size = this.direction == 'vertical' && !this.verticalAdaptive ? this.stepsSize[index] : 0;
return size
? {
height: size + 'px',
}
: null;
},
$_getStepStatusClass: function (index) {
var currentLength = this.currentLength;
var status = [];
if (index < currentLength) {
status.push('reached');
}
if (index == Math.floor(currentLength)) {
status.push('current');
}
return status.join(' ');
},
$_formatValue: function (val) {
if (val < 0) {
return 0;
} else if (val > this.steps.length - 1) {
return this.steps.length - 1;
} else {
return val;
}
},
$_sliceProgress: function (current) {
var self = this;
return this.steps.slice(0, this.steps.length - 1).map(function (step, index) {
var offset = current - index;
var progress = self.progress[index];
var isNewProgress = progress == undefined;
var len, time;
if (offset <= 0) {
len = 0;
} else if (offset >= 1) {
len = 1;
} else {
len = offset;
}
time = (isNewProgress ? len : Math.abs(progress.len - len)) * self.duration;
return {
len,
time,
};
});
},
$_doTransition: function (progress, isAdd, step) {
var currentLength = isAdd ? 0 : this.currentLength;
var self = this;
var walk = function (index) {
if ((index < progress.length) & (index > -1) && progress[index]) {
if (isAdd) {
currentLength += progress[index].len;
} else {
currentLength -= self.progress[index].len - progress[index].len;
}
setTimeout(function () {
index += isAdd ? 1 : -1;
step(currentLength);
walk(index);
}, progress[index].time * 1000);
}
self.$set(self.progress, index, progress[index]);
};
walk(isAdd ? 0 : progress.length - 1);
},
},
};
</script>
<style>
@import './index.css';
</style>