@unicef-polymer/etools-form-builder 2.1.0 → 2.1.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.
Files changed (87) hide show
  1. package/LICENSE +674 -674
  2. package/README.md +1 -1
  3. package/dist/assets/translations.d.ts +6 -0
  4. package/dist/assets/translations.js +67 -0
  5. package/dist/form-attachments-popup/form-attachments-popup.d.ts +86 -85
  6. package/dist/form-attachments-popup/form-attachments-popup.helper.d.ts +8 -8
  7. package/dist/form-attachments-popup/form-attachments-popup.helper.js +14 -14
  8. package/dist/form-attachments-popup/form-attachments-popup.js +255 -248
  9. package/dist/form-attachments-popup/form-attachments-popup.tpl.d.ts +3 -3
  10. package/dist/form-attachments-popup/form-attachments-popup.tpl.js +91 -89
  11. package/dist/form-attachments-popup/index.d.ts +2 -2
  12. package/dist/form-attachments-popup/index.js +2 -2
  13. package/dist/form-fields/abstract-field-base.class.d.ts +33 -28
  14. package/dist/form-fields/abstract-field-base.class.js +194 -176
  15. package/dist/form-fields/field-renderer-component.d.ts +20 -20
  16. package/dist/form-fields/field-renderer-component.js +249 -249
  17. package/dist/form-fields/index.d.ts +12 -12
  18. package/dist/form-fields/index.js +12 -12
  19. package/dist/form-fields/repeatable-fields/repeatable-attachment-field.d.ts +17 -17
  20. package/dist/form-fields/repeatable-fields/repeatable-attachment-field.js +209 -208
  21. package/dist/form-fields/repeatable-fields/repeatable-base-field.d.ts +20 -20
  22. package/dist/form-fields/repeatable-fields/repeatable-base-field.js +124 -123
  23. package/dist/form-fields/repeatable-fields/repeatable-number-field.d.ts +10 -10
  24. package/dist/form-fields/repeatable-fields/repeatable-number-field.js +67 -66
  25. package/dist/form-fields/repeatable-fields/repeatable-scale-field.d.ts +15 -15
  26. package/dist/form-fields/repeatable-fields/repeatable-scale-field.js +108 -108
  27. package/dist/form-fields/repeatable-fields/repeatable-text-field.d.ts +8 -8
  28. package/dist/form-fields/repeatable-fields/repeatable-text-field.js +53 -53
  29. package/dist/form-fields/single-fields/attachment-field.d.ts +16 -16
  30. package/dist/form-fields/single-fields/attachment-field.js +98 -97
  31. package/dist/form-fields/single-fields/base-field.d.ts +11 -11
  32. package/dist/form-fields/single-fields/base-field.js +58 -57
  33. package/dist/form-fields/single-fields/boolean-field.d.ts +8 -8
  34. package/dist/form-fields/single-fields/boolean-field.js +49 -49
  35. package/dist/form-fields/single-fields/number-field.d.ts +10 -10
  36. package/dist/form-fields/single-fields/number-field.js +67 -66
  37. package/dist/form-fields/single-fields/scale-field.d.ts +18 -18
  38. package/dist/form-fields/single-fields/scale-field.js +108 -107
  39. package/dist/form-fields/single-fields/text-field.d.ts +8 -8
  40. package/dist/form-fields/single-fields/text-field.js +54 -54
  41. package/dist/form-groups/form-abstract-group.d.ts +65 -60
  42. package/dist/form-groups/form-abstract-group.js +375 -353
  43. package/dist/form-groups/form-card.d.ts +32 -32
  44. package/dist/form-groups/form-card.js +113 -108
  45. package/dist/form-groups/form-collapsed-card.d.ts +70 -70
  46. package/dist/form-groups/form-collapsed-card.js +254 -253
  47. package/dist/form-groups/index.d.ts +3 -3
  48. package/dist/form-groups/index.js +3 -3
  49. package/dist/index.d.ts +3 -3
  50. package/dist/index.js +3 -3
  51. package/dist/lib/additional-components/confirmation-dialog.d.ts +18 -16
  52. package/dist/lib/additional-components/confirmation-dialog.js +72 -65
  53. package/dist/lib/additional-components/etools-fb-card.d.ts +21 -16
  54. package/dist/lib/additional-components/etools-fb-card.js +238 -215
  55. package/dist/lib/styles/attachments.styles.d.ts +2 -2
  56. package/dist/lib/styles/attachments.styles.js +72 -72
  57. package/dist/lib/styles/card-styles.d.ts +2 -2
  58. package/dist/lib/styles/card-styles.js +154 -154
  59. package/dist/lib/styles/dialog.styles.d.ts +2 -2
  60. package/dist/lib/styles/dialog.styles.js +85 -85
  61. package/dist/lib/styles/elevation-styles.d.ts +9 -9
  62. package/dist/lib/styles/elevation-styles.js +43 -43
  63. package/dist/lib/styles/flex-layout-classes.d.ts +2 -2
  64. package/dist/lib/styles/flex-layout-classes.js +319 -319
  65. package/dist/lib/styles/form-builder-card.styles.d.ts +2 -2
  66. package/dist/lib/styles/form-builder-card.styles.js +49 -49
  67. package/dist/lib/styles/input-styles.d.ts +2 -2
  68. package/dist/lib/styles/input-styles.js +138 -138
  69. package/dist/lib/styles/page-layout-styles.d.ts +2 -2
  70. package/dist/lib/styles/page-layout-styles.js +201 -201
  71. package/dist/lib/styles/shared-styles.d.ts +2 -2
  72. package/dist/lib/styles/shared-styles.js +70 -70
  73. package/dist/lib/types/form-builder.interfaces.d.ts +83 -83
  74. package/dist/lib/types/form-builder.interfaces.js +0 -0
  75. package/dist/lib/types/form-builder.types.d.ts +59 -59
  76. package/dist/lib/types/form-builder.types.js +0 -0
  77. package/dist/lib/types/global.types.d.ts +4 -4
  78. package/dist/lib/types/global.types.js +0 -0
  79. package/dist/lib/utils/dialog.d.ts +10 -10
  80. package/dist/lib/utils/dialog.js +21 -21
  81. package/dist/lib/utils/fire-custom-event.d.ts +1 -1
  82. package/dist/lib/utils/fire-custom-event.js +7 -7
  83. package/dist/lib/utils/translate.d.ts +1 -0
  84. package/dist/lib/utils/translate.js +9 -0
  85. package/dist/lib/utils/validations.helper.d.ts +26 -26
  86. package/dist/lib/utils/validations.helper.js +42 -35
  87. package/package.json +56 -56
