@mapomodule/uikit 2.0.0-beta.7 → 2.0.0-experimental.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/README.md +47 -174
  2. package/dist/__tests__/MapoConfirmDialog.component.test.d.ts +2 -0
  3. package/dist/__tests__/MapoConfirmDialog.component.test.d.ts.map +1 -0
  4. package/dist/__tests__/MapoDetail.component.test.d.ts +2 -0
  5. package/dist/__tests__/MapoDetail.component.test.d.ts.map +1 -0
  6. package/dist/__tests__/MapoDetailLangSwitch.component.test.d.ts +2 -0
  7. package/dist/__tests__/MapoDetailLangSwitch.component.test.d.ts.map +1 -0
  8. package/dist/__tests__/MapoListActions.component.test.d.ts +2 -0
  9. package/dist/__tests__/MapoListActions.component.test.d.ts.map +1 -0
  10. package/dist/__tests__/MapoListTable.component.test.d.ts +2 -0
  11. package/dist/__tests__/MapoListTable.component.test.d.ts.map +1 -0
  12. package/dist/__tests__/MapoSnackBar.component.test.d.ts +2 -0
  13. package/dist/__tests__/MapoSnackBar.component.test.d.ts.map +1 -0
  14. package/dist/__tests__/__mocks__/nuxt-app.d.ts +9 -0
  15. package/dist/__tests__/__mocks__/nuxt-app.d.ts.map +1 -0
  16. package/dist/__tests__/formValues.test.d.ts +2 -0
  17. package/dist/__tests__/formValues.test.d.ts.map +1 -0
  18. package/dist/__tests__/langInfo.test.d.ts +2 -0
  19. package/dist/__tests__/langInfo.test.d.ts.map +1 -0
  20. package/dist/__tests__/listTable.test.d.ts +2 -0
  21. package/dist/__tests__/listTable.test.d.ts.map +1 -0
  22. package/dist/__tests__/mediaLanguages.test.d.ts +2 -0
  23. package/dist/__tests__/mediaLanguages.test.d.ts.map +1 -0
  24. package/dist/__tests__/runtimePages.test.d.ts +2 -0
  25. package/dist/__tests__/runtimePages.test.d.ts.map +1 -0
  26. package/dist/index.d.mts +2 -0
  27. package/dist/index.d.ts +3 -0
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.mjs +1 -1
  30. package/dist/module.d.mts +2 -0
  31. package/dist/module.d.ts.map +1 -1
  32. package/dist/module.json +1 -1
  33. package/dist/module.mjs +23 -2
  34. package/dist/runtime/assets/main.css +1 -1
  35. package/dist/runtime/components/MapoConfirmDialog.vue +23 -4
  36. package/dist/runtime/components/MapoConfirmDialog.vue.d.ts.map +1 -1
  37. package/dist/runtime/components/MapoDetail.vue +301 -63
  38. package/dist/runtime/components/MapoDetail.vue.d.ts +191 -9
  39. package/dist/runtime/components/MapoDetail.vue.d.ts.map +1 -1
  40. package/dist/runtime/components/MapoDetailExtensions.vue +24 -0
  41. package/dist/runtime/components/MapoDetailExtensions.vue.d.ts +15 -0
  42. package/dist/runtime/components/MapoDetailExtensions.vue.d.ts.map +1 -0
  43. package/dist/runtime/components/MapoDetailLangSwitch.vue +22 -26
  44. package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts +4 -3
  45. package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts.map +1 -1
  46. package/dist/runtime/components/MapoDropArea.vue +5 -1
  47. package/dist/runtime/components/MapoDropArea.vue.d.ts +1 -1
  48. package/dist/runtime/components/MapoDropArea.vue.d.ts.map +1 -1
  49. package/dist/runtime/components/MapoLangSwitcher.vue +39 -0
  50. package/dist/runtime/components/MapoLangSwitcher.vue.d.ts +18 -0
  51. package/dist/runtime/components/MapoLangSwitcher.vue.d.ts.map +1 -0
  52. package/dist/runtime/components/MapoList.vue +7 -3
  53. package/dist/runtime/components/MapoList.vue.d.ts +14 -4
  54. package/dist/runtime/components/MapoList.vue.d.ts.map +1 -1
  55. package/dist/runtime/components/MapoListActions.vue +27 -13
  56. package/dist/runtime/components/MapoListActions.vue.d.ts +4 -0
  57. package/dist/runtime/components/MapoListActions.vue.d.ts.map +1 -1
  58. package/dist/runtime/components/MapoListFilters.vue +4 -2
  59. package/dist/runtime/components/MapoListFilters.vue.d.ts.map +1 -1
  60. package/dist/runtime/components/MapoListQuickEdit.vue +12 -8
  61. package/dist/runtime/components/MapoListQuickEdit.vue.d.ts.map +1 -1
  62. package/dist/runtime/components/MapoListTable.vue +116 -45
  63. package/dist/runtime/components/MapoListTable.vue.d.ts +11 -6
  64. package/dist/runtime/components/MapoListTable.vue.d.ts.map +1 -1
  65. package/dist/runtime/components/MapoLogin.vue +13 -10
  66. package/dist/runtime/components/MapoLogin.vue.d.ts.map +1 -1
  67. package/dist/runtime/components/MapoLogoutButton.vue +4 -2
  68. package/dist/runtime/components/MapoLogoutButton.vue.d.ts.map +1 -1
  69. package/dist/runtime/components/MapoMediaBreadcrumbs.vue +3 -1
  70. package/dist/runtime/components/MapoMediaBreadcrumbs.vue.d.ts.map +1 -1
  71. package/dist/runtime/components/MapoMediaEditor.vue +36 -20
  72. package/dist/runtime/components/MapoMediaEditor.vue.d.ts +1 -0
  73. package/dist/runtime/components/MapoMediaEditor.vue.d.ts.map +1 -1
  74. package/dist/runtime/components/MapoMediaFileChanger.vue +7 -3
  75. package/dist/runtime/components/MapoMediaFileChanger.vue.d.ts.map +1 -1
  76. package/dist/runtime/components/MapoMediaFolders.vue +36 -14
  77. package/dist/runtime/components/MapoMediaFolders.vue.d.ts.map +1 -1
  78. package/dist/runtime/components/MapoMediaGallery.vue +15 -5
  79. package/dist/runtime/components/MapoMediaGallery.vue.d.ts.map +1 -1
  80. package/dist/runtime/components/MapoMediaManager.vue +17 -7
  81. package/dist/runtime/components/MapoMediaManager.vue.d.ts.map +1 -1
  82. package/dist/runtime/components/MapoMediaManagerDialog.vue +8 -4
  83. package/dist/runtime/components/MapoMediaManagerDialog.vue.d.ts.map +1 -1
  84. package/dist/runtime/components/MapoMediaUploader.vue +53 -29
  85. package/dist/runtime/components/MapoMediaUploader.vue.d.ts.map +1 -1
  86. package/dist/runtime/components/MapoMenuManager.vue +293 -0
  87. package/dist/runtime/components/MapoMenuManager.vue.d.ts +92 -0
  88. package/dist/runtime/components/MapoMenuManager.vue.d.ts.map +1 -0
  89. package/dist/runtime/components/MapoMenuNodeEditor.vue +201 -0
  90. package/dist/runtime/components/MapoMenuNodeEditor.vue.d.ts +61 -0
  91. package/dist/runtime/components/MapoMenuNodeEditor.vue.d.ts.map +1 -0
  92. package/dist/runtime/components/MapoMenuTreeview.vue +181 -0
  93. package/dist/runtime/components/MapoMenuTreeview.vue.d.ts +57 -0
  94. package/dist/runtime/components/MapoMenuTreeview.vue.d.ts.map +1 -0
  95. package/dist/runtime/components/MapoMenuTreeviewNode.vue +175 -0
  96. package/dist/runtime/components/MapoMenuTreeviewNode.vue.d.ts +27 -0
  97. package/dist/runtime/components/MapoMenuTreeviewNode.vue.d.ts.map +1 -0
  98. package/dist/runtime/components/MapoSidebar.vue +3 -1
  99. package/dist/runtime/components/MapoSidebar.vue.d.ts.map +1 -1
  100. package/dist/runtime/components/MapoSidebarList.vue +1 -1
  101. package/dist/runtime/components/MapoSidebarProfile.vue +6 -2
  102. package/dist/runtime/components/MapoSidebarProfile.vue.d.ts.map +1 -1
  103. package/dist/runtime/components/MapoSnackBar.vue +11 -0
  104. package/dist/runtime/components/MapoSnackBar.vue.d.ts.map +1 -1
  105. package/dist/runtime/components/MapoThemeToggle.vue +5 -1
  106. package/dist/runtime/components/MapoThemeToggle.vue.d.ts.map +1 -1
  107. package/dist/runtime/components/menuDndContext.d.ts +19 -0
  108. package/dist/runtime/components/menuDndContext.d.ts.map +1 -0
  109. package/dist/runtime/components/menuDndContext.js +3 -0
  110. package/dist/runtime/composables/defineDetailExtension.d.ts +32 -0
  111. package/dist/runtime/composables/defineDetailExtension.d.ts.map +1 -0
  112. package/dist/runtime/composables/defineDetailExtension.js +34 -0
  113. package/dist/runtime/composables/useMapoDetail.d.ts +18 -0
  114. package/dist/runtime/composables/useMapoDetail.d.ts.map +1 -0
  115. package/dist/runtime/composables/useMapoDetail.js +5 -0
  116. package/dist/runtime/page-meta.nuxt.d.ts +4 -0
  117. package/dist/runtime/pages/index.vue +4 -2
  118. package/dist/runtime/pages/index.vue.d.ts.map +1 -1
  119. package/dist/runtime/plugins/media-adapter.d.ts.map +1 -1
  120. package/dist/runtime/plugins/media-adapter.js +2 -1
  121. package/dist/runtime/stores/media.d.ts +8 -2
  122. package/dist/runtime/stores/media.d.ts.map +1 -1
  123. package/dist/runtime/stores/media.js +45 -13
  124. package/dist/runtime/types/detail.d.ts +167 -0
  125. package/dist/runtime/types/detail.d.ts.map +1 -0
  126. package/dist/runtime/types/detail.js +0 -0
  127. package/dist/runtime/types/list.d.ts +10 -2
  128. package/dist/runtime/types/list.d.ts.map +1 -1
  129. package/dist/runtime/types/menu.d.ts +66 -0
  130. package/dist/runtime/types/menu.d.ts.map +1 -0
  131. package/dist/runtime/types/menu.js +30 -0
  132. package/dist/runtime/utils/formValues.d.ts +8 -0
  133. package/dist/runtime/utils/formValues.d.ts.map +1 -0
  134. package/dist/runtime/utils/formValues.js +19 -0
  135. package/dist/runtime/utils/langInfo.d.ts +30 -0
  136. package/dist/runtime/utils/langInfo.d.ts.map +1 -0
  137. package/dist/runtime/utils/langInfo.js +8 -0
  138. package/dist/runtime/utils/listTable.d.ts +29 -0
  139. package/dist/runtime/utils/listTable.d.ts.map +1 -0
  140. package/dist/runtime/utils/listTable.js +21 -0
  141. package/package.json +12 -10
  142. package/src/__tests__/MapoConfirmDialog.component.test.ts +165 -0
  143. package/src/__tests__/MapoDetail.component.test.ts +1107 -0
  144. package/src/__tests__/MapoDetailLangSwitch.component.test.ts +145 -0
  145. package/src/__tests__/MapoListActions.component.test.ts +175 -0
  146. package/src/__tests__/MapoListTable.component.test.ts +504 -0
  147. package/src/__tests__/MapoSnackBar.component.test.ts +79 -0
  148. package/src/__tests__/__mocks__/nuxt-app.ts +8 -0
  149. package/src/__tests__/formValues.test.ts +31 -0
  150. package/src/__tests__/langInfo.test.ts +48 -0
  151. package/src/__tests__/listTable.test.ts +82 -0
  152. package/src/__tests__/mediaLanguages.test.ts +66 -0
  153. package/src/__tests__/runtimePages.test.ts +17 -0
  154. package/src/index.ts +20 -0
  155. package/src/module.ts +29 -0
  156. package/src/runtime/assets/main.css +13 -0
  157. package/src/runtime/components/MapoConfirmDialog.vue +33 -4
  158. package/src/runtime/components/MapoDetail.vue +520 -89
  159. package/src/runtime/components/MapoDetailExtensions.vue +39 -0
  160. package/src/runtime/components/MapoDetailLangSwitch.vue +27 -28
  161. package/src/runtime/components/MapoDropArea.vue +6 -1
  162. package/src/runtime/components/MapoLangSwitcher.vue +74 -0
  163. package/src/runtime/components/MapoList.vue +21 -6
  164. package/src/runtime/components/MapoListActions.vue +32 -14
  165. package/src/runtime/components/MapoListFilters.vue +5 -2
  166. package/src/runtime/components/MapoListQuickEdit.vue +12 -9
  167. package/src/runtime/components/MapoListTable.vue +147 -55
  168. package/src/runtime/components/MapoLogin.vue +13 -10
  169. package/src/runtime/components/MapoLogoutButton.vue +4 -2
  170. package/src/runtime/components/MapoMediaBreadcrumbs.vue +3 -1
  171. package/src/runtime/components/MapoMediaEditor.vue +43 -20
  172. package/src/runtime/components/MapoMediaFileChanger.vue +8 -3
  173. package/src/runtime/components/MapoMediaFolders.vue +37 -15
  174. package/src/runtime/components/MapoMediaGallery.vue +17 -5
  175. package/src/runtime/components/MapoMediaManager.vue +17 -7
  176. package/src/runtime/components/MapoMediaManagerDialog.vue +10 -4
  177. package/src/runtime/components/MapoMediaUploader.vue +60 -29
  178. package/src/runtime/components/MapoMenuManager.vue +437 -0
  179. package/src/runtime/components/MapoMenuNodeEditor.vue +282 -0
  180. package/src/runtime/components/MapoMenuTreeview.vue +264 -0
  181. package/src/runtime/components/MapoMenuTreeviewNode.vue +234 -0
  182. package/src/runtime/components/MapoSidebar.vue +3 -1
  183. package/src/runtime/components/MapoSidebarList.vue +1 -1
  184. package/src/runtime/components/MapoSidebarProfile.vue +6 -2
  185. package/src/runtime/components/MapoSnackBar.vue +12 -0
  186. package/src/runtime/components/MapoThemeToggle.vue +5 -1
  187. package/src/runtime/components/menuDndContext.ts +22 -0
  188. package/src/runtime/composables/defineDetailExtension.ts +78 -0
  189. package/src/runtime/composables/useMapoDetail.ts +24 -0
  190. package/src/runtime/page-meta.nuxt.d.ts +4 -0
  191. package/src/runtime/pages/index.vue +5 -2
  192. package/src/runtime/plugins/media-adapter.ts +9 -5
  193. package/src/runtime/stores/media.ts +61 -13
  194. package/src/runtime/types/detail.ts +178 -0
  195. package/src/runtime/types/list.ts +10 -2
  196. package/src/runtime/types/menu.ts +105 -0
  197. package/src/runtime/utils/formValues.ts +28 -0
  198. package/src/runtime/utils/langInfo.ts +38 -0
  199. package/src/runtime/utils/listTable.ts +54 -0
