2018-02-04 23:25:19 -06:00
|
|
|
<template>
|
2020-10-27 04:11:41 -05:00
|
|
|
<time :title="absolute">
|
2020-10-17 06:12:00 -05:00
|
|
|
<template v-if="mode == 'relative'">{{ relative }}</template>
|
|
|
|
<template v-else-if="mode == 'absolute'">{{ absolute }}</template>
|
|
|
|
<template v-else-if="mode == 'detail'">{{ absolute }} ({{ relative }})</template>
|
2018-02-12 22:49:48 -06:00
|
|
|
</time>
|
2018-02-04 23:25:19 -06:00
|
|
|
</template>
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-02-12 22:49:48 -06:00
|
|
|
<script lang="ts">
|
2020-10-17 06:12:00 -05:00
|
|
|
import { defineComponent } from 'vue';
|
2018-02-09 03:28:06 -06:00
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
export default defineComponent({
|
2018-02-12 22:49:48 -06:00
|
|
|
props: {
|
|
|
|
time: {
|
|
|
|
type: [Date, String],
|
|
|
|
required: true
|
2018-02-04 23:25:19 -06:00
|
|
|
},
|
2018-02-12 22:49:48 -06: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 23:25:19 -06:00
|
|
|
},
|
2018-02-12 22:49:48 -06:00
|
|
|
absolute(): string {
|
2018-12-18 10:10:53 -06:00
|
|
|
return this._time.toLocaleString();
|
2018-02-04 23:25:19 -06:00
|
|
|
},
|
2018-02-12 22:49:48 -06:00
|
|
|
relative(): string {
|
|
|
|
const time = this._time;
|
|
|
|
const ago = (this.now.getTime() - time.getTime()) / 1000/*ms*/;
|
|
|
|
return (
|
2020-01-29 13:37:25 -06:00
|
|
|
ago >= 31536000 ? this.$t('_ago.yearsAgo', { n: (~~(ago / 31536000)).toString() }) :
|
|
|
|
ago >= 2592000 ? this.$t('_ago.monthsAgo', { n: (~~(ago / 2592000)).toString() }) :
|
|
|
|
ago >= 604800 ? this.$t('_ago.weeksAgo', { n: (~~(ago / 604800)).toString() }) :
|
|
|
|
ago >= 86400 ? this.$t('_ago.daysAgo', { n: (~~(ago / 86400)).toString() }) :
|
|
|
|
ago >= 3600 ? this.$t('_ago.hoursAgo', { n: (~~(ago / 3600)).toString() }) :
|
|
|
|
ago >= 60 ? this.$t('_ago.minutesAgo', { n: (~~(ago / 60)).toString() }) :
|
|
|
|
ago >= 10 ? this.$t('_ago.secondsAgo', { n: (~~(ago % 60)).toString() }) :
|
|
|
|
ago >= -1 ? this.$t('_ago.justNow') :
|
|
|
|
ago < -1 ? this.$t('_ago.future') :
|
2020-02-11 15:27:11 -06:00
|
|
|
this.$t('_ago.unknown'));
|
2018-02-12 22:49:48 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
if (this.mode == 'relative' || this.mode == 'detail') {
|
2018-06-09 18:03:02 -05:00
|
|
|
this.tickId = window.requestAnimationFrame(this.tick);
|
2018-02-12 22:49:48 -06:00
|
|
|
}
|
|
|
|
},
|
2020-10-17 06:12:00 -05:00
|
|
|
unmounted() {
|
2018-02-12 22:49:48 -06:00
|
|
|
if (this.mode === 'relative' || this.mode === 'detail') {
|
2018-06-09 18:03:02 -05:00
|
|
|
window.clearTimeout(this.tickId);
|
2018-02-12 22:49:48 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
tick() {
|
2020-07-14 10:21:14 -05:00
|
|
|
// TODO: パフォーマンス向上のため、このコンポーネントが画面内に表示されている場合のみ更新する
|
2018-02-12 22:49:48 -06:00
|
|
|
this.now = new Date();
|
2018-06-09 18:03:02 -05:00
|
|
|
|
|
|
|
this.tickId = setTimeout(() => {
|
|
|
|
window.requestAnimationFrame(this.tick);
|
|
|
|
}, 10000);
|
2018-02-04 23:25:19 -06:00
|
|
|
}
|
2018-02-12 22:49:48 -06:00
|
|
|
}
|
|
|
|
});
|
2018-02-04 23:25:19 -06:00
|
|
|
</script>
|