rb-document-form-constructor 0.8.38 → 0.8.40

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,239 +1,265 @@
1
1
  <template>
2
- <b-form v-if="formConfig" class="rb-doc-form">
3
- <b-container v-if="formConfig && formConfig.sections"
4
- v-for="(section) in formConfig.sections"
5
- :key="section.labelRu"
6
-
7
- class="rb-form-section">
8
- <b-row>
2
+ <b-form v-if="formConfig" class="rb-doc-form">
3
+ <b-container
4
+ v-if="formConfig && formConfig.sections"
5
+ v-for="section in formConfig.sections"
6
+ :key="section.labelRu"
7
+ class="rb-form-section"
8
+ >
9
+ <b-row>
10
+ <b-col lg="12">
11
+ <h4>{{ section.labelRu }}</h4>
12
+ </b-col>
13
+ <template v-for="column in section.columns">
14
+ <b-col :key="column.index" :lg="getColumnSize(section)" :sm="12">
15
+ <template v-for="field in column.fields">
16
+ {{ field.valueName }}
17
+ <b-form-row :key="field.name" v-if="field.visible">
9
18
  <b-col lg="12">
10
- <h4>{{ section.labelRu }}</h4>
19
+ <b-form-group
20
+ :state="validationState[field.name]"
21
+ :invalid-feedback="validationState[`${field.name}__feedback`]"
22
+ ref="inputContainer"
23
+ >
24
+ <component
25
+ v-bind:is="field.input.type"
26
+ v-model="doc[field.name]"
27
+ :resolve-value="getResolveValue(field)"
28
+ :disabled="!editable || !field.editable"
29
+ :id="field.name"
30
+ :state="validationState[field.name]"
31
+ :ref="field.name"
32
+ :required="field.required"
33
+ @input="onEventFired('input', $event, field)"
34
+ @change="onEventFired('change', $event, field)"
35
+ @click="onEventFired('click', $event, field)"
36
+ @set-resolve-value="setResolveValue"
37
+ v-bind="field.input.propsData"
38
+ ></component>
39
+ <template #label>
40
+ <span :title="field.labelRu">{{ field.labelRu }}</span>
41
+ <span v-if="showRequiredInLabel && field.required" class="text-danger"
42
+ >*</span
43
+ >
44
+ </template>
45
+ </b-form-group>
11
46
  </b-col>
12
- <template v-for="column in section.columns">
13
- <b-col :key="column.index" :lg="getColumnSize(section)" :sm="12">
14
- <template v-for="field in column.fields">
15
- {{field.valueName}}
16
- <b-form-row :key="field.name" v-if="field.visible">
17
- <b-col lg="12">
18
- <b-form-group :state="validationState[field.name]"
19
- :invalid-feedback="validationState[`${field.name}__feedback`]"
20
- ref="inputContainer">
21
- <component v-bind:is="field.input.type"
22
- v-model="doc[field.name]"
23
- :resolve-value="getResolveValue(field)"
24
- :disabled="!editable || !field.editable"
25
- :id="field.name"
26
- :state="validationState[field.name]"
27
- :ref="field.name"
28
- :required="field.required"
29
- @input="onEventFired('input', $event, field)"
30
- @change="onEventFired('change', $event, field)"
31
- @click="onEventFired('click', $event, field)"
32
- @set-resolve-value="setResolveValue"
33
- v-bind="field.input.propsData"></component>
34
- <template #label>
35
- <span :title="field.labelRu">{{ field.labelRu }}</span>
36
- <span v-if="showRequiredInLabel && field.required"
37
- class="text-danger">*</span>
38
- </template>
39
- </b-form-group>
40
- </b-col>
41
- </b-form-row>
42
- </template>
43
- </b-col>
44
-
45
- </template>
46
- </b-row>
47
- </b-container>
48
- </b-form>
47
+ </b-form-row>
48
+ </template>
49
+ </b-col>
50
+ </template>
51
+ </b-row>
52
+ </b-container>
53
+ </b-form>
49
54
  </template>
50
55
 
51
56
  <script>
52
57
  import Vue from 'vue';
53
- import {UtFormConstructor} from "../utils/UtFormConstructor";
58
+ import { UtFormConstructor } from '../utils/UtFormConstructor';
54
59
  import typeOf from 'typeof';
