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

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.
@@ -1,177 +1,192 @@
1
1
  <template>
2
2
  <div class="mapo-detail--wrapper">
3
- <div class="mapo-detail--loader" v-if="loading">
4
- <v-progress-linear
5
- color="primary"
6
- indeterminate
7
- absolute
8
- ></v-progress-linear>
9
- <span class="detail-component-loading">
10
- <v-icon size="30">mdi-timer-sand</v-icon>
11
- {{ $t('mapo.fetchingData') }}
12
- </span>
13
- </div>
14
- <div class="mapo-detail--loaded" v-else>
15
- <!-- Use this to override the title of the detail component. -->
16
- <slot name="title" v-bind="slotBindings">
17
- <!-- `<h1> "Create | Edit" + modelName </h1>` -->
18
- <h1 class="mapo-detail--title">{{ isNew ? $t("mapo.create") : $t("mapo.edit") }} {{ modelName }}</h1>
19
- </slot>
20
- <v-form class="mapo-detail--form" ref="form">
21
- <v-row>
22
- <v-col class="mapo-detail--main" cols="12" :md="12 - sidenavCol">
23
- <!-- Use this to add content at the top of the central layout. -->
24
- <slot name="body.top" v-bind="slotBindings"></slot>
25
- <!-- Use this to override the Language Switch panel. -->
26
- <slot name="body.lang" v-bind="slotBindings">
27
- <!-- [`DetailLangSwitch`](/components/detail/DetailLangSwitch/) -->
28
- <DetailLangSwitch
29
- v-if="langs && langs.length"
30
- v-show="langs.length > 1"
31
- class="mapo-detail--language-tabs"
32
- v-model="currentLang"
33
- :langs="langs"
34
- :errors="errors"
35
- /></slot>
36
- <!-- Use this to add content under the Language Switch panel. -->
37
- <slot name="body.top.underlang" v-bind="slotBindings"></slot>
38
- <!-- Use this to override the content of the main body. -->
39
- <slot name="body" v-bind="slotBindings">
40
- <Form
41
- v-model="model"
42
- :currentLang="currentLang"
43
- :languages="langs"
44
- :errors="errors"
45
- :fields="mainFields"
46
- :moreSlotBindings="slotBindings"
47
- :readonly="isReadonly"
48
- >
49
- <template v-for="(_, slot) in $slots" :slot="slot">
50
- <!-- @vuese-ignore -->
51
- <slot :name="slot"></slot>
52
- </template>
53
- <template v-for="(_, slot) in $scopedSlots" v-slot:[slot]="props">
54
- <!-- @vuese-ignore -->
55
- <slot :name="slot" v-bind="props" />
56
- </template>
57
- </Form>
58
- </slot>
59
- <!-- Use this to add content under the main body. -->
60
- <slot name="body.bottom" v-bind="slotBindings"></slot>
61
- </v-col>
62
- <v-col class="mapo-detail--sidenav" cols="12" :md="sidenavCol">
63
- <div class="mapo-detail--sidenav-col" :style="stickySide">
64
- <!-- Use this to add content on the top of the sidebar button panel. -->
65
- <slot name="side.buttons.top" v-bind="slotBindings"></slot>
66
- <!-- Use this to override the the sidebar button panel. -->
67
- <slot name="side.buttons" v-bind="slotBindings">
68
- <!-- Save, Save and continue, Back, and Delete buttons. -->
69
- <div class="mapo-detail--buttons">
70
- <!-- Use this to override the Save button. -->
71
- <slot name="button.save" v-bind="slotBindings">
72
- <!-- The Save button. -->
73
- <v-btn v-show="canGoBack" :loading="buttonClicked == 'saveAndBackBtn'" :disabled="isReadonly" tile block @click="saveItem(true)">{{
74
- isNew ? $t("mapo.create") : $t("mapo.save")
75
- }}</v-btn>
76
- </slot>
77
- <!-- Use this to override the Save and continue button. -->
78
- <slot name="button.savecontinue" v-bind="slotBindings">
79
- <!-- The Save and continue button. -->
80
- <v-btn :loading="buttonClicked == 'saveBtn'" :disabled="isReadonly" tile block @click="saveItem(false)"
81
- >{{ isNew ? $t("mapo.createContinue") : $t("mapo.saveContinue") }}</v-btn
82
- >
83
- </slot>
84
- <!-- Use this to override the Back button. -->
85
- <slot name="button.back" v-bind="slotBindings">
86
- <!-- The Back button. -->
87
- <v-btn v-show="canGoBack" tile block @click="back"
88
- >{{ $t("mapo.back") }}</v-btn
89
- >
90
- </slot>
91
- <!-- Use this to override the Delete button. -->
92
- <slot name="button.delete" :loading="buttonClicked == 'deleteBtn'" v-bind="slotBindings">
93
- <!-- The Delete button. -->
94
- <v-btn
95
- v-if="!isNew"
96
- color="error"
97
- tile
98
- block
99
- :disabled="!canDelete"
100
- @click="deleteItem"
101
- >{{ $t("mapo.delete") }}</v-btn
102
- >
103
- </slot>
104
- </div>
3
+ <div class="mapo-detail--loader" v-if="loading">
4
+ <v-progress-linear color="primary" indeterminate absolute></v-progress-linear>
5
+ <span class="detail-component-loading">
6
+ <v-icon size="30">mdi-timer-sand</v-icon>
7
+ {{ $t("mapo.fetchingData") }}
8
+ </span>
9
+ </div>
10
+ <div class="mapo-detail--loaded" v-else>
11
+ <!-- Use this to override the title of the detail component. -->
12
+ <slot name="title" v-bind="slotBindings">
13
+ <!-- `<h1> "Create | Edit" + modelName </h1>` -->
14
+ <h1 class="mapo-detail--title">
15
+ {{ isNew ? $t("mapo.create") : $t("mapo.edit") }} {{ modelName }}
16
+ </h1>
17
+ </slot>
18
+ <v-form class="mapo-detail--form" ref="form">
19
+ <v-row>
20
+ <v-col class="mapo-detail--main" cols="12" :md="12 - sidenavCol">
21
+ <!-- Use this to add content at the top of the central layout. -->
22
+ <slot name="body.top" v-bind="slotBindings"></slot>
23
+ <!-- Use this to override the Language Switch panel. -->
24
+ <slot name="body.lang" v-bind="slotBindings">
25
+ <!-- [`DetailLangSwitch`](/components/detail/DetailLangSwitch/) -->
26
+ <DetailLangSwitch
27
+ v-if="langs && langs.length"
28
+ v-show="langs.length > 1"
29
+ class="mapo-detail--language-tabs"
30
+ v-model="currentLang"
31
+ :langs="langs"
32
+ :errors="errors"
33
+ /></slot>
34
+ <!-- Use this to add content under the Language Switch panel. -->
35
+ <slot name="body.top.underlang" v-bind="slotBindings"></slot>
36
+ <!-- Use this to override the content of the main body. -->
37
+ <slot name="body" v-bind="slotBindings">
38
+ <Form
39
+ v-model="model"
40
+ :currentLang="currentLang"
41
+ :languages="langs"
42
+ :errors="errors"
43
+ :fields="mainFields"
44
+ :moreSlotBindings="slotBindings"
45
+ :readonly="isReadonly"
46
+ >
47
+ <template v-for="(_, slot) in $slots" :slot="slot">
48
+ <!-- @vuese-ignore -->
49
+ <slot :name="slot"></slot>
50
+ </template>
51
+ <template v-for="(_, slot) in $scopedSlots" v-slot:[slot]="props">
52
+ <!-- @vuese-ignore -->
53
+ <slot :name="slot" v-bind="props" />
54
+ </template>
55
+ </Form>
105
56
  </slot>
