useDateTime.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. calendarWrap: i18n.t('Area for calendar month display'),
  37. calendarDays: i18n.t('Area for calendar days display'),
  38. openTimePicker: i18n.t('Open the time picker'),
  39. closeTimePicker: i18n.t('Close the time picker'),
  40. incrementValue: (type: string) => {
  41. switch (type) {
  42. case 'hours':
  43. return i18n.t('Increment hours value')
  44. case 'minutes':
  45. return i18n.t('Increment minutes value')
  46. case 'seconds':
  47. default:
  48. return i18n.t('Increment seconds value')
  49. }
  50. },
  51. decrementValue: (type: string) => {
  52. switch (type) {
  53. case 'hours':
  54. return i18n.t('Decrement hours value')
  55. case 'minutes':
  56. return i18n.t('Decrement minutes value')
  57. case 'seconds':
  58. default:
  59. return i18n.t('Decrement seconds value')
  60. }
  61. },
  62. openTpOverlay: (type: string) => {
  63. switch (type) {
  64. case 'hours':
  65. return i18n.t('Open the hours overlay')
  66. case 'minutes':
  67. return i18n.t('Open the minutes overlay')
  68. case 'seconds':
  69. default:
  70. return i18n.t('Open the seconds overlay')
  71. }
  72. },
  73. amPmButton: i18n.t('Toggle AM/PM mode'),
  74. openYearsOverlay: i18n.t('Open the years overlay'),
  75. openMonthsOverlay: i18n.t('Open the months overlay'),
  76. nextMonth: i18n.t('Next month'),
  77. prevMonth: i18n.t('Previous month'),
  78. nextYear: i18n.t('Next year'),
  79. prevYear: i18n.t('Previous year'),
  80. }))
  81. return {
  82. ariaLabels,
  83. displayFormat,
  84. is24,
  85. localeStore,
  86. minDate,
  87. position,
  88. timePicker,
  89. valueFormat,
  90. }
  91. }