mirror of https://github.com/ElemeFE/element
* 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 fontpull/22126/head^2
parent
425235b1fe
commit
fa18776b4b
|
@ -1,5 +1,6 @@
|
||||||
src/utils/popper.js
|
src/utils/popper.js
|
||||||
src/utils/date.js
|
src/utils/date.js
|
||||||
|
src/utils/lodash.js
|
||||||
examples/play
|
examples/play
|
||||||
*.sh
|
*.sh
|
||||||
node_modules
|
node_modules
|
||||||
|
|
|
@ -46,4 +46,4 @@ exports.vue = {
|
||||||
amd: 'vue'
|
amd: 'vue'
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.jsexclude = /node_modules|utils\/popper\.js|utils\/date\.js/;
|
exports.jsexclude = /node_modules|utils\/popper\.js|utils\/date\.js|utils\/lodash\.js/;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<template >
|
<template>
|
||||||
<div class="el-statistic">
|
<div class="el-statistic">
|
||||||
<div class="head">
|
<div class="head">
|
||||||
<slot name="title">
|
<slot name="title">
|
||||||
|
@ -13,8 +13,6 @@
|
||||||
{{ prefix }}
|
{{ prefix }}
|
||||||
</slot>
|
</slot>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
|
|
||||||
<span class="number" :style="valueStyle">
|
<span class="number" :style="valueStyle">
|
||||||
<slot name="formatter"> {{ disposeValue }}</slot>
|
<slot name="formatter"> {{ disposeValue }}</slot>
|
||||||
</span>
|
</span>
|
||||||
|
@ -23,22 +21,18 @@
|
||||||
{{ suffix }}
|
{{ suffix }}
|
||||||
</slot>
|
</slot>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { isNumber, ceil, fill, chain, multiply, padStart, reduce} from 'element-ui/src/utils/lodash';
|
||||||
import _ from 'lodash';
|
|
||||||
// const dayjs = require('dayjs');
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'ElStatistic',
|
name: 'ElStatistic',
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
disposeValue: '',
|
disposeValue: '',
|
||||||
timeTask: undefined,
|
timeTask: null,
|
||||||
REFRESH_INTERVAL: 1000 / 30
|
REFRESH_INTERVAL: 1000 / 30
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
@ -89,11 +83,9 @@ export default {
|
||||||
type: Number,
|
type: Number,
|
||||||
default: 1000
|
default: 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
this.branch();
|
this.branch();
|
||||||
|
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
value: function() {
|
value: function() {
|
||||||
|
@ -102,14 +94,11 @@ export default {
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
branch() {
|
branch() {
|
||||||
if (this.timeIndices) {
|
let { timeIndices, countDown, dispose} = this;
|
||||||
clearInterval(this.timeTask);
|
timeIndices ? countDown() : dispose();
|
||||||
this.countDown();
|
|
||||||
} else {
|
|
||||||
this.dispose();
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
magnification(num, _mulriple = 1000, _groupSeparator = ',') { // magnification factor
|
magnification(num, _mulriple = 1000, _groupSeparator = ',') {
|
||||||
|
// magnification factor
|
||||||
const level = String(_mulriple).length - 1;
|
const level = String(_mulriple).length - 1;
|
||||||
const reg = new RegExp(`\\d{1,${level}}(?=(\\d{${level}})+$)`, 'g');
|
const reg = new RegExp(`\\d{1,${level}}(?=(\\d{${level}})+$)`, 'g');
|
||||||
const result = String(num)
|
const result = String(num)
|
||||||
|
@ -121,13 +110,15 @@ export default {
|
||||||
dispose() {
|
dispose() {
|
||||||
let { value, precision, groupSeparator, rate } = this;
|
let { value, precision, groupSeparator, rate } = this;
|
||||||
|
|
||||||
if (!_.isNumber(value)) return false;
|
if (!isNumber(value)) return false;
|
||||||
if (precision) {
|
if (precision) {
|
||||||
value = _.ceil(value, precision);
|
value = ceil(value, precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
let integer = String(value).split('.')[0];
|
let integer = String(value).split('.')[0];
|
||||||
let decimals = String(value).split('.')[1] || (precision ? _.fill(Array(precision), 0).join('') : '');
|
let decimals =
|
||||||
|
String(value).split('.')[1] ||
|
||||||
|
(precision ? fill(Array(precision), 0).join('') : '');
|
||||||
let result = 0;
|
let result = 0;
|
||||||
// 1000 multiplying power
|
// 1000 multiplying power
|
||||||
if (groupSeparator) {
|
if (groupSeparator) {
|
||||||
|
@ -135,69 +126,85 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
result = [integer, decimals].join(
|
result = [integer, decimals].join(
|
||||||
decimals ? this.decimalSeparator || '.' : ''
|
decimals ? this.decimalSeparator : ''
|
||||||
);
|
);
|
||||||
this.disposeValue = result;
|
this.disposeValue = result;
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
diffDate(minuend, subtrahend) {
|
diffDate(minuend, subtrahend) {
|
||||||
return _.subtract(minuend, subtrahend);
|
return Math.max(minuend - subtrahend, 0);
|
||||||
},
|
},
|
||||||
suspend(isStop) {
|
suspend(isStop) {
|
||||||
if (isStop) {
|
if (isStop) {
|
||||||
clearInterval(this.timeTask);
|
if (this.timeTask) {
|
||||||
|
clearInterval(this.timeTask);
|
||||||
|
this.timeTask = null;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
this.branch();
|
this.branch();
|
||||||
}
|
}
|
||||||
return this.disposeValue;
|
return this.disposeValue;
|
||||||
},
|
},
|
||||||
countDown() {
|
formatTimeStr: function(time) {
|
||||||
let { format, value, REFRESH_INTERVAL, diffDate, suspend } = this;
|
let {format} = this;
|
||||||
let diffTiem = diffDate(value, Date.now());
|
const escapeRegex = /\[[^\]]*]/g;
|
||||||
let formatTimeStr = function(format, time) {
|
const keepList = (format.match(escapeRegex) || []).map(str => str.slice(1, -1));
|
||||||
const timeUnits = [
|
const timeUnits = [
|
||||||
['Y', 1000 * 60 * 60 * 24 * 365], // years
|
['Y', 1000 * 60 * 60 * 24 * 365], // years
|
||||||
['M', 1000 * 60 * 60 * 24 * 30], // months
|
['M', 1000 * 60 * 60 * 24 * 30], // months
|
||||||
['D', 1000 * 60 * 60 * 24], // days
|
['D', 1000 * 60 * 60 * 24], // days
|
||||||
['H', 1000 * 60 * 60], // hours
|
['H', 1000 * 60 * 60], // hours
|
||||||
['m', 1000 * 60], // minutes
|
['m', 1000 * 60], // minutes
|
||||||
['s', 1000], // seconds
|
['s', 1000], // seconds
|
||||||
['S', 1] // million seconds
|
['S', 1] // million seconds
|
||||||
];
|
];
|
||||||
return _.reduce(timeUnits, (con, item) => {
|
let formatText = reduce(
|
||||||
let name = item[0];
|
timeUnits,
|
||||||
|
(con, item) => {
|
||||||
|
const name = item[0];
|
||||||
return con.replace(new RegExp(`${name}+`, 'g'), (match) => {
|
return con.replace(new RegExp(`${name}+`, 'g'), (match) => {
|
||||||
let sum = _.chain(time).divide(item[1]).floor().value();
|
let sum = chain(time).divide(item[1]).floor(0).value();
|
||||||
time -= _.multiply(sum, item[1]);
|
time -= multiply(sum, item[1]);
|
||||||
sum = _.padStart(String(sum), String(match).length, 0); // autoCompletion
|
return padStart(String(sum), String(match).length, 0);
|
||||||
if (!sum)suspend();
|
|
||||||
return sum;
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}, format);
|
},
|
||||||
};
|
format
|
||||||
|
);
|
||||||
|
let index = 0;
|
||||||
|
return formatText.replace(escapeRegex, () => {
|
||||||
|
const match = keepList[index];
|
||||||
|
index += 1;
|
||||||
|
return match;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
stopTime(time) {
|
||||||
|
let result = true; // stop
|
||||||
|
if (time) {
|
||||||
|
this.$emit('change', time);
|
||||||
|
result = false;
|
||||||
|
} else {
|
||||||
|
result = true;
|
||||||
|
this.suspend(true);
|
||||||
|
this.$emit('finish', true);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
countDown() {
|
||||||
|
let {REFRESH_INTERVAL, timeTask, diffDate, formatTimeStr, stopTime, suspend} = this;
|
||||||
|
if (timeTask) return;
|
||||||
let than = this;
|
let than = this;
|
||||||
let disappearTime = function(time) {
|
this.timeTask = setInterval(()=> {
|
||||||
let result = true;// stop
|
let {value} = than;
|
||||||
if (value > Date.now()) {
|
let diffTiem = diffDate(value, Date.now());
|
||||||
than.$emit('change', time);
|
than.disposeValue = formatTimeStr(diffTiem);
|
||||||
|
stopTime(diffTiem);
|
||||||
result = false;
|
|
||||||
} else {
|
|
||||||
result = true;
|
|
||||||
|
|
||||||
than.$emit('finish', true);
|
|
||||||
}
|
|
||||||
return (result);
|
|
||||||
};
|
|
||||||
this.timeTask = setInterval(function() {
|
|
||||||
if (disappearTime(diffTiem)) clearInterval(than.timeTask);
|
|
||||||
diffTiem = diffTiem < REFRESH_INTERVAL ? 0 : diffTiem - REFRESH_INTERVAL;
|
|
||||||
than.disposeValue = formatTimeStr(format, diffTiem);
|
|
||||||
}, REFRESH_INTERVAL);
|
}, REFRESH_INTERVAL);
|
||||||
|
this.$once('hook:beforeDestroy', () => {
|
||||||
|
suspend(true);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
|
@ -21,6 +21,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.con{
|
.con{
|
||||||
|
font-family: Sans-serif;
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content :center;
|
justify-content :center;
|
||||||
align-items: center ;
|
align-items: center ;
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue