@unicef-polymer/etools-form-builder 2.1.7 → 2.1.9

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