DBEditor.vue 11.2 KB
Newer Older
1
<template>
2
3
    <b-modal v-if="object_definition != null" @ok="commit()" cancel-variant="outline-secondary" cancel-title="Abbrechen"
             :ok-title="function2text(object_function)"
4
5
             :ok-variant="function2variant(object_function)" size="lg" scrollable :busy="object_definition == null"
             :id="modal_id"
janis.streib's avatar
janis.streib committed
6
7
             :title="object_definition.name.replace(/^\w/, c => c.toUpperCase()) + ' ' + function2text(object_function)"
             :static="false">
8
        <form :id="modal_id + '-dbeditform'" v-if="object_definition != null">
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
            <template v-if="!non_optionals_order">
                <template v-for="(data,attr) in object_functions[object_function].parameters">
                    <DataEditField :object_attribute="data" :attribute_preset="presets[attr]"
                                   :attribute_reference_name="object_definition_reference_names_by_attribute[attr]"
                                   :attribute_referencing="object_definition_referencing_by_attribute[attr]"
                                   :attribute_name="attr"
                                   :input_reducer="input_reducer[attr]"
                                   v-bind:key="attr"
                                   v-if="data.new.isRequired || non_optionals.includes(attr)"></DataEditField>
                </template>
                <template>
                    <b-link v-b-toggle.collapse-optional>Optionale Parameter</b-link>
                    <b-collapse id="collapse-optional">
                        <hr/>
                        <template v-for="(data,attr) in object_functions[object_function].parameters">
                            <DataEditField :object_attribute="data" :attribute_preset="presets[attr]"
                                           :attribute_reference_name="object_definition_reference_names_by_attribute[attr]"
                                           :attribute_referencing="object_definition_referencing_by_attribute[attr]"
                                           :attribute_name="attr"
                                           :input_reducer="input_reducer[attr]"
                                           v-bind:key="attr"
                                           v-if="!data.new.isRequired && !non_optionals.includes(attr)"></DataEditField>
                        </template>

                    </b-collapse>
                </template>
            </template>
            <template v-else>
                <template v-for="attr in non_optionals_order">
38
39
                    <DataEditField :object_attribute="object_functions[object_function].parameters[attr]"
                                   :attribute_preset="presets[attr]"
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
                                   :attribute_reference_name="object_definition_reference_names_by_attribute[attr]"
                                   :attribute_referencing="object_definition_referencing_by_attribute[attr]"
                                   :attribute_name="attr"
                                   :input_reducer="input_reducer[attr]"
                                   v-bind:key="attr"></DataEditField>
                </template>
                <template>
                    <b-link v-b-toggle.collapse-optional>Optionale Parameter</b-link>
                    <b-collapse id="collapse-optional">
                        <hr/>
                        <template v-for="(data,attr) in object_functions[object_function].parameters">
                            <DataEditField :object_attribute="data" :attribute_preset="presets[attr]"
                                           :attribute_reference_name="object_definition_reference_names_by_attribute[attr]"
                                           :attribute_referencing="object_definition_referencing_by_attribute[attr]"
                                           :attribute_name="attr"
                                           :input_reducer="input_reducer[attr]"
                                           v-bind:key="attr"
                                           v-if="!non_optionals_order.includes(attr)"></DataEditField>
                        </template>

                    </b-collapse>
                </template>
            </template>
        </form>
    </b-modal>
</template>

<script>
    import ObjectTypeService from '@/api-services.gen/wapi.object_type';
    import FunctionService from '@/api-services.gen/wapi.function';
    import ApiUtil from '@/util/apiutil'
    import DataEditField from "./DataEditField";
72
    import TransactionUtil from '@/util/transactionutil';
73
    import {v4 as uuidv4} from 'uuid';
