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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | 149x 2x 2x 2x 268x 2x 1x 289x 2x 2x 1x 2x 5x 2x 2x 2x 2x 2x 2x 2x 1x 2x 404x 1x 2x 2x 2x 2x 2x 134x 2x 2x 50x 1x 449x 1x 60x 3x 1x 2x 1x 2x 2x 2x 2x 2x 1x 2x 23x 23x 23x 23x 3107x 23x 23x 23x 1603x 145x 59x 23x 636x 23x 578x 23x 528x 578x 23x 58x 23x 58x 58x 58x 58x 645x 116x 23x 60x 59x 60x 656x 118x 50x 26x 4x 4x 4x 2x 2x 1x 2x 2x 2x 2x 5x 5x 5x 5x 2x 2x 5x 2x 5x 5x 4x 4x 11x 4x 4x 3x 1x 4x 1x 1x 23x 11x 5x 3x 3x 3x 421x 85x 85x 8x 77x 44x 80x 33x 16x 25x 10x 10x 15x 23x 2x 23x 23x 23x | <template>
<div class="space-data-block-editor" :data-testid="`data-block-editor-${block.type}`">
<div v-if="loadError" class="alert alert-danger py-2 small">
{{ t('spacePages.data.loadError') }}
</div>
<div
v-if="block.unavailable"
class="alert alert-warning py-2 small"
data-testid="data-block-unavailable"
>
{{ t('spacePages.data.unavailableHint') }}
</div>
<div class="row g-2">
<div class="col-sm-6">
<label :for="id('entity')" class="form-label small">{{
t('spacePages.data.entity')
}}</label>
<select
:id="id('entity')"
:value="block.entityId"
class="form-select form-select-sm"
:data-testid="id('entity')"
@change="setEntity(selectValue($event))"
>
<option value="" disabled>{{ t('spacePages.data.chooseEntity') }}</option>
<option v-for="choice in entities" :key="choice.id" :value="choice.id">
{{ nameOf(choice.name) }}
</option>
</select>
</div>
<div class="col-sm-6">
<label :for="id('form')" class="form-label small">{{ t('spacePages.data.form') }}</label>
<select
:id="id('form')"
:value="block.formId"
class="form-select form-select-sm"
:disabled="!entity"
:data-testid="id('form')"
@change="update({ formId: selectValue($event) })"
>
<option value="" disabled>{{ t('spacePages.data.chooseForm') }}</option>
<option v-for="form in entity?.forms || []" :key="form.id" :value="form.id">
{{ nameOf(form.name) }}
</option>
</select>
</div>
</div>
<div v-if="hiddenFieldNames.length" class="form-text" data-testid="data-block-hidden-fields">
{{ t('spacePages.data.hiddenFields').replace('{fields}', hiddenFieldNames.join(', ')) }}
</div>
<div v-if="block.type !== 'itemForm'" class="form-text">
{{ t('spacePages.data.frozenHint') }}
</div>
<!-- Item list -->
<template v-if="block.type === 'itemList' && entity">
<div class="row g-2 mt-1">
<div class="col-sm-6">
<label :for="id('title')" class="form-label small">{{
t('spacePages.fields.title')
}}</label>
<input
:id="id('title')"
:value="block.title?.[lang] || ''"
type="text"
class="form-control form-control-sm"
maxlength="120"
@input="update({ title: withLang(block.title, inputValue($event)) })"
/>
</div>
<div class="col-6 col-sm-3">
<label :for="id('layout')" class="form-label small">{{
t('spacePages.data.layout')
}}</label>
<select
:id="id('layout')"
:value="block.layout"
class="form-select form-select-sm"
:data-testid="id('layout')"
@change="setLayout(selectValue($event))"
>
<option value="cards">{{ t('spacePages.data.layoutCards') }}</option>
<option value="list">{{ t('spacePages.data.layoutList') }}</option>
<option value="agenda">{{ t('spacePages.data.layoutAgenda') }}</option>
<option value="map" :disabled="!mapPlaceField && block.layout !== 'map'">
{{
mapPlaceField || block.layout === 'map'
? t('spacePages.data.layoutMap')
: t('spacePages.data.layoutMapNeedsPlace')
}}
</option>
</select>
</div>
<!-- UC-SPACE-022.1: the map takes the first place field of the form -->
<div
v-if="block.layout === 'map'"
class="col-12 form-text mt-0"
:class="{ 'text-danger': !mapPlaceField }"
:data-testid="id('map-hint')"
>
{{
mapPlaceField
? t('spacePages.data.mapHint').replace('{field}', nameOf(mapPlaceField.name))
: t('spacePages.data.mapNeedsPlace')
}}
</div>
<div class="col-6 col-sm-3">
<label :for="id('limit')" class="form-label small">{{
t('spacePages.data.limit')
}}</label>
<input
:id="id('limit')"
:value="block.limit"
type="number"
min="1"
:max="LIST_MAX_ITEMS"
class="form-control form-control-sm"
:data-testid="id('limit')"
@change="setLimit(inputValue($event))"
/>
</div>
<div class="col-sm-6">
<label :for="id('sort')" class="form-label small">{{ t('spacePages.data.sort') }}</label>
<div class="input-group input-group-sm">
<select
:id="id('sort')"
:value="block.sort?.fieldId || 'createdAt'"
class="form-select"
:data-testid="id('sort')"
@change="setSort(selectValue($event), block.sort?.direction || 'asc')"
>
<option value="createdAt">{{ t('spacePages.data.createdAt') }}</option>
<option v-for="field in listSortFields" :key="field.fieldId" :value="field.fieldId">
{{ nameOf(field.name) }}
</option>
</select>
<select
:value="block.sort?.direction || 'desc'"
class="form-select"
:aria-label="t('spacePages.data.direction')"
:data-testid="id('direction')"
@change="
setSort(block.sort?.fieldId || 'createdAt', selectValue($event) as 'asc' | 'desc')
"
>
<option value="asc">{{ t('spacePages.data.ascending') }}</option>
<option value="desc">{{ t('spacePages.data.descending') }}</option>
</select>
</div>
<div v-if="block.layout === 'agenda'" class="form-text" :data-testid="id('agenda-hint')">
{{ t('spacePages.data.agendaHint') }}
</div>
</div>
<div class="col-sm-6">
<label :for="id('upcoming')" class="form-label small">{{
t('spacePages.data.upcoming')
}}</label>
<select
:id="id('upcoming')"
:value="block.upcomingFieldId || ''"
class="form-select form-select-sm"
:data-testid="id('upcoming')"
@change="update({ upcomingFieldId: selectValue($event) || undefined })"
>
<option value="">{{ t('spacePages.data.allDates') }}</option>
<option v-for="field in dateFields" :key="field.fieldId" :value="field.fieldId">
{{ t('spacePages.data.upcomingOn').replace('{field}', nameOf(field.name)) }}
</option>
</select>
</div>
</div>
<div class="mt-2">
<div class="small text-muted mb-1">{{ t('spacePages.data.filters') }}</div>
<div
v-for="(filter, index) in block.filters"
:key="index"
class="d-flex gap-2 mb-2"
:data-testid="`data-filter-${index}`"
>
<select
:value="filter.fieldId"
class="form-select form-select-sm"
:aria-label="t('spacePages.data.filterField')"
@change="setFilter(index, { fieldId: selectValue($event), values: [] })"
>
<option v-for="field in filterFields" :key="field.fieldId" :value="field.fieldId">
{{ nameOf(field.name) }}
</option>
</select>
<select
v-if="valueChoices(filter.fieldId)"
:value="String(filter.values[0] ?? '')"
class="form-select form-select-sm"
:aria-label="t('spacePages.data.filterValue')"
:data-testid="`data-filter-value-${index}`"
@change="setFilterValue(index, selectValue($event))"
>
<option value="" disabled>{{ t('spacePages.data.chooseValue') }}</option>
<option
v-for="choice in valueChoices(filter.fieldId)"
:key="choice.value"
:value="choice.value"
>
{{ choice.label }}
</option>
</select>
<input
v-else
:value="filter.values[0] ?? ''"
type="text"
class="form-control form-control-sm"
:aria-label="t('spacePages.data.filterValue')"
:data-testid="`data-filter-value-${index}`"
@change="setFilterValue(index, inputValue($event))"
/>
<button
type="button"
class="btn btn-sm btn-outline-danger"
:title="t('spacePages.data.removeFilter')"
@click="update({ filters: block.filters.filter((_, i) => i !== index) })"
>
<font-awesome-icon icon="fa-solid fa-trash" />
</button>
</div>
<button
type="button"
class="btn btn-sm btn-outline-secondary"
:disabled="block.filters.length >= LIST_MAX_FILTERS || filterFields.length === 0"
data-testid="data-add-filter"
@click="addFilter"
>
<font-awesome-icon icon="fa-solid fa-plus" class="me-1" />{{
t('spacePages.data.addFilter')
}}
</button>
</div>
</template>
<!-- Item card -->
<div v-else-if="block.type === 'itemCard' && entity" class="mt-2">
<label :for="id('item')" class="form-label small">{{ t('spacePages.data.item') }}</label>
<select
:id="id('item')"
:value="block.itemId"
class="form-select form-select-sm"
:data-testid="id('item')"
@change="update({ itemId: selectValue($event) })"
>
<option value="" disabled>{{ t('spacePages.data.chooseItem') }}</option>
<option v-for="choice in cardChoices" :key="choice.value" :value="choice.value">
{{ choice.label }}
</option>
</select>
</div>
<!-- Item form -->
<div v-else-if="block.type === 'itemForm'" class="row g-2 mt-1">
<div class="col-sm-6">
<label :for="id('label')" class="form-label small">{{
t('spacePages.fields.buttonLabel')
}}</label>
<input
:id="id('label')"
:value="block.label[lang] || ''"
type="text"
class="form-control form-control-sm"
maxlength="120"
@input="update({ label: withLang(block.label, inputValue($event)) })"
/>
</div>
<div class="col-sm-6">
<label :for="id('text')" class="form-label small">{{ t('spacePages.fields.text') }}</label>
<input
:id="id('text')"
:value="block.text?.[lang] || ''"
type="text"
class="form-control form-control-sm"
maxlength="2000"
@input="update({ text: withLang(block.text, inputValue($event)) })"
/>
</div>
<div class="col-12 form-text">{{ t('spacePages.data.itemFormHint') }}</div>
</div>
</div>
</template>
<script setup lang="ts">
// @implements UC-SPACE-011.1, UC-SPACE-014.1
/**
* The settings of an item list, an item card or an item form block: an entity
* of the flow and one of its forms, then the list's filters, sort and limit,
* the card's item, or the form's button. Only the flow's own entities are
* offered; the API checks them again.
*/
import { computed, ref, watch } from 'vue'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { useUILanguage } from '@/composables/useUILanguage'
import { useSpaceEditorEntities, type ItemChoice } from '@/composables/useSpaceEditorEntities'
import type { EntityField } from '@/stores/entities'
import {
FILTER_FIELD_TYPES,
HIDDEN_FIELD_TYPES,
LIST_MAX_FILTERS,
LIST_MAX_ITEMS,
SORT_FIELD_TYPES,
localize,
type ItemCardBlock,
type ItemFormBlock,
type ItemListBlock,
type ListFilter,
type LocalizedText
} from '@/utils/spacePages'
type EntityBlock = ItemListBlock | ItemCardBlock | ItemFormBlock
const props = defineProps<{
block: EntityBlock
lang: string
flowId: string
}>()
const emit = defineEmits<{ update: [block: EntityBlock] }>()
const { t, uiLanguage } = useUILanguage()
const { entities, loadError, itemChoices } = useSpaceEditorEntities(props.flowId)
const id = (name: string) => `block-${props.block.id}-${name}`
const inputValue = (event: Event) => (event.target as HTMLInputElement).value
const selectValue = (event: Event) => (event.target as HTMLSelectElement).value
const nameOf = (name: string | Record<string, string> | undefined) =>
typeof name === 'string' ? name : localize(name, uiLanguage.value, props.lang) || ''
const entity = computed(() => entities.value.find((e) => e.id === props.block.entityId))
const fields = computed<EntityField[]>(() => entity.value?.fields || [])
const shownFields = computed(() =>
fields.value.filter((field) => !HIDDEN_FIELD_TYPES.includes(field.type || 'text'))
)
const filterFields = computed(() =>
shownFields.value.filter((field) => FILTER_FIELD_TYPES.includes(field.type || 'text'))
)
const sortFields = computed(() =>
shownFields.value.filter((field) => SORT_FIELD_TYPES.includes(field.type || 'text'))
)
const dateFields = computed(() => shownFields.value.filter((field) => field.type === 'date'))
/** An agenda groups by the date it is sorted on: then only dates (UC-SPACE-019.1) */
const listSortFields = computed(() =>
props.block.type === 'itemList' && props.block.layout === 'agenda'
? dateFields.value
: sortFields.value
)
/** The first place field of the chosen form: where a map puts the items (UC-SPACE-022.1) */
const mapPlaceField = computed(() => {
Iif (props.block.type !== 'itemList') return undefined
const form = entity.value?.forms?.find((f) => f.id === props.block.formId)
return [...(form?.fields || [])]
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
.map((placement) => fields.value.find((field) => field.fieldId === placement.fieldId))
.find((field) => field?.type === 'geo_location')
})
/** The fields of the chosen form that a page never shows */
const hiddenFieldNames = computed(() => {
if (props.block.type === 'itemForm') return []
const form = entity.value?.forms?.find((f) => f.id === props.block.formId)
return (form?.fields || [])
.map((placement) => fields.value.find((field) => field.fieldId === placement.fieldId))
.filter((field) => field && HIDDEN_FIELD_TYPES.includes(field.type || 'text'))
.map((field) => nameOf(field!.name))
})
function update(changes: Record<string, unknown>) {
emit('update', { ...props.block, ...changes } as EntityBlock)
}
function withLang(texts: LocalizedText | undefined, value: string): LocalizedText {
const { [props.lang]: _previous, ...others } = texts || {}
return value ? { ...others, [props.lang]: value } : others
}
/** A new entity: its first form, and nothing of the previous entity's settings */
function setEntity(entityId: string) {
const chosen = entities.value.find((e) => e.id === entityId)
const changes: Record<string, unknown> = { entityId, formId: chosen?.forms?.[0]?.id || '' }
if (props.block.type === 'itemList') {
Object.assign(changes, { filters: [], sort: undefined, upcomingFieldId: undefined })
}
if (props.block.type === 'itemCard') changes.itemId = ''
update(changes)
}
function setLimit(value: string) {
const limit = Math.min(Math.max(Math.round(Number(value)) || 1, 1), LIST_MAX_ITEMS)
update({ limit })
}
/**
* An agenda sorted on something else than a date is sorted on the first date
* field, soonest first (an event list), else on the creation date
*/
function setLayout(layout: string) {
const changes: Record<string, unknown> = { layout }
const sortId = props.block.type === 'itemList' ? props.block.sort?.fieldId : undefined
const sortsOnDate =
!sortId || sortId === 'createdAt' || dateFields.value.some((f) => f.fieldId === sortId)
if (layout === 'agenda' && !sortsOnDate) {
const first = dateFields.value[0]
changes.sort = first ? { fieldId: first.fieldId, direction: 'asc' } : undefined
}
update(changes)
}
function setSort(fieldId: string, direction: 'asc' | 'desc') {
update({ sort: { fieldId, direction } })
}
function setFilter(index: number, filter: ListFilter) {
Iif (props.block.type !== 'itemList') return
update({ filters: props.block.filters.map((f, i) => (i === index ? filter : f)) })
}
function setFilterValue(index: number, raw: string) {
Iif (props.block.type !== 'itemList') return
const filter = props.block.filters[index]
const field = fields.value.find((f) => f.fieldId === filter.fieldId)
let value: string | number | boolean = raw
if (field?.type === 'boolean') value = raw === 'true'
else if (field?.type === 'number' && raw.trim() !== '' && !Number.isNaN(Number(raw))) {
value = Number(raw)
}
setFilter(index, { ...filter, values: raw === '' ? [] : [value] })
}
function addFilter() {
Iif (props.block.type !== 'itemList' || !filterFields.value.length) return
update({
filters: [...props.block.filters, { fieldId: filterFields.value[0].fieldId!, values: [] }]
})
}
// Items of referenced entities, for entity reference filters, and of the card's entity
const referenceChoices = ref<Record<string, ItemChoice[]>>({})
async function loadChoices(entityId: string) {
if (referenceChoices.value[entityId]) return
const target = entities.value.find((e) => e.id === entityId)
Iif (!target) return
const choices = await itemChoices(target, props.lang)
referenceChoices.value = { ...referenceChoices.value, [entityId]: choices }
}
/** The values a filter field offers, or null for a free value */
function valueChoices(fieldId: string): ItemChoice[] | null {
const field = fields.value.find((f) => f.fieldId === fieldId)
Iif (!field) return null
if (field.type === 'boolean') {
return [
{ value: 'true', label: t('common.yes') },
{ value: 'false', label: t('common.no') }
]
}
if (field.type === 'select' || (field.type as string) === 'multiselect') {
return (field.options || []).map((option) =>
typeof option === 'string'
? { value: option, label: option }
: { value: option.value, label: option.label || option.value }
)
}
if (field.type === 'workflow') {
return (field.workflowDefinition?.states || []).map((state) => ({
value: state.id,
label: typeof state.label === 'string' ? state.label : nameOf(state.label)
}))
}
if (field.type === 'entity_ref' && field.reference?.entityId) {
void loadChoices(field.reference.entityId)
return referenceChoices.value[field.reference.entityId] || []
}
return null
}
const cardChoices = computed(() =>
props.block.type === 'itemCard' ? referenceChoices.value[props.block.entityId] || [] : []
)
watch(
() => (props.block.type === 'itemCard' && entity.value ? props.block.entityId : null),
(entityId) => {
if (entityId) void loadChoices(entityId)
},
{ immediate: true }
)
</script>
|