iview/src/components/progress/progress.vue

170 lines
5 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
<div :class="wrapClasses">
2016-09-22 16:24:20 +08:00
<div :class="outerClasses">
<div :class="innerClasses">
<div :class="bgClasses" :style="bgStyle"><div class="ivu-progress-inner-text" v-if="textInside">{{ percent }}%</div></div><div :class="successBgClasses" :style="successBgStyle"></div>
2016-09-22 16:24:20 +08:00
</div>
</div>
2016-09-09 14:29:19 +08:00
<span v-if="!hideInfo" :class="textClasses">
<slot>
<span v-if="isStatus" :class="textInnerClasses">
<Icon :type="statusIcon"></Icon>
</span>
<span v-else :class="textInnerClasses">
{{ percent }}%
</span>
</slot>
</span>
</div>
</template>
<script>
import Icon from '../icon';
import { oneOf } from '../../utils/assist';
const prefixCls = 'ivu-progress';
export default {
2018-08-08 17:07:17 +08:00
name: 'Progress',
2016-09-09 14:29:19 +08:00
components: { Icon },
props: {
percent: {
type: Number,
default: 0
},
2018-06-27 17:15:18 +08:00
successPercent: {
type: Number,
default: 0
},
2016-09-09 14:29:19 +08:00
status: {
validator (value) {
return oneOf(value, ['normal', 'active', 'wrong', 'success']);
},
default: 'normal'
},
hideInfo: {
type: Boolean,
default: false
},
strokeWidth: {
type: Number,
default: 10
2017-07-21 12:36:16 -05:00
},
vertical: {
type: Boolean,
default: false
2019-03-04 17:46:36 +08:00
},
strokeColor: {
type: String
},
textInside: {
type: Boolean,
default: false
2016-09-09 14:29:19 +08:00
}
},
data () {
return {
currentStatus: this.status
2017-03-02 15:05:06 +08:00
};
},
2016-09-09 14:29:19 +08:00
computed: {
isStatus () {
return this.currentStatus == 'wrong' || this.currentStatus == 'success';
2016-09-09 14:29:19 +08:00
},
statusIcon () {
let type = '';
switch (this.currentStatus) {
2016-09-09 14:29:19 +08:00
case 'wrong':
2018-06-25 14:06:14 +08:00
type = 'ios-close-circle';
2016-09-09 14:29:19 +08:00
break;
case 'success':
2018-06-25 14:06:14 +08:00
type = 'ios-checkmark-circle';
2016-09-09 14:29:19 +08:00
break;
}
return type;
},
bgStyle () {
2019-03-04 17:46:36 +08:00
const style = this.vertical ? {
2017-07-21 12:36:16 -05:00
height: `${this.percent}%`,
2019-03-04 17:46:36 +08:00
width: `${this.strokeWidth}px`,
2017-07-21 12:36:16 -05:00
} : {
2016-09-09 14:29:19 +08:00
width: `${this.percent}%`,
height: `${this.strokeWidth}px`
2016-12-25 22:49:42 +08:00
};
2019-03-04 17:46:36 +08:00
if (this.strokeColor) {
style['background-color'] = this.strokeColor;
}
return style;
2016-09-09 14:29:19 +08:00
},
2018-06-27 17:15:18 +08:00
successBgStyle () {
return this.vertical ? {
height: `${this.successPercent}%`,
width: `${this.strokeWidth}px`
} : {
width: `${this.successPercent}%`,
height: `${this.strokeWidth}px`
};
},
2016-09-09 14:29:19 +08:00
wrapClasses () {
return [
`${prefixCls}`,
`${prefixCls}-${this.currentStatus}`,
2016-09-09 14:29:19 +08:00
{
[`${prefixCls}-show-info`]: !this.hideInfo && !this.textInside,
2017-07-21 12:36:16 -05:00
[`${prefixCls}-vertical`]: this.vertical
2016-09-09 14:29:19 +08:00
}
2016-12-25 22:49:42 +08:00
];
2016-09-09 14:29:19 +08:00
},
textClasses () {
return `${prefixCls}-text`;
},
textInnerClasses () {
return `${prefixCls}-text-inner`;
},
outerClasses () {
return `${prefixCls}-outer`;
},
innerClasses () {
return `${prefixCls}-inner`;
},
bgClasses () {
return `${prefixCls}-bg`;
2018-06-27 17:15:18 +08:00
},
successBgClasses () {
return `${prefixCls}-success-bg`;
2016-09-09 14:29:19 +08:00
}
},
created () {
2016-09-09 14:29:19 +08:00
this.handleStatus();
},
methods: {
handleStatus (isDown) {
if (isDown) {
this.currentStatus = 'normal';
this.$emit('on-status-change', 'normal');
2016-09-09 14:29:19 +08:00
} else {
if (parseInt(this.percent, 10) == 100) {
this.currentStatus = 'success';
this.$emit('on-status-change', 'success');
2016-09-09 14:29:19 +08:00
}
}
}
},
watch: {
percent (val, oldVal) {
if (val < oldVal) {
this.handleStatus(true);
} else {
this.handleStatus();
}
},
status (val) {
this.currentStatus = val;
2016-09-09 14:29:19 +08:00
}
}
2016-12-25 22:49:42 +08:00
};
</script>