rb-document-form-constructor 0.6.1 → 0.6.2

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