rb-document-form-constructor 0.6.4 → 0.6.7

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,202 +1,205 @@
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>
9
- <b-col lg="12">
10
- <h4>{{ section.labelRu }}</h4>
11
- </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
- <b-form-row :key="field.name" v-if="field.visible">
16
- <b-col lg="12">
17
- <b-form-group :state="validationState[field.name]"
18
- :invalid-feedback="validationState[`${field.name}__feedback`]"
19
- ref="inputContainer">
20
- <component v-bind:is="field.input.type"
21
- v-model="doc[field.name]"
22
- :disabled="!editable || !field.editable"
23
- :id="field.name"
24
- :state="validationState[field.name]"
25
- :ref="field.name"
26
- :required="field.required"
27
- @input="onEventFired('input', $event, field)"
28
- @change="onEventFired('change', $event, field)"
29
- @click="onEventFired('click', $event, field)"
30
- v-bind="field.input.propsData"></component>
31
- <template #label>
32
- <span :title="field.labelRu">{{ field.labelRu }}</span>
33
- <span v-if="showRequiredInLabel && field.required"
34
- class="text-danger">*</span>
35
- </template>
36
- </b-form-group>
37
- </b-col>
38
- </b-form-row>
39
- </template>
40
- </b-col>
41
-
42
- </template>
43
- </b-row>
44
- </b-container>
45
- </b-form>
46
- </template>
47
-
48
- <script>
49
- import Vue from 'vue';
50
- import {UtFormConstructor} from "../utils/UtFormConstructor";
51
- import typeOf from 'typeof';
52
- import safeEval from "notevil";
53
-
54
- export default {
55
- name: 'DocForm',
56
- props: {
57
- formConfig: Object,
58
- applyDefaultValues: {type: Boolean, default: true},
59
- doc: {type: Object, default: () => ({})},
60
- refSuffix: {type: String, default: 'Id'},
61
- editable: {type: Boolean, default: true},
62
- showRequiredInLabel: {type: Boolean, default: true},
63
- },
64
- data() {
65
- return {
66
- validationState: {}
67
- }
68
- },
69
- watch: {
70
- formConfig() {
71
- this.validationState = {};
72
- this.execApplyDefaultValues();
73
- this.execApplyDefaultValRule();
74
- }
75
- },
76
- methods: {
77
- onEventFired(eventName, event, field) {
78
- if (eventName === 'input' && field.ref && !field.multiple) {
79
- let dataField = null;
80
- if (field.name.lastIndexOf(this.refSuffix) >= 0) {
81
- dataField = field.name.substring(0, field.name.lastIndexOf(this.refSuffix));
82
- }
83
-
84
- if (dataField && dataField.length > 0) {
85
- this.doc[dataField] = null;
86
- }
87
- }
88
-
89
- if (field.rules) {
90
- field.rules.forEach(rule => {
91
- if (rule.event === eventName && rule.script) {
92
- let ruleContext = UtFormConstructor.getRuleContext();
93
- ruleContext.form = this;
94
- ruleContext.doc = this.doc;
95
- ruleContext.event = event;
96
- ruleContext.eventName = eventName;
97
- UtFormConstructor.runRule(ruleContext, rule.script);
98
- }
99
- })
100
- }
101
- },
102
- isValueEmpty(fieldName) {
103
- if (this.doc[fieldName] == null) {
104
- return true;
105
- }
106
-
107
- if (typeOf(this.doc[fieldName] === 'string') && this.doc[fieldName] === '') {
108
- return true;
109
- }
110
-
111
- return false;
112
- },
113
- validate() {
114
- this.formConfig.sections.forEach(s => {
115
- s.columns.forEach(c => {
116
- c.fields.forEach(f => {
117
- if (f.required && this.isValueEmpty(f.name)) {
118
- Vue.set(this.validationState, f.name, false);
119
- Vue.set(this.validationState, `${f.name}__feedback`,
120
- `Поле "${f.labelRu} обязательно"`);
121
- } else {
122
- Vue.set(this.validationState, f.name, null);
123
- }
124
-
125
- this.onEventFired('validate', {
126
- validationState: this.validationState,
127
- doc: this.doc,
128
- }, f);
129
- })
130
- })
131
- });
132
-
133
- for (let fieldName in this.validationState) {
134
- if (this.validationState[fieldName] === false) {
135
- return false;
136
- }
137
- }
138
-
139
- return true;
140
- },
141
- getColumnSize(section) {
142
- const MAX_COLUMN_SIZE = 12;
143
- if (!section || !section.columnCount) {
144
- return MAX_COLUMN_SIZE;
145
- }
146
-
147
- let colSize = Math.floor(MAX_COLUMN_SIZE / section.columnCount);
148
- return colSize;
149
- },
150
- execApplyDefaultValues() {
151
- if (this.applyDefaultValues) {
152
- this.formConfig.sections.forEach(r => {
153
- r.columns.forEach(c => {
154
- c.fields.forEach(f => {
155
- let defValue;
156
- if (this.defaultValue && typeOf(f.defaultValue) === 'function') {
157
- defValue = f.defaultValue();
158
- } else {
159
- defValue = f.defaultValue == null ? null : f.defaultValue;
160
- }
161
-
162
- this.$set(
163
- this.doc,
164
- f.name,
165
- f.defaultValue = defValue
166
- );
167
- })
168
- });
169
- });
170
- }
171
- },
172
- execApplyDefaultValRule() {
173
- this.formConfig.sections.forEach(el => {
174
- el.columns.forEach(c => {
175
- c.fields.forEach(f => {
176
- if (f.rules) {
177
- if (!f.defaultValue) {
178
- const rule = f.rules.find(rule => rule.event === 'defaultValue')
179
- if (rule && !this.doc[f.name]) {
180
- this.$set(
181
- this.doc,
182
- f.name,
183
- f.defaultValue = eval(rule.script)
184
- );
185
- }
186
- }
187
- }
188
- })
189
- });
190
- });
191
- }
192
- },
193
- mounted() {
194
- this.execApplyDefaultValues();
195
- this.execApplyDefaultValRule();
196
- },
197
- activated() {
198
- this.execApplyDefaultValues();
199
- this.execApplyDefaultValRule();
200
- }
201
- }
202
- </script>
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>
9
+ <b-col lg="12">
10
+ <h4>{{ section.labelRu }}</h4>
11
+ </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
+ <b-form-row :key="field.name" v-if="field.visible">
16
+ <b-col lg="12">
17
+ <b-form-group :state="validationState[field.name]"
18
+ :invalid-feedback="validationState[`${field.name}__feedback`]"
19
+ ref="inputContainer">
20
+ <component v-bind:is="field.input.type"
21
+ v-model="doc[field.name]"
22
+ :disabled="!editable || !field.editable"
23
+ :id="field.name"
24
+ :state="validationState[field.name]"
25
+ :ref="field.name"
26
+ :required="field.required"
27
+ @input="onEventFired('input', $event, field)"
28
+ @change="onEventFired('change', $event, field)"
29
+ @click="onEventFired('click', $event, field)"
30
+ v-bind="field.input.propsData"></component>
31
+ <template #label>
32
+ <span :title="field.labelRu">{{ field.labelRu }}</span>
33
+ <span v-if="showRequiredInLabel && field.required"
34
+ class="text-danger">*</span>
35
+ </template>
36
+ </b-form-group>
37
+ </b-col>
38
+ </b-form-row>
39
+ </template>
40
+ </b-col>
41
+
42
+ </template>
43
+ </b-row>
44
+ </b-container>
45
+ </b-form>
46
+ </template>
47
+
48
+ <script>
49
+ import Vue from 'vue';
50
+ import {UtFormConstructor} from "../utils/UtFormConstructor";
51
+ import typeOf from 'typeof';
52
+ import safeEval from "notevil";
53
+
54
+ export default {
55
+ name: 'DocForm',
56
+ props: {
57
+ formConfig: Object,
58
+ applyDefaultValues: {type: Boolean, default: true},
59
+ doc: {type: Object, default: () => ({})},
60
+ refSuffix: {type: String, default: 'Id'},
61
+ editable: {type: Boolean, default: true},
62
+ showRequiredInLabel: {type: Boolean, default: true},
63
+ },
64
+ data() {
65
+ return {
66
+ validationState: {}
67
+ }
68
+ },
69
+ watch: {
70
+ formConfig() {
71
+ this.validationState = {};
72
+ this.execApplyDefaultValues();
73
+ this.execApplyDefaultValRule();
74
+ }
75
+ },
76
+ methods: {
77
+ onEventFired(eventName, event, field) {
78
+ if (eventName === 'input' && field.ref && !field.multiple) {
79
+ let dataField = null;
80
+ if (field.name.lastIndexOf(this.refSuffix) >= 0) {
81
+ dataField = field.name.substring(0, field.name.lastIndexOf(this.refSuffix));
82
+ }
83
+
84
+ if (dataField && dataField.length > 0) {
85
+ this.doc[dataField] = null;
86
+ }
87
+ }
88
+
89
+ if (field.rules) {
90
+ field.rules.forEach(rule => {
91
+ if (rule.event === eventName && rule.script) {
92
+ let ruleContext = UtFormConstructor.getRuleContext();
93
+ ruleContext.form = this;
94
+ ruleContext.doc = this.doc;
95
+ ruleContext.event = event;
96
+ ruleContext.eventName = eventName;
97
+ UtFormConstructor.runRule(ruleContext, rule.script);
98
+ }
99
+ })
100
+ }
101
+ },
102
+ isValueEmpty(fieldName) {
103
+ if (this.doc[fieldName] == null) {
104
+ return true;
105
+ }
106
+
107
+ if (typeOf(this.doc[fieldName] === 'string') && this.doc[fieldName] === '') {
108
+ return true;
109
+ }
110
+
111
+ return false;
112
+ },
113
+ validate() {
114
+ this.formConfig.sections.forEach(s => {
115
+ s.columns.forEach(c => {
116
+ c.fields.forEach(f => {
117
+ if (f.required && this.isValueEmpty(f.name)) {
118
+ Vue.set(this.validationState, f.name, false);
119
+ Vue.set(this.validationState, `${f.name}__feedback`,
120
+ `Поле "${f.labelRu} обязательно"`);
121
+ } else {
122
+ Vue.set(this.validationState, f.name, null);
123
+ }
124
+
125
+ this.onEventFired('validate', {
126
+ validationState: this.validationState,
127
+ doc: this.doc,
128
+ }, f);
129
+ })
130
+ })
131
+ });
132
+
133
+ for (let fieldName in this.validationState) {
134
+ if (this.validationState[fieldName] === false) {
135
+ return false;
136
+ }
137
+ }
138
+
139
+ return true;
140
+ },
141
+ getColumnSize(section) {
142
+ const MAX_COLUMN_SIZE = 12;
143
+ if (!section || !section.columnCount) {
144
+ return MAX_COLUMN_SIZE;
145
+ }
146
+
147
+ let colSize = Math.floor(MAX_COLUMN_SIZE / section.columnCount);
148
+ return colSize;
149
+ },
150
+ execApplyDefaultValues() {
151
+ if (this.applyDefaultValues) {
152
+ this.formConfig.sections.forEach(r => {
153
+ r.columns.forEach(c => {
154
+ c.fields.forEach(f => {
155
+ let defValue;
156
+ if (this.defaultValue && typeOf(f.defaultValue) === 'function') {
157
+ defValue = f.defaultValue();
158
+ } else {
159
+ defValue = f.defaultValue == null ? null : f.defaultValue;
160
+ }
161
+
162
+ this.$set(
163
+ this.doc,
164
+ f.name,
165
+ f.defaultValue = defValue
166
+ );
167
+ })
168
+ });
169
+ });
170
+ }
171
+ },
172
+ execApplyDefaultValRule() {
173
+ this.formConfig.sections.forEach(el => {
174
+ el.columns.forEach(c => {
175
+ c.fields.forEach(f => {
176
+ if (f.rules) {
177
+ if (!f.defaultValue) {
178
+ const rule = f.rules.find(rule => rule.event === 'defaultValue')
179
+ if (rule && !this.doc[f.name]) {
180
+ this.$set(
181
+ this.doc,
182
+ f.name,
183
+ f.defaultValue = eval(rule.script)
184
+ );
185
+ }
186
+ }
187
+ }
188
+ })
189
+ });
190
+ });
191
+ }
192
+ },
193
+ mounted() {
194
+ this.$nextTick(() => {
195
+ this.execApplyDefaultValues();
196
+ this.execApplyDefaultValRule();
197
+ })
198
+
199
+ },
200
+ activated() {
201
+ this.execApplyDefaultValues();
202
+ this.execApplyDefaultValRule();
203
+ }
204
+ }
205
+ </script>