logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

timeago.vue (1877B)


  1. <template>
  2. <time
  3. :datetime="time"
  4. :title="localeDateString"
  5. >
  6. {{ relativeTimeString }}
  7. </time>
  8. </template>
  9. <script>
  10. import * as DateUtils from 'src/services/date_utils/date_utils.js'
  11. import localeService from 'src/services/locale/locale.service.js'
  12. export default {
  13. name: 'Timeago',
  14. props: ['time', 'autoUpdate', 'longFormat', 'nowThreshold', 'templateKey'],
  15. data () {
  16. return {
  17. relativeTime: { key: 'time.now', num: 0 },
  18. interval: null
  19. }
  20. },
  21. computed: {
  22. localeDateString () {
  23. const browserLocale = localeService.internalToBrowserLocale(this.$i18n.locale)
  24. return typeof this.time === 'string'
  25. ? new Date(Date.parse(this.time)).toLocaleString(browserLocale)
  26. : this.time.toLocaleString(browserLocale)
  27. },
  28. relativeTimeString () {
  29. const timeString = this.$i18n.tc(this.relativeTime.key, this.relativeTime.num, [this.relativeTime.num])
  30. if (typeof this.templateKey === 'string' && this.relativeTime.key !== 'time.now') {
  31. return this.$i18n.t(this.templateKey, [timeString])
  32. }
  33. return timeString
  34. }
  35. },
  36. watch: {
  37. time (newVal, oldVal) {
  38. if (oldVal !== newVal) {
  39. clearTimeout(this.interval)
  40. this.refreshRelativeTimeObject()
  41. }
  42. }
  43. },
  44. created () {
  45. this.refreshRelativeTimeObject()
  46. },
  47. unmounted () {
  48. clearTimeout(this.interval)
  49. },
  50. methods: {
  51. refreshRelativeTimeObject () {
  52. const nowThreshold = typeof this.nowThreshold === 'number' ? this.nowThreshold : 1
  53. this.relativeTime = this.longFormat
  54. ? DateUtils.relativeTime(this.time, nowThreshold)
  55. : DateUtils.relativeTimeShort(this.time, nowThreshold)
  56. if (this.autoUpdate) {
  57. this.interval = setTimeout(
  58. this.refreshRelativeTimeObject,
  59. 1000 * this.autoUpdate
  60. )
  61. }
  62. }
  63. }
  64. }
  65. </script>