rb-document-form-constructor 0.9.16 → 0.9.18

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,287 +1,287 @@
1
- <template>
2
- <b-modal :id="id"
3
- :title="title"
4
- modal-class="rb-field-rule-form-modal"
5
- size="lg"
6
- @ok.prevent="onOk"
7
- ok-title="Сохранить правило"
8
- cancel-variant="outline-gray"
9
- cancel-title="Отмена">
10
- <b-card no-body>
11
- <b-tabs card @activate-tab="onActivateTab">
12
- <b-tab title="Правило" active>
13
- <b-form v-if="innerRule">
14
- <b-form-row>
15
- <b-col log="12">
16
- <b-form-group label="Выбрать" v-if="rulePresets.length > 0">
17
- <template #description>
18
- Выберите готовое правило из списка и если нужно, измените
19
- </template>
20
- <b-form-select @input="onRuleSelected($event)">
21
- <b-form-select-option v-for="r in rulePresets" :key="r.name"
22
- :value="r.name">
23
- {{ r.labelRu }} ({{ r.name }})
24
- </b-form-select-option>
25
- </b-form-select>
26
- </b-form-group>
27
- </b-col>
28
- <b-col lg="12">
29
- <b-form-group label="Название">
30
- <template #description>
31
- Задайте уникальное название, чтобы отличать правило в списке
32
- </template>
33
- <b-form-input v-model="innerRule.name"
34
- :state="state.name"
35
- :invalid-feedback="state.name_feedback"
36
- ></b-form-input>
37
- </b-form-group>
38
- </b-col>
39
- <b-col lg="12">
40
- <b-form-group label="Событие">
41
- <template #description>
42
- При возникновении этого события будет выполняться правило
43
- </template>
44
- <b-form-select v-model="innerRule.event"
45
- :state="state.event"
46
- :invalid-feedback="state.event_feedback">
47
- <b-form-select-option :value="'input'">
48
- Ввод значения
49
- </b-form-select-option>
50
- <b-form-select-option :value="'change'">
51
- Изменение значения
52
- </b-form-select-option>
53
- <b-form-select-option :value="'click'">
54
- Клик
55
- </b-form-select-option>
56
- <b-form-select-option :value="'validate'">
57
- Валидация
58
- </b-form-select-option>
59
- <b-form-select-option :value="'defaultValue'">
60
- Установить значение по умолчанию
61
- </b-form-select-option>
62
- <b-form-select-option :value="'created'">
63
- Инпут создан
64
- </b-form-select-option>
65
- <b-form-select-option :value="'mounted'">
66
- Инпут подключен
67
- </b-form-select-option>
68
- <b-form-select-option :value="'activated'">
69
- Инпут активирован
70
- </b-form-select-option>
71
- <b-form-select-option :value="'form-mounted'">
72
- Форма создана
73
- </b-form-select-option>
74
- <b-form-select-option :value="'form-activated'">
75
- Форма показана
76
- </b-form-select-option>
77
- </b-form-select>
78
- </b-form-group>
79
- </b-col>
80
- <b-col lg="12">
81
- <b-form-group label="Скрипт">
82
- <template #description>
83
- Здесь указывается скрипт правила. Скрипт должен быть написан на языке
84
- javascript
85
- </template>
86
- <div class="rb-script-input">
87
- <b-button-toolbar>
88
- <b-dropdown text="Переменные" variant="outline-secondary"
89
- size="sm" class="mx-1">
90
- <b-dropdown-item @click="addVariableToScript('doc')">
91
- Документ
92
- </b-dropdown-item>
93
- <b-dropdown-item @click="addVariableToScript('form')">
94
- Форма
95
- </b-dropdown-item>
96
- <b-dropdown-item @click="addVariableToScript('event')">
97
- Значение события
98
- </b-dropdown-item>
99
- </b-dropdown>
100
- <b-dropdown text="Задать значение" variant="outline-secondary"
101
- size="sm" class="mx-1">
102
- <b-dropdown-item v-for="f in fields" :key="f.name"
103
- v-if="fields" @click="addSetVariableToScript(f)">
104
- {{ f.labelRu }} ({{ f.name }})
105
- </b-dropdown-item>
106
- </b-dropdown>
107
- <b-dropdown text="Вызвать функцию инпута" variant="outline-secondary"
108
- size="sm" class="mx-1">
109
- <b-dropdown-item v-for="f in fields" :key="f.name"
110
- v-if="fields" @click="addCallInputFunction(f)">
111
- {{ f.labelRu }} ({{ f.name }})
112
- </b-dropdown-item>
113
- </b-dropdown>
114
- </b-button-toolbar>
115
- <b-form-textarea v-model="innerRule.script"
116
- :state="state.script"
117
- :invalid-feedback="state.script_feedback"
118
- rows="8"
119
- ref="scriptInput"
120
- ></b-form-textarea>
121
- </div>
122
- </b-form-group>
123
- </b-col>
124
- </b-form-row>
125
- </b-form>
126
- </b-tab>
127
- <b-tab title="Протестировать правило" v-if="innerFormConfig">
128
- <doc-form :form-config="innerFormConfig">
129
- </doc-form>
130
- </b-tab>
131
- </b-tabs>
132
- </b-card>
133
- </b-modal>
134
- </template>
135
-
136
- <script>
137
- import {UtFormConfig} from "../utils/UtFormConfig";
138
- import {UtFormConstructor} from "../utils/UtFormConstructor";
139
- import DocForm from "./DocForm";
140
- import {UtRandom} from "@/utils/UtRandom.js";
141
-
142
- export default {
143
- name: 'FieldRuleFormModal',
144
- components: {DocForm},
145
- props: {
146
- field: Object,
147
- rule: Object,
148
- formConfig: Object,
149
- mode: {type: String, default: 'ins'},
150
- onAfterOk: Function,
151
- },
152
- data() {
153
- return {
154
- id: 'rb-field-rule-form-modal',
155
- state: this.getDefaultState(),
156
- innerFormConfig: null,
157
- innerRule: null,
158
- }
159
- },
160
- computed: {
161
- title() {
162
- return this.mode === 'ins' ? 'Добавление правила' : 'Редактирование правила';
163
- },
164
- fields() {
165
- return UtFormConfig.getFields(this.formConfig);
166
- },
167
- rulePresets() {
168
- return UtFormConstructor.getAvailableFieldRules(this.field);
169
- }
170
- },
171
- watch: {
172
- formConfig() {
173
- this.copyToInnerFormConfig();
174
- },
175
- rule() {
176
- if (this.rule) {
177
- this.innerRule = this.rule;
178
- } else {
179
- this.innerRule = this.getDefaultRule();
180
- }
181
-
182
- }
183
- },
184
- methods: {
185
- validateFields(fieldName) {
186
- let fields = fieldName ? [fieldName] : ['name', 'event', 'script'];
187
- fields.forEach(field => {
188
- if (!this.innerRule[field]) {
189
- this.state[field] = false;
190
- this.state[`${field}_feedback`] = 'Заполните название';
191
- } else {
192
- this.state[field] = true;
193
- this.state[`${field}_feedback`] = null;
194
- }
195
- });
196
- },
197
- copyToInnerFormConfig() {
198
- this.innerFormConfig = JSON.parse(JSON.stringify(this.formConfig));
199
- },
200
- applyRuleToInnerFormConfig() {
201
- if (this.innerFormConfig) {
202
- let foundRule = UtFormConfig.findRule(this.innerRule.id, this.innerFormConfig);
203
-
204
- if (!foundRule) {
205
- let foundField = UtFormConfig.findField(
206
- this.field.name, this.innerFormConfig
207
- );
208
-
209
- if (foundField) {
210
- foundField.rules = foundField.rules ? foundField.rules : [];
211
- foundField.rules.push(this.rule);
212
- }
213
- } else {
214
- Object.assign(foundRule, this.innerRule);
215
- }
216
- }
217
- },
218
- onActivateTab(index) {
219
- if (index > 0) {
220
- this.copyToInnerFormConfig();
221
- this.applyRuleToInnerFormConfig();
222
- }
223
- },
224
- getDefaultState() {
225
- return {
226
- name: null,
227
- event: null,
228
- script: null,
229
- }
230
- },
231
- getDefaultRule() {
232
- return {
233
- id: UtRandom.getRandomString(10),
234
- name: null,
235
- event: null,
236
- script: null,
237
- }
238
- },
239
- resetModal() {
240
- this.state = this.getDefaultState();
241
- this.innerRule = null;
242
- },
243
- addVariableToScript(varName) {
244
- let caretPosition = this.$refs.scriptInput.selectionStart;
245
- this.insertTextToScript(varName, caretPosition);
246
- },
247
- addSetVariableToScript(field) {
248
- let caretPosition = this.$refs.scriptInput.selectionStart;
249
- this.insertTextToScript(`doc['${field.name}'] = Значение;`, caretPosition);
250
- },
251
- addCallInputFunction(field) {
252
- let caretPosition = this.$refs.scriptInput.selectionStart;
253
- this.insertTextToScript(`form.$refs['${field.name}'][0].Название функции();`, caretPosition);
254
- },
255
- insertTextToScript(text, position) {
256
- position = position != null ? position : this.script.length;
257
- this.innerRule.script = this.innerRule.script != null ? this.innerRule.script : '';
258
- let scriptSplit = this.innerRule.script.split('');
259
- scriptSplit.splice(position, 0, text);
260
- this.innerRule.script = scriptSplit.join('');
261
- },
262
- onRuleSelected(ruleName) {
263
- let rule = this.rulePresets.find(rule => rule.name === ruleName);
264
- if (rule) {
265
- Object.assign(this.innerRule, rule);
266
- this.innerRule.script = this.innerRule.script.trim();
267
- }
268
- },
269
- onOk() {
270
- this.validateFields();
271
-
272
- if (this.state.name && this.state.script) {
273
- if (this.onAfterOk) {
274
- this.onAfterOk(this.innerRule);
275
- }
276
- this.$nextTick(() => {
277
- this.resetModal();
278
- this.$bvModal.hide(this.id);
279
- });
280
- }
281
- }
282
- },
283
- created() {
284
- this.copyToInnerFormConfig();
285
- }
286
- }
287
- </script>
1
+ <template>
2
+ <b-modal :id="id"
3
+ :title="title"
4
+ modal-class="rb-field-rule-form-modal"
5
+ size="lg"
6
+ @ok.prevent="onOk"
7
+ ok-title="Сохранить правило"
8
+ cancel-variant="outline-gray"
9
+ cancel-title="Отмена">
10
+ <b-card no-body>
11
+ <b-tabs card @activate-tab="onActivateTab">
12
+ <b-tab title="Правило" active>
13
+ <b-form v-if="innerRule">
14
+ <b-form-row>
15
+ <b-col log="12">
16
+ <b-form-group label="Выбрать" v-if="rulePresets.length > 0">
17
+ <template #description>
18
+ Выберите готовое правило из списка и если нужно, измените
19
+ </template>
20
+ <b-form-select @input="onRuleSelected($event)">
21
+ <b-form-select-option v-for="r in rulePresets" :key="r.name"
22
+ :value="r.name">
23
+ {{ r.labelRu }} ({{ r.name }})
24
+ </b-form-select-option>
25
+ </b-form-select>
26
+ </b-form-group>
27
+ </b-col>
28
+ <b-col lg="12">
29
+ <b-form-group label="Название">
30
+ <template #description>
31
+ Задайте уникальное название, чтобы отличать правило в списке
32
+ </template>
33
+ <b-form-input v-model="innerRule.name"
34
+ :state="state.name"
35
+ :invalid-feedback="state.name_feedback"
36
+ ></b-form-input>
37
+ </b-form-group>
38
+ </b-col>
39
+ <b-col lg="12">
40
+ <b-form-group label="Событие">
41
+ <template #description>
42
+ При возникновении этого события будет выполняться правило
43
+ </template>
44
+ <b-form-select v-model="innerRule.event"
45
+ :state="state.event"
46
+ :invalid-feedback="state.event_feedback">
47
+ <b-form-select-option :value="'input'">
48
+ Ввод значения
49
+ </b-form-select-option>
50
+ <b-form-select-option :value="'change'">
51
+ Изменение значения
52
+ </b-form-select-option>
53
+ <b-form-select-option :value="'click'">
54
+ Клик
55
+ </b-form-select-option>
56
+ <b-form-select-option :value="'validate'">
57
+ Валидация
58
+ </b-form-select-option>
59
+ <b-form-select-option :value="'defaultValue'">
60
+ Установить значение по умолчанию
61
+ </b-form-select-option>
62
+ <b-form-select-option :value="'created'">
63
+ Инпут создан
64
+ </b-form-select-option>
65
+ <b-form-select-option :value="'mounted'">
66
+ Инпут подключен
67
+ </b-form-select-option>
68
+ <b-form-select-option :value="'activated'">
69
+ Инпут активирован
70
+ </b-form-select-option>
71
+ <b-form-select-option :value="'form-mounted'">
72
+ Форма создана
73
+ </b-form-select-option>
74
+ <b-form-select-option :value="'form-activated'">
75
+ Форма показана
76
+ </b-form-select-option>
77
+ </b-form-select>
78
+ </b-form-group>
79
+ </b-col>
80
+ <b-col lg="12">
81
+ <b-form-group label="Скрипт">
82
+ <template #description>
83
+ Здесь указывается скрипт правила. Скрипт должен быть написан на языке
84
+ javascript
85
+ </template>
86
+ <div class="rb-script-input">
87
+ <b-button-toolbar>
88
+ <b-dropdown text="Переменные" variant="outline-secondary"
89
+ size="sm" class="mx-1">
90
+ <b-dropdown-item @click="addVariableToScript('doc')">
91
+ Документ
92
+ </b-dropdown-item>
93
+ <b-dropdown-item @click="addVariableToScript('form')">
94
+ Форма
95
+ </b-dropdown-item>
96
+ <b-dropdown-item @click="addVariableToScript('event')">
97
+ Значение события
98
+ </b-dropdown-item>
99
+ </b-dropdown>
100
+ <b-dropdown text="Задать значение" variant="outline-secondary"
101
+ size="sm" class="mx-1">
102
+ <b-dropdown-item v-for="f in fields" :key="f.name"
103
+ v-if="fields" @click="addSetVariableToScript(f)">
104
+ {{ f.labelRu }} ({{ f.name }})
105
+ </b-dropdown-item>
106
+ </b-dropdown>
107
+ <b-dropdown text="Вызвать функцию инпута" variant="outline-secondary"
108
+ size="sm" class="mx-1">
109
+ <b-dropdown-item v-for="f in fields" :key="f.name"
110
+ v-if="fields" @click="addCallInputFunction(f)">
111
+ {{ f.labelRu }} ({{ f.name }})
112
+ </b-dropdown-item>
113
+ </b-dropdown>
114
+ </b-button-toolbar>
115
+ <b-form-textarea v-model="innerRule.script"
116
+ :state="state.script"
117
+ :invalid-feedback="state.script_feedback"
118
+ rows="8"
119
+ ref="scriptInput"
120
+ ></b-form-textarea>
121
+ </div>
122
+ </b-form-group>
123
+ </b-col>
124
+ </b-form-row>
125
+ </b-form>
126
+ </b-tab>
127
+ <b-tab title="Протестировать правило" v-if="innerFormConfig">
128
+ <doc-form :form-config="innerFormConfig">
129
+ </doc-form>
130
+ </b-tab>
131
+ </b-tabs>
132
+ </b-card>
133
+ </b-modal>
134
+ </template>
135
+
136
+ <script>
137
+ import {UtFormConfig} from "../utils/UtFormConfig";
138
+ import {UtFormConstructor} from "../utils/UtFormConstructor";
139
+ import DocForm from "./DocForm";
140
+ import {UtRandom} from "@/utils/UtRandom.js";
141
+
142
+ export default {
143
+ name: 'FieldRuleFormModal',
144
+ components: {DocForm},
145
+ props: {
146
+ field: Object,
147
+ rule: Object,
148
+ formConfig: Object,
149
+ mode: {type: String, default: 'ins'},
150
+ onAfterOk: Function,
151
+ },
152
+ data() {
153
+ return {
154
+ id: 'rb-field-rule-form-modal',
155
+ state: this.getDefaultState(),
156
+ innerFormConfig: null,
157
+ innerRule: null,
158
+ }
159
+ },
160
+ computed: {
161
+ title() {
162
+ return this.mode === 'ins' ? 'Добавление правила' : 'Редактирование правила';
163
+ },
164
+ fields() {
165
+ return UtFormConfig.getFields(this.formConfig);
166
+ },
167
+ rulePresets() {
168
+ return UtFormConstructor.getAvailableFieldRules(this.field);
169
+ }
170
+ },
171
+ watch: {
172
+ formConfig() {
173
+ this.copyToInnerFormConfig();
174
+ },
175
+ rule() {
176
+ if (this.rule) {
177
+ this.innerRule = this.rule;
178
+ } else {
179
+ this.innerRule = this.getDefaultRule();
180
+ }
181
+
182
+ }
183
+ },
184
+ methods: {
185
+ validateFields(fieldName) {
186
+ let fields = fieldName ? [fieldName] : ['name', 'event', 'script'];
187
+ fields.forEach(field => {
188
+ if (!this.innerRule[field]) {
189
+ this.state[field] = false;
190
+ this.state[`${field}_feedback`] = 'Заполните название';
191
+ } else {
192
+ this.state[field] = true;
193
+ this.state[`${field}_feedback`] = null;
194
+ }
195
+ });
196
+ },
197
+ copyToInnerFormConfig() {
198
+ this.innerFormConfig = JSON.parse(JSON.stringify(this.formConfig));
199
+ },
200
+ applyRuleToInnerFormConfig() {
201
+ if (this.innerFormConfig) {
202
+ let foundRule = UtFormConfig.findRule(this.innerRule.id, this.innerFormConfig);
203
+
204
+ if (!foundRule) {
205
+ let foundField = UtFormConfig.findField(
206
+ this.field.name, this.innerFormConfig
207
+ );
208
+
209
+ if (foundField) {
210
+ foundField.rules = foundField.rules ? foundField.rules : [];
211
+ foundField.rules.push(this.rule);
212
+ }
213
+ } else {
214
+ Object.assign(foundRule, this.innerRule);
215
+ }
216
+ }
217
+ },
218
+ onActivateTab(index) {
219
+ if (index > 0) {
220
+ this.copyToInnerFormConfig();
221
+ this.applyRuleToInnerFormConfig();
222
+ }
223
+ },
224
+ getDefaultState() {
225
+ return {
226
+ name: null,
227
+ event: null,
228
+ script: null,
229
+ }
230
+ },
231
+ getDefaultRule() {
232
+ return {
233
+ id: UtRandom.getRandomString(10),
234
+ name: null,
235
+ event: null,
236
+ script: null,
237
+ }
238
+ },
239
+ resetModal() {
240
+ this.state = this.getDefaultState();
241
+ this.innerRule = null;
242
+ },
243
+ addVariableToScript(varName) {
244
+ let caretPosition = this.$refs.scriptInput.selectionStart;
245
+ this.insertTextToScript(varName, caretPosition);
246
+ },
247
+ addSetVariableToScript(field) {
248
+ let caretPosition = this.$refs.scriptInput.selectionStart;
249
+ this.insertTextToScript(`doc['${field.name}'] = Значение;`, caretPosition);
250
+ },
251
+ addCallInputFunction(field) {
252
+ let caretPosition = this.$refs.scriptInput.selectionStart;
253
+ this.insertTextToScript(`form.$refs['${field.name}'][0].Название функции();`, caretPosition);
254
+ },
255
+ insertTextToScript(text, position) {
256
+ position = position != null ? position : this.script.length;
257
+ this.innerRule.script = this.innerRule.script != null ? this.innerRule.script : '';
258
+ let scriptSplit = this.innerRule.script.split('');
259
+ scriptSplit.splice(position, 0, text);
260
+ this.innerRule.script = scriptSplit.join('');
261
+ },
262
+ onRuleSelected(ruleName) {
263
+ let rule = this.rulePresets.find(rule => rule.name === ruleName);
264
+ if (rule) {
265
+ Object.assign(this.innerRule, rule);
266
+ this.innerRule.script = this.innerRule.script.trim();
267
+ }
268
+ },
269
+ onOk() {
270
+ this.validateFields();
271
+
272
+ if (this.state.name && this.state.script) {
273
+ if (this.onAfterOk) {
274
+ this.onAfterOk(this.innerRule);
275
+ }
276
+ this.$nextTick(() => {
277
+ this.resetModal();
278
+ this.$bvModal.hide(this.id);
279
+ });
280
+ }
281
+ }
282
+ },
283
+ created() {
284
+ this.copyToInnerFormConfig();
285
+ }
286
+ }
287
+ </script>