1
- <template >
1
+ <template >
2
2
<div class =" el-statistic" >
3
3
<div class =" head" >
4
4
<slot name =" title" >
13
13
{{ prefix }}
14
14
</slot >
15
15
</span >
16
-
17
-
18
16
<span class =" number" :style =" valueStyle" >
19
17
<slot name =" formatter" > {{ disposeValue }}</slot >
20
18
</span >
23
21
{{ suffix }}
24
22
</slot >
25
23
</span >
26
-
27
24
</div >
28
25
</div >
29
26
</template >
30
27
31
28
<script >
32
-
33
- import _ from ' lodash' ;
34
- // const dayjs = require('dayjs');
35
-
29
+ import { isNumber , ceil , fill , chain , multiply , padStart , reduce } from ' element-ui/src/utils/lodash' ;
36
30
export default {
37
31
name: ' ElStatistic' ,
38
32
data () {
39
33
return {
40
34
disposeValue: ' ' ,
41
- timeTask: undefined ,
35
+ timeTask: null ,
42
36
REFRESH_INTERVAL : 1000 / 30
43
37
};
44
38
},
@@ -89,11 +83,9 @@ export default {
89
83
type: Number ,
90
84
default: 1000
91
85
}
92
-
93
86
},
94
87
created () {
95
88
this .branch ();
96
-
97
89
},
98
90
watch: {
99
91
value : function () {
@@ -102,14 +94,11 @@ export default {
102
94
},
103
95
methods: {
104
96
branch () {
105
- if (this .timeIndices ) {
106
- clearInterval (this .timeTask );
107
- this .countDown ();
108
- } else {
109
- this .dispose ();
110
- }
97
+ let { timeIndices, countDown, dispose} = this ;
98
+ timeIndices ? countDown () : dispose ();
111
99
},
112
- magnification (num , _mulriple = 1000 , _groupSeparator = ' ,' ) { // magnification factor
100
+ magnification (num , _mulriple = 1000 , _groupSeparator = ' ,' ) {
101
+ // magnification factor
113
102
const level = String (_mulriple).length - 1 ;
114
103
const reg = new RegExp (` \\ d{1,${ level} }(?=(\\ d{${ level} })+$)` , ' g' );
115
104
const result = String (num)
@@ -121,83 +110,101 @@ export default {
121
110
dispose () {
122
111
let { value, precision, groupSeparator, rate } = this ;
123
112
124
- if (! _ . isNumber (value)) return false ;
113
+ if (! isNumber (value)) return false ;
125
114
if (precision) {
126
- value = _ . ceil (value, precision);
115
+ value = ceil (value, precision);
127
116
}
128
117
129
118
let integer = String (value).split (' .' )[0 ];
130
- let decimals = String (value).split (' .' )[1 ] || (precision ? _ .fill (Array (precision), 0 ).join (' ' ) : ' ' );
119
+ let decimals =
120
+ String (value).split (' .' )[1 ] ||
121
+ (precision ? fill (Array (precision), 0 ).join (' ' ) : ' ' );
131
122
let result = 0 ;
132
123
// 1000 multiplying power
133
124
if (groupSeparator) {
134
125
integer = this .magnification (integer, rate, groupSeparator);
135
126
}
136
127
137
128
result = [integer, decimals].join (
138
- decimals ? this .decimalSeparator || ' . ' : ' '
129
+ decimals ? this .decimalSeparator : ' '
139
130
);
140
131
this .disposeValue = result;
141
132
return result;
142
133
},
143
134
diffDate (minuend , subtrahend ) {
144
- return _ . subtract (minuend, subtrahend);
135
+ return Math . max (minuend - subtrahend, 0 );
145
136
},
146
137
suspend (isStop ) {
147
138
if (isStop) {
148
- clearInterval (this .timeTask );
139
+ if (this .timeTask ) {
140
+ clearInterval (this .timeTask );
141
+ this .timeTask = null ;
142
+ }
149
143
150
144
} else {
151
145
this .branch ();
152
146
}
153
147
return this .disposeValue ;
154
148
},
155
- countDown () {
156
- let { format, value, REFRESH_INTERVAL , diffDate, suspend } = this ;
157
- let diffTiem = diffDate (value, Date .now ());
158
- let formatTimeStr = function (format , time ) {
159
- const timeUnits = [
160
- [' Y' , 1000 * 60 * 60 * 24 * 365 ], // years
161
- [' M' , 1000 * 60 * 60 * 24 * 30 ], // months
162
- [' D' , 1000 * 60 * 60 * 24 ], // days
163
- [' H' , 1000 * 60 * 60 ], // hours
164
- [' m' , 1000 * 60 ], // minutes
165
- [' s' , 1000 ], // seconds
166
- [' S' , 1 ] // million seconds
167
- ];
168
- return _ .reduce (timeUnits, (con , item ) => {
169
- let name = item[0 ];
149
+ formatTimeStr : function (time ) {
150
+ let {format} = this ;
151
+ const escapeRegex = / \[ [^ \] ] * ]/ g ;
152
+ const keepList = (format .match (escapeRegex) || []).map (str => str .slice (1 , - 1 ));
153
+ const timeUnits = [
154
+ [' Y' , 1000 * 60 * 60 * 24 * 365 ], // years
155
+ [' M' , 1000 * 60 * 60 * 24 * 30 ], // months
156
+ [' D' , 1000 * 60 * 60 * 24 ], // days
157
+ [' H' , 1000 * 60 * 60 ], // hours
158
+ [' m' , 1000 * 60 ], // minutes
159
+ [' s' , 1000 ], // seconds
160
+ [' S' , 1 ] // million seconds
161
+ ];
162
+ let formatText = reduce (
163
+ timeUnits,
164
+ (con , item ) => {
165
+ const name = item[0 ];
170
166
return con .replace (new RegExp (` ${ name} +` , ' g' ), (match ) => {
171
- let sum = _ .chain (time).divide (item[1 ]).floor ().value ();
172
- time -= _ .multiply (sum, item[1 ]);
173
- sum = _ .padStart (String (sum), String (match).length , 0 ); // autoCompletion
174
- if (! sum)suspend ();
175
- return sum;
176
-
167
+ let sum = chain (time).divide (item[1 ]).floor (0 ).value ();
168
+ time -= multiply (sum, item[1 ]);
169
+ return padStart (String (sum), String (match).length , 0 );
177
170
});
178
- }, format);
179
- };
171
+ },
172
+ format
173
+ );
174
+ let index = 0 ;
175
+ return formatText .replace (escapeRegex, () => {
176
+ const match = keepList[index];
177
+ index += 1 ;
178
+ return match;
179
+ });
180
+ },
181
+ stopTime (time ) {
182
+ let result = true ; // stop
183
+ if (time) {
184
+ this .$emit (' change' , time);
185
+ result = false ;
186
+ } else {
187
+ result = true ;
188
+ this .suspend (true );
189
+ this .$emit (' finish' , true );
190
+ }
191
+ return result;
192
+ },
193
+ countDown () {
194
+ let {REFRESH_INTERVAL , timeTask, diffDate, formatTimeStr, stopTime, suspend} = this ;
195
+ if (timeTask) return ;
180
196
let than = this ;
181
- let disappearTime = function (time ) {
182
- let result = true ;// stop
183
- if (value > Date .now ()) {
184
- than .$emit (' change' , time);
185
-
186
- result = false ;
187
- } else {
188
- result = true ;
189
-
190
- than .$emit (' finish' , true );
191
- }
192
- return (result);
193
- };
194
- this .timeTask = setInterval (function () {
195
- if (disappearTime (diffTiem)) clearInterval (than .timeTask );
196
- diffTiem = diffTiem < REFRESH_INTERVAL ? 0 : diffTiem - REFRESH_INTERVAL ;
197
- than .disposeValue = formatTimeStr (format, diffTiem);
197
+ this .timeTask = setInterval (()=> {
198
+ let {value} = than;
199
+ let diffTiem = diffDate (value, Date .now ());
200
+ than .disposeValue = formatTimeStr (diffTiem);
201
+ stopTime (diffTiem);
198
202
}, REFRESH_INTERVAL );
203
+ this .$once (' hook:beforeDestroy' , () => {
204
+ suspend (true );
205
+ });
199
206
200
207
}
201
208
}
202
209
};
203
- </script >
210
+ </script >
0 commit comments