| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <span class="md-amount" :class="{numerical: !isCapital}"> | 
					
						
							| 
									
										
										
										
											2018-11-27 16:55:00 +08:00
										 |  |  |     <template v-if="!isCapital">{{ formatValue | doPrecision(legalPrecision, isRoundUp) | doFormat(hasSeparator, separator) }}</template> <template v-else> {{ formatValue | doPrecision(4, isRoundUp) | doCapital }} </template> | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  |   </span> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 16:28:45 +08:00
										 |  |  | <script>
import {noop, inBrowser} from '../_util' | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  | import Animate from '../_util/animate' | 
					
						
							|  |  |  | import {formatValueByGapStep} from '../_util/formate-value' | 
					
						
							|  |  |  | import numberCapital from './number-capital' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |   name: 'md-amount', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   filters: { | 
					
						
							|  |  |  |     doPrecision(value, precision, isRoundUp) { | 
					
						
							|  |  |  |       const exponentialForm = Number(`${value}e${precision}`) | 
					
						
							|  |  |  |       const rounded = isRoundUp ? Math.round(exponentialForm) : Math.floor(exponentialForm) | 
					
						
							|  |  |  |       return Number(`${rounded}e-${precision}`).toFixed(precision) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     doFormat(value, hasSeparator, separator) { | 
					
						
							|  |  |  |       if (!hasSeparator) { | 
					
						
							|  |  |  |         return value | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const numberParts = value.split('.') | 
					
						
							| 
									
										
										
										
											2020-02-04 14:12:38 +08:00
										 |  |  |       let integerValue = numberParts[0] | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  |       const decimalValue = numberParts[1] || '' | 
					
						
							| 
									
										
										
										
											2020-02-04 14:12:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       let sign = '' | 
					
						
							|  |  |  |       if (integerValue.startsWith('-')) { | 
					
						
							|  |  |  |         integerValue = integerValue.substring(1) | 
					
						
							|  |  |  |         sign = '-' | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  |       const formateValue = formatValueByGapStep(3, integerValue, separator, 'right', 0, 1) | 
					
						
							| 
									
										
										
										
											2020-02-04 14:12:38 +08:00
										 |  |  |       return decimalValue ? `${sign}${formateValue.value}.${decimalValue}` : `${sign}${formateValue.value}` | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  |     }, | 
					
						
							|  |  |  |     doCapital(value) { | 
					
						
							|  |  |  |       return numberCapital(value) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   props: { | 
					
						
							|  |  |  |     value: { | 
					
						
							|  |  |  |       type: Number, | 
					
						
							|  |  |  |       default: 0, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     precision: { | 
					
						
							|  |  |  |       type: Number, | 
					
						
							|  |  |  |       default: 2, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     isRoundUp: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     hasSeparator: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     separator: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							|  |  |  |       default: ',', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     isAnimated: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-10-25 11:12:06 +08:00
										 |  |  |     transition: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  |     isCapital: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     duration: { | 
					
						
							|  |  |  |       type: Number, | 
					
						
							|  |  |  |       default: 1000, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   data() { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       formatValue: 0, | 
					
						
							| 
									
										
										
										
											2018-12-06 16:28:45 +08:00
										 |  |  |       isMounted: false, | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   watch: { | 
					
						
							|  |  |  |     value: { | 
					
						
							|  |  |  |       handler(val, oldVal) { | 
					
						
							|  |  |  |         /* istanbul ignore if  */ | 
					
						
							| 
									
										
										
										
											2018-12-06 16:28:45 +08:00
										 |  |  |         if (!inBrowser && !this.isMounted) { | 
					
						
							|  |  |  |           this.formatValue = val | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-10-25 11:12:06 +08:00
										 |  |  |         if (this.isAnimated || this.transition) { | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  |           this.$_doAnimateDisplay(oldVal, val) | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           this.formatValue = val | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       immediate: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 10:48:38 +08:00
										 |  |  |   computed: { | 
					
						
							|  |  |  |     legalPrecision() { | 
					
						
							|  |  |  |       return this.precision > 0 ? this.precision : 0 | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 16:28:45 +08:00
										 |  |  |   mounted() { | 
					
						
							|  |  |  |     this.isMounted = true | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   methods: { | 
					
						
							|  |  |  |     // MARK: private methods
 | 
					
						
							|  |  |  |     $_doAnimateDisplay(fromValue = 0, toValue = 0) { | 
					
						
							|  |  |  |       /* istanbul ignore next  */ | 
					
						
							|  |  |  |       const step = percent => { | 
					
						
							| 
									
										
										
										
											2019-08-03 10:09:26 +08:00
										 |  |  |         if (percent === 1) { | 
					
						
							|  |  |  |           this.formatValue = toValue | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  |         this.formatValue = fromValue + (toValue - fromValue) * percent | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /* istanbul ignore next  */ | 
					
						
							| 
									
										
										
										
											2019-01-04 16:52:06 +08:00
										 |  |  |       const verify = id => id | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  |       Animate.start(step, verify, noop, this.duration) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
</script> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 20:23:01 +08:00
										 |  |  | <style lang="stylus"> | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  | .md-amount | 
					
						
							|  |  |  |   &.numerical | 
					
						
							| 
									
										
										
										
											2018-11-30 20:23:01 +08:00
										 |  |  |     font-family font-family-number | 
					
						
							| 
									
										
										
										
											2018-06-30 23:12:00 +08:00
										 |  |  | </style> |