@@ -1,353 +1,375 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { LitElement, property, html, css, customElement } from 'lit-element';
8
- import '../form-fields/single-fields/text-field';
9
- import '../form-fields/single-fields/number-field';
10
- import '../form-fields/single-fields/scale-field';
11
- import '@polymer/paper-input/paper-textarea';
12
- import { SharedStyles } from '../lib/styles/shared-styles';
13
- import { pageLayoutStyles } from '../lib/styles/page-layout-styles';
14
- import { elevationStyles } from '../lib/styles/elevation-styles';
15
- import { CardStyles } from '../lib/styles/card-styles';
16
- import { FlexLayoutClasses } from '../lib/styles/flex-layout-classes';
17
- import { fireEvent } from '../lib/utils/fire-custom-event';
18
- import { clone } from 'ramda';
19
- import { live } from 'lit-html/directives/live';
20
- import { openDialog } from '../lib/utils/dialog';
21
- import { FormBuilderCardStyles } from '../lib/styles/form-builder-card.styles';
22
- export var FieldTypes;
23
- (function (FieldTypes) {
24
- FieldTypes["FILE_TYPE"] = "file";
25
- FieldTypes["TEXT_TYPE"] = "text";
26
- FieldTypes["NUMBER_TYPE"] = "number";
27
- FieldTypes["BOOL_TYPE"] = "bool";
28
- FieldTypes["SCALE_TYPE"] = "likert_scale";
29
- FieldTypes["NUMBER_INTEGER_TYPE"] = "number-integer";
30
- FieldTypes["NUMBER_FLOAT_TYPE"] = "number-float";
31
- })(FieldTypes || (FieldTypes = {}));
32
- export var StructureTypes;
33
- (function (StructureTypes) {
34
- StructureTypes["WIDE"] = "wide";
35
- StructureTypes["ADDITIONAL"] = "additional";
36
- StructureTypes["CARD"] = "card";
37
- StructureTypes["ABSTRACT"] = "abstract";
38
- StructureTypes["COLLAPSED"] = "collapse";
39
- StructureTypes["ATTACHMENTS_BUTTON"] = "floating_attachments";
40
- })(StructureTypes || (StructureTypes = {}));
41
- let FormAbstractGroup = class FormAbstractGroup extends LitElement {
42
- constructor() {
43
- super(...arguments);
44
- this.parentGroupName = '';
45
- this.readonly = false;
46
- this._errors = {};
47
- this._value = {};
48
- this.computedPath = [];
49
- }
50
- /**
51
- * Make value property immutable
52
- * @param value
53
- */
54
- set value(value) {
55
- this._value = this.groupStructure.name === 'root' ? clone(value) : value;
56
- }
57
- get value() {
58
- return this._value;
59
- }
60
- /**
61
- * Setter for handling error.
62
- * Normally we wouldn't have errors as string or string[] for FormGroups.
63
- * In cases they appear - show toast with error text and reset it.
64
- * Otherwise it will be impossible to clear that error from field elements
65
- * @param errors
66
- */
67
- set errors(errors) {
68
- if (Array.isArray(errors)) {
69
- fireEvent(this, 'toast', { text: errors[0] });
70
- fireEvent(this, 'error-changed', { error: null });
71
- }
72
- else if (errors) {
73
- this._errors = errors;
74
- }
75
- }
76
- render() {
77
- if (!this.groupStructure || !this.metadata) {
78
- return html ``;
79
- }
80
- return html `
81
- ${this.groupStructure.children.map((child) => this.renderChild(child))}
82
- `;
83
- }
84
- renderChild(child) {
85
- const type = child.type;
86
- switch (child.type) {
87
- case 'field':
88
- return this.renderField(child);
89
- case 'group':
90
- return this.renderGroup(child);
91
- case 'information':
92
- return this.renderInformation(child);
93
- default:
94
- console.warn(`FormBuilderGroup: Unknown group type ${type}. Please, specify rendering method`);
95
- return html ``;
96
- }
97
- }
98
- renderField(blueprintField) {
99
- var _a;
100
- return html `
101
- <field-renderer
102
- .field="${blueprintField}"
103
- ?readonly="${live(this.readonly)}"
104
- .value="${this.value && this.value[blueprintField.name]}"
105
- .validations="${blueprintField.validations.map((validation) => this.metadata.validations[validation])}"
106
- .errorMessage="${this.getErrorMessage(blueprintField.name)}"
107
- .options="${((_a = this.metadata.options[blueprintField.options_key || '']) === null || _a === void 0 ? void 0 : _a.values) || []}"
108
- .computedPath="${this.computedPath.concat(this.groupStructure.name === 'root' ? [blueprintField.name] : [this.groupStructure.name, blueprintField.name])}"
109
- @value-changed="${(event) => this.valueChanged(event, blueprintField.name)}"
110
- @error-changed="${(event) => this.errorChanged(event, blueprintField.name)}"
111
- ></field-renderer>
112
- `;
113
- }
114
- renderInformation(information) {
115
- return html `<section class="elevation page-content" elevation="1">${information.text}</section>`;
116
- }
117
- renderGroup(groupStructure) {
118
- if (!groupStructure.repeatable) {
119
- return this.getGroupTemplate(groupStructure);
120
- }
121
- const value = (this.value && this.value[groupStructure.name]) || [{}];
122
- return html `
123
- ${value.map((_, index) => this.getGroupTemplate(groupStructure, index))}
124
- <paper-button class="add-group save-button" @click="${() => this.addGroup(groupStructure.name)}">
125
- Add ${!groupStructure.title || groupStructure.title.length > 15 ? 'group' : groupStructure.title}
126
- </paper-button>
127
- `;
128
- }
129
- getGroupTemplate(groupStructure, index) {
130
- const isAbstract = groupStructure.styling.includes(StructureTypes.ABSTRACT);
131
- const isCard = groupStructure.styling.includes(StructureTypes.CARD);
132
- const isCollapsed = groupStructure.styling.includes(StructureTypes.COLLAPSED);
133
- let value = this.value && this.value[groupStructure.name];
134
- if (typeof index === 'number') {
135
- value = value && value[index];
136
- }
137
- let errors = this._errors[groupStructure.name];
138
- if (typeof index === 'number') {
139
- errors = errors && errors[index];
140
- }
141
- if (isAbstract) {
142
- return html `
143
- <form-abstract-group
144
- .groupStructure="${groupStructure}"
145
- .value="${value}"
146
- .metadata="${this.metadata}"
147
- .parentGroupName="${this.groupStructure.name}"
148
- .computedPath="${this.computedPath.concat(this.groupStructure.name === 'root' ? [] : [this.groupStructure.name])}"
149
- .readonly="${this.readonly}"
150
- .errors="${errors || null}"
151
- @value-changed="${(event) => this.valueChanged(event, groupStructure.name, index)}"
152
- @error-changed="${(event) => this.errorChanged(event, groupStructure.name, index)}"
153
- ></form-abstract-group>
154
- `;
155
- }
156
- else if (isCard && isCollapsed) {
157
- return html `
158
- <form-collapsed-card
159
- .groupStructure="${groupStructure}"
160
- .value="${value}"
161
- .metadata="${this.metadata}"
162
- .parentGroupName="${this.groupStructure.name}"
163
- .computedPath="${this.computedPath.concat(this.groupStructure.name === 'root' ? [] : [this.groupStructure.name])}"
164
- .readonly="${this.readonly}"
165
- .errors="${errors || null}"
166
- @remove-group="${() => this.removeGroup(groupStructure, index)}"
167
- @value-changed="${(event) => this.valueChanged(event, groupStructure.name, index)}"
168
- @error-changed="${(event) => this.errorChanged(event, groupStructure.name, index)}"
169
- ></form-collapsed-card>
170
- `;
171
- }
172
- else if (isCard) {
173
- return html `
174
- <form-card
175
- .groupStructure="${groupStructure}"
176
- .value="${value}"
177
- .metadata="${this.metadata}"
178
- .parentGroupName="${this.groupStructure.name}"
179
- .computedPath="${this.computedPath.concat(this.groupStructure.name === 'root' ? [] : [this.groupStructure.name])}"
180
- .readonly="${this.readonly}"
181
- .errors="${errors || null}"
182
- @remove-group="${() => this.removeGroup(groupStructure, index)}"
183
- @value-changed="${(event) => this.valueChanged(event, groupStructure.name, index)}"
184
- @error-changed="${(event) => this.errorChanged(event, groupStructure.name, index)}"
185
- ></form-card>
186
- `;
187
- }
188
- else {
189
- console.warn(`FormBuilderGroup: Unknown group type: ${groupStructure.styling}`);
190
- return html ``;
191
- }
192
- }
193
- valueChanged(event, name, index) {
194
- if (!this.value) {
195
- this.value = {};
196
- }
197
- if (typeof index === 'number') {
198
- const value = this.value[name] || [];
199
- value[index] = event.detail.value;
200
- this.value[name] = value;
201
- }
202
- else {
203
- this.value[name] = event.detail.value;
204
- }
205
- if (event.stopPropagation) {
206
- event.stopPropagation();
207
- }
208
- fireEvent(this, 'value-changed', { value: this.value });
209
- this.requestUpdate();
210
- }
211
- errorChanged(event, name, index) {
212
- const errorMessage = event.detail.error;
213
- if (typeof index === 'number') {
214
- const errors = this._errors[name] || (this.value && new Array(this.value[name].length).fill(null)) || [];
215
- errors.splice(index, 1, errorMessage);
216
- const hasErrors = errors.some((error) => error !== null);
217
- this._errors[name] = hasErrors ? errors : null;
218
- }
219
- else if (errorMessage) {
220
- this._errors[name] = errorMessage;
221
- }
222
- else {
223
- delete this._errors[name];
224
- }
225
- event.stopPropagation();
226
- const errors = Object.keys(this._errors).length ? this._errors : null;
227
- fireEvent(this, 'error-changed', { error: errors });
228
- }
229
- addGroup(name) {
230
- const value = this.value[name] || [];
231
- value.push({});
232
- this.valueChanged({ detail: { value } }, name);
233
- }
234
- removeGroup(group, index) {
235
- if (typeof index !== 'number') {
236
- return;
237
- }
238
- const value = (this.value && this.value[group.name]) || [];
239
- if (group.required && value.length < 2) {
240
- openDialog({
241
- dialog: 'confirmation-popup',
242
- dialogData: {
243
- text: `Group is required. At least one group must exist`,
244
- hideConfirmBtn: true,
245
- dialogTitle: ''
246
- }
247
- });
248
- return;
249
- }
250
- value.splice(index, 1);
251
- this.valueChanged({ detail: { value } }, group.name);
252
- }
253
- getErrorMessage(fieldName) {
254
- const error = this._errors && this._errors[fieldName];
255
- return Array.isArray(error) ? error[0] : error || null;
256
- }
257
- static get styles() {
258
- // language=CSS
259
- return [
260
- SharedStyles,
261
- pageLayoutStyles,
262
- elevationStyles,
263
- CardStyles,
264
- FlexLayoutClasses,
265
- FormBuilderCardStyles,
266
- css `
267
- :host {
268
- display: flex;
269
- flex-direction: column;
270
- }
271
- .add-group {
272
- align-self: flex-end;
273
- margin-right: 23px;
274
- margin-top: 20px;
275
- box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12),
276
- 0 2px 4px -1px rgba(0, 0, 0, 0.4);
277
- }
278
- .card-header {
279
- display: flex;
280
- align-items: center;
281
- justify-content: space-between;
282
- }
283
- .card-header .remove-group {
284
- display: flex;
285
- align-items: center;
286
- justify-content: flex-end;
287
- padding: 0 10px 8px;
288
- cursor: pointer;
289
- width: min-content;
290
- white-space: nowrap;
291
- }
292
- .card-header .title {
293
- padding: 0 24px 8px;
294
- font-size: 18px;
295
- font-weight: bold;
296
- }
297
- .save-button {
298
- margin-top: 8px;
299
- color: var(--primary-background-color);
300
- background-color: var(--primary-color);
301
- }
302
- .information-source {
303
- padding: 0.5% 2% 0.5% 1%;
304
- }
305
-
306
- .additional-field {
307
- padding-top: 15px;
308
- padding-bottom: 20px;
309
- background-color: var(--secondary-background-color);
310
- }
311
-
312
- .actions-container {
313
- padding: 0 25px 5px 45px;
314
- box-sizing: border-box;
315
- }
316
-
317
- .card-container.form-card {
318
- padding: 12px 0 15px;
319
- }
320
-
321
- .attachments-warning {
322
- color: red;
323
- }
324
- paper-icon-button[icon='close'] {
325
- cursor: pointer;
326
- color: var(--primary-text-color);
327
- }
328
- `
329
- ];
330
- }
331
- };
332
- __decorate([
333
- property({ type: Object })
334
- ], FormAbstractGroup.prototype, "groupStructure", void 0);
335
- __decorate([
336
- property({ type: Object })
337
- ], FormAbstractGroup.prototype, "metadata", void 0);
338
- __decorate([
339
- property({ type: String })
340
- ], FormAbstractGroup.prototype, "parentGroupName", void 0);
341
- __decorate([
342
- property({ type: Boolean, attribute: 'readonly' })
343
- ], FormAbstractGroup.prototype, "readonly", void 0);
344
- __decorate([
345
- property()
346
- ], FormAbstractGroup.prototype, "_errors", void 0);
347
- __decorate([
348
- property()
349
- ], FormAbstractGroup.prototype, "_value", void 0);
350
- FormAbstractGroup = __decorate([
351
- customElement('form-abstract-group')
352
- ], FormAbstractGroup);
353
- export { FormAbstractGroup };
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { LitElement, property, html, css, customElement } from 'lit-element';
8
+ import '../form-fields/single-fields/text-field';
9
+ import '../form-fields/single-fields/number-field';
10
+ import '../form-fields/single-fields/scale-field';
11
+ import '@polymer/paper-input/paper-textarea';
12
+ import { SharedStyles } from '../lib/styles/shared-styles';
13
+ import { pageLayoutStyles } from '../lib/styles/page-layout-styles';
14
+ import { elevationStyles } from '../lib/styles/elevation-styles';
15
+ import { CardStyles } from '../lib/styles/card-styles';
16
+ import { FlexLayoutClasses } from '../lib/styles/flex-layout-classes';
17
+ import { fireEvent } from '../lib/utils/fire-custom-event';
18
+ import { clone } from 'ramda';
19
+ import { live } from 'lit-html/directives/live';
20
+ import { openDialog } from '../lib/utils/dialog';
21
+ import { FormBuilderCardStyles } from '../lib/styles/form-builder-card.styles';
22
+ import { getTranslation } from '../lib/utils/translate';
23
+ export var FieldTypes;
24
+ (function (FieldTypes) {
25
+ FieldTypes["FILE_TYPE"] = "file";
26
+ FieldTypes["TEXT_TYPE"] = "text";
27
+ FieldTypes["NUMBER_TYPE"] = "number";
28
+ FieldTypes["BOOL_TYPE"] = "bool";
29
+ FieldTypes["SCALE_TYPE"] = "likert_scale";
30
+ FieldTypes["NUMBER_INTEGER_TYPE"] = "number-integer";
31
+ FieldTypes["NUMBER_FLOAT_TYPE"] = "number-float";
32
+ })(FieldTypes || (FieldTypes = {}));
33
+ export var StructureTypes;
34
+ (function (StructureTypes) {
35
+ StructureTypes["WIDE"] = "wide";
36
+ StructureTypes["ADDITIONAL"] = "additional";
37
+ StructureTypes["CARD"] = "card";
38
+ StructureTypes["ABSTRACT"] = "abstract";
39
+ StructureTypes["COLLAPSED"] = "collapse";
40
+ StructureTypes["ATTACHMENTS_BUTTON"] = "floating_attachments";
41
+ })(StructureTypes || (StructureTypes = {}));
42
+ let FormAbstractGroup = class FormAbstractGroup extends LitElement {
43
+ constructor() {
44
+ super();
45
+ this.parentGroupName = '';
46
+ this.readonly = false;
47
+ this._errors = {};
48
+ this._value = {};
49
+ this.computedPath = [];
50
+ if (!this.language) {
51
+ this.language = window.localStorage.defaultLanguage || 'en';
52
+ }
53
+ this.handleLanguageChange = this.handleLanguageChange.bind(this);
54
+ }
55
+ /**
56
+ * Make value property immutable
57
+ * @param value
58
+ */
59
+ set value(value) {
60
+ this._value = this.groupStructure.name === 'root' ? clone(value) : value;
61
+ }
62
+ get value() {
63
+ return this._value;
64
+ }
65
+ /**
66
+ * Setter for handling error.
67
+ * Normally we wouldn't have errors as string or string[] for FormGroups.
68
+ * In cases they appear - show toast with error text and reset it.
69
+ * Otherwise it will be impossible to clear that error from field elements
70
+ * @param errors
71
+ */
72
+ set errors(errors) {
73
+ if (Array.isArray(errors)) {
74
+ fireEvent(this, 'toast', { text: errors[0] });
75
+ fireEvent(this, 'error-changed', { error: null });
76
+ }
77
+ else if (errors) {
78
+ this._errors = errors;
79
+ }
80
+ }
81
+ connectedCallback() {
82
+ super.connectedCallback();
83
+ document.addEventListener('language-changed', this.handleLanguageChange.bind(this));
84
+ }
85
+ disconnectedCallback() {
86
+ super.disconnectedCallback();
87
+ document.removeEventListener('language-changed', this.handleLanguageChange.bind(this));
88
+ }
89
+ handleLanguageChange(e) {
90
+ this.language = e.detail.language;
91
+ }
92
+ render() {
93
+ if (!this.groupStructure || !this.metadata) {
94
+ return html ``;
95
+ }
96
+ return html `
97
+ ${this.groupStructure.children.map((child) => this.renderChild(child))}
98
+ `;
99
+ }
100
+ renderChild(child) {
101
+ const type = child.type;
102
+ switch (child.type) {
103
+ case 'field':
104
+ return this.renderField(child);
105
+ case 'group':
106
+ return this.renderGroup(child);
107
+ case 'information':
108
+ return this.renderInformation(child);
109
+ default:
110
+ console.warn(`FormBuilderGroup: Unknown group type ${type}. Please, specify rendering method`);
111
+ return html ``;
112
+ }
113
+ }
114
+ renderField(blueprintField) {
115
+ var _a;
116
+ return html `
117
+ <field-renderer
118
+ .field="${blueprintField}"
119
+ ?readonly="${live(this.readonly)}"
120
+ .value="${this.value && this.value[blueprintField.name]}"
121
+ .validations="${blueprintField.validations.map((validation) => this.metadata.validations[validation])}"
122
+ .errorMessage="${this.getErrorMessage(blueprintField.name)}"
123
+ .options="${((_a = this.metadata.options[blueprintField.options_key || '']) === null || _a === void 0 ? void 0 : _a.values) || []}"
124
+ .computedPath="${this.computedPath.concat(this.groupStructure.name === 'root' ? [blueprintField.name] : [this.groupStructure.name, blueprintField.name])}"
125
+ @value-changed="${(event) => this.valueChanged(event, blueprintField.name)}"
126
+ @error-changed="${(event) => this.errorChanged(event, blueprintField.name)}"
127
+ ></field-renderer>
128
+ `;
129
+ }
130
+ renderInformation(information) {
131
+ return html `<section class="elevation page-content" elevation="1">${information.text}</section>`;
132
+ }
133
+ renderGroup(groupStructure) {
134
+ if (!groupStructure.repeatable) {
135
+ return this.getGroupTemplate(groupStructure);
136
+ }
137
+ const value = (this.value && this.value[groupStructure.name]) || [{}];
138
+ return html `
139
+ ${value.map((_, index) => this.getGroupTemplate(groupStructure, index))}
140
+ <paper-button class="add-group save-button" @click="${() => this.addGroup(groupStructure.name)}">
141
+ ${getTranslation(this.language, 'ADD')}
142
+ ${!groupStructure.title || groupStructure.title.length > 15
143
+ ? getTranslation(this.language, 'GROUP')
144
+ : groupStructure.title}
145
+ </paper-button>
146
+ `;
147
+ }
148
+ getGroupTemplate(groupStructure, index) {
149
+ const isAbstract = groupStructure.styling.includes(StructureTypes.ABSTRACT);
150
+ const isCard = groupStructure.styling.includes(StructureTypes.CARD);
151
+ const isCollapsed = groupStructure.styling.includes(StructureTypes.COLLAPSED);
152
+ let value = this.value && this.value[groupStructure.name];
153
+ if (typeof index === 'number') {
154
+ value = value && value[index];
155
+ }
156
+ let errors = this._errors[groupStructure.name];
157
+ if (typeof index === 'number') {
158
+ errors = errors && errors[index];
159
+ }
160
+ if (isAbstract) {
161
+ return html `
162
+ <form-abstract-group
163
+ .groupStructure="${groupStructure}"
164
+ .value="${value}"
165
+ .metadata="${this.metadata}"
166
+ .parentGroupName="${this.groupStructure.name}"
167
+ .computedPath="${this.computedPath.concat(this.groupStructure.name === 'root' ? [] : [this.groupStructure.name])}"
168
+ .readonly="${this.readonly}"
169
+ .errors="${errors || null}"
170
+ @value-changed="${(event) => this.valueChanged(event, groupStructure.name, index)}"
171
+ @error-changed="${(event) => this.errorChanged(event, groupStructure.name, index)}"
172
+ ></form-abstract-group>
173
+ `;
174
+ }
175
+ else if (isCard && isCollapsed) {
176
+ return html `
177
+ <form-collapsed-card
178
+ .groupStructure="${groupStructure}"
179
+ .value="${value}"
180
+ .metadata="${this.metadata}"
181
+ .parentGroupName="${this.groupStructure.name}"
182
+ .computedPath="${this.computedPath.concat(this.groupStructure.name === 'root' ? [] : [this.groupStructure.name])}"
183
+ .readonly="${this.readonly}"
184
+ .errors="${errors || null}"
185
+ @remove-group="${() => this.removeGroup(groupStructure, index)}"
186
+ @value-changed="${(event) => this.valueChanged(event, groupStructure.name, index)}"
187
+ @error-changed="${(event) => this.errorChanged(event, groupStructure.name, index)}"
188
+ ></form-collapsed-card>
189
+ `;
190
+ }
191
+ else if (isCard) {
192
+ return html `
193
+ <form-card
194
+ .groupStructure="${groupStructure}"
195
+ .value="${value}"
196
+ .metadata="${this.metadata}"
197
+ .parentGroupName="${this.groupStructure.name}"
198
+ .computedPath="${this.computedPath.concat(this.groupStructure.name === 'root' ? [] : [this.groupStructure.name])}"
199
+ .readonly="${this.readonly}"
200
+ .errors="${errors || null}"
201
+ @remove-group="${() => this.removeGroup(groupStructure, index)}"
202
+ @value-changed="${(event) => this.valueChanged(event, groupStructure.name, index)}"
203
+ @error-changed="${(event) => this.errorChanged(event, groupStructure.name, index)}"
204
+ ></form-card>
205
+ `;
206
+ }
207
+ else {
208
+ console.warn(`FormBuilderGroup: Unknown group type: ${groupStructure.styling}`);
209
+ return html ``;
210
+ }
211
+ }
212
+ valueChanged(event, name, index) {
213
+ if (!this.value) {
214
+ this.value = {};
215
+ }
216
+ if (typeof index === 'number') {
217
+ const value = this.value[name] || [];
218
+ value[index] = event.detail.value;
219
+ this.value[name] = value;
220
+ }
221
+ else {
222
+ this.value[name] = event.detail.value;
223
+ }
224
+ if (event.stopPropagation) {
225
+ event.stopPropagation();
226
+ }
227
+ fireEvent(this, 'value-changed', { value: this.value });
228
+ this.requestUpdate();
229
+ }
230
+ errorChanged(event, name, index) {
231
+ const errorMessage = event.detail.error;
232
+ if (typeof index === 'number') {
233
+ const errors = this._errors[name] || (this.value && new Array(this.value[name].length).fill(null)) || [];
234
+ errors.splice(index, 1, errorMessage);
235
+ const hasErrors = errors.some((error) => error !== null);
236
+ this._errors[name] = hasErrors ? errors : null;
237
+ }
238
+ else if (errorMessage) {
239
+ this._errors[name] = errorMessage;
240
+ }
241
+ else {
242
+ delete this._errors[name];
243
+ }
244
+ event.stopPropagation();
245
+ const errors = Object.keys(this._errors).length ? this._errors : null;
246
+ fireEvent(this, 'error-changed', { error: errors });
247
+ }
248
+ addGroup(name) {
249
+ const value = this.value[name] || [];
250
+ value.push({});
251
+ this.valueChanged({ detail: { value } }, name);
252
+ }
253
+ removeGroup(group, index) {
254
+ if (typeof index !== 'number') {
255
+ return;
256
+ }
257
+ const value = (this.value && this.value[group.name]) || [];
258
+ if (group.required && value.length < 2) {
259
+ openDialog({
260
+ dialog: 'confirmation-popup',
261
+ dialogData: {
262
+ text: getTranslation(this.language, 'GROUP_REQUIRED'),
263
+ hideConfirmBtn: true,
264
+ dialogTitle: ''
265
+ }
266
+ });
267
+ return;
268
+ }
269
+ value.splice(index, 1);
270
+ this.valueChanged({ detail: { value } }, group.name);
271
+ }
272
+ getErrorMessage(fieldName) {
273
+ const error = this._errors && this._errors[fieldName];
274
+ return Array.isArray(error) ? error[0] : error || null;
275
+ }
276
+ static get styles() {
277
+ // language=CSS
278
+ return [
279
+ SharedStyles,
280
+ pageLayoutStyles,
281
+ elevationStyles,
282
+ CardStyles,
283
+ FlexLayoutClasses,
284
+ FormBuilderCardStyles,
285
+ css `
286
+ :host {
287
+ display: flex;
288
+ flex-direction: column;
289
+ }
290
+ .add-group {
291
+ align-self: flex-end;
292
+ margin-right: 23px;
293
+ margin-top: 20px;
294
+ box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12),
295
+ 0 2px 4px -1px rgba(0, 0, 0, 0.4);
296
+ }
297
+ .card-header {
298
+ display: flex;
299
+ align-items: center;
300
+ justify-content: space-between;
301
+ }
302
+ .card-header .remove-group {
303
+ display: flex;
304
+ align-items: center;
305
+ justify-content: flex-end;
306
+ padding: 0 10px 8px;
307
+ cursor: pointer;
308
+ width: min-content;
309
+ white-space: nowrap;
310
+ }
311
+ .card-header .title {
312
+ padding: 0 24px 8px;
313
+ font-size: 18px;
314
+ font-weight: bold;
315
+ }
316
+ .save-button {
317
+ margin-top: 8px;
318
+ color: var(--primary-background-color);
319
+ background-color: var(--primary-color);
320
+ }
321
+ .information-source {
322
+ padding: 0.5% 2% 0.5% 1%;
323
+ }
324
+
325
+ .additional-field {
326
+ padding-top: 15px;
327
+ padding-bottom: 20px;
328
+ background-color: var(--secondary-background-color);
329
+ }
330
+
331
+ .actions-container {
332
+ padding: 0 25px 5px 45px;
333
+ box-sizing: border-box;
334
+ }
335
+
336
+ .card-container.form-card {
337
+ padding: 12px 0 15px;
338
+ }
339
+
340
+ .attachments-warning {
341
+ color: red;
342
+ }
343
+ paper-icon-button[icon='close'] {
344
+ cursor: pointer;
345
+ color: var(--primary-text-color);
346
+ }
347
+ `
348
+ ];
349
+ }
350
+ };
351
+ __decorate([
352
+ property({ type: Object })
353
+ ], FormAbstractGroup.prototype, "groupStructure", void 0);
354
+ __decorate([
355
+ property({ type: Object })
356
+ ], FormAbstractGroup.prototype, "metadata", void 0);
357
+ __decorate([
358
+ property({ type: String })
359
+ ], FormAbstractGroup.prototype, "parentGroupName", void 0);
360
+ __decorate([
361
+ property()
362
+ ], FormAbstractGroup.prototype, "language", void 0);
363
+ __decorate([
364
+ property({ type: Boolean, attribute: 'readonly' })
365
+ ], FormAbstractGroup.prototype, "readonly", void 0);
366
+ __decorate([
367
+ property()
368
+ ], FormAbstractGroup.prototype, "_errors", void 0);
369
+ __decorate([
370
+ property()
371
+ ], FormAbstractGroup.prototype, "_value", void 0);
372
+ FormAbstractGroup = __decorate([
373
+ customElement('form-abstract-group')
374
+ ], FormAbstractGroup);
375
+ export { FormAbstractGroup };