2019-05-25 09:18:04 +00:00
|
|
|
import * as moment from 'moment';
|
|
|
|
import interopDefault from '../_util/interopDefault';
|
2020-06-22 09:50:38 +00:00
|
|
|
import { initDefaultProps } from '../_util/props-util';
|
2019-05-25 09:18:04 +00:00
|
|
|
import Statistic, { StatisticProps } from './Statistic';
|
|
|
|
import { formatCountdown } from './utils';
|
|
|
|
|
|
|
|
const REFRESH_INTERVAL = 1000 / 30;
|
|
|
|
|
|
|
|
function getTime(value) {
|
|
|
|
return interopDefault(moment)(value).valueOf();
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'AStatisticCountdown',
|
|
|
|
props: initDefaultProps(StatisticProps, {
|
|
|
|
format: 'HH:mm:ss',
|
|
|
|
}),
|
|
|
|
|
2019-09-18 12:03:13 +00:00
|
|
|
created() {
|
|
|
|
this.countdownId = undefined;
|
2019-05-25 09:18:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
mounted() {
|
2019-09-18 12:03:13 +00:00
|
|
|
this.syncTimer();
|
2019-05-25 09:18:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
updated() {
|
2019-09-18 12:03:13 +00:00
|
|
|
this.syncTimer();
|
2019-05-25 09:18:04 +00:00
|
|
|
},
|
|
|
|
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2019-05-25 09:18:04 +00:00
|
|
|
this.stopTimer();
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
syncTimer() {
|
|
|
|
const { value } = this.$props;
|
|
|
|
const timestamp = getTime(value);
|
|
|
|
if (timestamp >= Date.now()) {
|
|
|
|
this.startTimer();
|
|
|
|
} else {
|
|
|
|
this.stopTimer();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
startTimer() {
|
2019-09-18 12:03:13 +00:00
|
|
|
if (this.countdownId) return;
|
2019-05-25 09:18:04 +00:00
|
|
|
this.countdownId = window.setInterval(() => {
|
2019-09-18 12:03:13 +00:00
|
|
|
this.$refs.statistic.$forceUpdate();
|
2020-02-07 02:03:25 +00:00
|
|
|
this.syncTimer();
|
2019-05-25 09:18:04 +00:00
|
|
|
}, REFRESH_INTERVAL);
|
|
|
|
},
|
|
|
|
|
|
|
|
stopTimer() {
|
|
|
|
const { value } = this.$props;
|
|
|
|
if (this.countdownId) {
|
|
|
|
clearInterval(this.countdownId);
|
|
|
|
this.countdownId = undefined;
|
|
|
|
|
|
|
|
const timestamp = getTime(value);
|
|
|
|
if (timestamp < Date.now()) {
|
|
|
|
this.$emit('finish');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-09-18 12:03:13 +00:00
|
|
|
formatCountdown({ value, config }) {
|
2019-05-25 09:18:04 +00:00
|
|
|
const { format } = this.$props;
|
|
|
|
return formatCountdown(value, { ...config, format });
|
|
|
|
},
|
|
|
|
|
2020-03-26 09:26:23 +00:00
|
|
|
valueRenderHtml: node => node,
|
2019-05-25 09:18:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Statistic
|
2019-09-18 12:03:13 +00:00
|
|
|
ref="statistic"
|
2019-05-25 09:18:04 +00:00
|
|
|
{...{
|
2020-06-22 09:56:03 +00:00
|
|
|
...this.$props,
|
|
|
|
valueRender: this.valueRenderHtml,
|
|
|
|
formatter: this.formatCountdown,
|
2019-05-25 09:18:04 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
};
|