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


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