106
- <!-- Use this to add content on the top of the sidebar fields (or under sidebar buttons). -->
107
- <slot name="side.top" v-bind="slotBindings"></slot>
108
- <Form
109
- v-model="model"
110
- :currentLang="currentLang"
111
- :languages="langs"
112
- :errors="errors"
113
- :fields="sideFields"
114
- :moreSlotBindings="slotBindings"
115
- >
116
- <template v-for="(_, slot) in $slots" :slot="slot">
117
- <!-- @vuese-ignore -->
118
- <slot :name="slot"></slot>
119
- </template>
120
- <template v-for="(_, slot) in $scopedSlots" v-slot:[slot]="props">
121
- <!-- @vuese-ignore -->
122
- <slot :name="slot" v-bind="props" />
123
- </template>
124
- </Form>
125
- <!-- Use this to add content under the sidebar fields. -->
126
- <slot name="side.bottom" v-bind="slotBindings"></slot>
127
- </div>
128
- </v-col>
129
- </v-row>
130
- </v-form>
131
- </div>
57
+ <!-- Use this to add content under the main body. -->
58
+ <slot name="body.bottom" v-bind="slotBindings"></slot>
59
+ </v-col>
60
+ <v-col class="mapo-detail--sidenav" cols="12" :md="sidenavCol">
61
+ <div class="mapo-detail--sidenav-col" :style="stickySide">
62
+ <!-- Use this to add content on the top of the sidebar button panel. -->
63
+ <slot name="side.buttons.top" v-bind="slotBindings"></slot>
64
+ <!-- Use this to override the the sidebar button panel. -->
65
+ <slot name="side.buttons" v-bind="slotBindings">
66
+ <!-- Save, Save and continue, Back, and Delete buttons. -->
67
+ <div class="mapo-detail--buttons">
68
+ <!-- Use this to override the Save button. -->
69
+ <slot name="button.save" v-bind="slotBindings">
70
+ <!-- The Save button. -->
71
+ <v-btn
72
+ v-show="canGoBack"
73
+ :loading="buttonClicked == 'saveAndBackBtn'"
74
+ :disabled="isReadonly"
75
+ tile
76
+ block
77
+ @click="saveItem(true)"
78
+ >{{ isNew ? $t("mapo.create") : $t("mapo.save") }}</v-btn
79
+ >
80
+ </slot>
81
+ <!-- Use this to override the Save and continue button. -->
82
+ <slot name="button.savecontinue" v-bind="slotBindings">
83
+ <!-- The Save and continue button. -->
84
+ <v-btn
85
+ :loading="buttonClicked == 'saveBtn'"
86
+ :disabled="isReadonly"
87
+ tile
88
+ block
89
+ @click="saveItem(false)"
90
+ >{{
91
+ isNew ? $t("mapo.createContinue") : $t("mapo.saveContinue")
92
+ }}</v-btn
93
+ >
94
+ </slot>
95
+ <!-- Use this to override the Back button. -->
96
+ <slot name="button.back" v-bind="slotBindings">
97
+ <!-- The Back button. -->
98
+ <v-btn v-show="canGoBack" tile block @click="back">{{
99
+ $t("mapo.back")
100
+ }}</v-btn>
101
+ </slot>
102
+ <!-- Use this to override the Delete button. -->
103
+ <slot
104
+ name="button.delete"
105
+ :loading="buttonClicked == 'deleteBtn'"
106
+ v-bind="slotBindings"
107
+ >
108
+ <!-- The Delete button. -->
109
+ <v-btn
110
+ v-if="!isNew"
111
+ color="error"
112
+ tile
113
+ block
114
+ :disabled="!canDelete"
115
+ @click="deleteItem"
116
+ >{{ $t("mapo.delete") }}</v-btn
117
+ >
118
+ </slot>
119
+ </div>
120
+ </slot>
121
+ <!-- Use this to add content on the top of the sidebar fields (or under sidebar buttons). -->
122
+ <slot name="side.top" v-bind="slotBindings"></slot>
123
+ <Form
124
+ v-model="model"
125
+ :currentLang="currentLang"
126
+ :languages="langs"
127
+ :errors="errors"
128
+ :fields="sideFields"
129
+ :moreSlotBindings="slotBindings"
130
+ >
131
+ <template v-for="(_, slot) in $slots" :slot="slot">
132
+ <!-- @vuese-ignore -->
133
+ <slot :name="slot"></slot>
134
+ </template>
135
+ <template v-for="(_, slot) in $scopedSlots" v-slot:[slot]="props">
136
+ <!-- @vuese-ignore -->
137
+ <slot :name="slot" v-bind="props" />
138
+ </template>
139
+ </Form>
140
+ <!-- Use this to add content under the sidebar fields. -->
141
+ <slot name="side.bottom" v-bind="slotBindings"></slot>
142
+ </div>
143
+ </v-col>
144
+ </v-row>
145
+ </v-form>
146
+ </div>
132
147
  </div>
