@mapomodule/uikit 1.0.0-alpha.21 → 1.0.0-alpha.22

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,224 @@
1
+ <template>
2
+ <div class="row">
3
+ <div
4
+ class="col-12"
5
+ :class="field.class"
6
+ v-for="(field, index) in computedFields"
7
+ :key="index"
8
+ v-show="show(field)"
9
+ :style="show(field, 'visibility') ? '' : 'opacity: 0; visibility: hidden;'"
10
+ >
11
+ <FormTabs
12
+ v-if="field.tabs"
13
+ v-model="model"
14
+ v-bind="{ currentLang, errors, languages }"
15
+ :conf="field"
16
+ :moreSlotBindings="slotBindings"
17
+ >
18
+ <template
19
+ v-for="slot in nameSpacedSlots($slots, `group.${field.group.slug}.`)"
20
+ :slot="slot"
21
+ >
22
+ <slot :name="`group.${field.group.slug}.${slot}`"></slot>
23
+ </template>
24
+ <template
25
+ v-for="slot in nameSpacedSlots($scopedSlots, `group.${field.group.slug}.`)"
26
+ v-slot:[slot]="props"
27
+ >
28
+ <slot :name="`group.${field.group.slug}.${slot}`" v-bind="props" />
29
+ </template>
30
+ </FormTabs>
31
+ <FormGroup
32
+ v-else-if="field.group"
33
+ v-model="model"
34
+ v-bind="{ currentLang, errors, languages }"
35
+ :conf="field"
36
+ :moreSlotBindings="slotBindings"
37
+ >
38
+ <template
39
+ v-for="slot in nameSpacedSlots($slots, `group.${field.group.slug}.`)"
40
+ :slot="slot"
41
+ >
42
+ <slot :name="`group.${field.group.slug}.${slot}`"></slot>
43
+ </template>
44
+ <template
45
+ v-for="slot in nameSpacedSlots($scopedSlots, `group.${field.group.slug}.`)"
46
+ v-slot:[slot]="props"
47
+ >
48
+ <slot :name="`group.${field.group.slug}.${slot}`" v-bind="props" />
49
+ </template>
50
+ </FormGroup>
51
+
52
+ <div v-else>
53
+ <slot :name="field.slotName + '.before'" v-bind="{ conf: field, ...slotBindings, }" />
54
+ <slot
55
+ :name="field.slotName"
56
+ v-bind="{ conf: field, ...slotBindings, }"
57
+ >
58
+ <FormField
59
+ v-model="model"
60
+ :langs="languages"
61
+ :currentLang="currentLang"
62
+ :conf="field"
63
+ :errors="errors"
64
+ >
65
+ <template
66
+ v-for="slot in nameSpacedSlots($slots, `fields.${field.value}.`)"
67
+ :slot="slot"
68
+ >
69
+ <slot :name="`fields.${field.value}.${slot}`"></slot>
70
+ </template>
71
+ <template
72
+ v-for="slot in nameSpacedSlots($scopedSlots, `fields.${field.value}.`)"
73
+ v-slot:[slot]="props"
74
+ >
75
+ <slot :name="`fields.${field.value}.${slot}`" v-bind="props" />
76
+ </template>
77
+ </FormField>
78
+ </slot>
79
+ <slot :name="field.slotName + '.after'" v-bind="{ conf: field, ...slotBindings, }" />
80
+ </div>
81
+ </div>
82
+ </div>
83
+ </template>
84
+ <script>
85
+ import { nameSpacedSlots } from "@mapomodule/utils/helpers/slots";
86
+ import { slugify, titleCase } from "@mapomodule/utils/helpers/formatters";
87
+ import { findPropPaths, getPointed, setPointed } from "@mapomodule/utils/helpers/objHelpers";
88
+
89
+ export default {
90
+ name: "Form",
91
+ data() {
92
+ return {
93
+ model: {},
94
+ };
95
+ },
96
+ props: {
97
+ // V-model of the object we are editing.
98
+ value: {
99
+ type: Object,
100
+ required: true,
101
+ },
102
+ // Set the current lang to value.
103
+ currentLang: {
104
+ type: String,
105
+ required: false,
106
+ },
107
+ // An object representing all the errors of all fields. This means that this component will find the error of its field following the dottedPath of the value in the error dict.
108
+ errors: Object,
109
+ // Additional bindings to indrease slots capabilities
110
+ moreSlotBindings: Object,
111
+ // The main configuration that determines the arrangement of the fields in the detail layout.
112
+ fields: {
113
+ // [`DetailConfiguration`](#detailconfiguration)
114
+ type: Object | Array,
115
+ required: true,
116
+ },
117
+ // A list of languages into which the payload needs to be translated.
118
+ languages: {
119
+ type: Array,
120
+ default: () => [],
121
+ },
122
+ },
123
+ watch: {
124
+ value(val) {
125
+ this.model = val;
126
+ this.initLang();
127
+ },
128
+ model(val) {
129
+ this.$emit("input", val);
130
+ },
131
+ },
132
+ methods: {
133
+ nameSpacedSlots,
134
+ initLang(lang = this.currentLang) {
135
+ if (lang && !this.model.translations) {
136
+ this.model.translations = {};
137
+ }
138
+ if (lang && !this.model.translations[lang]) {
139
+ this.model.translations[lang] = {};
140
+ }
141
+ },
142
+ parseConf(field, i) {
143
+ const conf = typeof field === "string" ? { value: field } : field;
144
+ conf.value = conf.value || "";
145
+ conf.value = conf.value.replace(new RegExp(`^translations\.(${this.languages.join("|")})\.?`), "");
146
+ conf.slotName = `fields.${conf.value || i}`;
147
+ if (this.currentLang && !field.synci18n) {
148
+ const base = `translations.${this.currentLang}`;
149
+ conf.value = (conf.value && `${base}.${conf.value}`) || base;
150
+ }
151
+ if (this.isReadonly) {
152
+ conf.attrs = { ...conf.attrs, readonly: true };
153
+ }
154
+ return conf;
155
+ },
156
+ parseGroup(group, icon="mdi-cube-outline") {
157
+ return typeof group === "string"
158
+ ? { name: group, slug: slugify && slugify(group), icon }
159
+ : { slug: slugify && slugify(group.name), icon, ...group };
160
+ },
161
+ parseTabs(tabs) {
162
+ return Object.keys(tabs).map(key => {
163
+ const tab = tabs[key]
164
+ return {
165
+ label: titleCase(key.replace(/_/, " ")),
166
+ slug: key,
167
+ ...tab.tab,
168
+ fields: this.mapConf(Array.isArray(tab) && tab || tab.fields || [])
169
+ }
170
+ })
171
+ },
172
+ parseTagsGroup(field){
173
+ const gType = typeof field.group
174
+ const slug = gType == "string" && slugify(field.group) || gType == "object" && slugify(field.group?.name) || Object.keys(field.tabs).join("-")
175
+ return this.parseGroup(gType == "string" ? { ...this.parseGroup(field.group, null), slug} : { slug,...field.group }, null)
176
+ },
177
+ mapConf(fields) {
178
+ ////// This is an hack to prevent render loop
179
+ const functions = findPropPaths(fields, ({ val }) => typeof val == "function" ).reduce((acc, key) => ({[key]: getPointed(fields, key, undefined) , ...acc}), {})
180
+ fields = JSON.parse(JSON.stringify(fields))
181
+ Object.keys(functions).forEach(key => setPointed(fields, key, functions[key]))
182
+ //// End of hack ====> TODO: find a better way to avoid loops
183
+ return fields.map((f, i) =>
184
+ f.tabs
185
+ ? { group: this.parseTagsGroup(f), tabs: this.parseTabs(f.tabs) }
186
+ : f.group
187
+ ? { group: this.parseGroup(f.group), fields: this.mapConf(f.fields) }
188
+ : this.parseConf(f, i)
189
+ );
190
+ },
191
+ show(conf, type = "display"){
192
+ const prop = type == "visibility" ? "vVisible" : "vShow"
193
+ if (typeof conf[prop] == "function") {
194
+ return conf[prop]({
195
+ model: this.model,
196
+ errors: this.errors,
197
+ languages: this.languages,
198
+ currentLang: this.currentLang
199
+ })
200
+ }
201
+ return true
202
+ }
203
+
204
+ },
205
+ computed: {
206
+ slotBindings() {
207
+ return {
208
+ model: this.model,
209
+ errors: this.errors,
210
+ currentLang: this.currentLang,
211
+ langs: this.languages,
212
+ ...this.moreSlotBindings,
213
+ };
214
+ },
215
+ computedFields() {
216
+ return this.mapConf(this.fields);
217
+ },
218
+ },
219
+ created(){
220
+ this.model = this.value
221
+ this.initLang();
222
+ }
223
+ };
224
+ </script>
@@ -1,7 +1,16 @@
1
1
  <template>
