All files / src/components EntityItemHeader.vue

95.65% Statements 22/23
96.55% Branches 28/29
90% Functions 9/10
95.23% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162  29x         2x 2x 42x 43x                       3x                                                     21x                                         21x   21x     261x       28x 5x       3x 1x     21x 29x 29x     21x 21x   21x                                                                                                                                    
<template>
  <div
    ref="rootEl"
    class="entity-item-header card"
    :style="{ marginLeft: `${marginLeft}px`, marginRight: `${marginRight}px` }"
  >
    <div class="card-body d-flex align-items-center gap-2">
      <div class="table-view-fields">
        <div v-for="row in visibleFieldRows" :key="row.key" class="item-fields-row">
          <div
            v-for="cell in row.cells"
            :key="cell.key"
            class="item-field-cell"
            :class="{
              'item-field-cell-sortable': isSortableField(cell.field),
              'item-field-cell-sort-disabled': isSortableField(cell.field) && !isSortable
            }"
            :style="{ gridColumn: `${cell.start} / span ${cell.span}` }"
            :role="isSortableField(cell.field) && isSortable ? 'button' : undefined"
            :tabindex="isSortableField(cell.field) && isSortable ? 0 : undefined"
            :title="isSortableField(cell.field) && !isSortable ? sortDisabledTooltip : undefined"
            @click="handleSortClick(cell.field)"
            @keydown.enter="handleSortClick(cell.field)"
          >
            {{ getTranslation(cell.field.name) }}
            <span v-if="cell.field.mandatory" class="text-danger">*</span>
            <font-awesome-icon
              v-if="isSortableField(cell.field)"
              :icon="sortIconFor(cell.field.fieldId!)"
              class="item-field-sort-icon"
            />
          </div>
        </div>
      </div>
      <div v-if="showActions" class="card-actions-column">{{ t('common.actions') }}</div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { computed, ref } from 'vue'
import type { EntityField } from '@/stores/entities'
import type { ItemSort } from '@/stores/items'
import { SORTABLE_FIELD_TYPES } from '@/utils/constants'
import { useEntityFieldLayout, type FormFieldPlacement } from '@/composables/useEntityFieldLayout'
import { useHeaderRowAlignment } from '@/composables/useHeaderRowAlignment'
import { useUILanguage } from '@/composables/useUILanguage'
 
const props = withDefaults(
  defineProps<{
    fields: EntityField[]
    layout?: FormFieldPlacement[]
    getTranslation: (value: string | Record<string, string> | null | undefined) => string
    scrollbarWidth?: number
    showActions?: boolean
    sort?: ItemSort | null
    isSortable?: boolean
    sortDisabledTooltip?: string
  }>(),
  {
    layout: () => [],
    scrollbarWidth: 0,
    showActions: true,
    sort: null,
    isSortable: true,
    sortDisabledTooltip: ''
  }
)
 
const emit = defineEmits<{ sort: [fieldId: string] }>()
 
const { t } = useUILanguage()
 
function isSortableField(field: EntityField) {
  return SORTABLE_FIELD_TYPES.includes(field.type as (typeof SORTABLE_FIELD_TYPES)[number])
}
 
function sortIconFor(fieldId: string) {
  if (props.sort?.fieldId !== fieldId) return 'fa-solid fa-sort'
  return props.sort.direction === 'asc' ? 'fa-solid fa-sort-up' : 'fa-solid fa-sort-down'
}
 
function handleSortClick(field: EntityField) {
  if (!isSortableField(field) || !props.isSortable) return
  emit('sort', field.fieldId!)
}
 
const { visibleFieldRows } = useEntityFieldLayout(
  () => props.fields,
  () => props.layout ?? []
)
 
const rootEl = ref<HTMLElement | null>(null)
const { marginLeft, marginRight } = useHeaderRowAlignment(
  rootEl,
  computed(() => props.scrollbarWidth)
)
</script>
 
<style scoped>
.entity-item-header {
  border-radius: 0.375rem 0.375rem 0 0;
}
 
.entity-item-header .card-body {
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}
 
.item-fields-row {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 0.75rem;
}
 
.item-field-cell {
  min-width: 0;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--bw-text-muted);
}
 
.item-field-cell-sortable {
  cursor: pointer;
  user-select: none;
}
 
.item-field-cell-sortable:hover {
  color: var(--bw-text);
}
 
.item-field-cell-sort-disabled {
  cursor: not-allowed;
}
 
.item-field-cell-sort-disabled:hover {
  color: var(--bw-text-muted);
}
 
.item-field-sort-icon {
  margin-left: 0.25rem;
  font-size: 0.75em;
}
 
.table-view-fields {
  flex: 1 1 auto;
  min-width: 0;
}
 
.card-actions-column {
  flex: 0 0 9rem;
  text-align: right;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--bw-text-muted);
}
</style>