|
| 1 | +<template> |
| 2 | + <BlocWithTitleAndSubtitle |
| 3 | + v-model="bloc" |
| 4 | + :edit |
| 5 | + > |
| 6 | + <!-- Accordion items --> |
| 7 | + <AccordionGroup> |
| 8 | + <div ref="sortableRef"> |
| 9 | + <div |
| 10 | + v-for="(item, itemIndex) in bloc.items" |
| 11 | + :key="itemIndex" |
| 12 | + class="relative" |
| 13 | + :class="{ 'cursor-grab active:cursor-grabbing': edit }" |
| 14 | + > |
| 15 | + <!-- Delete button in edit mode --> |
| 16 | + <button |
| 17 | + v-if="edit" |
| 18 | + type="button" |
| 19 | + class="absolute -left-8 top-3 text-gray-500 hover:text-gray-700 z-10" |
| 20 | + :title="$t('Supprimer')" |
| 21 | + @click="bloc.items.splice(itemIndex, 1)" |
| 22 | + > |
| 23 | + <RiDeleteBinLine class="size-5" /> |
| 24 | + </button> |
| 25 | + |
| 26 | + <Accordion |
| 27 | + :id="`accordion-${bloc.id}-${itemIndex}`" |
| 28 | + :title="item.title || $t('Titre de l\'élément')" |
| 29 | + > |
| 30 | + <template |
| 31 | + v-if="edit" |
| 32 | + #title |
| 33 | + > |
| 34 | + <EditableText |
| 35 | + :model-value="item.title || $t('Titre de l\'élément')" |
| 36 | + tag="span" |
| 37 | + class="flex-1" |
| 38 | + @update:model-value="item.title = $event" |
| 39 | + /> |
| 40 | + </template> |
| 41 | + |
| 42 | + <div class="space-y-8"> |
| 43 | + <template |
| 44 | + v-for="(contentBloc, contentIndex) in item.content" |
| 45 | + :key="contentBloc.id" |
| 46 | + > |
| 47 | + <!-- Add button above each bloc --> |
| 48 | + <div |
| 49 | + v-if="edit" |
| 50 | + class="flex items-center justify-center" |
| 51 | + > |
| 52 | + <AddBlocDropdown |
| 53 | + content-only |
| 54 | + @new-bloc="item.content.splice(contentIndex, 0, $event as ContentBloc)" |
| 55 | + > |
| 56 | + <button |
| 57 | + type="button" |
| 58 | + class="text-gray-400 hover:text-gray-600 flex items-center gap-1 text-sm" |
| 59 | + > |
| 60 | + <RiAddLine class="size-4" /> |
| 61 | + {{ $t('Ajouter un bloc') }} |
| 62 | + </button> |
| 63 | + </AddBlocDropdown> |
| 64 | + </div> |
| 65 | + |
| 66 | + <div |
| 67 | + class="relative" |
| 68 | + data-testid="accordion-content-bloc" |
| 69 | + > |
| 70 | + <!-- Content bloc toolbar --> |
| 71 | + <div |
| 72 | + v-if="edit" |
| 73 | + class="absolute -left-14 top-1/2 -translate-y-1/2 flex flex-col gap-1" |
| 74 | + > |
| 75 | + <BrandedButton |
| 76 | + color="tertiary" |
| 77 | + size="xs" |
| 78 | + :icon="RiArrowUpLine" |
| 79 | + :disabled="contentIndex === 0" |
| 80 | + :title="$t('Monter')" |
| 81 | + @click="moveContent(item.content, contentIndex, -1)" |
| 82 | + /> |
| 83 | + <BrandedButton |
| 84 | + color="tertiary" |
| 85 | + size="xs" |
| 86 | + :icon="RiArrowDownLine" |
| 87 | + :disabled="contentIndex === item.content.length - 1" |
| 88 | + :title="$t('Descendre')" |
| 89 | + @click="moveContent(item.content, contentIndex, 1)" |
| 90 | + /> |
| 91 | + <BrandedButton |
| 92 | + color="tertiary" |
| 93 | + size="xs" |
| 94 | + :icon="RiDeleteBinLine" |
| 95 | + :title="$t('Supprimer')" |
| 96 | + @click="item.content.splice(contentIndex, 1)" |
| 97 | + /> |
| 98 | + </div> |
| 99 | + |
| 100 | + <DatasetsListBloc |
| 101 | + v-if="contentBloc.class === 'DatasetsListBloc'" |
| 102 | + v-model="(item.content[contentIndex] as DatasetsListBlocType)" |
| 103 | + :edit |
| 104 | + /> |
| 105 | + <DataservicesListBloc |
| 106 | + v-if="contentBloc.class === 'DataservicesListBloc'" |
| 107 | + v-model="(item.content[contentIndex] as DataservicesListBlocType)" |
| 108 | + :edit |
| 109 | + /> |
| 110 | + <ReusesListBloc |
| 111 | + v-if="contentBloc.class === 'ReusesListBloc'" |
| 112 | + v-model="(item.content[contentIndex] as ReusesListBlocType)" |
| 113 | + :edit |
| 114 | + /> |
| 115 | + <LinksListBloc |
| 116 | + v-if="contentBloc.class === 'LinksListBloc'" |
| 117 | + v-model="(item.content[contentIndex] as LinksListBlocType)" |
| 118 | + :edit |
| 119 | + /> |
| 120 | + </div> |
| 121 | + </template> |
| 122 | + |
| 123 | + <!-- Add button at the end (or when empty) --> |
| 124 | + <div |
| 125 | + v-if="edit" |
| 126 | + class="flex items-center justify-center" |
| 127 | + > |
| 128 | + <AddBlocDropdown |
| 129 | + content-only |
| 130 | + @new-bloc="item.content.push($event as ContentBloc)" |
| 131 | + > |
| 132 | + <button |
| 133 | + type="button" |
| 134 | + class="text-gray-400 hover:text-gray-600 flex items-center gap-1 text-sm" |
| 135 | + data-testid="accordion-add-content" |
| 136 | + > |
| 137 | + <RiAddLine class="size-4" /> |
| 138 | + {{ $t('Ajouter un bloc') }} |
| 139 | + </button> |
| 140 | + </AddBlocDropdown> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + </Accordion> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + |
| 147 | + <!-- New accordion placeholder in edit mode --> |
| 148 | + <div |
| 149 | + v-if="edit" |
| 150 | + data-testid="accordion-new-item" |
| 151 | + > |
| 152 | + <Accordion |
| 153 | + :id="`accordion-${bloc.id}-new-${newItemKey}`" |
| 154 | + :key="newItemKey" |
| 155 | + :title="$t('Titre de l\'élément')" |
| 156 | + > |
| 157 | + <template #title> |
| 158 | + <EditableText |
| 159 | + :model-value="newItemTitle" |
| 160 | + tag="span" |
| 161 | + class="flex-1" |
| 162 | + data-testid="accordion-new-item-title" |
| 163 | + :placeholder="$t('Titre de l\'élément')" |
| 164 | + @update:model-value="createNewItem($event)" |
| 165 | + /> |
| 166 | + </template> |
| 167 | + <div class="text-gray-400 text-sm"> |
| 168 | + {{ $t('Renseignez un titre pour créer un nouvel élément') }} |
| 169 | + </div> |
| 170 | + </Accordion> |
| 171 | + </div> |
| 172 | + </AccordionGroup> |
| 173 | + </BlocWithTitleAndSubtitle> |
| 174 | +</template> |
| 175 | + |
| 176 | +<script setup lang="ts"> |
| 177 | +import { BrandedButton } from '@datagouv/components-next' |
| 178 | +import { RiAddLine, RiArrowDownLine, RiArrowUpLine, RiDeleteBinLine } from '@remixicon/vue' |
| 179 | +import BlocWithTitleAndSubtitle from './BlocWithTitleAndSubtitle.vue' |
| 180 | +import EditableText from './EditableText.vue' |
| 181 | +import AddBlocDropdown from './AddBlocDropdown.vue' |
| 182 | +import DatasetsListBloc from './DatasetsListBloc.vue' |
| 183 | +import DataservicesListBloc from './DataservicesListBloc.vue' |
| 184 | +import ReusesListBloc from './ReusesListBloc.vue' |
| 185 | +import LinksListBloc from './LinksListBloc.vue' |
| 186 | +import type { |
| 187 | + AccordionListBloc, |
| 188 | + ContentBloc, |
| 189 | + DatasetsListBloc as DatasetsListBlocType, |
| 190 | + DataservicesListBloc as DataservicesListBlocType, |
| 191 | + ReusesListBloc as ReusesListBlocType, |
| 192 | + LinksListBloc as LinksListBlocType, |
| 193 | +} from '~/types/pages' |
| 194 | +
|
| 195 | +const props = defineProps<{ |
| 196 | + edit: boolean |
| 197 | +}>() |
| 198 | +
|
| 199 | +const bloc = defineModel<AccordionListBloc>({ required: true }) |
| 200 | +
|
| 201 | +const { sortableRef } = useBlocSortable( |
| 202 | + computed(() => bloc.value.items), |
| 203 | + toRef(props, 'edit'), |
| 204 | +) |
| 205 | +
|
| 206 | +const newItemTitle = ref('') |
| 207 | +const newItemKey = ref(0) |
| 208 | +
|
| 209 | +function createNewItem(title: string) { |
| 210 | + if (title.trim()) { |
| 211 | + bloc.value.items.push({ title, content: [] }) |
| 212 | + newItemTitle.value = '' |
| 213 | + newItemKey.value++ |
| 214 | + } |
| 215 | +} |
| 216 | +
|
| 217 | +function moveContent(content: Array<ContentBloc>, index: number, direction: -1 | 1) { |
| 218 | + const newIndex = index + direction |
| 219 | + if (newIndex < 0 || newIndex >= content.length) return |
| 220 | + const temp = content[index] |
| 221 | + content[index] = content[newIndex] |
| 222 | + content[newIndex] = temp |
| 223 | +} |
| 224 | +</script> |
0 commit comments