@@ -0,0 +1,437 @@
1
+ <script setup lang="ts">
2
+ import { ref, computed, watch, onMounted } from "vue";
3
+ import { useI18n } from "vue-i18n";
4
+ import { useCrud } from "@mapomodule/core/runtime/api/crud";
5
+ import {
6
+ getErrorData,
7
+ getErrorDetail,
8
+ getErrorStatus,
9
+ } from "@mapomodule/core/runtime/utils/fetchError";
10
+ import { useSnackStore } from "@mapomodule/store/runtime/stores/snack";
11
+ import { usePermissions } from "@mapomodule/store/runtime/composables/usePermissions";
12
+ import { languagesFromMetadata } from "../utils/langInfo";
13
+ import { deepClone, objectDiff } from "@mapomodule/utils";
14
+ import type { AnyFieldDescriptor } from "@mapomodule/form/types";
15
+ import {
16
+ findMenuNode,
17
+ type MapoMenu,
18
+ type MenuTreeNode,
19
+ } from "../types/menu.js";
20
+ import MapoMenuTreeview from "./MapoMenuTreeview.vue";
21
+ import MapoMenuNodeEditor from "./MapoMenuNodeEditor.vue";
22
+
23
+ /**
24
+ * Menu Manager shell — split-pane editor for hierarchical navigation menus.
25
+ *
26
+ * Left: drag & drop node tree (`MapoMenuTreeview`).
27
+ * Right: form editor for the selected node (`MapoMenuNodeEditor`).
28
+ *
29
+ * Loads the menu from `endpoint`/`identifier` on mount (v1 parity), keeps
30
+ * per-language node trees when `translatable`, and saves the whole structure
31
+ * with `create`/`update`/`partialUpdate` depending on state and `usePatch`.
32
+ *
33
+ * @example
34
+ * <MapoMenuManager
35
+ * endpoint="/api/menus"
36
+ * :identifier="route.params.id"
37
+ * :languages="['it', 'en']"
38
+ * :max-depth="5"
39
+ * />
40
+ */
41
+ const props = withDefaults(
42
+ defineProps<{
43
+ /** Menu CRUD endpoint (e.g. `/api/menus`). */
44
+ endpoint: string;
45
+ /** Menu id to load, or `"new"` to start from an empty menu. */
46
+ identifier?: string | number;
47
+ /** Menu payload (v-model). Used as the initial model when `identifier` is `"new"`. */
48
+ modelValue?: MapoMenu | null;
49
+ /** Manage one node tree per language under `translations`. */
50
+ translatable?: boolean;
51
+ /** Active editing language (v-model:lang). Defaults to the first of `languages`. */
52
+ lang?: string;
53
+ /** Translation language codes (e.g. `['it', 'en']`). */
54
+ languages?: string[];
55
+ /** Save with `partialUpdate` sending only the changed keys. */
56
+ usePatch?: boolean;
57
+ /** Max nesting depth; `-1` = unlimited. */
58
+ maxDepth?: number;
59
+ /**
60
+ * Django model used for permission gating (`add_<model>` / `change_<model>`).
61
+ * When the user lacks the permission the manager renders read-only.
62
+ */
63
+ permissionModel?: string;
64
+ /** Force read-only mode. */
65
+ readonly?: boolean;
66
+ /** Replaces the node editor's default core fields. */
67
+ coreFields?: AnyFieldDescriptor[] | null;
68
+ /** Extra node editor fields appended after the core ones. */
69
+ additionalFields?: AnyFieldDescriptor[];
70
+ /** CSS classes offered by the node `style` select, as `{ label: cssClass }`. */
71
+ availableClasses?: Record<string, string>;
72
+ }>(),
73
+ {
74
+ identifier: "new",
75
+ modelValue: null,
76
+ translatable: true,
77
+ lang: undefined,
78
+ languages: () => [],
79
+ usePatch: false,
80
+ maxDepth: -1,
81
+ permissionModel: undefined,
82
+ readonly: false,
83
+ coreFields: null,
84
+ additionalFields: () => [],
85
+ availableClasses: () => ({}),
86
+ },
87
+ );
88
+
89
+ const emit = defineEmits<{
90
+ "update:modelValue": [menu: MapoMenu];
91
+ "update:lang": [lang: string];
92
+ /** Emitted after a successful save with the server response. */
93
+ saved: [menu: MapoMenu];
94
+ }>();
95
+
96
+ defineSlots<{
97
+ /** Rendered when no node is selected (right pane placeholder). */
98
+ empty(): unknown;
99
+ /** Content above the node editor form. */
100
+ "editor-form-top"(props: Record<string, unknown>): unknown;
101
+ /** Replaces the node editor form. */
102
+ "editor-form"(props: Record<string, unknown>): unknown;
103
+ /** Content below the node editor form. */
104
+ "editor-form-bottom"(props: Record<string, unknown>): unknown;
105
+ }>();
106
+
107
+ const { t } = useI18n();
108
+ const snack = useSnackStore();
109
+ const crud = useCrud<MapoMenu>(props.endpoint);
110
+
111
+ // ─── State ────────────────────────────────────────────────────────────────────
112
+
113
+ const model = ref<MapoMenu>(
114
+ props.modelValue ?? { key: "", nodes: [], translations: {} },
115
+ );
116
+ let backup: MapoMenu = deepClone(model.value);
117
+
118
+ const selected = ref<MenuTreeNode | null>(null);
119
+ // ─── Languages (auto-derive from lang_info) ──────────────────────────────────
120
+ // Camomilla describes the model's language situation in the endpoint's OPTIONS
121
+ // metadata; reading it means a host page no longer hardcodes a list, and a menu
122
+ // model that is not registered for translation shows no switcher instead of tabs
123
+ // the backend would drop. props.languages still wins when a page insists.
124
+ const derivedLangs = ref<string[]>([]);
125
+ const activeLangs = computed<string[]>(() =>
126
+ props.languages.length ? props.languages : derivedLangs.value,
127
+ );
128
+
129
+ const currentLang = ref(props.lang ?? props.languages[0] ?? "");
130
+ const isLoading = ref(String(props.identifier) !== "new");
131
+ const isSaving = ref(false);
132
+
133
+ const isNew = computed(() => String(props.identifier) === "new");
134
+ const translationsActive = computed(
135
+ () => props.translatable && activeLangs.value.length > 0,
136
+ );
137
+
138
+ // ─── Permissions ─────────────────────────────────────────────────────────────
139
+
140
+ const { canAdd, canChange } = usePermissions();
141
+ const effectiveReadonly = computed(() => {
142
+ if (props.readonly) return true;
143
+ if (!props.permissionModel) return false;
144
+ return isNew.value
145
+ ? !canAdd(props.permissionModel)
146
+ : !canChange(props.permissionModel);
147
+ });
148
+
149
+ // ─── Language handling ───────────────────────────────────────────────────────
150
+
151
+ // Derivation resolves after mount, so the initial currentLang is "" whenever the
152
+ // list is not hardcoded. Adopt the first language as soon as one is known.
153
+ watch(
154
+ activeLangs,
155
+ (langs) => {
156
+ if (!currentLang.value && langs.length) currentLang.value = langs[0]!;
157
+ },
158
+ { immediate: true },
159
+ );
160
+
161
+ watch(
162
+ () => props.lang,
163
+ (val) => {
164
+ if (val && activeLangs.value.includes(val)) currentLang.value = val;
165
+ },
166
+ );
167
+
168
+ watch(currentLang, (val) => {
169
+ // Nodes are per-language: the old selection does not exist in the new tree.
170
+ selected.value = null;
171
+ emit("update:lang", val);
172
+ });
173
+
174
+ // ─── Active node tree (translations-aware) ───────────────────────────────────
175
+ // The container the active tree lives in is created eagerly by a watcher rather
176
+ // than lazily inside the `nodes` computed: the tree is mutated in place by the
177
+ // drag & drop lists, so it must be a stable reactive array, and a computed that
178
+ // writes to the model on read would be a side effect.
179
+
180
+ function ensureTree() {
181
+ if (translationsActive.value) {
182
+ model.value.translations ??= {};
183
+ const lang = currentLang.value;
184
+ model.value.translations[lang] ??= { nodes: [] };
185
+ model.value.translations[lang].nodes ??= [];
186
+ } else {
187
+ model.value.nodes ??= [];
188
+ }
189
+ }
190
+
191
+ watch([model, currentLang, translationsActive], ensureTree, {
192
+ immediate: true,
193
+ });
194
+
195
+ const nodes = computed<MenuTreeNode[]>(() => {
196
+ if (translationsActive.value) {
197
+ return model.value.translations?.[currentLang.value]?.nodes ?? [];
198
+ }
199
+ return model.value.nodes ?? [];
200
+ });
201
+
202
+ // ─── Load ────────────────────────────────────────────────────────────────────
203
+
204
+ watch(
205
+ () => props.modelValue,
206
+ (val) => {
207
+ if (val) model.value = val;
208
+ },
209
+ );
210
+
211
+ watch(model, (val) => emit("update:modelValue", val));
212
+
213
+ /**
214
+ * Ask the endpoint to describe itself. `lang_info` lives on OPTIONS only, so
215
+ * this runs for an existing menu as well as a brand new one.
216
+ */
217
+ async function deriveLanguages() {
218
+ if (props.languages.length) return;
219
+ try {
220
+ derivedLangs.value = languagesFromMetadata(await crud.options());
221
+ } catch {
222
+ // Backend without OPTIONS metadata: just no auto-derived languages.
223
+ }
224
+ }
225
+
226
+ onMounted(async () => {
227
+ deriveLanguages();
228
+ if (isNew.value) return;
229
+ isLoading.value = true;
230
+ try {
231
+ model.value = await crud.detail(props.identifier);
232
+ backup = deepClone(model.value);
233
+ } catch (err: unknown) {
234
+ snack.show(getErrorDetail(err) ?? t("mapo.loadItemError"), "error");
235
+ } finally {
236
+ isLoading.value = false;
237
+ }
238
+ });
239
+
240
+ // ─── Save ────────────────────────────────────────────────────────────────────
241
+
242
+ async function save() {
243
+ if (effectiveReadonly.value) return;
244
+ clearErrors();
245
+ isSaving.value = true;
246
+ try {
247
+ let response: MapoMenu;
248
+ if (!model.value.id) {
249
+ response = await crud.create(model.value);
250
+ } else if (props.usePatch) {
251
+ const diff = objectDiff(backup, model.value) as Partial<MapoMenu>;
252
+ response = await crud.partialUpdate(model.value.id, diff);
253
+ } else {
254
+ response = await crud.update(model.value.id, model.value);
255
+ }
256
+ model.value = response;
257
+ backup = deepClone(response);
258
+ selected.value = null;
259
+ snack.show(
260
+ isNew.value ? t("mapo.createSuccess") : t("mapo.saveSuccess"),
261
+ "success",
262
+ );
263
+ emit("saved", response);
264
+ } catch (err: unknown) {
265
+ const data = getErrorData(err);
266
+ if (getErrorStatus(err) === 400 && data) parseErrors(data);
267
+ snack.show(getErrorDetail(err) ?? t("mapo.genericError"), "error");
268
+ } finally {
269
+ isSaving.value = false;
270
+ }
271
+ }
272
+
273
+ // ─── Backend error mapping ───────────────────────────────────────────────────
274
+ // DRF mirrors the payload shape: errors for nested nodes arrive at the same
275
+ // position in a nested `nodes` array. Walk both trees in parallel and attach
276
+ // the node-level errors; select the first errored node in the active tree.
277
+
278
+ function clearErrors() {
279
+ const walk = (list: MenuTreeNode[]) => {
280
+ for (const node of list) {
281
+ if (node.errors) delete node.errors;
282
+ walk(node.nodes ?? []);
283
+ }
284
+ };
285
+ if (model.value.nodes) walk(model.value.nodes);
286
+ for (const tr of Object.values(model.value.translations ?? {})) {
287
+ walk(tr.nodes ?? []);
288
+ }
289
+ }
290
+
291
+ function parseErrors(data: Record<string, unknown>) {
292
+ const attach = (
293
+ list: MenuTreeNode[],
294
+ errors: unknown[],
295
+ inActiveTree: boolean,
296
+ ) => {
297
+ errors.forEach((err, i) => {
298
+ const node = list[i];
299
+ if (!node || !err || typeof err !== "object") return;
300
+ const { nodes: childErrors, ...nodeErrors } = err as Record<
301
+ string,
302
+ unknown
303
+ >;
304
+ if (Object.keys(nodeErrors).length > 0) {
305
+ node.errors = nodeErrors as Record<string, string[]>;
306
+ if (inActiveTree && !selected.value) selected.value = node;
307
+ }
308
+ if (Array.isArray(childErrors)) {
309
+ attach(node.nodes ?? [], childErrors, inActiveTree);
310
+ }
311
+ });
312
+ };
313
+
314
+ if (translationsActive.value) {
315
+ const translations = data.translations as
316
+ | Record<string, { nodes?: unknown[] }>
317
+ | undefined;
318
+ for (const [lang, trErrors] of Object.entries(translations ?? {})) {
319
+ const tree = model.value.translations?.[lang]?.nodes ?? [];
320
+ if (Array.isArray(trErrors?.nodes)) {
321
+ attach(tree, trErrors.nodes, lang === currentLang.value);
322
+ }
323
+ }
324
+ } else if (Array.isArray(data.nodes)) {
325
+ attach(model.value.nodes ?? [], data.nodes, true);
326
+ }
327
+ }
328
+
329
+ // ─── Editor errors for the lang switch ───────────────────────────────────────
330
+ // MapoDetailLangSwitch highlights languages whose keys start with
331
+ // `translations.<lang>.` — build that map from the per-node errors.
332
+
333
+ const langErrors = computed<Record<string, string[]>>(() => {
334
+ const map: Record<string, string[]> = {};
335
+ const hasErrors = (list: MenuTreeNode[]): boolean =>
336
+ list.some(
337
+ (n) =>
338
+ (n.errors && Object.keys(n.errors).length > 0) ||
339
+ hasErrors(n.nodes ?? []),
340
+ );
341
+ for (const [lang, tr] of Object.entries(model.value.translations ?? {})) {
342
+ if (hasErrors(tr.nodes ?? [])) {
343
+ map[`translations.${lang}.nodes`] = [t("mapo.fixErrors")];
344
+ }
345
+ }
346
+ return map;
347
+ });
348
+
349
+ const treeview = ref<InstanceType<typeof MapoMenuTreeview> | null>(null);
350
+ </script>
351
+
352
+ <template>
353
+ <div class="mapo-menu-manager flex h-full min-h-0 overflow-hidden">
354
+ <!-- Left pane: tree -->
355
+ <div class="flex w-72 shrink-0 flex-col border-r border-default bg-default">
356
+ <MapoMenuTreeview
357
+ ref="treeview"
358
+ v-model="selected"
359
+ :nodes="nodes"
360
+ :title="`${t('mapo.menuManager.menu')}: ${model.key || '—'}`"
361
+ :max-depth="maxDepth"
362
+ :readonly="effectiveReadonly"
363
+ :loading="isLoading"
364
+ >
365
+ <template v-if="translationsActive" #top>
366
+ <div class="border-b border-default px-2 pt-2">
367
+ <MapoDetailLangSwitch
368
+ v-model="currentLang"
369
+ :langs="activeLangs"
370
+ :errors="langErrors"
371
+ no-route-change
372
+ />
373
+ </div>
374
+ </template>
375
+ <template #bottom>
376
+ <div class="border-t border-default p-2">
377
+ <UButton
378
+ v-if="!effectiveReadonly"
379
+ block
380
+ icon="i-lucide-save"
381
+ :loading="isSaving"
382
+ @click="save"
383
+ >
384
+ {{ t("mapo.save") }}
385
+ </UButton>
386
+ </div>
387
+ </template>
388
+ </MapoMenuTreeview>
389
+ </div>
390
+
391
+ <!-- Right pane -->
392
+ <div v-if="isLoading" class="flex flex-1 items-center justify-center">
393
+ <UIcon
394
+ name="i-lucide-loader-circle"
395
+ class="size-8 animate-spin text-muted"
396
+ />
397
+ </div>
398
+
399
+ <MapoMenuNodeEditor
400
+ v-else-if="selected"
401
+ :model-value="selected"
402
+ :nodes="nodes"
403
+ :menu-endpoint="endpoint"
404
+ :core-fields="coreFields"
405
+ :additional-fields="additionalFields"
406
+ :available-classes="availableClasses"
407
+ :readonly="effectiveReadonly"
408
+ @update:model-value="
409
+ selected = findMenuNode(nodes, $event.id)?.node ?? $event
410
+ "
411
+ @delete="treeview?.deleteSelectedNode()"
412
+ >
413
+ <template #form-top="slotProps">
414
+ <slot name="editor-form-top" v-bind="slotProps" />
415
+ </template>
416
+ <template v-if="$slots['editor-form']" #form="slotProps">
417
+ <slot name="editor-form" v-bind="slotProps" />
418
+ </template>
419
+ <template #form-bottom="slotProps">
420
+ <slot name="editor-form-bottom" v-bind="slotProps" />
421
+ </template>
422
+ </MapoMenuNodeEditor>
423
+
424
+ <!-- Empty state -->
425
+ <div
426
+ v-else
427
+ class="flex flex-1 flex-col items-center justify-center gap-3 p-8 text-center"
428
+ >
429
+ <slot name="empty">
430
+ <UIcon name="i-lucide-network" class="size-12 text-dimmed" />
431
+ <p class="max-w-xs text-sm text-muted">
432
+ {{ t("mapo.menuManager.noSelectedNode") }}
433
+ </p>
434
+ </slot>
435
+ </div>
436
+ </div>
437
+ </template>