aboutsummaryrefslogtreecommitdiff
path: root/src/components/timeago/timeago.vue
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2019-06-18 20:28:31 +0000
committerShpuld Shpludson <shp@cock.li>2019-06-18 20:28:31 +0000
commit0eed2ccca8a0980c161bb5a52b211c507e0ffef5 (patch)
tree967230faa328d35db7459ad7126ccf5bf3e2ece1 /src/components/timeago/timeago.vue
parent69eff65130170c0cd8fffda45b952d3bec49c218 (diff)
Feature/polls attempt 2
Diffstat (limited to 'src/components/timeago/timeago.vue')
-rw-r--r--src/components/timeago/timeago.vue48
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