akkoma-fe/src/components/timeago/timeago.vue

54 lines
1.4 KiB
Vue
Raw Normal View History

2019-06-18 20:28:31 +00:00
<template>
2019-07-05 07:17:44 +00:00
<time
:datetime="time"
:title="localeDateString"
>
{{ $tc(relativeTime.key, relativeTime.num, [relativeTime.num]) }}
2019-06-18 20:28:31 +00:00
</time>
</template>
<script>
import * as DateUtils from 'src/services/date_utils/date_utils.js'
import localeService from 'src/services/locale/locale.service.js'
2019-06-18 20:28:31 +00:00
export default {
name: 'Timeago',
props: ['time', 'autoUpdate', 'longFormat', 'nowThreshold'],
data () {
return {
relativeTime: { key: 'time.now', num: 0 },
interval: null
}
},
computed: {
localeDateString () {
const browserLocale = localeService.internalToBrowserLocale(this.$i18n.locale)
2019-06-18 20:28:31 +00:00
return typeof this.time === 'string'
? new Date(Date.parse(this.time)).toLocaleString(browserLocale)
: this.time.toLocaleString(browserLocale)
2019-06-18 20:28:31 +00:00
}
},
2019-07-05 07:17:44 +00:00
created () {
this.refreshRelativeTimeObject()
},
2021-04-25 10:44:50 +00:00
unmounted () {
2019-07-05 07:17:44 +00:00
clearTimeout(this.interval)
},
2019-06-18 20:28:31 +00:00
methods: {
refreshRelativeTimeObject () {
const nowThreshold = typeof this.nowThreshold === 'number' ? this.nowThreshold : 1
this.relativeTime = this.longFormat
? DateUtils.relativeTime(this.time, nowThreshold)
: DateUtils.relativeTimeShort(this.time, nowThreshold)
2022-07-19 10:01:24 +00:00
console.log({ ...this.relativeTime })
2019-06-18 20:28:31 +00:00
if (this.autoUpdate) {
this.interval = setTimeout(
this.refreshRelativeTimeObject,
1000 * this.autoUpdate
)
}
}
}
}
2019-07-05 07:17:44 +00:00
</script>