74
75
76
77
78
79
80
81
82

    export default {
        name: "DBEditor",
        components: {DataEditField},
        data() {
            return {
                object_definition: null,
                object_definition_referencing_by_attribute: null,
                object_definition_reference_names_by_attribute: null,
83
                object_functions: null,
84
85
86
            }
        },
        methods: {
87
88
89
90
91
            commit() {
                let form = new FormData(document.getElementById(this.modal_id + '-dbeditform'))
                let elems = form.entries()
                let ta = {'object_fq_name': this.object_fq_name, 'object_function': this.object_function}
                let new_params = {}
92
                for (let e of elems) {
93
94
                    new_params[e[0]] = e[1]
                }
janis.streib's avatar
janis.streib committed
95
                ta['parameters'] = {'new': new_params, 'old': this.old_data}
96
97
98
99
                ta['old_uuid'] = this.old_uuid
                ta['uuid'] = uuidv4()
                ta['object_fq_name'] = this.object_fq_name
                ta['object_function'] = this.object_function
janis.streib's avatar
janis.streib committed
100
101
102
                ta['input_reducer'] = this.input_reducer
                ta['non_optionals_order'] = this.non_optionals_order
                ta['non_optionals'] = this.non_optionals
janis.streib's avatar
janis.streib committed
103
                ta['func_descr'] = this.object_functions[this.object_function].parameters
janis.streib's avatar
janis.streib committed
104
105
                let prev_fields = this.preview_fields
                if (prev_fields == null) {
106
                    if (this.non_optionals.length !== 0) {
janis.streib's avatar
janis.streib committed
107
108
                        prev_fields = this.non_optionals
                    }
109
                    else if (this.non_optionals_order && this.non_optionals_order.length !==  0) {
janis.streib's avatar
janis.streib committed
110
111
112
113
114
115
116
117
118
119
120
121
                        prev_fields = this.non_optionals_order
                    }
                    else {
                        prev_fields = []
                        for(let data in this.object_functions[this.object_function].parameters) {
                            if(this.object_functions[this.object_function].parameters[data].new.isRequired) {
                                prev_fields.push(data)
                            }
                        }
                    }
                }
                ta['preview_fields'] = prev_fields
122
                this.$store.commit('addTransactionElement', ta)
123
                this.$emit('commited', ta)
124
125
126
                if(!this.isMobile() && !this.$store.state.show_sidebar) {
                    this.$store.state.show_sidebar = true
                }
127
            },
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
            async validateObjectFQName() {
                let parts = this.object_fq_name.split('.')
                let system = parts[0]
                let name = parts[1]
                let object_result = ObjectTypeService.list(this.$store.state.netdb_axios_config, {
                    name_list: [name],
                    system_list: [system]
                })
                try {
                    let object_definition = await object_result
                    object_definition = object_definition.data[0]
                    if (object_definition.length !== 1) {
                        return false
                    }
                    let obj_functions = await FunctionService.list(this.$store.state.netdb_axios_config, {
                        object_type_list: [name],
                        system_list: [system]
                    })
                    return {
                        'object_definition': object_definition[0],
                        'object_functions': ApiUtil.dict_by_value_of_array(obj_functions.data[0], 'name')
                    }
                } catch (e) {
                    return false
                }
            },
154
155
            function2variant: TransactionUtil.function2variant,
            function2text: TransactionUtil.function2text,
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
            async validateObjectFunction() {
                // TODO
            }
        },
        props: {
            object_fq_name: {
                required: true
            },
            modal_id: String,
            object_function: {
                type: String,
                required: true
            },
            presets: {
                type: Object,
                default() {
                    return {}
                }
            },
            input_reducer: {
                type: Object,
                default() {
                    return {}
                }
            },
janis.streib's avatar
janis.streib committed
181
182
183
184
185
186
187
            old_data: {
                required: false,
                default() {
                    return {}
                },
                type: Object
            },
188
189
190
191
192
193
194
195
196
197
            non_optionals: {
                required: false,
                default() {
                    return []
                },
                type: Array
            },
            non_optionals_order: {
                required: false,
                type: Array
198
199
200
201
202
203
204
205
            },
            old_uuid: {
                required: false,
                type: String,
                default() {
                    return null
                }
            },
janis.streib's avatar
janis.streib committed
206
207
208
209
210
211
212
            preview_fields: {
                required: false,
                type: Array,
                default() {
                    return null
                }
            },
213
214
215
216
        },
        watch: {
            $props: {
                immediate: true,
217
                deep: true,
218
                async handler() {
219
220
221
222
223
                    window.console.debug("Watch update")
                    if (this.object_fq_name == null) {
                        window.console.debug("Skip object_fq_name check")
                        return
                    }
janis.streib's avatar
janis.streib committed
224

225
226
227
228
229
230
231
232
233
234
235
236
237
238
                    let data = await this.validateObjectFQName()
                    if (data === false) {
                        window.console.error("Invalid object_fq_name!")
                    } else {
                        this.object_definition = data.object_definition
                        this.object_definition_referencing_by_attribute = {}
                        this.object_definition_reference_names_by_attribute = {}
                        for (let ref in this.object_definition.referencing) {
                            for (let attrib of this.object_definition.referencing[ref].attributes) {
                                this.object_definition_referencing_by_attribute[attrib] = this.object_definition.referencing[ref].references
                                this.object_definition_reference_names_by_attribute[attrib] = ref
                            }
                        }
                        this.object_functions = data.object_functions
239
                        this.$emit('ready')
240
241
242
243
244
245
246
247
248
249
                    }
                }
            }
        }
    }
</script>

<style scoped>

</style>