2
2
  <div>
3
3
  <span class="mapo-label" :class="`mapo-label-${conf.value}`">{{label}}</span>
4
- <component :is="is" v-model="model" v-bind="fieldAttrs"></component>
4
+ <component :is="is" v-model="model" v-bind="fieldAttrs">
5
+ <template v-for="(_, slot) in $slots" :slot="slot">
6
+ <!-- @vuese-ignore -->
7
+ <slot :name="slot"></slot>
8
+ </template>
9
+ <template v-for="(_, slot) in $scopedSlots" v-slot:[slot]="props">
10
+ <!-- @vuese-ignore -->
11
+ <slot :name="slot" v-bind="props" />
12
+ </template>
13
+ </component>
5
14
  </div>
6
15
  </template>
7
16
 
@@ -12,23 +21,25 @@
12
21
  </style>
13
22
 
14
23
  <script>
15
- import wygEditor from "@mapomodule/uikit/components/fields/wygEditor/wygEditor.vue";
16
- import DateField from "@mapomodule/uikit/components/fields/DateField.vue";
17
- import FksField from "@mapomodule/uikit/components/fields/FksField.vue";
18
- import MediaM2mField from "@mapomodule/uikit/components/fields/MediaM2mField.vue";
19
- import MediaField from "@mapomodule/uikit/components/fields/MediaField.vue";
20
- import SeoPreview from "@mapomodule/uikit/components/fields/SeoPreview.vue";
21
- import FileField from "@mapomodule/uikit/components/fields/FileField.vue";
22
- import Repeater from "@mapomodule/uikit/components/fields/Repeater.vue"
24
+ import wygEditor from "@mapomodule/uikit/components/Form/fields/wygEditor/wygEditor.vue";
25
+ import DateField from "@mapomodule/uikit/components/Form/fields/DateField.vue";
26
+ import FksField from "@mapomodule/uikit/components/Form/fields/FksField.vue";
27
+ import MediaM2mField from "@mapomodule/uikit/components/Form/fields/MediaM2mField.vue";
28
+ import MediaField from "@mapomodule/uikit/components/Form/fields/MediaField.vue";
29
+ import SeoPreview from "@mapomodule/uikit/components/Form/fields/SeoPreview.vue";
30
+ import FileField from "@mapomodule/uikit/components/Form/fields/FileField.vue";
31
+ import Repeater from "@mapomodule/uikit/components/Form/fields/Repeater.vue"
23
32
 
