@mapomodule/uikit 1.0.0-alpha.42 → 1.0.0-alpha.44

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.
@@ -0,0 +1,327 @@
1
+ <template>
2
+ <div v-if="model" class="menu-manager--wrapper">
3
+ <v-navigation-drawer
4
+ v-model="mobileOpened"
5
+ :bottom="isMobile"
6
+ :absolute="isMobile"
7
+ :permanent="!isMobile"
8
+ >
9
+ <MenuTreeview
10
+ v-model="selected"
11
+ :nodes.sync="nodes"
12
+ :title="`Menu: ${model.key}`"
13
+ ref="treeview"
14
+ >
15
+ <template v-if="translationsActive" v-slot:treeview.top>
16
+ <DetailLangSwitch
17
+ :langs="languages"
18
+ v-model="currentLang"
19
+ :errors="errors"
20
+ />
21
+ </template>
22
+ <template v-slot:treeview.bottom>
23
+ <v-btn tile @click="save" :loading="loading">
24
+ {{ $t("mapo.save") }}
25
+ </v-btn>
26
+ </template>
27
+ </MenuTreeview>
28
+ </v-navigation-drawer>
29
+ <MenuNodeEditor
30
+ v-if="selected"
31
+ v-model="selected"
32
+ @delete="$refs.treeview.deleteSelectedNode"
33
+ :menu-endpoint="endpoint"
34
+ :nodes="nodes"
35
+ :lang="currentLang"
36
+ :languages="languages"
37
+ :readonly="isReadonly"
38
+ v-bind="$attrs"
39
+ >
40
+ <template v-for="slot in nameSpacedSlots($slots, 'editor.')" :slot="slot">
41
+ <slot :name="`editor.${slot}`"></slot>
42
+ </template>
43
+ <template v-for="slot in nameSpacedSlots($scopedSlots, 'editor.')" v-slot:[slot]="props">
44
+ <slot :name="`editor.${slot}`" v-bind="props" />
45
+ </template>
46
+ </MenuNodeEditor>
47
+ <div class="menu-manager--empty" v-else>
48
+ <v-icon size="60"> mdi-resistor-nodes </v-icon>
49
+ <p>{{ $t("mapo.menuManager.noSelectedNode") }}</p>
50
+ </div>
51
+ <v-btn
52
+ v-if="isMobile"
53
+ class="menu-manager--bottom-btn"
54
+ tile
55
+ block
56
+ @click="save"
57
+ :loading="loading"
58
+ >
59
+ {{ $t("mapo.save") }}
60
+ </v-btn>
61
+ <v-btn
62
+ v-if="isMobile"
63
+ color="primary"
64
+ class="menu-manager--bottom-btn"
65
+ tile
66
+ @click="mobileOpened = !mobileOpened"
67
+ ><v-icon>mdi-menu-open</v-icon></v-btn
68
+ >
69
+ </div>
70
+ </template>
71
+ <style lang="scss">
72
+ .menu-manager--wrapper {
73
+ display: flex;
74
+ flex-direction: row;
75
+ height: 100%;
76
+ .v-tabs {
77
+ flex: 0;
78
+ }
79
+ }
80
+ .menu-node-editor--wrapper {
81
+ flex: 1;
82
+ max-width: 100%;
83
+ }
84
+ .menu-manager--bottom-btn {
85
+ position: absolute;
86
+ bottom: 0;
87
+ }
88
+ .menu-manager--empty {
89
+ flex: 1;
90
+ max-width: 100%;
91
+ overflow: hidden;
92
+ display: flex;
93
+ flex-direction: column;
94
+ align-content: center;
95
+ justify-content: center;
96
+ text-align: center;
97
+ }
98
+ </style>
99
+
100
+ <script>
101
+ import { diffObjs, deepClone } from "@mapomodule/utils/helpers/objHelpers";
102
+ import { nameSpacedSlots } from "@mapomodule/utils/helpers/slots";
103
+
104
+ export default {
105
+ name: "MenuManager",
106
+ props: {
107
+ // V-model of the object we are editing.
108
+ value: {
109
+ type: Object,
110
+ required: false,
111
+ },
112
+ // Set the current lang to value.
113
+ translatable: {
114
+ type: Boolean,
115
+ default: true,
116
+ },
117
+ // Set the current lang to value.
118
+ lang: {
119
+ type: String,
120
+ required: false,
121
+ },
122
+ languages: {
123
+ type: Array,
124
+ default() {
125
+ return this.$mapo.$options?.content?.languages || [];
126
+ },
127
+ },
128
+ usePatch: {
129
+ type: Boolean,
130
+ default() {
131
+ return this.$mapo.$options?.content?.usePatch || false;
132
+ },
133
+ },
134
+ endpoint: {
135
+ type: String,
136
+ default: null,
137
+ },
138
+ identifier: {
139
+ // `String|Number`.
140
+ type: String | Number,
141
+ default: "new",
142
+ },
143
+ },
144
+ data() {
145
+ return {
146
+ selected: null,
147
+ mobileOpened: false,
148
+ currentLang: this.lang || this.$i18n.locale,
149
+ loading: false,
150
+ errors: {},
151
+ model: { key: Math.random().toString(36).slice(2) },
152
+ crud: this.$mapo.$api.crud(this.endpoint),
153
+ };
154
+ },
155
+ watch: {
156
+ currentLang(val) {
157
+ // Fired when the current language changes.
158
+ // @arg Emits the language name as a string.
159
+ this.$emit("update:lang", val);
160
+ },
161
+ model(val) {
162
+ // Fired when the v-model changes.
163
+ // @arg Emits the entire payload modified.
164
+ this.$emit("input", val);
165
+ },
166
+ lang(val) {
167
+ if (val && this.languages.includes(val)) {
168
+ this.currentLang = val;
169
+ }
170
+ },
171
+ value(val) {
172
+ if (val) {
173
+ this.model = val;
174
+ }
175
+ },
176
+ currentLang(val) {
177
+ this.selected = null;
178
+ },
179
+ },
180
+ computed: {
181
+ nodes: {
182
+ get() {
183
+ if (this.translationsActive) {
184
+ this.initTranslations();
185
+ return this.model.translations[this.currentLang].nodes;
186
+ } else {
187
+ return this.model.nodes;
188
+ }
189
+ },
190
+ set(val) {
191
+ if (this.translationsActive) {
192
+ this.initTranslations();
193
+ return this.$set(
194
+ this.model.translations[this.currentLang],
195
+ "nodes",
196
+ val
197
+ );
198
+ } else {
199
+ return this.$set(this.model, "nodes", val);
200
+ }
201
+ },
202
+ },
203
+ isNew() {
204
+ return this.identifier && this.identifier == "new";
205
+ },
206
+ isReadonly() {
207
+ if (this.$mapo.$auth.routeMiddlewares.includes("permissions")) {
208
+ return !this.$mapo.$auth.user.permissions.includes(
209
+ this.isNew ? "add" : "change"
210
+ );
211
+ }
212
+ return !!this.buttonClicked;
213
+ },
214
+ translationsActive() {
215
+ return this.translatable && !!this.languages?.length;
216
+ },
217
+ modelDiff() {
218
+ return diffObjs(this.modelBkp, this.model) || {};
219
+ },
220
+ hasDiff() {
221
+ return !!(this.modelDiff && Object.keys(this.modelDiff).length > 0);
222
+ },
223
+ apiSendPayload() {
224
+ let method, args;
225
+ if (!this.model.id) {
226
+ method = this.crud.create;
227
+ args = [this.model];
228
+ } else if (this.usePatch) {
229
+ method = this.crud.partialUpdate;
230
+ args = [this.model.id, this.modelDiff];
231
+ } else {
232
+ method = this.crud.update;
233
+ args = [this.model.id, this.model];
234
+ }
235
+ return async () => method(...args);
236
+ },
237
+ isMobile() {
238
+ return this.$vuetify.breakpoint.xs;
239
+ },
240
+ },
241
+ async mounted() {
242
+ if (this.identifier && this.identifier !== "new") {
243
+ try {
244
+ this.model = await this.crud.detail(this.identifier);
245
+ } catch (error) {
246
+ this.$nuxt.error({
247
+ statusCode: error.response.status,
248
+ message: error.response.data?.detail || this.$t("mapo.genericError"),
249
+ });
250
+ }
251
+ } else if (this.value) {
252
+ this.model = this.value;
253
+ }
254
+ this.modelBkp = deepClone(this.model);
255
+ },
256
+ methods: {
257
+ nameSpacedSlots,
258
+ save() {
259
+ this.errors = null;
260
+ this.loading = true;
261
+ this.apiSendPayload()
262
+ .then((resp) => {
263
+ this.model = resp;
264
+ this.modelBkp = deepClone(this.model);
265
+ this.$mapo.$snack.open({
266
+ message: this.isNew
267
+ ? this.$t("mapo.createSuccess")
268
+ : this.$t("mapo.saveSuccess"),
269
+ color: "success",
270
+ });
271
+ this.isNew && this.$router.replace({ params: { detail: resp.id } });
272
+ })
273
+ .catch((error) => {
274
+ this.errors =
275
+ (error.response?.status == 400 && error.response?.data) || null;
276
+ this.parseErrors(this.errors);
277
+ this.$mapo.$snack.open({
278
+ message:
279
+ error.response?.data?.detail || this.$t("mapo.genericError"),
280
+ color: "error",
281
+ });
282
+ })
283
+ .finally(() => {
284
+ this.loading = false;
285
+ });
286
+ },
287
+ initTranslations() {
288
+ this.model.translations = this.model.translations || {};
289
+ if (!this.model.translations[this.currentLang]) {
290
+ this.model.translations[this.currentLang] = { nodes: [] };
291
+ }
292
+ },
293
+
294
+ parseErrors(errors) {
295
+ if (!errors) return;
296
+ const recurse = (node, error, lang = null) => {
297
+ let { nodes, ...nodeErrors } = error || {};
298
+ if (Object.keys(nodeErrors).length > 0) {
299
+ if (this.currentLang == lang || !this.translationsActive) {
300
+ this.selected = node;
301
+ this.$refs?.treeview?.$refs.nodes?.forEach((nodeRef) =>
302
+ nodeRef.openIfChild(node)
303
+ );
304
+ }
305
+ this.$set(node, "errors", nodeErrors);
306
+ }
307
+ let index = 0;
308
+ while (nodes && index < nodes.length) {
309
+ recurse(node.nodes[index], error.nodes[index]);
310
+ index++;
311
+ }
312
+ };
313
+ if (this.translationsActive) {
314
+ Object.keys(errors.translations).forEach((lang) =>
315
+ recurse(
316
+ this.model.translations[lang],
317
+ errors.translations[lang],
318
+ lang
319
+ )
320
+ );
321
+ } else {
322
+ recurse(this.model, errors);
323
+ }
324
+ },
325
+ },
326
+ };
327
+ </script>
@@ -0,0 +1,255 @@
1
+ <template>
2
+ <div class="menu-node-editor--wrapper">
3
+ <div class="menu-node-editor--topbar">
4
+ <h5 class="menu-node-editor--breadcrumbs">
5
+ <span class="menu-node-editor--breadcrumbs-title"
6
+ >{{ $t("mapo.menuNodeEditor.editNode") }}:</span
7
+ >
8
+ <template v-for="(node, i) in parents">
9
+ <span
10
+ :key="`i${i}`"
11
+ class="menu-node-editor--breadcrumbs-item"
12
+ @click="model = node"
13
+ >{{ node.title }}</span
14
+ >
15
+ <span
16
+ :key="`d${i}`"
17
+ class="menu-node-editor--breadcrumbs-divider"
18
+ v-if="i != parents.length - 1"
19
+ >
20
+ >
21
+ </span>
22
+ </template>
23
+ </h5>
24
+ <div>
25
+ <v-btn tile color="error" @click="$emit('delete')">
26
+ <v-icon>mdi-delete</v-icon>
27
+ </v-btn>
28
+ </div>
29
+ </div>
30
+ <slot name="form.top" v-bind="slotBindings"></slot>
31
+ <slot name="form" v-bind="slotBindings">
32
+ <Form
33
+ v-model="model"
34
+ class="menu-node-editor--form"
35
+ :currentLang="lang"
36
+ :languages="languages"
37
+ :errors="model.errors"
38
+ :fields="fields"
39
+ :moreSlotBindings="slotBindings"
40
+ :readonly="readonly"
41
+ >
42
+ <template v-for="(_, slot) in $slots" :slot="slot">
43
+ <!-- @vuese-ignore -->
44
+ <slot :name="slot"></slot>
45
+ </template>
46
+ <template v-for="(_, slot) in $scopedSlots" v-slot:[slot]="props">
47
+ <!-- @vuese-ignore -->
48
+ <slot :name="slot" v-bind="props" />
49
+ </template>
50
+ </Form>
51
+ </slot>
52
+ <slot name="form.bottom" v-bind="slotBindings"></slot>
53
+ </div>
54
+ </template>
55
+
56
+ <style scoped>
57
+ .menu-node-editor--topbar {
58
+ background-color: var(--v-primary-base);
59
+ justify-content: space-between;
60
+ display: flex;
61
+ align-items: center;
62
+ padding-left: 10px;
63
+ }
64
+ .menu-node-editor--breadcrumbs-item {
65
+ cursor: pointer;
66
+ }
67
+ .menu-node-editor--breadcrumbs-title,
68
+ .menu-node-editor--breadcrumbs-divider {
69
+ color: var(--v-primary-darken3);
70
+ }
71
+ .menu-node-editor--form {
72
+ margin: 20px 0;
73
+ }
74
+ </style>
75
+
76
+ <script>
77
+ export default {
78
+ props: {
79
+ value: {
80
+ type: Object,
81
+ required: true,
82
+ },
83
+ nodes: {
84
+ type: Array,
85
+ default: () => [],
86
+ },
87
+ availableClasses: {
88
+ type: Object,
89
+ default: () => ({}),
90
+ },
91
+ coreFields: {
92
+ type: Array | null,
93
+ },
94
+ additionalFields: {
95
+ type: Array,
96
+ default: () => [],
97
+ },
98
+ // Set the current lang to value.
99
+ lang: {
100
+ type: String,
101
+ required: false,
102
+ },
103
+ languages: {
104
+ type: Array,
105
+ default() {
106
+ return this.$mapo.$options?.content?.languages || [];
107
+ },
108
+ },
109
+ readonly: Boolean,
110
+ menuEndpoint: String,
111
+ },
112
+ data() {
113
+ return {
114
+ model: this.value,
115
+ availablePages: [],
116
+ };
117
+ },
118
+ watch: {
119
+ value(val) {
120
+ this.model = val;
121
+ this.loadPages();
122
+ },
123
+ model(val) {
124
+ this.$emit("input", val);
125
+ },
126
+ },
127
+ computed: {
128
+ fields() {
129
+ return [
130
+ ...(this.coreFields || this.defaultCoreFields),
131
+ ...this.additionalFields,
132
+ ];
133
+ },
134
+ pageTypesEndpoint() {
135
+ return `${this.menuEndpoint.replace(/\/$/, "")}/page_types`;
136
+ },
137
+ slotBindings() {
138
+ return {
139
+ model: this.model,
140
+ errors: this.model.errors,
141
+ currentLang: this.lang,
142
+ langs: this.langs,
143
+ fields: this.fields,
144
+ readonly: this.readonly,
145
+ };
146
+ },
147
+ parents() {
148
+ let find = (nodes, parents = []) => {
149
+ let node = nodes.find(({ id }) => id == this.model.id);
150
+ return node
151
+ ? [...parents, node]
152
+ : nodes
153
+ .filter(({ nodes }) => nodes.length)
154
+ .reduce((acc, node) => {
155
+ return acc.length ? acc : find(node.nodes, [...parents, node]);
156
+ }, []);
157
+ };
158
+ return find(this.nodes);
159
+ },
160
+ defaultCoreFields() {
161
+ return [
162
+ {
163
+ value: "title",
164
+ label: this.$t("mapo.menuNodeEditor.title"),
165
+ synci18n: true,
166
+ },
167
+ {
168
+ value: "link.link_type",
169
+ label: this.$t("mapo.menuNodeEditor.linkTypeLabel"),
170
+ type: "select",
171
+ class: "col-md-4",
172
+ synci18n: true,
173
+ attrs: {
174
+ items: [
175
+ { text: this.$t("mapo.menuNodeEditor.relational"), value: "RE" },
176
+ { text: this.$t("mapo.menuNodeEditor.static"), value: "ST" },
177
+ ],
178
+ },
179
+ },
180
+ {
181
+ value: "meta.style",
182
+ label: this.$t("mapo.menuNodeEditor.styleLabel"),
183
+ type: "select",
184
+ class: "col-md-4",
185
+ synci18n: true,
186
+ attrs: {
187
+ items: Object.keys(this.availableClasses).map((text) => ({
188
+ text,
189
+ value: this.availableClasses[text],
190
+ })),
191
+ },
192
+ },
193
+ {
194
+ value: "meta.target_bank",
195
+ label: this.$t("mapo.menuNodeEditor.openInNewTabLabel"),
196
+ class: "col-md-4",
197
+ type: "switch",
198
+ synci18n: true,
199
+ },
200
+ {
201
+ value: "link.relational.content_type",
202
+ label: this.$t("mapo.menuNodeEditor.relContentTypeLabel"),
203
+ vShow: ({ model }) => model.link.link_type == "RE",
204
+ onChange: this.loadPages,
205
+ class: "col-md-6",
206
+ type: "fks",
207
+ synci18n: true,
208
+ attrs: {
209
+ multiple: false,
210
+ returnObject: false,
211
+ itemText: "verbose_name_plural",
212
+ endpoint: this.pageTypesEndpoint,
213
+ },
214
+ },
215
+ {
216
+ value: "link.relational.page_id",
217
+ label: this.$t("mapo.menuNodeEditor.relPageIdLabel"),
218
+ vShow: ({ model }) => model.link.link_type == "RE",
219
+ class: "col-md-6",
220
+ type: "select",
221
+ synci18n: true,
222
+ attrs: {
223
+ items: this.availablePages?.map((item) => ({
224
+ text: item.name,
225
+ value: item.id,
226
+ })),
227
+ },
228
+ },
229
+ {
230
+ value: "link.static",
231
+ label: this.$t("mapo.menuNodeEditor.staticLabel"),
232
+ vShow: ({ model }) => model.link.link_type == "ST",
233
+ synci18n: true,
234
+ },
235
+ ];
236
+ },
237
+ },
238
+ mounted() {
239
+ this.loadPages();
240
+ },
241
+ methods: {
242
+ async loadPages() {
243
+ const { content_type } = this.model?.link?.relational || {};
244
+ if (content_type) {
245
+ const options = { params: { language_code: this.lang } };
246
+ this.availablePages = await this.$mapo.$api
247
+ .crud(this.pageTypesEndpoint)
248
+ .detail(content_type, options);
249
+ } else {
250
+ this.availablePages = [];
251
+ }
252
+ },
253
+ },
254
+ };
255
+ </script>