FieldSecurityInput.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed, toRef } from 'vue'
  4. import useValue from '#shared/components/Form/composables/useValue.ts'
  5. import { EnumSecurityStateType } from '#shared/components/Form/fields/FieldSecurity/types.ts'
  6. import type {
  7. FieldSecurityProps,
  8. SecurityOption,
  9. SecurityValue,
  10. } from '#shared/components/Form/fields/FieldSecurity/types.ts'
  11. import { useFieldSecurity } from '#shared/components/Form/fields/FieldSecurity/useFieldSecurity.ts'
  12. import { translateArticleSecurity } from '#shared/entities/ticket-article/composables/translateArticleSecurity.ts'
  13. import { i18n } from '#shared/i18n.ts'
  14. import CommonTabManager from '#desktop/components/CommonTabManager/CommonTabManager.vue'
  15. import type { Tab } from '#desktop/components/CommonTabManager/types.ts'
  16. const props = defineProps<FieldSecurityProps>()
  17. const contextReactive = toRef(props, 'context')
  18. const { localValue } = useValue<SecurityValue>(contextReactive)
  19. const {
  20. securityMethods,
  21. previewMethod,
  22. isCurrentSecurityOption,
  23. isSecurityOptionDisabled,
  24. changeSecurityState,
  25. } = useFieldSecurity(contextReactive, localValue)
  26. const securityMethodTabs = computed(() =>
  27. securityMethods.value.map((securityMethod) => ({
  28. key: securityMethod,
  29. label: translateArticleSecurity(securityMethod),
  30. })),
  31. )
  32. const getTooltipText = (option: SecurityOption) => {
  33. const { message, messagePlaceholder } =
  34. props.context.securityMessages?.[previewMethod.value]?.[option] || {}
  35. return i18n.t(message, ...(messagePlaceholder || []))
  36. }
  37. const optionTabs = computed(() => [
  38. {
  39. key: 'encryption' as SecurityOption,
  40. label: __('Encrypt'),
  41. icon: isCurrentSecurityOption('encryption')
  42. ? 'encryption-enabled'
  43. : 'encryption-disabled',
  44. tooltip: getTooltipText('encryption'),
  45. disabled: isSecurityOptionDisabled('encryption'),
  46. },
  47. {
  48. key: 'sign' as SecurityOption,
  49. label: __('Sign'),
  50. icon: isCurrentSecurityOption('sign') ? 'sign-enabled' : 'sign-disabled',
  51. tooltip: getTooltipText('sign'),
  52. disabled: isSecurityOptionDisabled('sign'),
  53. },
  54. ])
  55. const selectedOptionTabs = computed(() =>
  56. optionTabs.value
  57. .filter((optionTab) => isCurrentSecurityOption(optionTab.key))
  58. .map((optionTab) => optionTab.key),
  59. )
  60. const selectOption = (value: Tab['key'] | Tab['key'][]) => {
  61. const options = (value as SecurityOption[]).sort()
  62. localValue.value = {
  63. method: previewMethod.value,
  64. options,
  65. }
  66. }
  67. </script>
  68. <template>
  69. <div
  70. :id="context.id"
  71. :class="context.classes.input"
  72. class="flex h-auto flex-col gap-2"
  73. :aria-describedby="context.describedBy"
  74. v-bind="context.attrs"
  75. >
  76. <div class="flex gap-2">
  77. <CommonTabManager
  78. v-if="securityMethods.length > 1"
  79. :model-value="previewMethod"
  80. :tabs="securityMethodTabs"
  81. size="medium"
  82. @update:model-value="
  83. changeSecurityState($event as EnumSecurityStateType)
  84. "
  85. />
  86. <CommonTabManager
  87. :model-value="selectedOptionTabs"
  88. :tabs="optionTabs"
  89. size="medium"
  90. multiple
  91. @update:model-value="selectOption"
  92. />
  93. </div>
  94. </div>
  95. </template>