-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodel.js
More file actions
143 lines (134 loc) · 4.52 KB
/
model.js
File metadata and controls
143 lines (134 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { defineAsyncComponent, defineComponent } from 'vue';
export const model = defineComponent({
components: {
ObjectFormWrapper: defineAsyncComponent(() =>
import('@appscode/vue-openapi-form/src/components/ObjectFormWrapper.vue').then(
(module) => module.default
)
),
ObjectForm: defineAsyncComponent(() =>
import('@appscode/vue-openapi-form/src/components/ObjectForm.vue').then((module) => module.default)
),
ArrayInput: defineAsyncComponent(() =>
import('@appscode/vue-openapi-form/src/components/ArrayInput.vue').then((module) => module.default)
),
KeyValuePairs: defineAsyncComponent(() =>
import('@appscode/vue-openapi-form/src/components/KeyValuePairs.vue').then((module) => module.default)
),
SimpleInput: defineAsyncComponent(() =>
import('@appscode/vue-openapi-form/src/components/SimpleInput.vue').then((module) => module.default)
),
},
props: {
type: {
type: String,
default: 'string',
},
referenceModel: {
type: null,
default: () => ({}),
},
},
emits: ['update:modelValue'],
data() {
return {
modelData: null,
};
},
watch: {
modelData: {
immediate: true,
deep: true,
handler(newVal, oldVal) {
if (oldVal !== null && oldVal !== undefined) {
// clean the newVal if it's array or object if the cleanObject global data is true
if (this.cleanObject) this.clean(newVal);
// prevent number from converting to string
if (this.type === 'number' || this.type === 'integer') {
// if the newVal string is empty, emit null
if (newVal === '') this.$emit('update:modelValue', null);
else this.$emit('update:modelValue', +newVal);
} else this.$emit('update:modelValue', newVal);
}
},
},
modelValue: {
deep: true,
handler(newVal, oldVal) {
// do this only once, when the value object is initialized after api call or some delay
if (JSON.stringify(oldVal) !== JSON.stringify(newVal))
this.initModelData();
},
},
},
created() {
this.initModelData();
},
methods: {
initModelData() {
if (this.modelValue) {
if (
(this.type === 'object' || this.type === 'key-value-pairs') &&
Object.keys(this.modelValue).length > 0
)
this.modelData = JSON.parse(JSON.stringify(this.modelValue));
else if (this.type === 'array' && this.modelValue.length > 0)
this.modelData = JSON.parse(JSON.stringify(this.modelValue));
else if (this.type === 'boolean' && this.modelValue !== null)
this.modelData = this.modelValue;
else if (
this.type === 'string' ||
this.type === 'number' ||
this.type === 'integer'
)
this.modelData = this.modelValue;
else this.modelData = this.initWithBlank();
} else this.modelData = this.initWithBlank();
},
initWithBlank() {
if (this.type === 'object' || this.type === 'key-value-pairs') return {};
else if (this.type === 'array') return [];
else if (this.type === 'boolean') return false;
else if (this.type === 'number' || this.type === 'integer') return null;
else return '';
},
clean(ob) {
if (this.type === 'object' || this.type === 'key-value-pairs') {
Object.keys(ob).forEach((key) => {
let stringify = '';
if (typeof ob[key] !== 'string') stringify = JSON.stringify(ob[key]);
else stringify = ob[key];
if (
stringify === undefined ||
stringify === 'null' ||
stringify === '' ||
stringify === '{}' ||
stringify === '[]'
) {
delete ob[key];
}
});
} else if (this.type === 'array') {
let arrayOfDeleteIndexes = ob
.map((item, idx) => ({ item, idx }))
.filter((el) => {
const item = el.item;
let stringify = '';
if (typeof item !== 'string') stringify = JSON.stringify(item);
else stringify = item;
if (
stringify === undefined ||
stringify === 'null' ||
stringify === '' ||
stringify === '{}' ||
stringify === '[]'
) {
return true;
} else return false;
})
.map((item) => item.idx);
arrayOfDeleteIndexes.forEach((idx) => ob.splice(idx, 1));
}
},
},
});