All files / src/components SpaceDataItem.vue

100% Statements 31/31
100% Branches 21/21
100% Functions 9/9
100% Lines 24/24

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  29x 41x 42x             7x     7x                                                   29x                     71x     29x 59x 45x 44x 33x     29x 29x 29x 59x   42x 59x 59x 59x 42x   29x 12x 41x                                                                                                              
<template>
  <div class="space-data-item" :class="{ 'space-data-item-compact': compact }">
    <div v-for="row in rows" :key="row.key" class="space-data-row">
      <div
        v-for="cell in row.cells"
        :key="cell.field.fieldId"
        class="space-data-cell"
        :style="{ gridColumn: `${cell.start} / span ${cell.span}` }"
        :data-testid="`space-data-${cell.field.fieldId}`"
      >
        <div class="space-data-label">
          {{ dataFieldLabel(cell.field, lang, fallbackLang) }}
        </div>
        <div class="space-data-value">
          <SpaceDataValue
            :field="cell.field"
            :value="item.values[cell.field.fieldId]"
            :lang="lang"
            :fallback-lang="fallbackLang"
            :max-images="compact ? 3 : undefined"
          />
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-SPACE-011.1
/**
 * An item shown by a data block, laid out like its form: each row of the
 * form is a 12-column grid, each field at its column and width. Fields
 * without a value are left out. `compact` (an item list's card) truncates
 * long texts and image sets.
 */
import { computed } from 'vue'
import SpaceDataValue from '@/components/SpaceDataValue.vue'
import { dataFieldLabel, type DataField, type DataItem } from '@/utils/spacePages'
 
const props = withDefaults(
  defineProps<{
    fields: DataField[]
    item: DataItem
    lang: string
    fallbackLang?: string
    compact?: boolean
  }>(),
  { fallbackLang: undefined, compact: false }
)
 
const isBlank = (value: unknown) => value === undefined || value === null || value === ''
 
/** No value, an empty list, or a text with no translation (`{}`, `{ en: '' }`) */
const isEmpty = (value: unknown): boolean => {
  if (isBlank(value)) return true
  if (Array.isArray(value)) return !value.length
  if (typeof value === 'object') return Object.values(value as object).every(isBlank)
  return false
}
 
const rows = computed(() => {
  const byRow = new Map<number, { field: DataField; start: number; span: number }[]>()
  props.fields.forEach((field, index) => {
    if (isEmpty(props.item.values[field.fieldId])) return
    // A form without layout puts each field on its own row
    const y = field.y ?? index
    const start = Math.min(Math.max((field.x ?? 0) + 1, 1), 12)
    const span = Math.min(Math.max(field.w ?? 12, 1), 13 - start)
    if (!byRow.has(y)) byRow.set(y, [])
    byRow.get(y)!.push({ field, start, span })
  })
  return [...byRow.entries()]
    .sort(([a], [b]) => a - b)
    .map(([y, cells]) => ({ key: y, cells: cells.sort((a, b) => a.start - b.start) }))
})
</script>
 
<style scoped>
.space-data-item {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: 100%;
}
 
.space-data-row {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 0.75rem;
}
 
/* Like the app's item cards: a small muted label, the value in a tinted box */
.space-data-label {
  font-size: 0.875rem;
  opacity: 0.65;
  margin-bottom: 0.25rem;
}
 
.space-data-value {
  padding: 0.5rem;
  min-height: 38px;
  background: color-mix(in srgb, var(--space-text) 5%, transparent);
  border-radius: 0.25rem;
  overflow-wrap: anywhere;
}
 
/* Images are shown as they are, not in a box */
.space-data-value:has(img) {
  padding: 0;
  min-height: unset;
  background: transparent;
}
 
.space-data-item-compact :deep(.space-data-long) {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
 
/* On a phone, every field takes the whole width */
@media (max-width: 575.98px) {
  .space-data-cell {
    grid-column: 1 / -1 !important;
  }
}
</style>