Skip to content

Commit fa18776

Browse files
authored
statistic:Updated countdown feature to localize lodash Closes ElemeFE#22260 Closes#22258 (ElemeFE#22263)
* statistic:Fixed the thousandth bit bug statistic:Fixed the thousandth bit bug * Modifying Spaces Modifying Spaces * statistic:Updated countdown feature to localize lodash * Add lodash localization filtering * statistic:Under the default font, the problem caused by the non-equal width font, causing the countdown to jitter, will be replaced with a concise font
1 parent 425235b commit fa18776

File tree

5 files changed

+18150
-66
lines changed

5 files changed

+18150
-66
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
src/utils/popper.js
22
src/utils/date.js
3+
src/utils/lodash.js
34
examples/play
45
*.sh
56
node_modules

build/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ exports.vue = {
4646
amd: 'vue'
4747
};
4848

49-
exports.jsexclude = /node_modules|utils\/popper\.js|utils\/date\.js/;
49+
exports.jsexclude = /node_modules|utils\/popper\.js|utils\/date\.js|utils\/lodash\.js/;

packages/statistic/src/main.vue

+72-65
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<template >
1+
<template>
22
<div class="el-statistic">
33
<div class="head">
44
<slot name="title">
@@ -13,8 +13,6 @@
1313
{{ prefix }}
1414
</slot>
1515
</span>
16-
17-
1816
<span class="number" :style="valueStyle">
1917
<slot name="formatter"> {{ disposeValue }}</slot>
2018
</span>
@@ -23,22 +21,18 @@
2321
{{ suffix }}
2422
</slot>
2523
</span>
26-
2724
</div>
2825
</div>
2926
</template>
3027

3128
<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';
3630
export default {
3731
name: 'ElStatistic',
3832
data() {
3933
return {
4034
disposeValue: '',
41-
timeTask: undefined,
35+
timeTask: null,
4236
REFRESH_INTERVAL: 1000 / 30
4337
};
4438
},
@@ -89,11 +83,9 @@ export default {
8983
type: Number,
9084
default: 1000
9185
}
92-
9386
},
9487
created() {
9588
this.branch();
96-
9789
},
9890
watch: {
9991
value: function() {
@@ -102,14 +94,11 @@ export default {
10294
},
10395
methods: {
10496
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();
11199
},
112-
magnification(num, _mulriple = 1000, _groupSeparator = ',') { // magnification factor
100+
magnification(num, _mulriple = 1000, _groupSeparator = ',') {
101+
// magnification factor
113102
const level = String(_mulriple).length - 1;
114103
const reg = new RegExp(`\\d{1,${level}}(?=(\\d{${level}})+$)`, 'g');
115104
const result = String(num)
@@ -121,83 +110,101 @@ export default {
121110
dispose() {
122111
let { value, precision, groupSeparator, rate } = this;
123112
124-
if (!_.isNumber(value)) return false;
113+
if (!isNumber(value)) return false;
125114
if (precision) {
126-
value = _.ceil(value, precision);
115+
value = ceil(value, precision);
127116
}
128117
129118
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('') : '');
131122
let result = 0;
132123
// 1000 multiplying power
133124
if (groupSeparator) {
134125
integer = this.magnification(integer, rate, groupSeparator);
135126
}
136127
137128
result = [integer, decimals].join(
138-
decimals ? this.decimalSeparator || '.' : ''
129+
decimals ? this.decimalSeparator : ''
139130
);
140131
this.disposeValue = result;
141132
return result;
142133
},
143134
diffDate(minuend, subtrahend) {
144-
return _.subtract(minuend, subtrahend);
135+
return Math.max(minuend - subtrahend, 0);
145136
},
146137
suspend(isStop) {
147138
if (isStop) {
148-
clearInterval(this.timeTask);
139+
if (this.timeTask) {
140+
clearInterval(this.timeTask);
141+
this.timeTask = null;
142+
}
149143
150144
} else {
151145
this.branch();
152146
}
153147
return this.disposeValue;
154148
},
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];
170166
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);
177170
});
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;
180196
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);
198202
}, REFRESH_INTERVAL);
203+
this.$once('hook:beforeDestroy', () => {
204+
suspend(true);
205+
});
199206
200207
}
201208
}
202209
};
203-
</script>
210+
</script>

packages/theme-chalk/src/statistic.scss

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
}
2222

2323
.con{
24+
font-family: Sans-serif;
2425
display: flex;
2526
justify-content :center;
2627
align-items: center ;

0 commit comments

Comments
 (0)