133
148
  </template>
134
149
 
135
150
  <style lang="scss" scoped>
136
- .mapo-detail--loader {
137
- .v-progress-linear {
138
- top: 0;
139
- }
140
- span{
141
- position: absolute;
142
- top: 50%;
143
- transform: translateY(-50%);
144
- width: 100%;
145
- display: flex;
146
- flex-direction: column;
147
- justify-content: center;
148
- align-items: center;
149
- }
150
- }
151
- .mapo-detail--title{
152
- margin-bottom: 20px;
153
- font-weight: normal;
151
+ .mapo-detail--loader {
152
+ .v-progress-linear {
153
+ top: 0;
154
154
  }
155
- .mapo-detail--language-tabs {
156
- margin-bottom: 20px;
157
- }
158
- .mapo-detail--sidenav-col {
155
+ span {
156
+ position: absolute;
157
+ top: 50%;
158
+ transform: translateY(-50%);
159
+ width: 100%;
159
160
  display: flex;
160
161
  flex-direction: column;
162
+ justify-content: center;
163
+ align-items: center;
161
164
  }
162
- .mapo-detail--buttons {
163
- margin-top: 16px;
164
- margin-bottom: 0px;
165
- order: 1;
166
- @media (min-width: 960px) {
167
- margin-bottom: 16px;
168
- margin-top: 0px;
169
- order: 0;
170
- }
171
- .v-btn{
172
- margin-bottom: 8px;
173
- }
165
+ }
166
+ .mapo-detail--title {
167
+ margin-bottom: 20px;
168
+ font-weight: normal;
169
+ }
170
+ .mapo-detail--language-tabs {
171
+ margin-bottom: 20px;
172
+ }
173
+ .mapo-detail--sidenav-col {
174
+ display: flex;
175
+ flex-direction: column;
176
+ }
177
+ .mapo-detail--buttons {
178
+ margin-top: 16px;
179
+ margin-bottom: 0px;
180
+ order: 1;
181
+ @media (min-width: 960px) {
182
+ margin-bottom: 16px;
183
+ margin-top: 0px;
184
+ order: 0;
174
185
  }
186
+ .v-btn {
187
+ margin-bottom: 8px;
188
+ }
189
+ }
175
190
  </style>
176
191
 
177
192
  <script>
@@ -182,280 +197,317 @@ import { diffObjs, deepClone } from "@mapomodule/utils/helpers/objHelpers";
182
197
  * A use case example could be "build a page that allows you to change the specifications of a product for an ecommerce".<br> <h4>Index:</h4> [[toc]]
183
198
  */
184
199
  export default {
185
- name: "Detail",
186
- data() {
187
- return {
188
- model: {},
189
- modelLanguages: [],
190
- currentLang: this.lang,
191
- errors: null,
192
- buttonClicked: null
193
- };
194
- },
195
- props: {
196
- // V-model of the object we are editing.
197
- value: {
198
- type: Object,
199
- required: false
200
- },
201
- // Set the current lang to value.
202
- lang: {
203
- type: String,
204
- required: false
205
- },
206
- // The main configuration that determines the arrangement of the fields in the detail layout.
207
- fields: {
208
- // [`DetailConfiguration`](#detailconfiguration)
209
- type: Object | Array,
210
- required: true
211
- },
212
- usePatch: {
213
- type: Boolean,
214
- default() { return this.$mapo.$options?.content?.usePatch || false; },
215
- },
216
- // A list of languages into which the payload needs to be translated.
217
- languages: {
218
- type: Array,
219
- default() { return this.$mapo.$options?.content?.languages || []; },
220
- },
221
- // Forces use of languages passed as prop, instead of those loaded with model
222
- forceLanguages: {
223
- type: Boolean,
224
- default() { return this.$mapo.$options?.content?.forceLanguages || false; },
225
- },
226
- // The url of the endpoint to which the payload is to be sent. From this url a complete crud (See [this.$mapo.$api.crud](/core/#$api.crud)) will be created.
227
- endpoint: {
228
- type: String,
229
- default: null
230
- },
231
- // This determines the width of the sidebar in desktop view in a 12 col grid.
232
- sidenavCol: {
233
- type: Number | String,
234
- default: "4",
235
- validator: function (value) {
236
- // The value must be a number between 0 and 12
237
- return [...Array(12).keys()].indexOf(+value) !== -1;
238
- },
239
- },
240
- // The identifier of the object you need to retrieve and edit. It can be "new" if you need to create a new payload.
241
- identifier: {
242
- // `String|Number`.
243
- type: String | Number,
244
- default: "new"
245
- },
246
- // The title of the detail page.
247
- modelName: String,
248
- // This determines the style of the sidebar. If set to true the sidebar will remain sticky during the scroll.
249
- sticky: {
250
- type: Boolean,
251
- default: true
252
- },
253
- // Set the multipart politic. Accepts `'auto'|'force|'disable'`. If auto is set the request is transformed in multipart if any file is in the payload. If set to force the request is transformed in multipart no matter if files are found. If set to `'disable'` the request is never transformed in multipart.
254
- multipart: {
255
- type: String,
256
- default: "auto",
257
- validator: function (value) {
258
- // The value must match one of these strings
259
- return ["auto", "force", "disable"].indexOf(value) !== -1;
260
- },
261
- },
200
+ name: "Detail",
201
+ data() {
202
+ return {
203
+ model: {},
204
+ modelLanguages: [],
205
+ currentLang: this.lang,
206
+ errors: null,
207
+ buttonClicked: null,
208
+ };
209
+ },
210
+ props: {
211
+ // V-model of the object we are editing.
212
+ value: {
213
+ type: Object,
214
+ required: false,
262
215
  },
263
- methods: {
264
- back() {
265
- this.$nuxt.context?.from?.name == "login" ? this.$router.go(-3) : this.$router.back();
266
- },
267
- saveItem(back = false) {
268
- this.buttonClicked = back ? "saveAndBackBtn" : "saveBtn";
269
- this.errors = null;
270
- this.$refs.form?.resetValidation();
271
- this.apiSendPayload().then((resp) => {
272
- this.model = resp;
273
- this.modelBkp = deepClone(this.model);
274
- this.$mapo.$snack.open({ message: this.isNew ? this.$t("mapo.createSuccess") : this.$t("mapo.saveSuccess"), color: "success" })
275
- this.isNew && this.$router.replace({ params: { detail: resp.id } })
276
- back && this.back();
277
- }).catch(error => {
278
- this.errors =
279
- (error.response.status == 400 && error.response.data) || null;
280
- this.$mapo.$snack.open({
281
- message: error.response?.data?.detail || this.$t("mapo.genericError"),
282
- color: "error"
216
+ // Set the current lang to value.
217
+ lang: {
218
+ type: String,
219
+ required: false,
220
+ },
221
+ // The main configuration that determines the arrangement of the fields in the detail layout.
222
+ fields: {
223
+ // [`DetailConfiguration`](#detailconfiguration)
224
+ type: Object | Array,
225
+ required: true,
226
+ },
227
+ usePatch: {
228
+ type: Boolean,
229
+ default() {
230
+ return this.$mapo.$options?.content?.usePatch || false;
231
+ },
232
+ },
233
+ // A list of languages into which the payload needs to be translated.
234
+ languages: {
235
+ type: Array,
236
+ default() {
237
+ return this.$mapo.$options?.content?.languages || [];
238
+ },
239
+ },
240
+ // Forces use of languages passed as prop, instead of those loaded with model
241
+ forceLanguages: {
242
+ type: Boolean,
243
+ default() {
244
+ return this.$mapo.$options?.content?.forceLanguages || false;
245
+ },
246
+ },
247
+ // The url of the endpoint to which the payload is to be sent. From this url a complete crud (See [this.$mapo.$api.crud](/core/#$api.crud)) will be created.
248
+ endpoint: {
249
+ type: String,
250
+ default: null,
251
+ },
252
+ // This determines the width of the sidebar in desktop view in a 12 col grid.
253
+ sidenavCol: {
254
+ type: Number | String,
255
+ default: "4",
256
+ validator: function (value) {
257
+ // The value must be a number between 0 and 12
258
+ return [...Array(12).keys()].indexOf(+value) !== -1;
259
+ },
260
+ },
261
+ // The identifier of the object you need to retrieve and edit. It can be "new" if you need to create a new payload.
262
+ identifier: {
263
+ // `String|Number`.
264
+ type: String | Number,
265
+ default: "new",
266
+ },
267
+ // The title of the detail page.
268
+ modelName: String,
269
+ // This determines the style of the sidebar. If set to true the sidebar will remain sticky during the scroll.
270
+ sticky: {
271
+ type: Boolean,
272
+ default: true,
273
+ },
274
+ // Set the multipart politic. Accepts `'auto'|'force|'disable'`. If auto is set the request is transformed in multipart if any file is in the payload. If set to force the request is transformed in multipart no matter if files are found. If set to `'disable'` the request is never transformed in multipart.
275
+ multipart: {
276
+ type: String,
277
+ default: "auto",
278
+ validator: function (value) {
279
+ // The value must match one of these strings
280
+ return ["auto", "force", "disable"].indexOf(value) !== -1;
281
+ },
282
+ },
283
+ },
284
+ methods: {
285
+ back() {
286
+ this.$nuxt.context?.from?.name == "login"
287
+ ? this.$router.go(-3)
288
+ : this.$router.back();
289
+ },
290
+ saveItem(back = false) {
291
+ this.buttonClicked = back ? "saveAndBackBtn" : "saveBtn";
292
+ this.errors = null;
293
+ this.$refs.form?.resetValidation();
294
+ this.apiSendPayload()
295
+ .then((resp) => {
296
+ this.model = resp;
297
+ this.modelBkp = deepClone(this.model);
298
+ this.$mapo.$snack.open({
299
+ message: this.isNew
300
+ ? this.$t("mapo.createSuccess")
301
+ : this.$t("mapo.saveSuccess"),
302
+ color: "success",
303
+ });
304
+ this.isNew && this.$router.replace({ params: { detail: resp.id } });
305
+ back && this.back();
306
+ })
307
+ .catch((error) => {
308
+ this.errors = (error.response.status == 400 && error.response.data) || null;
309
+ this.$mapo.$snack.open({
310
+ message: error.response?.data?.detail || this.$t("mapo.genericError"),
311
+ color: "error",
312
+ });
313
+ })
314
+ .finally(() => {
315
+ this.buttonClicked = null;
316
+ });
317
+ },
318
+ deleteItem() {
319
+ this.$mapo.$confirm
320
+ .open({
321
+ title: "Delete",
322
+ question: this.$t("mapo.confirmDelete"),
323
+ approveButton: {
324
+ text: this.$t("mapo.delete"),
325
+ attrs: { color: "red", text: true },
326
+ },
327
+ })
328
+ .then((res) => {
329
+ if (res) {
330
+ this.buttonClicked = "deleteBtn";
331
+ this.crud
332
+ .delete(this.identifier)
333
+ .then(() => {
334
+ this.unsetRouterGuard();
335
+ this.back();
283
336
  })
284
- }).finally(() => {this.buttonClicked = null;});;
285
- },
286
- deleteItem() {
287
- this.$mapo.$confirm
288
- .open({
289
- title: "Delete",
290
- question: this.$t("mapo.confirmDelete"),
291
- approveButton: { text: this.$t("mapo.delete"), attrs: { color: "red", text: true } }
292
- })
293
- .then(res => {
294
- if (res) {
295
- this.buttonClicked = "deleteBtn";
296
- this.crud
297
- .delete(this.identifier)
298
- .then(() => this.back())
299
- .catch(error => this.$mapo.$snack.open({
300
- message: error.response?.data?.detail || this.$t("mapo.genericError"),
301
- color: "error"
302
- })).finally(()=> {this.buttonClicked = null;});
303
- }
304
- });
305
- },
306
- setRouterGuard(){
307
- const checkUnsaved = async (to, from, next) => {
308
- if (to.name !== from.name && this.hasDiff){
309
- this.$mapo.$confirm
310
- .open({
311
- title: this.$t("mapo.unsavedData"),
312
- question: this.$t("mapo.confirmLeaveChanges"),
313
- approveButton: { text: this.$t("mapo.confirm"), attrs: { text: true } }
314
- }).then(next);
315
- } else {
316
- next();
317
- }
318
- };
319
- checkUnsaved.bind(this);
320
- this.$router.beforeHooks.push(checkUnsaved);
321
- window?.addEventListener('beforeunload', this.preventWindowClose);
322
- },
323
- preventWindowClose(event){
324
- const text = this.$t("mapo.confirmLeaveChanges");
325
- if (this.hasDiff) {
326
- event.returnValue = text;
327
- return text;
337
+ .catch((error) =>
338
+ this.$mapo.$snack.open({
339
+ message: error.response?.data?.detail || this.$t("mapo.genericError"),
340
+ color: "error",
341
+ })
342
+ )
343
+ .finally(() => {
344
+ this.buttonClicked = null;
345
+ });
328
346
  }
329
- },
330
- unsetRouterGuard(){
331
- window?.removeEventListener('beforeunload', this.preventWindowClose);
332
- this.$router.beforeHooks = this.$router.beforeHooks.filter(({name})=> name != 'checkUnsaved');
333
- }
347
+ });
334
348
  },
335
- watch: {
336
- currentLang(val) {
337
- // Fired when the current language changes.
338
- // @arg Emits the language name as a string.
339
- this.$emit("update:lang", val);
340
- },
341
- model(val) {
342
- // Fired when the v-model changes.
343
- // @arg Emits the entire payload modified.
344
- this.$emit("input", val);
345
- },
346
- lang(val) {
347
- if (val && this.langs.includes(val)) {
348
- this.currentLang = val;
349
- }
350
- },
351
- value(val) {
352
- if (val) {
353
- this.model = val;
354
- }
355
- },
349
+ async checkUnsavedHook(to, from, next) {
350
+ if (to.name !== from.name && this.hasDiff) {
351
+ this.$mapo.$confirm
352
+ .open({
353
+ title: this.$t("mapo.unsavedData"),
354
+ question: this.$t("mapo.confirmLeaveChanges"),
355
+ approveButton: { text: this.$t("mapo.confirm"), attrs: { text: true } },
356
+ })
357
+ .then(next);
358
+ } else {
359
+ next();
360
+ }
356
361
  },
357
- computed: {
358
- canGoBack() {
359
- if (typeof window !== "undefined") {
360
- return this.$nuxt?.context?.from?.name !== "login" && window.history.length > 1 || window.history.length > 3;
361
- }
362
- return true;
363
- },
364
- crud() {
365
- return this.$mapo.$api.crud(this.endpoint);
366
- },
367
- apiSendPayload(){
368
- let method, args;
369
- if (!this.model.id){
370
- method = this.crud.create;
371
- args = [this.model, {}, { multipart: this.multipart }]
372
- } else if (this.usePatch) {
373
- method = this.crud.partialUpdate;
374
- args = [this.model.id, this.modelDiff, {}, { multipart: this.multipart }]
375
- } else {
376
- method = this.crud.update;
377
- args = [this.model.id, this.model, {}, { multipart: this.multipart }]
378
- }
379
- return async () => method(...args)
380
- },
381
- isReadonly() {
382
- if (this.$mapo.$auth.routeMiddlewares.includes("permissions")) {
383
- return !this.$mapo.$auth.user.permissions.includes(this.isNew ? "add" : "change");
384
- }
385
- return !!this.buttonClicked;
386
- },
387
- canDelete() {
388
- if (this.$mapo.$auth.routeMiddlewares.includes("permissions")) {
389
- return this.$mapo.$auth.user.permissions.includes("delete");
390
- }
391
- return true;
392
- },
393
- mainFields() {
394
- return this.fields instanceof Array ? this.fields : this.fields.main || [];
395
- },
396
- sideFields() {
397
- return (this.fields instanceof Object && this.fields.sidenav) || [];
398
- },
399
- isNew() {
400
- return this.identifier && this.identifier == "new";
401
- },
402
- loading() {
403
- if (this.identifier === "new") {
404
- return false;
405
- }
406
- return !Object.keys(this.model).length;
407
- },
408
- langs() {
409
- return (!this.forceLanguages && (this.modelLanguages || []).length) ? this.modelLanguages : this.languages;
410
- },
411
- slotBindings() {
412
- return {
413
- model: this.model,
414
- errors: this.errors,
415
- currentLang: this.currentLang,
416
- crud: this.crud,
417
- isNew: this.isNew,
418
- langs: this.langs,
419
- loading: this.loading,
420
- form: this.$refs.form,
421
- saveItem: this.saveItem,
422
- deleteItem: this.deleteItem
423
- };
424
- },
425
- stickySide() {
426
- return this.sticky
427
- ? {
428
- position: "sticky",
429
- top: "60px"
430
- }
431
- : {};
432
- },
433
- modelDiff(){
434
- return diffObjs(this.modelBkp, this.model) || {};
435
- },
436
- hasDiff() {
437
- return !!(this.modelDiff && Object.keys(this.modelDiff).length > 0);
438
- }
362
+ setRouterGuard() {
363
+ this.$router.beforeHooks.push(this.checkUnsavedHook);
364
+ window?.addEventListener("beforeunload", this.preventWindowClose);
439
365
  },
440
- async mounted() {
441
- if (this.identifier && this.identifier !== "new") {
442
- try {
443
- this.model = await this.crud.detail(this.identifier);
444
- this.modelLanguages = this.model?.lang_info?.site_languages.map(l => l.id);
445
- } catch (error) {
446
- this.$nuxt.error({ statusCode: error.response.status, message: error.response.data?.detail || this.$t("mapo.genericError") });
366
+ preventWindowClose(event) {
367
+ const text = this.$t("mapo.confirmLeaveChanges");
368
+ if (this.hasDiff) {
369
+ event.returnValue = text;
370
+ return text;
371
+ }
372
+ },
373
+ unsetRouterGuard() {
374
+ window?.removeEventListener("beforeunload", this.preventWindowClose);
375
+ this.$router.beforeHooks = this.$router.beforeHooks.filter(
376
+ (f) => f != this.checkUnsavedHook
377
+ );
378
+ },
379
+ },
380
+ watch: {
381
+ currentLang(val) {
382
+ // Fired when the current language changes.
383
+ // @arg Emits the language name as a string.
384
+ this.$emit("update:lang", val);
385
+ },
386
+ model(val) {
387
+ // Fired when the v-model changes.
388
+ // @arg Emits the entire payload modified.
389
+ this.$emit("input", val);
390
+ },
391
+ lang(val) {
392
+ if (val && this.langs.includes(val)) {
393
+ this.currentLang = val;
394
+ }
395
+ },
396
+ value(val) {
397
+ if (val) {
398
+ this.model = val;
399
+ }
400
+ },
401
+ },
402
+ computed: {
403
+ canGoBack() {
404
+ if (typeof window !== "undefined") {
405
+ return (
406
+ (this.$nuxt?.context?.from?.name !== "login" && window.history.length > 1) ||
407
+ window.history.length > 3
408
+ );
409
+ }
410
+ return true;
411
+ },
412
+ crud() {
413
+ return this.$mapo.$api.crud(this.endpoint);
414
+ },
415
+ apiSendPayload() {
416
+ let method, args;
417
+ if (!this.model.id) {
418
+ method = this.crud.create;
419
+ args = [this.model, {}, { multipart: this.multipart }];
420
+ } else if (this.usePatch) {
421
+ method = this.crud.partialUpdate;
422
+ args = [this.model.id, this.modelDiff, {}, { multipart: this.multipart }];
423
+ } else {
424
+ method = this.crud.update;
425
+ args = [this.model.id, this.model, {}, { multipart: this.multipart }];
426
+ }
427
+ return async () => method(...args);
428
+ },
429
+ isReadonly() {
430
+ if (this.$mapo.$auth.routeMiddlewares.includes("permissions")) {
431
+ return !this.$mapo.$auth.user.permissions.includes(this.isNew ? "add" : "change");
432
+ }
433
+ return !!this.buttonClicked;
434
+ },
435
+ canDelete() {
436
+ if (this.$mapo.$auth.routeMiddlewares.includes("permissions")) {
437
+ return this.$mapo.$auth.user.permissions.includes("delete");
438
+ }
439
+ return true;
440
+ },
441
+ mainFields() {
442
+ return this.fields instanceof Array ? this.fields : this.fields.main || [];
443
+ },
444
+ sideFields() {
445
+ return (this.fields instanceof Object && this.fields.sidenav) || [];
446
+ },
447
+ isNew() {
448
+ return this.identifier && this.identifier == "new";
449
+ },
450
+ loading() {
451
+ if (this.identifier === "new") {
452
+ return false;
453
+ }
454
+ return !Object.keys(this.model).length;
455
+ },
456
+ langs() {
457
+ return !this.forceLanguages && (this.modelLanguages || []).length
458
+ ? this.modelLanguages
459
+ : this.languages;
460
+ },
461
+ slotBindings() {
462
+ return {
463
+ model: this.model,
464
+ errors: this.errors,
465
+ currentLang: this.currentLang,
466
+ crud: this.crud,
467
+ isNew: this.isNew,
468
+ langs: this.langs,
469
+ loading: this.loading,
470
+ form: this.$refs.form,
471
+ saveItem: this.saveItem,
472
+ deleteItem: this.deleteItem,
473
+ };
474
+ },
475
+ stickySide() {
476
+ return this.sticky
477
+ ? {
478
+ position: "sticky",
479
+ top: "60px",
447
480
  }
448
- }
449
- else if (this.value) {
450
- this.model = this.value;
451
- this.modelLanguages = this.model?.lang_info?.site_languages.map(l => l.id);
452
- }
453
- this.modelBkp = deepClone(this.model);
454
- this.setRouterGuard();
481
+ : {};
482
+ },
483
+ modelDiff() {
484
+ return diffObjs(this.modelBkp, this.model) || {};
485
+ },
486
+ hasDiff() {
487
+ return !!(this.modelDiff && Object.keys(this.modelDiff).length > 0);
455
488
  },
456
- destroyed(){
457
- this.unsetRouterGuard();
489
+ },
490
+ async mounted() {
491
+ if (this.identifier && this.identifier !== "new") {
492
+ try {
493
+ this.model = await this.crud.detail(this.identifier);
494
+ this.modelLanguages = this.model?.lang_info?.site_languages.map((l) => l.id);
495
+ } catch (error) {
496
+ this.$nuxt.error({
497
+ statusCode: error.response.status,
498
+ message: error.response.data?.detail || this.$t("mapo.genericError"),
499
+ });
500
+ }
501
+ } else if (this.value) {
502
+ this.model = this.value;
503
+ this.modelLanguages = this.model?.lang_info?.site_languages.map((l) => l.id);
458
504
  }
505
+ this.modelBkp = deepClone(this.model);
506
+ this.setRouterGuard();
507
+ },
508
+ destroyed() {
509
+ this.unsetRouterGuard();
510
+ },
459
511
  };
460
512
  </script>
461
513