forked from didi/mand-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.vue
426 lines (402 loc) · 10.7 KB
/
index.vue
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
<template>
<div
class="md-steps"
:class="{
'md-steps-vertical': direction == 'vertical',
'md-steps-horizontal': direction == 'horizontal',
'vertical-adaptive': direction == 'vertical' && verticalAdaptive,
'no-current': currentLength % 1 !== 0
}"
>
<template v-for="(step, index) of steps">
<div class="step-wrapper"
:class="[$_getStepStatusClass(index)]"
:key="`steps-${index}`"
>
<!-- Customize uniformly -->
<div v-if="$scopedSlots.icon" class="icon-wrapper" >
<slot name="icon" :index="index" :current-index="currentLength"></slot>
</div>
<!-- Customize by status-->
<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: 6px;height: 6px;border-radius: 50%;"></div>
</div>
</template>
<template v-else-if="index === currentLength">
<slot
v-if="$scopedSlots.current || $slots.current"
name="current"
:index="index"
></slot>
<md-icon v-else name="success"></md-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: 6px;height: 6px;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="[direction === 'horizontal' ? 'horizontal-bar' : 'vertical-bar']"
:style="$_getStepSizeForStyle(index)"
:key="`bar-${index}`"
>
<i
class="bar-inner"
v-if="progress[index]"
:style="$_barInnerStyle(index)"
></i>
</div>
</template>
</div>
</template>
<script>import Icon from '../icon'
import {toArray} from '../_util'
export default {
name: 'md-steps',
components: {
[Icon.name]: Icon,
},
props: {
steps: {
type: Array,
default() {
/* 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() {
return {
initialed: false,
progress: [],
stepsSize: [],
currentLength: 0,
duration: 0.3,
timer: null,
}
},
computed: {
$_barInnerStyle() {
return index => {
const {progress} = this
const transform =
this.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(val, oldVal) {
const currentStep = this.$_formatValue(val)
const newProgress = this.$_sliceProgress(currentStep)
if (this.transition) {
const isAdd = currentStep >= oldVal
this.timer && clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.$_doTransition(newProgress, isAdd, len => {
if ((isAdd && len > this.currentLength) || (!isAdd && len < this.currentLength)) {
this.currentLength = len
}
})
}, 100)
} else {
this.progress = newProgress
this.currentLength = currentStep
}
},
},
created() {
const currentStep = this.$_formatValue(this.current)
this.currentLength = currentStep
this.progress = this.$_sliceProgress(currentStep)
},
mounted() {
this.$_initStepSize()
},
updated() {
this.$nextTick(() => {
this.$_initStepSize()
})
},
methods: {
// MARK: private methods
$_initStepSize() {
if (this.direction !== 'vertical' || this.verticalAdaptive) {
return
}
const iconWrappers = this.$el.querySelectorAll('.icon-wrapper')
const textWrappers = this.$el.querySelectorAll('.text-wrapper')
const stepsSize = toArray(textWrappers).map((wrapper, index) => {
let stepHeight = wrapper.clientHeight
const 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
}
return stepHeight > 0 ? stepHeight : 0
})
if (stepsSize.toString() !== this.stepsSize.toString()) {
this.stepsSize = stepsSize
}
},
$_getStepSizeForStyle(index) {
const size = this.direction === 'vertical' && !this.verticalAdaptive ? this.stepsSize[index] : 0
return size
? {
height: `${size}px`,
}
: null
},
$_getStepStatusClass(index) {
const currentLength = this.currentLength
let status = []
if (index < currentLength) {
status.push('reached')
}
if (index === Math.floor(currentLength)) {
status.push('current')
}
return status.join(' ')
},
$_formatValue(val) {
if (val < 0) {
return 0
} else if (val > this.steps.length - 1) {
return this.steps.length - 1
} else {
return val
}
},
$_sliceProgress(current) {
return this.steps.slice(0, this.steps.length - 1).map((step, index) => {
const offset = current - index
const progress = this.progress[index]
const isNewProgress = progress === undefined
let len, time
if (offset <= 0) {
len = 0
} else if (offset >= 1) {
len = 1
} else {
len = offset
}
time = (isNewProgress ? len : Math.abs(progress.len - len)) * this.duration
return {
len,
time,
}
})
},
$_doTransition(progress, isAdd, step) {
let currentLength = isAdd ? 0 : this.currentLength
const walk = index => {
if ((index < progress.length) & (index > -1) && progress[index]) {
if (isAdd) {
currentLength += progress[index].len
} else {
currentLength -= this.progress[index].len - progress[index].len
}
setTimeout(() => {
index += isAdd ? 1 : -1
step(currentLength)
walk(index)
}, progress[index].time * 1000)
}
this.$set(this.progress, index, progress[index])
}
walk(isAdd ? 0 : progress.length - 1)
},
},
}
</script>
<style lang="stylus">
.md-steps
display flex
justify-content space-around
font-size 28px
&.md-steps-horizontal
align-items center
padding 40px 100px 100px
.step-wrapper
margin 0 4px
justify-content center
align-items center
flex-direction column
&.reached
.text-wrapper .name
color steps-text-color
&.current
.text-wrapper .name
color steps-color-active
.text-wrapper
top 100%
padding-top steps-text-gap-horizontal
text-align center
.name
color steps-desc-color
.desc
margin-top 10px
color steps-desc-color
&.no-current
.reached:last-of-type
display none !important
&.md-steps-vertical
align-items flex-start
padding 40px
flex-direction column
&.vertical-adaptive
justify-content normal
padding 40px 40px 8px
.bar.vertical-bar
flex 1
.step-wrapper
width 100%
margin 4px 0
align-items stretch
.icon-wrapper
position relative
.step-node-default
min-width steps-icon-size
min-height steps-icon-size
.text-wrapper
left steps-icon-size
padding-left steps-text-gap-vertical
.name, .desc
white-space normal
.name
color steps-text-color
.desc
margin-top 18px
color steps-desc-color
.icon-wrapper
display flex
justify-content center
align-items center
color steps-color
>div
display flex
justify-content center
align-items center
&:nth-child(2)
display none
.step-node-default-icon
background steps-color
.step-wrapper
display flex
position relative
min-width steps-icon-size
min-height steps-icon-size
.icon-wrapper
min-width steps-icon-size
min-height steps-icon-size
.md-icon
width steps-icon-size
height steps-icon-size
font-size steps-icon-size
line-height steps-icon-size
.text-wrapper
position absolute
.name, .desc
white-space nowrap
.name
line-height steps-text-font-size
font-size steps-text-font-size
.desc
line-height steps-text-font-size
font-size steps-desc-font-size
&.reached, &.current
.icon-wrapper
color steps-color-active
.step-node-default-icon
background steps-color-active
.bar
position relative
background-color steps-color
overflow hidden
.bar-inner
z-index 10
position absolute
top 0
left 0
display block
content ''
transition all linear 1s
&.horizontal-bar
flex 1
height steps-border-size
.bar-inner
width 100%
height steps-border-size
background-color steps-color-active
&.vertical-bar
left 16px
width steps-border-size
transform translateX(-50%)
.bar-inner
width steps-border-size
height 100%
background-color steps-color-active
&:last-of-type
&.horizontal-bar
display none
&.vertical-bar
visibility hidden
</style>