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 (3401B)


  1. <template>
  2. <time
  3. :datetime="time"
  4. :title="localeDateString"
  5. >
  6. {{ relativeOrAbsoluteTimeString }}
  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. relativeTimeMs: 0,
  18. relativeTime: { key: 'time.now', num: 0 },
  19. interval: null
  20. }
  21. },
  22. computed: {
  23. shouldUseAbsoluteTimeFormat () {
  24. if (!this.$store.getters.mergedConfig.useAbsoluteTimeFormat) {
  25. return false
  26. }
  27. return DateUtils.durationStrToMs(this.$store.getters.mergedConfig.absoluteTimeFormatMinAge) <= this.relativeTimeMs
  28. },
  29. browserLocale () {
  30. return localeService.internalToBrowserLocale(this.$i18n.locale)
  31. },
  32. timeAsDate () {
  33. return typeof this.time === 'string'
  34. ? new Date(Date.parse(this.time))
  35. : this.time
  36. },
  37. localeDateString () {
  38. return this.timeAsDate.toLocaleString(this.browserLocale)
  39. },
  40. relativeTimeString () {
  41. const timeString = this.$i18n.t(this.relativeTime.key, [this.relativeTime.num], this.relativeTime.num)
  42. if (typeof this.templateKey === 'string' && this.relativeTime.key !== 'time.now') {
  43. return this.$i18n.t(this.templateKey, [timeString])
  44. }
  45. return timeString
  46. },
  47. absoluteTimeString () {
  48. if (this.longFormat) {
  49. return this.localeDateString
  50. }
  51. const now = new Date()
  52. const formatter = (() => {
  53. if (DateUtils.isSameDay(this.timeAsDate, now)) {
  54. return new Intl.DateTimeFormat(this.browserLocale, {
  55. minute: 'numeric',
  56. hour: 'numeric'
  57. })
  58. } else if (DateUtils.isSameMonth(this.timeAsDate, now)) {
  59. return new Intl.DateTimeFormat(this.browserLocale, {
  60. month: 'short',
  61. day: 'numeric'
  62. })
  63. } else if (DateUtils.isSameYear(this.timeAsDate, now)) {
  64. return new Intl.DateTimeFormat(this.browserLocale, {
  65. month: 'short',
  66. day: 'numeric'
  67. })
  68. } else {
  69. return new Intl.DateTimeFormat(this.browserLocale, {
  70. year: 'numeric',
  71. month: 'short'
  72. })
  73. }
  74. })()
  75. return formatter.format(this.timeAsDate)
  76. },
  77. relativeOrAbsoluteTimeString () {
  78. return this.shouldUseAbsoluteTimeFormat ? this.absoluteTimeString : this.relativeTimeString
  79. }
  80. },
  81. watch: {
  82. time (newVal, oldVal) {
  83. if (oldVal !== newVal) {
  84. clearTimeout(this.interval)
  85. this.refreshRelativeTimeObject()
  86. }
  87. }
  88. },
  89. created () {
  90. this.refreshRelativeTimeObject()
  91. },
  92. unmounted () {
  93. clearTimeout(this.interval)
  94. },
  95. methods: {
  96. refreshRelativeTimeObject () {
  97. const nowThreshold = typeof this.nowThreshold === 'number' ? this.nowThreshold : 1
  98. this.relativeTimeMs = DateUtils.relativeTimeMs(this.time)
  99. this.relativeTime = this.longFormat
  100. ? DateUtils.relativeTime(this.time, nowThreshold)
  101. : DateUtils.relativeTimeShort(this.time, nowThreshold)
  102. if (this.autoUpdate) {
  103. this.interval = setTimeout(
  104. this.refreshRelativeTimeObject,
  105. 1000 * this.autoUpdate
  106. )
  107. }
  108. }
  109. }
  110. }
  111. </script>