CommonSectionMenu.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <!-- Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed, useSlots } from 'vue'
  4. import type { RouteLocationRaw } from 'vue-router'
  5. import { useSessionStore } from '@shared/stores/session'
  6. import type { MenuItem } from './types'
  7. import CommonSectionMenuLink from './CommonSectionMenuLink.vue'
  8. export interface Props {
  9. actionLabel?: string
  10. actionLink?: RouteLocationRaw
  11. headerLabel?: string
  12. items?: MenuItem[]
  13. help?: string
  14. }
  15. const props = defineProps<Props>()
  16. const emit = defineEmits<{
  17. (e: 'action-click', event: MouseEvent): void
  18. }>()
  19. const clickOnAction = (event: MouseEvent) => {
  20. emit('action-click', event)
  21. }
  22. const session = useSessionStore()
  23. const itemsWithPermission = computed(() => {
  24. if (!props.items || !props.items.length) return null
  25. return props.items.filter((item) => {
  26. if (item.permission) {
  27. return session.hasPermission(item.permission)
  28. }
  29. return true
  30. })
  31. })
  32. const slots = useSlots()
  33. const hasHelp = computed(() => slots.help || props.help)
  34. const showLabel = computed(() => {
  35. if (!itemsWithPermission.value && !slots.default) return false
  36. return slots.header || props.headerLabel || props.actionLabel
  37. })
  38. </script>
  39. <template>
  40. <div v-if="showLabel" class="mb-2 flex flex-row justify-between">
  41. <div class="text-white/80 ltr:pl-3 rtl:pr-3">
  42. <slot name="header">{{ i18n.t(headerLabel) }}</slot>
  43. </div>
  44. <component
  45. :is="actionLink ? 'CommonLink' : 'div'"
  46. v-if="actionLabel"
  47. :link="actionLink"
  48. class="cursor-pointer text-blue ltr:pr-4 rtl:pl-4"
  49. @click="clickOnAction"
  50. >
  51. {{ i18n.t(actionLabel) }}
  52. </component>
  53. </div>
  54. <div
  55. v-if="itemsWithPermission || $slots.default"
  56. class="flex w-full flex-col rounded-xl bg-gray-500 px-3 py-1 text-base text-white"
  57. :class="{ 'mb-6': !hasHelp }"
  58. v-bind="$attrs"
  59. >
  60. <slot name="before-items" />
  61. <slot>
  62. <template v-for="(item, idx) in itemsWithPermission" :key="idx">
  63. <CommonSectionMenuLink
  64. v-if="item.type === 'link'"
  65. :label="item.label"
  66. :link="item.link"
  67. :icon="item.icon"
  68. :icon-bg="item.iconBg"
  69. :information="item.information"
  70. :label-placeholder="item.labelPlaceholder"
  71. @click="item.onClick?.($event)"
  72. />
  73. </template>
  74. </slot>
  75. </div>
  76. <div v-if="hasHelp" class="mb-4 pt-1 text-xs text-gray-100 ltr:pl-3 rtl:pr-3">
  77. <slot name="help">
  78. {{ $t(help) }}
  79. </slot>
  80. </div>
  81. </template>