24
33
  import { getPointed, setPointed } from "@mapomodule/utils/helpers/objHelpers";
34
+ import { titleCase } from "@mapomodule/utils/helpers/formatters";
35
+
25
36
  import defaults from "./defaults";
26
37
 
27
38
  /**
28
39
  * This is mainly an internal component. It is used by the [`DetailComponent`](/components/detail/Detail/) in order to render dinamic fields inside the main form.
29
40
  */
30
41
  export default {
31
- name: "DetailField",
42
+ name: "FormField",
32
43
  components: {
33
44
  wygEditor,
34
45
  DateField,
@@ -81,12 +92,21 @@ export default {
81
92
  }
82
93
  },
83
94
  model(val) {
84
- const dump = { ...this.value };
95
+ const dump = { ...this.value }; // since we cannot directly edit a prop
85
96
  setPointed(
86
97
  dump,
87
98
  this.conf.value,
88
- this.accessor.set({ model: { ...this.value }, val })
99
+ this.accessor.set({ model: dump, val })
89
100
  );
101
+ if (typeof this.conf.onChange == "function")
102
+ this.conf.onChange({
103
+ val,
104
+ model: dump,
105
+ currentLang: this.currentLang,
106
+ languages: this.langs,
107
+ conf: this.conf
108
+ })
109
+
90
110
  // Fired when the v-model changes.
91
111
  // @arg Emits the entire payload modified.
92
112
  this.$emit("input", dump);
@@ -142,9 +162,6 @@ export default {
142
162
  if (this.conf.label !== undefined) {
143
163
  return this.conf.label;
144
164
  }
145
- const titleCase = (string) =>
146
- (string && string[0].toUpperCase() + string.slice(1).toLowerCase()) ||
147
- "";
148
165
  return titleCase(
149
166
  (this.conf.value || "")
150
167
  .replace(/^translations\.\w\w\./, "")
@@ -0,0 +1,88 @@
1
+ <template>
2
+ <v-card v-if="conf" class="my-2 rounded-0">
3
+ <v-card-title :class="conf.group.titleClass">
4
+ <slot name="title.before" v-bind="slotBindings" />
5
+ <slot name="title" v-bind="slotBindings">
6
+ <v-icon left> {{ conf.group.icon }} </v-icon>
7
+ <span>{{ conf.group.name }}</span>
8
+ </slot>
9
+ <slot name="title.after" v-bind="slotBindings" />
10
+ </v-card-title>
11
+ <div class="container">
12
+ <Form
13
+ v-model="model"
14
+ v-bind="{ currentLang, errors, languages }"
15
+ :fields="conf.fields"
16
+ :moreSlotBindings="slotBindings"
17
+ >
18
+ <template v-for="(_, slot) in $slots" :slot="slot">
19
+ <!-- @vuese-ignore -->
20
+ <slot :name="slot" />
21
+ </template>
22
+ <template v-for="(_, slot) in $scopedSlots" v-slot:[slot]="props">
23
+ <!-- @vuese-ignore -->
24
+ <slot :name="slot" v-bind="props" />
25
+ </template>
26
+ </Form>
27
+ </div>
28
+ </v-card>
29
+ </template>
30
+
31
+ <script>
32
+ export default {
33
+ name: "FormGroup",
34
+ data() {
35
+ return {
36
+ model: {},
37
+ };
38
+ },
39
+ props: {
40
+ // V-model of the object we are editing.
41
+ value: {
42
+ type: Object,
43
+ required: true,
44
+ },
45
+ // Set the current lang to value.
46
+ currentLang: {
47
+ type: String,
48
+ required: false,
49
+ },
50
+ // An object representing all the errors of all fields. This means that this component will find the error of its field following the dottedPath of the value in the error dict.
51
+ errors: Object,
52
+ // Additional bindings to indrease slots capabilities
53
+ moreSlotBindings: Object,
54
+ // The main configuration that determines the arrangement of the fields in the detail layout.
55
+ conf: {
56
+ type: Object,
57
+ required: true,
58
+ },
59
+ // A list of languages into which the payload needs to be translated.
60
+ languages: {
61
+ type: Array,
62
+ default: () => [],
63
+ },
64
+ },
65
+ watch: {
66
+ value(val) {
67
+ this.model = val;
68
+ },
69
+ model(val) {
70
+ this.$emit("input", val);
71
+ },
72
+ },
73
+ computed: {
74
+ slotBindings() {
75
+ return {
76
+ model: this.model,
77
+ errors: this.errors,
78
+ currentLang: this.currentLang,
79
+ langs: this.languages,
80
+ ...this.moreSlotBindings,
81
+ };
82
+ },
83
+ },
84
+ created() {
85
+ this.model = this.value;
86
+ },
87
+ };
88
+ </script>
@@ -0,0 +1,134 @@
1
+ <template>
2
+ <v-card v-if="conf" class="my-2 rounded-0">
3
+ <v-card-title :class="conf.group.titleClass" v-if="hasTitle">
4
+ <slot name="title.before" v-bind="slotBindings" />
5
+ <slot name="title" v-bind="slotBindings">
6
+ <v-icon left> {{ conf.group.icon }} </v-icon>
7
+ <span>{{ conf.group.name }}</span>
8
+ </slot>
9
+ <slot name="title.after" v-bind="slotBindings" />
10
+ </v-card-title>
11
+ <div class="container">
12
+ <v-tabs v-model="tabIndex" class="tab-wrapper">
13
+ <v-tab v-for="(tab, index) in conf.tabs" :key="index">
14
+ <v-badge :value="hasErrors(tab)" color="error" dot>
15
+ {{ tab.label }} <v-icon v-if="tab.icon">{{ tab.icon }}</v-icon>
16
+ </v-badge>
17
+ </v-tab>
18
+ </v-tabs>
19
+ <v-tabs-items v-model="tabIndex">
20
+ <v-tab-item v-for="(tab, index) in conf.tabs" :key="index" class="pt-8">
21
+ <slot :name="`tab.${tab.slug}`" v-bind="slotBindings">
22
+ <Form
23
+ v-model="model"
24
+ v-bind="{ currentLang, errors, languages }"
25
+ :fields="tab.fields"
26
+ :moreSlotBindings="slotBindings"
27
+ >
28
+ <template
29
+ v-for="slot in nameSpacedSlots($slots, `tab.${tab.slug}.`)"
30
+ :slot="slot"
31
+ >
32
+ <slot :name="`tab.${tab.slug}.${slot}`"></slot>
33
+ </template>
34
+ <template
35
+ v-for="slot in nameSpacedSlots(
36
+ $scopedSlots,
37
+ `tab.${tab.slug}.`
38
+ )"
39
+ v-slot:[slot]="props"
40
+ >
41
+ <slot :name="`tab.${tab.slug}.${slot}`" v-bind="props" />
42
+ </template>
43
+ </Form>
44
+ </slot>
45
+ </v-tab-item>
46
+ </v-tabs-items>
47
+ </div>
48
+ </v-card>
49
+ </template>
50
+
51
+ <style lang="scss" scoped>
52
+ .tab-wrapper {
53
+ margin: -12px 0 0 -12px;
54
+ }
55
+ </style>
56
+ <script>
57
+ import { findPropPaths } from "@mapomodule/utils/helpers/objHelpers";
58
+ import { nameSpacedSlots } from "@mapomodule/utils/helpers/slots";
59
+
60
+ export default {
61
+ name: "FormTabs",
62
+ data() {
63
+ return {
64
+ model: {},
65
+ tabIndex: 0,
66
+ };
67
+ },
68
+ props: {
69
+ // V-model of the object we are editing.
70
+ value: {
71
+ type: Object,
72
+ required: true,
73
+ },
74
+ // Set the current lang to value.
75
+ currentLang: {
76
+ type: String,
77
+ required: false,
78
+ },
79
+ // An object representing all the errors of all fields. This means that this component will find the error of its field following the dottedPath of the value in the error dict.
80
+ errors: Object,
81
+ // Additional bindings to indrease slots capabilities
82
+ moreSlotBindings: Object,
83
+ // The main configuration that determines the arrangement of the fields in the detail layout.
84
+ conf: {
85
+ type: Object,
86
+ required: true,
87
+ },
88
+ // A list of languages into which the payload needs to be translated.
89
+ languages: {
90
+ type: Array,
91
+ default: () => [],
92
+ },
93
+ },
94
+ watch: {
95
+ value(val) {
96
+ this.model = val;
97
+ },
98
+ model(val) {
99
+ this.$emit("input", val);
100
+ },
101
+ },
102
+ computed: {
103
+ slotBindings() {
104
+ return {
105
+ model: this.model,
106
+ errors: this.errors,
107
+ currentLang: this.currentLang,
108
+ langs: this.languages,
109
+ ...this.moreSlotBindings,
110
+ };
111
+ },
112
+ hasTitle() {
113
+ return !!(
114
+ Object.keys({ ...this.$scopedSlots, ...this.$slots }).some((s) =>
115
+ s.startsWith("title")
116
+ ) ||
117
+ this.conf.group.name ||
118
+ this.conf.group.icon
119
+ );
120
+ },
121
+ },
122
+ methods: {
123
+ nameSpacedSlots,
124
+ hasErrors(tab) {
125
+ return findPropPaths(this.errors || {}, ({ val }) =>
126
+ Array.isArray(val)
127
+ ).some((k) => tab.fields.some((f) => k.startsWith(f.value)));
128
+ },
129
+ },
130
+ created() {
131
+ this.model = this.value;
132
+ },
133
+ };
134
+ </script>
File without changes
File without changes
File without changes
File without changes