|
| 1 | +<template> |
| 2 | + <div |
| 3 | + :class="progressBarClasses" |
| 4 | + :style="progressBarStyles" |
| 5 | + role="progressbar" |
| 6 | + aria-valuemin="0" |
| 7 | + :aria-valuemax="props.max.toString()" |
| 8 | + :aria-valuenow="computedValue" |
| 9 | + > |
| 10 | + <slot>{{ text }}</slot> |
| 11 | + </div> |
| 12 | +</template> |
| 13 | + |
| 14 | +<script> |
| 15 | +import props from './progress-props' |
| 16 | +export default { |
| 17 | + name: 'CProgressBar', |
| 18 | + props, |
| 19 | + inject: { |
| 20 | + progress: { |
| 21 | + default: undefined |
| 22 | + } |
| 23 | + }, |
| 24 | + computed: { |
| 25 | + directlyDeclaredProps () { |
| 26 | + return Object.keys(this.$options.propsData) |
| 27 | + }, |
| 28 | + injectedProps () { |
| 29 | + return this.progress && this.progress.props ? this.progress.props : {} |
| 30 | + }, |
| 31 | + props () { |
| 32 | + return Object.keys(props).reduce((computedProps, key) => { |
| 33 | + const propIsDirectlyDeclared = this.directlyDeclaredProps.includes(key) |
| 34 | + const parentPropExists = this.injectedProps[key] !== undefined |
| 35 | + const propIsInherited = parentPropExists && !propIsDirectlyDeclared |
| 36 | + computedProps[key] = propIsInherited ? this.injectedProps[key] : this[key] |
| 37 | + return computedProps |
| 38 | + }, {}) |
| 39 | + }, |
| 40 | + progressBarClasses () { |
| 41 | + return [ |
| 42 | + 'progress-bar', |
| 43 | + { |
| 44 | + [`bg-${this.props.color}`]: this.props.color, |
| 45 | + 'progress-bar-striped': this.props.striped || this.props.animated, |
| 46 | + 'progress-bar-animated': this.props.animated |
| 47 | + } |
| 48 | + ] |
| 49 | + }, |
| 50 | +
|
| 51 | + progressBarStyles () { |
| 52 | + return { width: `${(100 * (this.props.value / this.props.max))}%` } |
| 53 | + }, |
| 54 | + progressValue () { |
| 55 | + const p = Math.pow(10, this.props.precision) |
| 56 | + return Math.round((100 * p * this.props.value) / this.props.max) / p |
| 57 | + }, |
| 58 | + computedValue () { |
| 59 | + return this.props.value.toFixed(props.precision) |
| 60 | + }, |
| 61 | + text () { |
| 62 | + if (this.props.showPercentage) { |
| 63 | + return this.progressValue + '%' |
| 64 | + } else if (this.props.showValue) { |
| 65 | + return this.computedValue |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +</script> |
0 commit comments