2018-02-04 22:25:19 -07:00
|
|
|
<template>
|
2018-02-16 11:18:48 -07:00
|
|
|
<time class="mk-time">
|
2018-02-12 21:49:48 -07:00
|
|
|
<span v-if=" mode == 'relative' ">{{ relative }}</span>
|
|
|
|
<span v-if=" mode == 'absolute' ">{{ absolute }}</span>
|
|
|
|
<span v-if=" mode == 'detail' ">{{ absolute }} ({{ relative }})</span>
|
|
|
|
</time>
|
2018-02-04 22:25:19 -07:00
|
|
|
</template>
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2018-02-12 21:49:48 -07:00
|
|
|
<script lang="ts">
|
|
|
|
import Vue from 'vue';
|
2018-11-08 11:44:35 -07:00
|
|
|
import i18n from '../../../i18n';
|
2018-02-09 02:28:06 -07:00
|
|
|
|
2018-02-12 21:49:48 -07:00
|
|
|
export default Vue.extend({
|
2018-11-08 11:44:35 -07:00
|
|
|
i18n: i18n(),
|
2018-02-12 21:49:48 -07:00
|
|
|
props: {
|
|
|
|
time: {
|
|
|
|
type: [Date, String],
|
|
|
|
required: true
|
2018-02-04 22:25:19 -07:00
|
|
|
},
|
2018-02-12 21:49:48 -07:00
|
|
|
mode: {
|
|
|
|
type: String,
|
|
|
|
default: 'relative'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
tickId: null,
|
|
|
|
now: new Date()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
_time(): Date {
|
|
|
|
return typeof this.time == 'string' ? new Date(this.time) : this.time;
|
2018-02-04 22:25:19 -07:00
|
|
|
},
|
2018-02-12 21:49:48 -07:00
|
|
|
absolute(): string {
|
2018-12-18 09:10:53 -07:00
|
|
|
return this._time.toLocaleString();
|
2018-02-04 22:25:19 -07:00
|
|
|
},
|
2018-02-12 21:49:48 -07:00
|
|
|
relative(): string {
|
|
|
|
const time = this._time;
|
|
|
|
const ago = (this.now.getTime() - time.getTime()) / 1000/*ms*/;
|
|
|
|
return (
|
2018-11-08 11:44:35 -07:00
|
|
|
ago >= 31536000 ? this.$t('@.time.years_ago') .replace('{}', (~~(ago / 31536000)).toString()) :
|
|
|
|
ago >= 2592000 ? this.$t('@.time.months_ago') .replace('{}', (~~(ago / 2592000)).toString()) :
|
|
|
|
ago >= 604800 ? this.$t('@.time.weeks_ago') .replace('{}', (~~(ago / 604800)).toString()) :
|
|
|
|
ago >= 86400 ? this.$t('@.time.days_ago') .replace('{}', (~~(ago / 86400)).toString()) :
|
|
|
|
ago >= 3600 ? this.$t('@.time.hours_ago') .replace('{}', (~~(ago / 3600)).toString()) :
|
|
|
|
ago >= 60 ? this.$t('@.time.minutes_ago').replace('{}', (~~(ago / 60)).toString()) :
|
|
|
|
ago >= 10 ? this.$t('@.time.seconds_ago').replace('{}', (~~(ago % 60)).toString()) :
|
2018-11-17 04:57:23 -07:00
|
|
|
ago >= -1 ? this.$t('@.time.just_now') :
|
|
|
|
ago < -1 ? this.$t('@.time.future') :
|
2018-11-08 11:44:35 -07:00
|
|
|
this.$t('@.time.unknown'));
|
2018-02-12 21:49:48 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
if (this.mode == 'relative' || this.mode == 'detail') {
|
2018-06-09 17:03:02 -06:00
|
|
|
this.tickId = window.requestAnimationFrame(this.tick);
|
2018-02-12 21:49:48 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
destroyed() {
|
|
|
|
if (this.mode === 'relative' || this.mode === 'detail') {
|
2018-06-09 17:03:02 -06:00
|
|
|
window.clearTimeout(this.tickId);
|
2018-02-12 21:49:48 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
tick() {
|
|
|
|
this.now = new Date();
|
2018-06-09 17:03:02 -06:00
|
|
|
|
|
|
|
this.tickId = setTimeout(() => {
|
|
|
|
window.requestAnimationFrame(this.tick);
|
|
|
|
}, 10000);
|
2018-02-04 22:25:19 -07:00
|
|
|
}
|
2018-02-12 21:49:48 -07:00
|
|
|
}
|
|
|
|
});
|
2018-02-04 22:25:19 -07:00
|
|
|
</script>
|