60
+ import { UtFormConfig } from '@/utils/UtFormConfig';
55
61
  // import safeEval from "notevil";
56
62
 
57
63
  export default {
58
- name: 'DocForm',
59
- props: {
60
- formConfig: Object,
61
- applyDefaultValues: {type: Boolean, default: true},
62
- doc: {type: Object, default: () => ({})},
63
- refSuffix: {type: String, default: 'Id'},
64
- editable: {type: Boolean, default: true},
65
- showRequiredInLabel: {type: Boolean, default: true},
64
+ name: 'DocForm',
65
+ props: {
66
+ formConfig: Object,
67
+ applyDefaultValues: { type: Boolean, default: true },
68
+ doc: { type: Object, default: () => ({}) },
69
+ refSuffix: { type: String, default: 'Id' },
70
+ editable: { type: Boolean, default: true },
71
+ showRequiredInLabel: { type: Boolean, default: true },
72
+ },
73
+ data() {
74
+ return {
75
+ validationState: {},
76
+ };
77
+ },
78
+ watch: {
79
+ formConfig() {
80
+ this.validationState = {};
81
+ this.execApplyDefaultValues();
82
+ this.execApplyDefaultValRule();
66
83
  },
67
- data() {
68
- return {
69
- validationState: {}
70
- }
84
+ },
85
+ methods: {
86
+ getResolveValueName(field) {
87
+ if ((field.dict || field.ref) && !field.multiple) {
88
+ return field.name.substring(0, field.name.length - 2);
89
+ }
90
+ return field.name;
71
91
  },
72
- watch: {
73
- formConfig() {
74
- this.validationState = {};
75
- this.execApplyDefaultValues();
76
- this.execApplyDefaultValRule();
92
+ getResolveValue(field) {
93
+ return this.doc[this.getResolveValueName(field)]
94
+ ? this.doc[this.getResolveValueName(field)]
95
+ : null;
96
+ },
97
+ setResolveValue({ field, multiple = false }) {
98
+ let resolveValue = this.getResolveValue(field);
99
+ if (resolveValue && !multiple) {
100
+ resolveValue = null;
101
+ }
102
+ },
103
+ onEventFired(eventName, event, field) {
104
+ if (eventName === 'input' && field.ref && !field.multiple) {
105
+ let dataField = null;
106
+ if (field.name.lastIndexOf(this.refSuffix) >= 0) {
107
+ dataField = field.name.substring(0, field.name.lastIndexOf(this.refSuffix));
77
108
  }
109
+
110
+ if (dataField && dataField.length > 0) {
111
+ this.doc[dataField] = null;
112
+ }
113
+ }
114
+
115
+ if (field.rules) {
116
+ field.rules.forEach((rule) => {
117
+ if (rule.event === eventName && rule.script) {
118
+ this.runRule(rule, { event, eventName });
119
+ }
120
+ });
121
+ }
78
122
  },
79
- methods: {
80
- getResolveValueName(field) {
81
- if ((field.dict || field.ref) && !field.multiple) {
82
- return field.name.substring(0, field.name.length - 2)
83
- }
84
- return field.name
85
- },
86
- getResolveValue(field) {
87
- return this.doc[this.getResolveValueName(field)] ? this.doc[this.getResolveValueName(field)] : null
88
- },
89
- setResolveValue({field, multiple = false}) {
90
- let resolveValue = this.getResolveValue(field);
91
- if (resolveValue && !multiple) {
92
- resolveValue = null
123
+ onGlobalEventFired(eventName, event) {
124
+ let fields = UtFormConfig.getFields(this.formConfig);
125
+ fields.forEach((f) => {
126
+ if (f.rules) {
127
+ f.rules.forEach((r) => {
128
+ if (r.event === eventName) {
129
+ this.runRule(r, { event, eventName });
93
130
  }
131
+ });
132
+ }
133
+ });
134
+ },
135
+ createRuleContext(additionalContext) {
136
+ return Object.assign(
137
+ {
138
+ form: this,
139
+ doc: this.doc,
140
+ ...additionalContext,
94
141
  },
95
- onEventFired(eventName, event, field) {
96
- if (eventName === 'input' && field.ref && !field.multiple) {
97
- let dataField = null;
98
- if (field.name.lastIndexOf(this.refSuffix) >= 0) {
99
- dataField = field.name.substring(0, field.name.lastIndexOf(this.refSuffix));
100
- }
142
+ UtFormConstructor.getRuleContext(),
143
+ );
144
+ },
145
+ runRule(rule, context) {
146
+ UtFormConstructor.runRule(this.createRuleContext(context), rule.script);
147
+ },
148
+ isValueEmpty(fieldName) {
149
+ if (!(this.doc[fieldName] && this.doc[fieldName]?.length)) {
150
+ return true;
151
+ }
152
+ if (Array.isArray(this.doc[fieldName]) && !this.doc[fieldName]?.length) {
153
+ return true;
154
+ }
101
155
 
102
- if (dataField && dataField.length > 0) {
103
- this.doc[dataField] = null;
104
- }
105
- }
156
+ if (typeOf(this.doc[fieldName] === 'string') && this.doc[fieldName] === '') {
157
+ return true;
158
+ }
159
+ return false;
160
+ },
161
+ isValueLessThanMin(fieldname, min) {
162
+ if (this.doc[fieldname] && min) {
163
+ return parseInt(this.doc[fieldname]) < parseInt(min);
164
+ }
106
165
 
107
- if (field.rules) {
108
- field.rules.forEach(rule => {
109
- if (rule.event === eventName && rule.script) {
110
- let ruleContext = UtFormConstructor.getRuleContext();
111
- ruleContext.form = this;
112
- ruleContext.doc = this.doc;
113
- ruleContext.event = event;
114
- ruleContext.eventName = eventName;
115
- UtFormConstructor.runRule(ruleContext, rule.script);
116
- }
117
- })
118
- }
119
- },
120
- isValueEmpty(fieldName) {
121
- if (this.doc[fieldName] == null) {
122
- return true;
123
- }
124
- if (!this.doc[fieldName]?.length) {
125
- return true;
126
- }
166
+ return false;
167
+ },
168
+ validate() {
169
+ this.formConfig.sections.forEach((s) => {
170
+ s.columns.forEach((c) => {
171
+ c.fields.forEach((f) => {
172
+ let feedback = '';
127
173
 
128
- if (typeOf(this.doc[fieldName] === 'string') && this.doc[fieldName] === '') {
129
- return true;
130
- }
131
- return false;
132
- },
133
- isValueLessThanMin(fieldname, min) {
134
- if (this.doc[fieldname] && min) {
135
- return parseInt(this.doc[fieldname]) < parseInt(min)
174
+ if (f.required && this.isValueEmpty(f.name)) {
175
+ feedback += `Поле "${f.labelRu}" обязательно`;
136
176
  }
137
177
 
138
- return false;
139
- },
140
- validate() {
141
- this.formConfig.sections.forEach(s => {
142
- s.columns.forEach(c => {
143
- c.fields.forEach(f => {
144
- let feedback = '';
178
+ if (f.type === 'integer' && this.isValueLessThanMin(f.name, f.input.propsData.min)) {
179
+ feedback += `\nМинимальное значение для этого поля ${f.input.propsData.min}`;
180
+ }
145
181
 
146
- if (f.required && this.isValueEmpty(f.name)) {
147
- feedback += `Поле "${f.labelRu}" обязательно`
148
- }
182
+ if (feedback) {
183
+ Vue.set(this.validationState, f.name, false);
184
+ Vue.set(this.validationState, `${f.name}__feedback`, feedback);
185
+ } else {
186
+ Vue.set(this.validationState, f.name, null);
187
+ }
149
188
 
150
- if (f.type === 'integer' && this.isValueLessThanMin(f.name, f.input.propsData.min)) {
151
- feedback += `\nМинимальное значение для этого поля ${f.input.propsData.min}`;
152
- }
189
+ this.onEventFired(
190
+ 'validate',
191
+ {
192
+ validationState: this.validationState,
193
+ doc: this.doc,
194
+ },
195
+ f,
196
+ );
197
+ });
198
+ });
199
+ });
200
+ for (let fieldName in this.validationState) {
201
+ if (this.validationState[fieldName] === false) {
202
+ return false;
203
+ }
204
+ }
153
205
 
154
- if (feedback) {
155
- Vue.set(this.validationState, f.name, false)
156
- Vue.set(this.validationState, `${f.name}__feedback`, feedback);
157
- } else {
158
- Vue.set(this.validationState, f.name, null);
159
- }
206
+ return true;
207
+ },
208
+ getColumnSize(section) {
209
+ const MAX_COLUMN_SIZE = 12;
210
+ if (!section || !section.columnCount) {
211
+ return MAX_COLUMN_SIZE;
212
+ }
160
213
 
161
- this.onEventFired('validate', {
162
- validationState: this.validationState,
163
- doc: this.doc,
164
- }, f);
165
- })
166
- })
167
- });
168
- for (let fieldName in this.validationState) {
169
- if (this.validationState[fieldName] === false) {
170
- return false;
214
+ let colSize = Math.floor(MAX_COLUMN_SIZE / section.columnCount);
215
+ return colSize;
216
+ },
217
+ execApplyDefaultValues() {
218
+ if (this.applyDefaultValues) {
219
+ this.formConfig.sections.forEach((r) => {
220
+ r.columns.forEach((c) => {
221
+ c.fields.forEach((f) => {
222
+ if (f.defaultValue) {
223
+ let defValue;
224
+ if (this.defaultValue && typeOf(f.defaultValue) === 'function') {
225
+ defValue = f.defaultValue();
226
+ } else {
227
+ defValue = f.defaultValue == null ? null : f.defaultValue;
171
228
  }
172
- }
173
-
174
- return true;
175
- },
176
- getColumnSize(section) {
177
- const MAX_COLUMN_SIZE = 12;
178
- if (!section || !section.columnCount) {
179
- return MAX_COLUMN_SIZE;
180
- }
181
-
182
- let colSize = Math.floor(MAX_COLUMN_SIZE / section.columnCount);
183
- return colSize;
184
- },
185
- execApplyDefaultValues() {
186
- if (this.applyDefaultValues) {
187
- this.formConfig.sections.forEach(r => {
188
- r.columns.forEach(c => {
189
- c.fields.forEach(f => {
190
- if (f.defaultValue) {
191
- let defValue;
192
- if (this.defaultValue && typeOf(f.defaultValue) === 'function') {
193
- defValue = f.defaultValue();
194
- } else {
195
- defValue = f.defaultValue == null ? null : f.defaultValue;
196
- }
197
229
 
198
- this.$set(
199
- this.doc,
200
- f.name,
201
- f.defaultValue = defValue
202
- );
203
- }
204
- })
205
- });
206
- });
207
- }
208
- },
209
- execApplyDefaultValRule() {
210
- this.formConfig.sections.forEach(el => {
211
- el.columns.forEach(c => {
212
- c.fields.forEach(f => {
213
- if (f.rules) {
214
- if (!f.defaultValue) {
215
- const rule = f.rules.find(rule => rule.event === 'defaultValue')
216
- if (rule && !this.doc[f.name]) {
217
- this.$set(
218
- this.doc,
219
- f.name,
220
- f.defaultValue = eval(rule.script)
221
- );
222
- }
223
- }
224
- }
225
- })
226
- });
230
+ this.$set(this.doc, f.name, (f.defaultValue = defValue));
231
+ }
227
232
  });
228
- }
233
+ });
234
+ });
235
+ }
229
236
  },
230
- mounted() {
231
- this.execApplyDefaultValues();
232
- this.execApplyDefaultValRule();
237
+ execApplyDefaultValRule() {
238
+ this.formConfig.sections.forEach((el) => {
239
+ el.columns.forEach((c) => {
240
+ c.fields.forEach((f) => {
241
+ if (f.rules) {
242
+ if (!f.defaultValue) {
243
+ const rule = f.rules.find((rule) => rule.event === 'defaultValue');
244
+ if (rule && !this.doc[f.name]) {
245
+ this.$set(this.doc, f.name, (f.defaultValue = eval(rule.script)));
246
+ }
247
+ }
248
+ }
249
+ });
250
+ });
251
+ });
233
252
  },
234
- activated() {
235
- this.execApplyDefaultValues();
236
- this.execApplyDefaultValRule();
237
- }
238
- }
253
+ },
254
+ mounted() {
255
+ this.execApplyDefaultValues();
256
+ this.execApplyDefaultValRule();
257
+ this.onGlobalEventFired('form-mounted', this);
258
+ },
259
+ activated() {
260
+ this.execApplyDefaultValues();
261
+ this.execApplyDefaultValRule();
262
+ this.onGlobalEventFired('form-activated', this);
263
+ },
264
+ };
239
265
  </script>