iview/src/components/steps/steps.vue

138 lines
4.3 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
<div :class="classes">
<slot></slot>
</div>
</template>
<script>
import { oneOf } from '../../utils/assist';
const prefixCls = 'ivu-steps';
2017-09-19 21:47:53 +08:00
function debounce(fn) {
let waiting;
return function() {
if (waiting) return;
waiting = true;
const context = this,
args = arguments;
const later = function() {
waiting = false;
fn.apply(context, args);
};
this.$nextTick(later);
};
}
2016-09-09 14:29:19 +08:00
export default {
name: 'Steps',
2016-09-09 14:29:19 +08:00
props: {
current: {
type: Number,
default: 0
},
status: {
validator (value) {
return oneOf(value, ['wait', 'process', 'finish', 'error']);
},
default: 'process'
},
size: {
validator (value) {
return oneOf(value, ['small']);
}
},
direction: {
validator (value) {
return oneOf(value, ['horizontal', 'vertical']);
},
default: 'horizontal'
}
},
computed: {
classes () {
return [
`${prefixCls}`,
`${prefixCls}-${this.direction}`,
{
[`${prefixCls}-${this.size}`]: !!this.size
}
2016-12-25 22:49:42 +08:00
];
2016-09-09 14:29:19 +08:00
}
},
methods: {
updateChildProps (isInit) {
2016-09-23 15:22:37 +08:00
const total = this.$children.length;
2016-09-09 14:29:19 +08:00
this.$children.forEach((child, index) => {
child.stepNumber = index + 1;
2016-09-23 15:22:37 +08:00
if (this.direction === 'horizontal') {
child.total = total;
}
2016-09-09 14:29:19 +08:00
// 如果已存在status,且在初始化时,则略过
// todo 如果当前是error,在current改变时需要处理
2017-03-01 23:24:23 +08:00
if (!(isInit && child.currentStatus)) {
2016-09-09 14:29:19 +08:00
if (index == this.current) {
if (this.status != 'error') {
2017-03-01 23:24:23 +08:00
child.currentStatus = 'process';
2016-09-09 14:29:19 +08:00
}
} else if (index < this.current) {
2017-03-01 23:24:23 +08:00
child.currentStatus = 'finish';
2016-09-09 14:29:19 +08:00
} else {
2017-03-01 23:24:23 +08:00
child.currentStatus = 'wait';
2016-09-09 14:29:19 +08:00
}
}
2017-03-01 23:24:23 +08:00
if (child.currentStatus != 'error' && index != 0) {
2016-09-09 14:29:19 +08:00
this.$children[index - 1].nextError = false;
}
});
},
setNextError () {
this.$children.forEach((child, index) => {
2017-03-01 23:24:23 +08:00
if (child.currentStatus == 'error' && index != 0) {
2016-09-09 14:29:19 +08:00
this.$children[index - 1].nextError = true;
}
});
},
updateCurrent (isInit) {
2017-03-01 23:24:23 +08:00
// 防止溢出边界
if (this.current < 0 || this.current >= this.$children.length ) {
return;
}
2016-09-09 14:29:19 +08:00
if (isInit) {
2017-03-01 23:24:23 +08:00
const current_status = this.$children[this.current].currentStatus;
2016-09-09 14:29:19 +08:00
if (!current_status) {
2017-03-01 23:24:23 +08:00
this.$children[this.current].currentStatus = this.status;
2016-09-09 14:29:19 +08:00
}
} else {
2017-03-01 23:24:23 +08:00
this.$children[this.current].currentStatus = this.status;
2016-09-09 14:29:19 +08:00
}
2017-09-19 21:47:53 +08:00
},
debouncedAppendRemove () {
return debounce(function () {
this.updateSteps();
});
},
updateSteps () {
this.updateChildProps(true);
this.setNextError();
this.updateCurrent(true);
2016-09-09 14:29:19 +08:00
}
},
2017-09-19 21:47:53 +08:00
mounted () {
this.updateSteps();
this.$on('append', this.debouncedAppendRemove());
this.$on('remove', this.debouncedAppendRemove());
},
2016-09-09 14:29:19 +08:00
watch: {
current () {
this.updateChildProps();
},
status () {
this.updateCurrent();
}
}
2016-12-25 22:49:42 +08:00
};
</script>