useDateTime.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, type Ref } from 'vue'
  3. import { EnumTextDirection } from '#shared/graphql/types.ts'
  4. import { i18n } from '#shared/i18n.ts'
  5. import { useLocaleStore } from '#shared/stores/locale.ts'
  6. import type { DateTimeContext } from './types.ts'
  7. export const useDateTime = (context: Ref<DateTimeContext>) => {
  8. const timePicker = computed(() => context.value.type === 'datetime')
  9. const valueFormat = computed(() => {
  10. if (timePicker.value) return 'iso'
  11. return 'yyyy-MM-dd'
  12. })
  13. const localeStore = useLocaleStore()
  14. const position = computed(() =>
  15. localeStore.localeData?.dir === EnumTextDirection.Rtl ? 'right' : 'left',
  16. )
  17. const minDate = computed(() => {
  18. if (context.value.futureOnly) return new Date()
  19. return context.value.minDate
  20. })
  21. const displayFormat = computed(() => {
  22. let result = i18n.getDateFormat()
  23. if (timePicker.value) result = i18n.getDateTimeFormat()
  24. result = result.replace(/m/g, '{m}')
  25. result = result.replace(/M/g, 'm')
  26. result = result.replace(/\{m\}/g, 'M')
  27. result = result.replace(/l/g, 'h')
  28. result = result.replace(/P/g, 'aaa')
  29. return result
  30. })
  31. const is24 = computed(() => i18n.getTimeFormatType() === '24hour')
  32. const ariaLabels = computed(() => ({
  33. toggleOverlay: i18n.t('Toggle the overlay'),
  34. menu: i18n.t('Datepicker menu'),
  35. input: i18n.t('Datepicker input field'),
  36. openTimePicker: i18n.t('Open the time picker'),
  37. closeTimePicker: i18n.t('Close the time picker'),
  38. incrementValue: (type: string) => {
  39. switch (type) {
  40. case 'hours':
  41. return i18n.t('Increment hours value')
  42. case 'minutes':
  43. return i18n.t('Increment minutes value')
  44. case 'seconds':
  45. default:
  46. return i18n.t('Increment seconds value')
  47. }
  48. },
  49. decrementValue: (type: string) => {
  50. switch (type) {
  51. case 'hours':
  52. return i18n.t('Decrement hours value')
  53. case 'minutes':
  54. return i18n.t('Decrement minutes value')
  55. case 'seconds':
  56. default:
  57. return i18n.t('Decrement seconds value')
  58. }
  59. },
  60. openTpOverlay: (type: string) => {
  61. switch (type) {
  62. case 'hours':
  63. return i18n.t('Open the hours overlay')
  64. case 'minutes':
  65. return i18n.t('Open the minutes overlay')
  66. case 'seconds':
  67. default:
  68. return i18n.t('Open the seconds overlay')
  69. }
  70. },
  71. amPmButton: i18n.t('Toggle AM/PM mode'),
  72. openYearsOverlay: i18n.t('Open the years overlay'),
  73. openMonthsOverlay: i18n.t('Open the months overlay'),
  74. nextMonth: i18n.t('Next month'),
  75. prevMonth: i18n.t('Previous month'),
  76. nextYear: i18n.t('Next year'),
  77. prevYear: i18n.t('Previous year'),
  78. clearInput: i18n.t('Clear the value'),
  79. calendarIcon: i18n.t('The calendar icon'),
  80. timePicker: i18n.t('The time picker'),
  81. monthPicker: (overlay: boolean) =>
  82. overlay ? i18n.t('The month picker overlay') : i18n.t('The month picker'),
  83. yearPicker: (overlay: boolean) =>
  84. overlay ? i18n.t('The year picker overlay') : i18n.t('The year picker'),
  85. timeOverlay: (type: 'hours' | 'minutes' | 'seconds') => {
  86. switch (type) {
  87. case 'hours':
  88. return i18n.t('The hours overlay')
  89. case 'minutes':
  90. return i18n.t('The minutes overlay')
  91. case 'seconds':
  92. default:
  93. return i18n.t('The seconds overlay')
  94. }
  95. },
  96. }))
  97. return {
  98. ariaLabels,
  99. displayFormat,
  100. is24,
  101. localeStore,
  102. minDate,
  103. position,
  104. timePicker,
  105. valueFormat,
  106. }
  107. }