forked from didi/mand-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.vue
More file actions
144 lines (129 loc) · 3.21 KB
/
index.vue
File metadata and controls
144 lines (129 loc) · 3.21 KB
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
<template>
<span class="md-amount" :class="{numerical: !isCapital}">
<template v-if="!isCapital">{{ formatValue | doPrecision(legalPrecision, isRoundUp) | doFormat(hasSeparator, separator) }}</template> <template v-else> {{ formatValue | doPrecision(4, isRoundUp) | doCapital }} </template>
</span>
</template>
<script>import {noop, inBrowser} from '../_util'
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('.')
let integerValue = numberParts[0]
const decimalValue = numberParts[1] || ''
let sign = ''
if (integerValue.startsWith('-')) {
integerValue = integerValue.substring(1)
sign = '-'
}
const formateValue = formatValueByGapStep(3, integerValue, separator, 'right', 0, 1)
return decimalValue ? `${sign}${formateValue.value}.${decimalValue}` : `${sign}${formateValue.value}`
},
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,
},
transition: {
type: Boolean,
default: false,
},
isCapital: {
type: Boolean,
default: false,
},
duration: {
type: Number,
default: 1000,
},
},
data() {
return {
formatValue: 0,
isMounted: false,
}
},
watch: {
value: {
handler(val, oldVal) {
/* istanbul ignore if */
if (!inBrowser && !this.isMounted) {
this.formatValue = val
return
}
if (this.isAnimated || this.transition) {
this.$_doAnimateDisplay(oldVal, val)
} else {
this.formatValue = val
}
},
immediate: true,
},
},
computed: {
legalPrecision() {
return this.precision > 0 ? this.precision : 0
},
},
mounted() {
this.isMounted = true
},
methods: {
// MARK: private methods
$_doAnimateDisplay(fromValue = 0, toValue = 0) {
/* istanbul ignore next */
const step = percent => {
if (percent === 1) {
this.formatValue = toValue
return
}
this.formatValue = fromValue + (toValue - fromValue) * percent
}
/* istanbul ignore next */
const verify = id => id
Animate.start(step, verify, noop, this.duration)
},
},
}
</script>
<style lang="stylus">
.md-amount
&.numerical
font-family font-family-number
</style>