diff options
| author | lain <lain@soykaf.club> | 2019-06-18 20:28:31 +0000 |
|---|---|---|
| committer | Shpuld Shpludson <shp@cock.li> | 2019-06-18 20:28:31 +0000 |
| commit | 0eed2ccca8a0980c161bb5a52b211c507e0ffef5 (patch) | |
| tree | 967230faa328d35db7459ad7126ccf5bf3e2ece1 /src/components/timeago | |
| parent | 69eff65130170c0cd8fffda45b952d3bec49c218 (diff) | |
Feature/polls attempt 2
Diffstat (limited to 'src/components/timeago')
| -rw-r--r-- | src/components/timeago/timeago.vue | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/components/timeago/timeago.vue b/src/components/timeago/timeago.vue new file mode 100644 index 00000000..3f185a2d --- /dev/null +++ b/src/components/timeago/timeago.vue @@ -0,0 +1,48 @@ +<template> + <time :datetime="time" :title="localeDateString"> + {{ $t(relativeTime.key, [relativeTime.num]) }} + </time> +</template> + +<script> +import * as DateUtils from 'src/services/date_utils/date_utils.js' + +export default { + name: 'Timeago', + props: ['time', 'autoUpdate', 'longFormat', 'nowThreshold'], + data () { + return { + relativeTime: { key: 'time.now', num: 0 }, + interval: null + } + }, + created () { + this.refreshRelativeTimeObject() + }, + destroyed () { + clearTimeout(this.interval) + }, + computed: { + localeDateString () { + return typeof this.time === 'string' + ? new Date(Date.parse(this.time)).toLocaleString() + : this.time.toLocaleString() + } + }, + 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) + + if (this.autoUpdate) { + this.interval = setTimeout( + this.refreshRelativeTimeObject, + 1000 * this.autoUpdate + ) + } + } + } +} +</script>
\ No newline at end of file |
