@glowgreen/gg-questionnaire-v2 0.0.84 → 0.0.85

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 (61) hide show
  1. package/bundles/glowgreen-gg-questionnaire-v2.umd.js +362 -288
  2. package/bundles/glowgreen-gg-questionnaire-v2.umd.js.map +1 -1
  3. package/bundles/glowgreen-gg-questionnaire-v2.umd.min.js +2 -2
  4. package/bundles/glowgreen-gg-questionnaire-v2.umd.min.js.map +1 -1
  5. package/esm2015/glowgreen-gg-questionnaire-v2.js +3 -1
  6. package/esm2015/lib/enums/condition-relationship.enum.js +1 -1
  7. package/esm2015/lib/enums/condition-type.enum.js +1 -1
  8. package/esm2015/lib/enums/input-type.enum.js +1 -1
  9. package/esm2015/lib/enums/question-type.enum.js +1 -1
  10. package/esm2015/lib/enums/repeater-question-type.enum.js +1 -1
  11. package/esm2015/lib/enums/validation-type.enum.js +1 -1
  12. package/esm2015/lib/gg-questionnaire-v2.module.js +5 -17
  13. package/esm2015/lib/interfaces/attachment.js +1 -1
  14. package/esm2015/lib/interfaces/checklist-item.js +1 -1
  15. package/esm2015/lib/interfaces/condition-group.js +1 -1
  16. package/esm2015/lib/interfaces/condition.js +1 -1
  17. package/esm2015/lib/interfaces/question.js +1 -1
  18. package/esm2015/lib/interfaces/questionnaire-options.js +1 -1
  19. package/esm2015/lib/interfaces/questionnaire.js +1 -1
  20. package/esm2015/lib/interfaces/repeater-question.js +1 -1
  21. package/esm2015/lib/interfaces/section.js +1 -1
  22. package/esm2015/lib/interfaces/select-option.js +1 -1
  23. package/esm2015/lib/interfaces/validator.js +1 -1
  24. package/esm2015/lib/services/filter.service.js +7 -8
  25. package/esm2015/lib/services/form-constructor.service.js +10 -8
  26. package/esm2015/lib/services/questionnaire.service.js +10 -11
  27. package/esm2015/public_api.js +1 -1
  28. package/fesm2015/glowgreen-gg-questionnaire-v2.js +30 -41
  29. package/fesm2015/glowgreen-gg-questionnaire-v2.js.map +1 -1
  30. package/glowgreen-gg-questionnaire-v2.d.ts +2 -1
  31. package/glowgreen-gg-questionnaire-v2.metadata.json +1 -0
  32. package/lib/gg-questionnaire-v2.module.d.ts +0 -4
  33. package/lib/services/filter.service.d.ts +0 -3
  34. package/lib/services/form-constructor.service.d.ts +0 -3
  35. package/lib/services/questionnaire.service.d.ts +0 -3
  36. package/package.json +9 -13
  37. package/esm5/glowgreen-gg-questionnaire-v2.js +0 -5
  38. package/esm5/lib/enums/condition-relationship.enum.js +0 -6
  39. package/esm5/lib/enums/condition-type.enum.js +0 -10
  40. package/esm5/lib/enums/input-type.enum.js +0 -15
  41. package/esm5/lib/enums/question-type.enum.js +0 -15
  42. package/esm5/lib/enums/repeater-question-type.enum.js +0 -7
  43. package/esm5/lib/enums/validation-type.enum.js +0 -10
  44. package/esm5/lib/gg-questionnaire-v2.module.js +0 -49
  45. package/esm5/lib/interfaces/attachment.js +0 -1
  46. package/esm5/lib/interfaces/checklist-item.js +0 -1
  47. package/esm5/lib/interfaces/condition-group.js +0 -1
  48. package/esm5/lib/interfaces/condition.js +0 -1
  49. package/esm5/lib/interfaces/question.js +0 -1
  50. package/esm5/lib/interfaces/questionnaire-options.js +0 -1
  51. package/esm5/lib/interfaces/questionnaire.js +0 -1
  52. package/esm5/lib/interfaces/repeater-question.js +0 -1
  53. package/esm5/lib/interfaces/section.js +0 -1
  54. package/esm5/lib/interfaces/select-option.js +0 -1
  55. package/esm5/lib/interfaces/validator.js +0 -1
  56. package/esm5/lib/services/filter.service.js +0 -384
  57. package/esm5/lib/services/form-constructor.service.js +0 -349
  58. package/esm5/lib/services/questionnaire.service.js +0 -572
  59. package/esm5/public_api.js +0 -12
  60. package/fesm5/glowgreen-gg-questionnaire-v2.js +0 -1397
  61. package/fesm5/glowgreen-gg-questionnaire-v2.js.map +0 -1
@@ -1,1397 +0,0 @@
1
- import { __values } from 'tslib';
2
- import { ɵɵdefineInjectable, ɵsetClassMetadata, Injectable, ɵɵinject, EventEmitter, Inject, ɵɵdefineNgModule, ɵɵdefineInjector, ɵɵsetNgModuleScope, NgModule } from '@angular/core';
3
- import { Validators, FormBuilder, FormArray, FormsModule, ReactiveFormsModule } from '@angular/forms';
4
- import { clone, get, keys, cloneDeep } from 'lodash';
5
-
6
- var QuestionType;
7
- (function (QuestionType) {
8
- QuestionType["Input"] = "input";
9
- QuestionType["Select"] = "select";
10
- QuestionType["Textarea"] = "textarea";
11
- QuestionType["Repeater"] = "repeater";
12
- QuestionType["Checklist"] = "checklist";
13
- QuestionType["Attachment"] = "attachment";
14
- QuestionType["Upload"] = "upload";
15
- QuestionType["Signature"] = "signature";
16
- QuestionType["Slider"] = "slider";
17
- QuestionType["Info"] = "info";
18
- QuestionType["Feedback"] = "feedback";
19
- })(QuestionType || (QuestionType = {}));
20
-
21
- var ConditionType;
22
- (function (ConditionType) {
23
- ConditionType["EqualTo"] = "equalTo";
24
- ConditionType["NotEqualTo"] = "notEqualTo";
25
- ConditionType["LessThan"] = "lessThan";
26
- ConditionType["GreaterThan"] = "greaterThan";
27
- ConditionType["Includes"] = "includes";
28
- ConditionType["Excludes"] = "excludes";
29
- })(ConditionType || (ConditionType = {}));
30
-
31
- var ConditionRelationship;
32
- (function (ConditionRelationship) {
33
- ConditionRelationship["Every"] = "every";
34
- ConditionRelationship["Any"] = "any";
35
- })(ConditionRelationship || (ConditionRelationship = {}));
36
-
37
- var FilterService = /** @class */ (function () {
38
- function FilterService() {
39
- }
40
- /**
41
- * Filters an array of sections based on their condition groups.
42
- * @param sections The array of sections to filter.
43
- * @param currentQuestionnaireForm The current form data, can be null/undefined.
44
- * @returns An array of filtered sections with their filtered questions.
45
- * @author Will Poulson
46
- */
47
- FilterService.prototype.filterSections = function (sections, currentQuestionnaireForm, additionalData) {
48
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
49
- var filteredSections = [];
50
- try {
51
- for (var sections_1 = __values(sections), sections_1_1 = sections_1.next(); !sections_1_1.done; sections_1_1 = sections_1.next()) {
52
- var section = sections_1_1.value;
53
- if (!this.meetsConditionGroups(section.conditionGroups, currentQuestionnaireForm, additionalData)) {
54
- continue;
55
- }
56
- var filteredQuestions = [];
57
- try {
58
- for (var _e = (e_2 = void 0, __values(section.questions)), _f = _e.next(); !_f.done; _f = _e.next()) {
59
- var question = _f.value;
60
- if (!this.meetsConditionGroups(question.conditionGroups, currentQuestionnaireForm, additionalData)) {
61
- continue;
62
- }
63
- // If it's a repeater we need to do a lot of extra filtering due to per item conditions.
64
- if (question.type === 'repeater' && currentQuestionnaireForm) {
65
- var items = currentQuestionnaireForm.get([section.name, question.name]);
66
- if (items && items.controls) {
67
- try {
68
- for (var _g = (e_3 = void 0, __values(items.controls)), _h = _g.next(); !_h.done; _h = _g.next()) {
69
- var repeaterItem = _h.value;
70
- var filteredRepeaterItemQuestions = [];
71
- try {
72
- for (var _j = (e_4 = void 0, __values(question.repeaterQuestions)), _k = _j.next(); !_k.done; _k = _j.next()) {
73
- var repeaterQuestion = _k.value;
74
- var meetsConditions = this.meetsConditionGroups(repeaterQuestion.conditionGroups, currentQuestionnaireForm, additionalData, repeaterItem);
75
- if (meetsConditions) {
76
- var cloneQuestion = clone(repeaterQuestion);
77
- cloneQuestion.selectOptions = this.getFetchOptionsForQuestion(repeaterQuestion, additionalData, currentQuestionnaireForm, repeaterItem);
78
- filteredRepeaterItemQuestions.push(cloneQuestion);
79
- }
80
- }
81
- }
82
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
83
- finally {
84
- try {
85
- if (_k && !_k.done && (_d = _j.return)) _d.call(_j);
86
- }
87
- finally { if (e_4) throw e_4.error; }
88
- }
89
- var index = items.controls.indexOf(repeaterItem);
90
- if (!question.filteredRepeaterQuestions) {
91
- question.filteredRepeaterQuestions = [];
92
- }
93
- question.filteredRepeaterQuestions[index] = filteredRepeaterItemQuestions;
94
- }
95
- }
96
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
97
- finally {
98
- try {
99
- if (_h && !_h.done && (_c = _g.return)) _c.call(_g);
100
- }
101
- finally { if (e_3) throw e_3.error; }
102
- }
103
- }
104
- }
105
- question.selectOptions = this.getFetchOptionsForQuestion(question, additionalData, currentQuestionnaireForm);
106
- filteredQuestions.push(question);
107
- }
108
- }
109
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
110
- finally {
111
- try {
112
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
113
- }
114
- finally { if (e_2) throw e_2.error; }
115
- }
116
- if (filteredQuestions.length === 0) {
117
- continue;
118
- }
119
- section.questions = filteredQuestions;
120
- filteredSections.push(section);
121
- }
122
- }
123
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
124
- finally {
125
- try {
126
- if (sections_1_1 && !sections_1_1.done && (_a = sections_1.return)) _a.call(sections_1);
127
- }
128
- finally { if (e_1) throw e_1.error; }
129
- }
130
- return filteredSections;
131
- };
132
- FilterService.prototype.getFetchOptionsForQuestion = function (question, additionalData, currentQuestionnaireForm, repeaterItem) {
133
- if (!question.fetchPath) {
134
- return question.selectOptions;
135
- }
136
- var options = additionalData[question.fetchPath];
137
- if (question.fetchSelectors && question.fetchSelectors.length > 0) {
138
- var values = this.getValuesForArray(question.fetchSelectors, currentQuestionnaireForm, additionalData, repeaterItem);
139
- options = get(additionalData[question.fetchPath], values);
140
- }
141
- options = this.getFinalFetchArray(options);
142
- if (!question.selectOptions) {
143
- question.selectOptions = [];
144
- }
145
- return question.selectOptions.concat(options);
146
- };
147
- FilterService.prototype.getFinalFetchArray = function (items) {
148
- var e_5, _a;
149
- if (!items) {
150
- return null;
151
- }
152
- if (!Array.isArray(items)) {
153
- items = keys(items);
154
- }
155
- if (items.length <= 0) {
156
- return null;
157
- }
158
- var convertedArray = [];
159
- try {
160
- for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
161
- var item = items_1_1.value;
162
- if (item) {
163
- if (item instanceof Object && (item.value !== undefined) && item.label) {
164
- convertedArray.push(item);
165
- }
166
- else {
167
- convertedArray.push({ label: this.uppercase(item), value: item });
168
- }
169
- }
170
- }
171
- }
172
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
173
- finally {
174
- try {
175
- if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
176
- }
177
- finally { if (e_5) throw e_5.error; }
178
- }
179
- return convertedArray;
180
- };
181
- FilterService.prototype.uppercase = function (item) {
182
- if (!isNaN(item)) {
183
- return item;
184
- }
185
- return item.charAt(0).toUpperCase() + item.slice(1);
186
- };
187
- /**
188
- * Checks if each condition groups conditions have been met.
189
- * @param conditionGroups The array of condition groups.
190
- * @param currentQuestionnaireForm The current form data, can be null/undefined.
191
- * @returns A boolean stating if the condition groups conditions has been met.
192
- * @author Will Poulson
193
- */
194
- FilterService.prototype.meetsConditionGroups = function (conditionGroups, currentQuestionnaireForm, additionalData, repeaterItem) {
195
- var _this = this;
196
- if (!conditionGroups || conditionGroups.length === 0) {
197
- return true;
198
- }
199
- return conditionGroups.every(function (x) {
200
- return _this.meetsConditionGroup(x, currentQuestionnaireForm, additionalData, repeaterItem) === true;
201
- });
202
- };
203
- /**
204
- * Checks if a condition groups conditions have been met.
205
- * @param conditionGroup The condition group to check.
206
- * @param currentQuestionnaireForm The current form data, can be null/undefined.
207
- * @returns A boolean stating if the condition group conditions has been met.
208
- * @author Will Poulson
209
- */
210
- FilterService.prototype.meetsConditionGroup = function (conditionGroup, currentQuestionnaireForm, additionalData, repeaterItem) {
211
- var e_6, _a;
212
- if (!conditionGroup.conditions || conditionGroup.conditions.length === 0) {
213
- return true;
214
- }
215
- var results = [];
216
- try {
217
- for (var _b = __values(conditionGroup.conditions), _c = _b.next(); !_c.done; _c = _b.next()) {
218
- var condition = _c.value;
219
- results.push(this.meetsCondition(condition, currentQuestionnaireForm, additionalData, repeaterItem));
220
- }
221
- }
222
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
223
- finally {
224
- try {
225
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
226
- }
227
- finally { if (e_6) throw e_6.error; }
228
- }
229
- switch (conditionGroup.relationship) {
230
- case ConditionRelationship.Every:
231
- return results.every(function (x) {
232
- return x === true;
233
- });
234
- case ConditionRelationship.Any:
235
- return !!results.find(function (x) {
236
- return x === true;
237
- });
238
- default:
239
- return true;
240
- }
241
- };
242
- /**
243
- * Checks if a condition has been met.
244
- * @param condition The condition to check.
245
- * @param currentQuestionnaireForm The current form data, can be null/undefined.
246
- * @returns A boolean stating if the condition group conditions has been met.
247
- * @author Will Poulson
248
- */
249
- FilterService.prototype.meetsCondition = function (condition, currentQuestionnaireForm, additionalData, repeaterItem) {
250
- var e_7, _a, e_8, _b;
251
- var v1s = this.getValuesForArray(condition.v1s, currentQuestionnaireForm, additionalData, repeaterItem);
252
- var v2s = this.getValuesForArray(condition.v2s, currentQuestionnaireForm, additionalData, repeaterItem);
253
- var result = false;
254
- switch (condition.type) {
255
- case ConditionType.Excludes:
256
- result = v1s.filter(function (v1) {
257
- return v2s.indexOf(v1) > -1;
258
- }).length === 0;
259
- break;
260
- case ConditionType.Includes:
261
- result = v1s.filter(function (v1) {
262
- return v2s.indexOf(v1) > -1;
263
- }).length !== 0;
264
- break;
265
- default: try {
266
- for (var v2s_1 = __values(v2s), v2s_1_1 = v2s_1.next(); !v2s_1_1.done; v2s_1_1 = v2s_1.next()) {
267
- var v2 = v2s_1_1.value;
268
- try {
269
- for (var v1s_1 = (e_8 = void 0, __values(v1s)), v1s_1_1 = v1s_1.next(); !v1s_1_1.done; v1s_1_1 = v1s_1.next()) {
270
- var v1 = v1s_1_1.value;
271
- switch (condition.type) {
272
- case ConditionType.EqualTo:
273
- v1 = this.getStringForValue(v1);
274
- v2 = this.getStringForValue(v2);
275
- if (v1 === v2) {
276
- result = true;
277
- }
278
- break;
279
- case ConditionType.NotEqualTo:
280
- v1 = this.getStringForValue(v1);
281
- v2 = this.getStringForValue(v2);
282
- if (v1 !== v2) {
283
- result = true;
284
- }
285
- break;
286
- case ConditionType.GreaterThan:
287
- v1 = parseFloat(v1);
288
- v2 = parseFloat(v2);
289
- if (v1 >= v2) {
290
- result = true;
291
- }
292
- break;
293
- case ConditionType.LessThan:
294
- v1 = parseFloat(v1);
295
- v2 = parseFloat(v2);
296
- if (v1 <= v2) {
297
- result = true;
298
- }
299
- break;
300
- }
301
- }
302
- }
303
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
304
- finally {
305
- try {
306
- if (v1s_1_1 && !v1s_1_1.done && (_b = v1s_1.return)) _b.call(v1s_1);
307
- }
308
- finally { if (e_8) throw e_8.error; }
309
- }
310
- }
311
- }
312
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
313
- finally {
314
- try {
315
- if (v2s_1_1 && !v2s_1_1.done && (_a = v2s_1.return)) _a.call(v2s_1);
316
- }
317
- finally { if (e_7) throw e_7.error; }
318
- }
319
- }
320
- return result;
321
- };
322
- FilterService.prototype.getStringForValue = function (value) {
323
- switch (value) {
324
- case null:
325
- return 'null';
326
- default:
327
- return value.toString();
328
- }
329
- };
330
- /**
331
- * Gets the current values for an array of strings.
332
- * @param paths The array of strings to check.
333
- * @param currentQuestionnaireForm The current form data, can be null/undefined.
334
- * @returns An array of strings/values
335
- * @author Will Poulson
336
- */
337
- FilterService.prototype.getValuesForArray = function (paths, currentQuestionnaireForm, additionalData, repeaterItem) {
338
- var e_9, _a;
339
- if (currentQuestionnaireForm) {
340
- var values = [];
341
- try {
342
- for (var paths_1 = __values(paths), paths_1_1 = paths_1.next(); !paths_1_1.done; paths_1_1 = paths_1.next()) {
343
- var path = paths_1_1.value;
344
- var value = this.getValueForString(path, currentQuestionnaireForm, additionalData, repeaterItem);
345
- values.push(value);
346
- }
347
- }
348
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
349
- finally {
350
- try {
351
- if (paths_1_1 && !paths_1_1.done && (_a = paths_1.return)) _a.call(paths_1);
352
- }
353
- finally { if (e_9) throw e_9.error; }
354
- }
355
- return values;
356
- }
357
- else {
358
- return paths;
359
- }
360
- };
361
- /**
362
- * Gets a current value for a string.
363
- * @param path The string value to check.
364
- * @param currentQuestionnaireForm The current form data, can be null/undefined.
365
- * @returns A string/value.
366
- * @author Will Poulson
367
- */
368
- FilterService.prototype.getValueForString = function (path, currentQuestionnaireForm, additionalData, repeaterItem) {
369
- if (currentQuestionnaireForm) {
370
- if (!additionalData) {
371
- additionalData = {};
372
- }
373
- var control = currentQuestionnaireForm.get(path.toString());
374
- if (!control && repeaterItem) {
375
- control = repeaterItem.get(path);
376
- }
377
- var value = control ? control.value : this.convertStringToValue(path);
378
- return get(additionalData, path, value);
379
- }
380
- else {
381
- return this.convertStringToValue(path);
382
- }
383
- };
384
- /**
385
- * Converts a string to a value.
386
- * Eg. true/false.
387
- * @param path The string value to check for conversion;
388
- * @returns Either a string or the converted value.
389
- * @author Will Poulson
390
- */
391
- FilterService.prototype.convertStringToValue = function (path) {
392
- switch (path) {
393
- case 'true':
394
- return true;
395
- case 'false':
396
- return false;
397
- case 'null':
398
- return null;
399
- default:
400
- return path;
401
- }
402
- };
403
- FilterService.ɵfac = function FilterService_Factory(t) { return new (t || FilterService)(); };
404
- FilterService.ɵprov = ɵɵdefineInjectable({ token: FilterService, factory: FilterService.ɵfac, providedIn: 'root' });
405
- return FilterService;
406
- }());
407
- /*@__PURE__*/ (function () { ɵsetClassMetadata(FilterService, [{
408
- type: Injectable,
409
- args: [{
410
- providedIn: 'root'
411
- }]
412
- }], function () { return []; }, null); })();
413
-
414
- var ValidationType;
415
- (function (ValidationType) {
416
- ValidationType["Required"] = "required";
417
- ValidationType["Boolean"] = "boolean";
418
- ValidationType["Number"] = "number";
419
- ValidationType["String"] = "string";
420
- ValidationType["Email"] = "email";
421
- ValidationType["Telephone"] = "telephone";
422
- })(ValidationType || (ValidationType = {}));
423
-
424
- var FormConstructorService = /** @class */ (function () {
425
- function FormConstructorService(fb, filterService) {
426
- this.fb = fb;
427
- this.filterService = filterService;
428
- }
429
- /**
430
- * Constructs a form group based on the questionnaire data.
431
- * This form group has been filtered down based on the conditions of the sections and questions.
432
- * @param questionnaire The questionnaire data to build the form group on.
433
- * @returns A filted form group.
434
- * @author Will Poulson
435
- */
436
- FormConstructorService.prototype.generateFormsForQuestionnaire = function (questionnaire, currentQuestionnaire, currentQuestionnaireForm, additionalData, skipFilter) {
437
- var sections = skipFilter ?
438
- questionnaire.sections :
439
- this.filterService.filterSections(questionnaire.sections, currentQuestionnaireForm, additionalData);
440
- var noChange = currentQuestionnaireForm && (sections === currentQuestionnaire.sections);
441
- if (noChange && !skipFilter) {
442
- return null;
443
- }
444
- var questionnaireForms = this.generateFormsForSections(sections, currentQuestionnaireForm);
445
- return { sections: sections, forms: questionnaireForms };
446
- };
447
- FormConstructorService.prototype.generateFormsForSections = function (sections, currentQuestionnaireForm) {
448
- var e_1, _a;
449
- var questionnaireForms = this.fb.group({});
450
- try {
451
- for (var sections_1 = __values(sections), sections_1_1 = sections_1.next(); !sections_1_1.done; sections_1_1 = sections_1.next()) {
452
- var section = sections_1_1.value;
453
- var sectionForms = this.generateFormsForSection(section, currentQuestionnaireForm);
454
- questionnaireForms.addControl(section.name, sectionForms);
455
- }
456
- }
457
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
458
- finally {
459
- try {
460
- if (sections_1_1 && !sections_1_1.done && (_a = sections_1.return)) _a.call(sections_1);
461
- }
462
- finally { if (e_1) throw e_1.error; }
463
- }
464
- return questionnaireForms;
465
- };
466
- /**
467
- * Constructs a form group based on the section data.
468
- * This form group has been filtered down based on the conditions of the questions.
469
- * @param section The section data to build the form group on.
470
- * @returns A filtered form group.
471
- * @author Will Poulson
472
- */
473
- FormConstructorService.prototype.generateFormsForSection = function (section, currentQuestionnaireForm) {
474
- var e_2, _a;
475
- var sectionForms = this.fb.group({});
476
- try {
477
- for (var _b = __values(section.questions), _c = _b.next(); !_c.done; _c = _b.next()) {
478
- var question = _c.value;
479
- if (question.type === QuestionType.Info) {
480
- continue;
481
- }
482
- var questionControl = this.generateControlForQuestion(question, section, currentQuestionnaireForm);
483
- sectionForms.addControl(question.name, questionControl);
484
- }
485
- }
486
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
487
- finally {
488
- try {
489
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
490
- }
491
- finally { if (e_2) throw e_2.error; }
492
- }
493
- return sectionForms;
494
- };
495
- /**
496
- * Constructs an astract form control based on the question data.
497
- * @param question The question data to build the abstract control on.
498
- * @returns An abstract control.
499
- * @author Will Poulson
500
- */
501
- FormConstructorService.prototype.generateControlForQuestion = function (question, section, currentQuestionnaireForm) {
502
- var e_3, _a;
503
- var convertedValidators = this.convertValidators(question.validators);
504
- var defaultValue = question.defaultValue ? question.defaultValue : null;
505
- switch (question.type) {
506
- case QuestionType.Repeater:
507
- var newArray = this.fb.array([], convertedValidators);
508
- var currentArray = currentQuestionnaireForm ? currentQuestionnaireForm.get([section.name, question.name]) : false;
509
- if (currentArray && question.filteredRepeaterQuestions) {
510
- var _loop_1 = function (control) {
511
- var e_4, _a, e_5, _b;
512
- var index = currentArray.controls.indexOf(control);
513
- if (index === -1) {
514
- return "continue";
515
- }
516
- var filteredQuestions = question.filteredRepeaterQuestions[index];
517
- if (!filteredQuestions) {
518
- return "continue";
519
- }
520
- var filteredQuestionNames = filteredQuestions.map(function (x) { return x.name; });
521
- var filteredOutQuestions = question.repeaterQuestions.filter(function (x) { return !filteredQuestionNames.includes(x.name); });
522
- try {
523
- for (var filteredOutQuestions_1 = (e_4 = void 0, __values(filteredOutQuestions)), filteredOutQuestions_1_1 = filteredOutQuestions_1.next(); !filteredOutQuestions_1_1.done; filteredOutQuestions_1_1 = filteredOutQuestions_1.next()) {
524
- var filteredOutQuestion = filteredOutQuestions_1_1.value;
525
- var childControl = control.get(filteredOutQuestion.name);
526
- childControl.setValidators([]);
527
- childControl.updateValueAndValidity({ onlySelf: true, emitEvent: false });
528
- }
529
- }
530
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
531
- finally {
532
- try {
533
- if (filteredOutQuestions_1_1 && !filteredOutQuestions_1_1.done && (_a = filteredOutQuestions_1.return)) _a.call(filteredOutQuestions_1);
534
- }
535
- finally { if (e_4) throw e_4.error; }
536
- }
537
- try {
538
- for (var filteredQuestions_1 = (e_5 = void 0, __values(filteredQuestions)), filteredQuestions_1_1 = filteredQuestions_1.next(); !filteredQuestions_1_1.done; filteredQuestions_1_1 = filteredQuestions_1.next()) {
539
- var filteredQuestion = filteredQuestions_1_1.value;
540
- var childControl = control.get(filteredQuestion.name);
541
- if (childControl.value) {
542
- // If there's a value just skip it, no need to redo the validators each time.
543
- continue;
544
- }
545
- var convertedValidators_1 = this_1.convertValidators(filteredQuestion.validators);
546
- childControl.setValidators(convertedValidators_1);
547
- childControl.updateValueAndValidity({ onlySelf: true, emitEvent: false });
548
- }
549
- }
550
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
551
- finally {
552
- try {
553
- if (filteredQuestions_1_1 && !filteredQuestions_1_1.done && (_b = filteredQuestions_1.return)) _b.call(filteredQuestions_1);
554
- }
555
- finally { if (e_5) throw e_5.error; }
556
- }
557
- };
558
- var this_1 = this;
559
- try {
560
- for (var _b = __values(currentArray.controls), _c = _b.next(); !_c.done; _c = _b.next()) {
561
- var control = _c.value;
562
- _loop_1(control);
563
- }
564
- }
565
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
566
- finally {
567
- try {
568
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
569
- }
570
- finally { if (e_3) throw e_3.error; }
571
- }
572
- }
573
- return currentArray ? currentArray : newArray;
574
- case QuestionType.Checklist:
575
- return this.generateGroupForChecklist(question, convertedValidators);
576
- case QuestionType.Slider:
577
- return this.fb.control(defaultValue, { validators: convertedValidators }); // Remove update on blur for sliders.
578
- default:
579
- return this.fb.control(defaultValue, { validators: convertedValidators, updateOn: 'blur' });
580
- }
581
- };
582
- /**
583
- * Generates the form group for a checklist question.
584
- * @param question The question. Must be of type checklist or else null is returned.
585
- * @returns A form group.
586
- * @author Will Poulson
587
- */
588
- FormConstructorService.prototype.generateGroupForChecklist = function (question, convertedValidators) {
589
- var e_6, _a;
590
- if (question.type !== QuestionType.Checklist || question.checklistItems.length === 0) {
591
- return null;
592
- }
593
- var checklistForms = this.fb.group({});
594
- try {
595
- for (var _b = __values(question.checklistItems), _c = _b.next(); !_c.done; _c = _b.next()) {
596
- var checklistItem = _c.value;
597
- var checklistItemControl = this.fb.control(null, convertedValidators);
598
- checklistForms.addControl(checklistItem.name, checklistItemControl);
599
- }
600
- }
601
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
602
- finally {
603
- try {
604
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
605
- }
606
- finally { if (e_6) throw e_6.error; }
607
- }
608
- return checklistForms;
609
- };
610
- /**
611
- * Generates the form template for a repeater question.
612
- * @param question The question. Must be of type repeater or else null is returned.
613
- * @returns A form group.
614
- * @author Will Poulson
615
- */
616
- FormConstructorService.prototype.generateFormsForRepeater = function (question, index) {
617
- var e_7, _a;
618
- if (question.type !== QuestionType.Repeater || question.repeaterQuestions.length === 0) {
619
- console.log('Question isnt a repeater or has no questions, returning null');
620
- return null;
621
- }
622
- var repeaterForms = this.fb.group({});
623
- try {
624
- for (var _b = __values(question.repeaterQuestions), _c = _b.next(); !_c.done; _c = _b.next()) {
625
- var repeaterQuestion = _c.value;
626
- var repeaterQuestionControl = this.generateControlForRepeaterQuestion(repeaterQuestion);
627
- repeaterForms.addControl(repeaterQuestion.name, repeaterQuestionControl);
628
- }
629
- }
630
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
631
- finally {
632
- try {
633
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
634
- }
635
- finally { if (e_7) throw e_7.error; }
636
- }
637
- return repeaterForms;
638
- };
639
- /**
640
- * Generates a control for a repeaters question.
641
- * @param repeaterQuestion A repeater question.
642
- * @returns An abstract control.
643
- * @author Will Poulson
644
- */
645
- FormConstructorService.prototype.generateControlForRepeaterQuestion = function (repeaterQuestion) {
646
- var convertedValidators = this.convertValidators(repeaterQuestion.validators);
647
- return this.fb.control(null, { validators: convertedValidators, updateOn: repeaterQuestion.type === 'select' ? 'change' : 'blur' });
648
- };
649
- /**
650
- * Converts an array of validators into form validators.
651
- * @param validators The uncoverted array of validators.
652
- * @returns A convered array of form validators.
653
- * @author Will Poulson
654
- */
655
- FormConstructorService.prototype.convertValidators = function (validators) {
656
- var e_8, _a;
657
- if (!validators || validators.length === 0) {
658
- return [];
659
- }
660
- var convertedValidators = [];
661
- try {
662
- for (var validators_1 = __values(validators), validators_1_1 = validators_1.next(); !validators_1_1.done; validators_1_1 = validators_1.next()) {
663
- var validator = validators_1_1.value;
664
- var convertedValidator = this.convertValidator(validator);
665
- if (convertedValidator) {
666
- convertedValidators.push(convertedValidator);
667
- }
668
- }
669
- }
670
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
671
- finally {
672
- try {
673
- if (validators_1_1 && !validators_1_1.done && (_a = validators_1.return)) _a.call(validators_1);
674
- }
675
- finally { if (e_8) throw e_8.error; }
676
- }
677
- return convertedValidators;
678
- };
679
- /**
680
- * Converts a single validator into a form validator.
681
- * @param validator The unconverted validator.
682
- * @returns A converted form validator.
683
- * @author Will Poulson
684
- */
685
- FormConstructorService.prototype.convertValidator = function (validator) {
686
- switch (validator.type) {
687
- case ValidationType.Required:
688
- return Validators.required;
689
- default:
690
- return null;
691
- }
692
- };
693
- /**
694
- * Constructs the forms for repeaters from saved state.
695
- * @param savedState The saved state to load.
696
- * @param sections The sections already generated by the form constructor.
697
- */
698
- FormConstructorService.prototype.constructRepeaterFromsFromState = function (savedState, sections, currentQuestionnaireForm) {
699
- var e_9, _a, e_10, _b, e_11, _c;
700
- try {
701
- for (var sections_2 = __values(sections), sections_2_1 = sections_2.next(); !sections_2_1.done; sections_2_1 = sections_2.next()) {
702
- var section = sections_2_1.value;
703
- try {
704
- for (var _d = (e_10 = void 0, __values(section.questions)), _e = _d.next(); !_e.done; _e = _d.next()) {
705
- var question = _e.value;
706
- if (question.type !== QuestionType.Repeater) {
707
- continue;
708
- }
709
- var repeaterArray = savedState[section.name][question.name];
710
- if (!repeaterArray || repeaterArray.length === 0) {
711
- continue;
712
- }
713
- var repeaterFormArray = currentQuestionnaireForm.get([section.name, question.name]);
714
- if (repeaterFormArray.controls.length === repeaterArray.length) {
715
- continue;
716
- }
717
- try {
718
- // For each item that used to be there push a new empty template there to be populated.
719
- for (var repeaterArray_1 = (e_11 = void 0, __values(repeaterArray)), repeaterArray_1_1 = repeaterArray_1.next(); !repeaterArray_1_1.done; repeaterArray_1_1 = repeaterArray_1.next()) {
720
- var repeaterItem = repeaterArray_1_1.value;
721
- var index = repeaterArray.indexOf(repeaterItem);
722
- var repeaterItemTemplate = this.generateFormsForRepeater(question, index);
723
- repeaterFormArray.push(repeaterItemTemplate);
724
- }
725
- }
726
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
727
- finally {
728
- try {
729
- if (repeaterArray_1_1 && !repeaterArray_1_1.done && (_c = repeaterArray_1.return)) _c.call(repeaterArray_1);
730
- }
731
- finally { if (e_11) throw e_11.error; }
732
- }
733
- }
734
- }
735
- catch (e_10_1) { e_10 = { error: e_10_1 }; }
736
- finally {
737
- try {
738
- if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
739
- }
740
- finally { if (e_10) throw e_10.error; }
741
- }
742
- }
743
- }
744
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
745
- finally {
746
- try {
747
- if (sections_2_1 && !sections_2_1.done && (_a = sections_2.return)) _a.call(sections_2);
748
- }
749
- finally { if (e_9) throw e_9.error; }
750
- }
751
- };
752
- FormConstructorService.ɵfac = function FormConstructorService_Factory(t) { return new (t || FormConstructorService)(ɵɵinject(FormBuilder), ɵɵinject(FilterService)); };
753
- FormConstructorService.ɵprov = ɵɵdefineInjectable({ token: FormConstructorService, factory: FormConstructorService.ɵfac, providedIn: 'root' });
754
- return FormConstructorService;
755
- }());
756
- /*@__PURE__*/ (function () { ɵsetClassMetadata(FormConstructorService, [{
757
- type: Injectable,
758
- args: [{
759
- providedIn: 'root'
760
- }]
761
- }], function () { return [{ type: FormBuilder }, { type: FilterService }]; }, null); })();
762
-
763
- var QuestionnaireService = /** @class */ (function () {
764
- function QuestionnaireService(formConstuctor, options) {
765
- this.formConstuctor = formConstuctor;
766
- this.options = options;
767
- this.dataChangedEvent = new EventEmitter();
768
- this.sectionFinishedEvent = new EventEmitter();
769
- this.skippable = true;
770
- }
771
- /**
772
- * Loads a questionnaire from data.
773
- * @param questionnaire The questionnaire data to load, often parsed JSON from the editor.
774
- * @param savedState The saved state of the questionnaire, often from localstorage.
775
- * @returns null
776
- * @author Will Poulson
777
- */
778
- QuestionnaireService.prototype.loadQuestionnaire = function (questionnaire, savedState, additionalData) {
779
- this.originalQuestionnaire = cloneDeep(questionnaire);
780
- this.currentQuestionnaire = cloneDeep(questionnaire);
781
- this.additionalData = additionalData;
782
- if (savedState && Object.keys(savedState).length > 0) {
783
- var newData = this.formConstuctor.generateFormsForQuestionnaire(cloneDeep(this.originalQuestionnaire), cloneDeep(this.currentQuestionnaire), this.currentQuestionnaireForm, this.additionalData, true);
784
- if (newData) {
785
- this.formConstuctor.constructRepeaterFromsFromState(savedState, newData.sections, newData.forms);
786
- this.currentQuestionnaireForm = newData.forms;
787
- this.currentQuestionnaireForm.patchValue(savedState);
788
- }
789
- }
790
- this.updateQuestionnaire(savedState);
791
- if (this.options.skipToFirstSection) {
792
- this.currentSectionName = Object.keys(this.currentQuestionnaireForm.controls)[0];
793
- }
794
- if (this.options.skipToFirstQuestion) {
795
- this.currentQuestionName = Object.keys(this.currentSection.formGroup.controls)[0];
796
- }
797
- };
798
- /**
799
- * Updates the current questionnaire, used when a value is updated to check conditions.
800
- * @param savedState The saved state of the questionnaire, often from localstorage.
801
- * @returns null
802
- * @author Will Poulson
803
- */
804
- QuestionnaireService.prototype.updateQuestionnaire = function (savedState) {
805
- var e_1, _a, e_2, _b;
806
- var _this = this;
807
- var oldLength = this.currentQuestionnaire.sections.map(function (x) { return x.questions.length; }).reduce(function (a, b) { return a + b; }, 0);
808
- var newData = this.formConstuctor.generateFormsForQuestionnaire(cloneDeep(this.originalQuestionnaire), cloneDeep(this.currentQuestionnaire), this.currentQuestionnaireForm, this.additionalData);
809
- if (newData) {
810
- this.currentQuestionnaireForm = newData.forms;
811
- this.currentQuestionnaire.sections = newData.sections;
812
- }
813
- if (savedState && Object.keys(savedState).length > 0) {
814
- this.currentQuestionnaireForm.patchValue(savedState);
815
- }
816
- if (this.questionnaireValueChangeSubscription) {
817
- this.questionnaireValueChangeSubscription.unsubscribe();
818
- }
819
- if (this.questionValueChangeSubscription) {
820
- this.questionValueChangeSubscription.unsubscribe();
821
- }
822
- this.questionnaireValueChangeSubscription = this.currentQuestionnaireForm.valueChanges.subscribe(function () {
823
- _this.dataChangedEvent.emit();
824
- _this.updateQuestionnaire(_this.currentQuestionnaireForm.getRawValue());
825
- });
826
- if (this.currentQuestion && this.currentQuestion.formControl) {
827
- this.questionValueChangeSubscription = this.currentQuestion.formControl.valueChanges.subscribe(function () {
828
- _this.clearFields(_this.currentQuestion.data.clearfields);
829
- });
830
- }
831
- var newLength = this.currentQuestionnaire.sections.map(function (x) { return x.questions.length; }).reduce(function (a, b) { return a + b; }, 0);
832
- if (oldLength !== newLength) {
833
- try {
834
- for (var _c = __values(this.currentQuestionnaire.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
835
- var section = _d.value;
836
- try {
837
- for (var _e = (e_2 = void 0, __values(section.questions)), _f = _e.next(); !_f.done; _f = _e.next()) {
838
- var question = _f.value;
839
- var control = this.currentQuestionnaireForm.get([section.name, question.name]);
840
- if (control && control.value === null && question.defaultValue) {
841
- control.setValue(question.defaultValue);
842
- }
843
- }
844
- }
845
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
846
- finally {
847
- try {
848
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
849
- }
850
- finally { if (e_2) throw e_2.error; }
851
- }
852
- }
853
- }
854
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
855
- finally {
856
- try {
857
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
858
- }
859
- finally { if (e_1) throw e_1.error; }
860
- }
861
- }
862
- };
863
- QuestionnaireService.prototype.updateAdditionalData = function (payload) {
864
- this.additionalData = Object.assign(this.additionalData, payload);
865
- this.updateQuestionnaire(this.currentQuestionnaireForm.getRawValue());
866
- };
867
- QuestionnaireService.prototype.clearAdditionalData = function () {
868
- this.additionalData = {};
869
- this.updateQuestionnaire(this.currentQuestionnaireForm.getRawValue());
870
- };
871
- Object.defineProperty(QuestionnaireService.prototype, "isValid", {
872
- get: function () {
873
- return this.currentQuestionnaireForm.valid;
874
- },
875
- enumerable: true,
876
- configurable: true
877
- });
878
- Object.defineProperty(QuestionnaireService.prototype, "isFirstQuestion", {
879
- get: function () {
880
- if (!this.currentSection || !this.currentQuestion) {
881
- return false;
882
- }
883
- var currentIndex = this.currentSection.data.questions.indexOf(this.currentQuestion.data);
884
- return currentIndex === 0;
885
- },
886
- enumerable: true,
887
- configurable: true
888
- });
889
- Object.defineProperty(QuestionnaireService.prototype, "isLastQuestion", {
890
- get: function () {
891
- if (!this.currentSection || !this.currentQuestion) {
892
- return false;
893
- }
894
- var currentIndex = this.currentSection.data.questions.indexOf(this.currentQuestion.data);
895
- var maxIndex = this.currentSection.data.questions.length - 1;
896
- return currentIndex === maxIndex;
897
- },
898
- enumerable: true,
899
- configurable: true
900
- });
901
- Object.defineProperty(QuestionnaireService.prototype, "isFirstSection", {
902
- get: function () {
903
- if (!this.currentSection) {
904
- return false;
905
- }
906
- var currentIndex = this.currentQuestionnaire.sections.indexOf(this.currentSection.data);
907
- return currentIndex === 0;
908
- },
909
- enumerable: true,
910
- configurable: true
911
- });
912
- Object.defineProperty(QuestionnaireService.prototype, "isLastSection", {
913
- get: function () {
914
- if (!this.currentSection) {
915
- return false;
916
- }
917
- var currentIndex = this.currentQuestionnaire.sections.indexOf(this.currentSection.data);
918
- var maxIndex = this.currentQuestionnaire.sections.length - 1;
919
- return currentIndex === maxIndex;
920
- },
921
- enumerable: true,
922
- configurable: true
923
- });
924
- QuestionnaireService.prototype.sectionValid = function (name) {
925
- return this.currentQuestionnaireForm.get(name).valid;
926
- };
927
- /**
928
- * Clears all the fields parsed
929
- * @param clearfields An array of strings, paths to clear.
930
- * @author Will Poulson
931
- */
932
- QuestionnaireService.prototype.clearFields = function (clearfields) {
933
- var e_3, _a;
934
- if (!clearfields || clearfields.length === 0) {
935
- return;
936
- }
937
- try {
938
- for (var clearfields_1 = __values(clearfields), clearfields_1_1 = clearfields_1.next(); !clearfields_1_1.done; clearfields_1_1 = clearfields_1.next()) {
939
- var clearfield = clearfields_1_1.value;
940
- var control = this.currentQuestionnaireForm.get(clearfield);
941
- if (control) {
942
- control.reset(undefined);
943
- }
944
- }
945
- }
946
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
947
- finally {
948
- try {
949
- if (clearfields_1_1 && !clearfields_1_1.done && (_a = clearfields_1.return)) _a.call(clearfields_1);
950
- }
951
- finally { if (e_3) throw e_3.error; }
952
- }
953
- };
954
- /**
955
- * Unloads the current questionnaire.
956
- * Useful when exiting a questionnaire page or completing a questionnaire.
957
- * @author Will Poulson
958
- */
959
- QuestionnaireService.prototype.unloadQuestionnaire = function () {
960
- this.originalQuestionnaire = undefined;
961
- this.currentQuestionnaireForm = undefined;
962
- this.currentQuestionnaire = undefined;
963
- this.currentSectionName = undefined;
964
- this.currentQuestionName = undefined;
965
- this.additionalData = undefined;
966
- };
967
- Object.defineProperty(QuestionnaireService.prototype, "currentSection", {
968
- /**
969
- * Gets the current selected section.
970
- * @returns An object containing the section data and the related form group.
971
- * @author Will Poulson
972
- */
973
- get: function () {
974
- var _this = this;
975
- if (!this.currentSectionName) {
976
- return null;
977
- }
978
- var data = this.currentQuestionnaire.sections.find(function (x) {
979
- return x.name === _this.currentSectionName;
980
- });
981
- if (!data) {
982
- return null;
983
- }
984
- return {
985
- data: data,
986
- formGroup: this.currentQuestionnaireForm.get(data.name)
987
- };
988
- },
989
- enumerable: true,
990
- configurable: true
991
- });
992
- Object.defineProperty(QuestionnaireService.prototype, "availableSections", {
993
- /**
994
- * Gets all the avaialble sections.
995
- * @returns An array of all avaialble sections.
996
- * @author Will Poulson
997
- */
998
- get: function () {
999
- if (!this.currentQuestionnaire || !this.currentQuestionnaire.sections) {
1000
- return [];
1001
- }
1002
- return this.currentQuestionnaire.sections;
1003
- },
1004
- enumerable: true,
1005
- configurable: true
1006
- });
1007
- Object.defineProperty(QuestionnaireService.prototype, "currentData", {
1008
- /**
1009
- * Gets the current data for the entire questionnaire.
1010
- * @returns An object of data.
1011
- * @author Will Poulson
1012
- */
1013
- get: function () {
1014
- return this.currentQuestionnaireForm ? this.currentQuestionnaireForm.getRawValue() : {};
1015
- },
1016
- enumerable: true,
1017
- configurable: true
1018
- });
1019
- Object.defineProperty(QuestionnaireService.prototype, "currentQuestion", {
1020
- /**
1021
- * Gets the current selected question.
1022
- * @returns An object containing the question data and the related form control.
1023
- * @author Will Poulson
1024
- */
1025
- get: function () {
1026
- var _this = this;
1027
- if (!this.currentQuestionName) {
1028
- return null;
1029
- }
1030
- var data = this.currentSection.data.questions.find(function (x) {
1031
- return x.name === _this.currentQuestionName;
1032
- });
1033
- if (!data) {
1034
- return null;
1035
- }
1036
- var formControl = this.currentSection.formGroup.get(data.name);
1037
- return {
1038
- data: data,
1039
- formControl: formControl
1040
- };
1041
- },
1042
- enumerable: true,
1043
- configurable: true
1044
- });
1045
- /**
1046
- * Navigates to the next question.
1047
- * If there is no question to navigate to it will reject with a out of bounds exception.
1048
- * If allowSkipRequiredField is false then it will reject if the current question is invalid.
1049
- * If emitSectionFinishEvent is true then it will emit an event if it's navigating past the length of questions.
1050
- * @returns A promise which will resolve when the question has been navigated to.
1051
- * @author Will Poulson
1052
- */
1053
- QuestionnaireService.prototype.nextQuestion = function () {
1054
- var _this = this;
1055
- return new Promise(function (resolve, reject) {
1056
- if (!_this.options.allowSkipRequiredField) {
1057
- if (_this.currentQuestion.formControl && !_this.currentQuestion.formControl.valid) {
1058
- return reject('Current question is invalid');
1059
- }
1060
- }
1061
- _this.getCurrentQuestionIndex().then(function (currentIndex) {
1062
- var lastQuestionIndex = _this.currentSection.data.questions.length - 1;
1063
- var isLastQuestion = lastQuestionIndex === currentIndex;
1064
- if (isLastQuestion) {
1065
- _this.sectionFinishedEvent.emit();
1066
- return resolve();
1067
- }
1068
- var newIndex = currentIndex + 1;
1069
- _this.navigateToQuestion(newIndex).then(function () {
1070
- return resolve();
1071
- }).catch(function (error) {
1072
- return reject(error);
1073
- });
1074
- }).catch(function (error) {
1075
- return reject(error);
1076
- });
1077
- });
1078
- };
1079
- /**
1080
- * Navigates to the previous question.
1081
- * If there is no question to navigate to it will reject with a out of bounds exception.
1082
- * If allowNavigateBackwards is false then it will reject.
1083
- * @returns A promise which will resolve when the question has been navigated to.
1084
- * @author Will Poulson
1085
- */
1086
- QuestionnaireService.prototype.prevQuestion = function () {
1087
- var _this = this;
1088
- return new Promise(function (resolve, reject) {
1089
- if (!_this.options.allowNavigateBackwards) {
1090
- return reject('This questionnaire does not allow for backwards navigation');
1091
- }
1092
- _this.getCurrentQuestionIndex().then(function (currentIndex) {
1093
- var newIndex = currentIndex - 1;
1094
- _this.navigateToQuestion(newIndex).then(function () {
1095
- return resolve();
1096
- }).catch(function (error) {
1097
- return reject(error);
1098
- });
1099
- }).catch(function (error) {
1100
- return reject(error);
1101
- });
1102
- });
1103
- };
1104
- /**
1105
- * Gets the current questions index.
1106
- * @returns A promise which resolves a number.
1107
- * @author Will Poulson
1108
- */
1109
- QuestionnaireService.prototype.getCurrentQuestionIndex = function () {
1110
- var _this = this;
1111
- return new Promise(function (resolve, reject) {
1112
- var currentIndex = _this.currentSection.data.questions.indexOf(_this.currentQuestion.data);
1113
- if (currentIndex === -1) {
1114
- return reject('Could not find current question');
1115
- }
1116
- return resolve(currentIndex);
1117
- });
1118
- };
1119
- /**
1120
- * Checks a question is in bounds then navigates to it.
1121
- * @param index The index to navigate to.
1122
- * @returns A promise which will resolve when the question has been navigated to.
1123
- * @author Will Poulson
1124
- */
1125
- QuestionnaireService.prototype.navigateToQuestion = function (index) {
1126
- var _this = this;
1127
- return new Promise(function (resolve, reject) {
1128
- if (_this.currentSection.data.questions.length - 1 < index || index < 0) {
1129
- return reject('Out of bounds exception');
1130
- }
1131
- _this.currentQuestionName = _this.currentSection.data.questions[index].name;
1132
- return resolve();
1133
- });
1134
- };
1135
- /**
1136
- * Navigates to the next section.
1137
- * @returns A promise which will resolve when the section has been navigated to.
1138
- * @author Will Poulson
1139
- */
1140
- QuestionnaireService.prototype.nextSection = function () {
1141
- var _this = this;
1142
- return new Promise(function (resolve, reject) {
1143
- _this.getCurrentSectionIndex().then(function (currentIndex) {
1144
- var newIndex = currentIndex + 1;
1145
- _this.navigateToSection(newIndex).then(function () {
1146
- return resolve();
1147
- }).catch(function (error) {
1148
- return reject(error);
1149
- });
1150
- }).catch(function (error) {
1151
- return reject(error);
1152
- });
1153
- });
1154
- };
1155
- /**
1156
- * Navigates to the previous section.
1157
- * @returns A promise which will resolve when the section has been navigated to.
1158
- * @author Will Poulson
1159
- */
1160
- QuestionnaireService.prototype.prevSection = function () {
1161
- var _this = this;
1162
- return new Promise(function (resolve, reject) {
1163
- _this.getCurrentSectionIndex().then(function (currentIndex) {
1164
- var newIndex = currentIndex - 1;
1165
- _this.navigateToSection(newIndex).then(function () {
1166
- return resolve();
1167
- }).catch(function (error) {
1168
- return reject(error);
1169
- });
1170
- }).catch(function (error) {
1171
- return reject(error);
1172
- });
1173
- });
1174
- };
1175
- /**
1176
- * Gets the current section index.
1177
- * @returns A promise which resolves a number.
1178
- * @author Will Poulson
1179
- */
1180
- QuestionnaireService.prototype.getCurrentSectionIndex = function () {
1181
- var _this = this;
1182
- return new Promise(function (resolve, reject) {
1183
- var currentIndex = _this.currentQuestionnaire.sections.indexOf(_this.currentSection.data);
1184
- if (currentIndex === -1) {
1185
- return reject('Could not find current section');
1186
- }
1187
- return resolve(currentIndex);
1188
- });
1189
- };
1190
- /**
1191
- * Checks a section is in bounds then navigates to it.
1192
- * @param index The index to navigate to.
1193
- * @returns A promise which will resolve when the section has been navigated to.
1194
- * @author Will Poulson
1195
- */
1196
- QuestionnaireService.prototype.navigateToSection = function (index) {
1197
- var _this = this;
1198
- return new Promise(function (resolve, reject) {
1199
- if (_this.currentQuestionnaire.sections.length - 1 < index || index < 0) {
1200
- return reject('Out of bounds exception');
1201
- }
1202
- _this.currentSectionName = _this.currentQuestionnaire.sections[index].name;
1203
- return resolve();
1204
- });
1205
- };
1206
- /**
1207
- * Answers the current question with the parsed value.
1208
- * @param answer The value to answer the question with.
1209
- * @author Will Poulson
1210
- */
1211
- QuestionnaireService.prototype.answerCurrentQuestion = function (answer, sendToNextQuestion) {
1212
- var _this = this;
1213
- if (!sendToNextQuestion) {
1214
- this.currentQuestion.formControl.setValue(answer);
1215
- return;
1216
- }
1217
- if (this.skippable) { // Timeout to prevent skipping glitch.
1218
- this.skippable = false;
1219
- setTimeout(function () {
1220
- _this.currentQuestion.formControl.setValue(answer);
1221
- _this.nextQuestion().then(function () {
1222
- _this.skippable = true;
1223
- });
1224
- }, 150);
1225
- }
1226
- };
1227
- /**
1228
- * Adds a repeater item to the current question.
1229
- * Current question must be of type Repeater.
1230
- * @returns A promise which resolves when the repeater item has been added.
1231
- * @author Will Poulson
1232
- */
1233
- QuestionnaireService.prototype.addRepeaterItem = function () {
1234
- var _this = this;
1235
- return new Promise(function (resolve, reject) {
1236
- if (_this.currentQuestion.data.type !== QuestionType.Repeater) {
1237
- return reject("Current question isn't a repeater");
1238
- }
1239
- var repeaterForm = _this.formConstuctor.generateFormsForRepeater(_this.currentQuestion.data);
1240
- if (!repeaterForm) {
1241
- return reject("Repeater template failed to generate. May be due to the repeater having no items in the editor.");
1242
- }
1243
- if (!(_this.currentQuestion.formControl instanceof FormArray)) {
1244
- return reject("The current questions control isn't of type FormArray");
1245
- }
1246
- _this.currentQuestion.formControl.push(repeaterForm);
1247
- return resolve();
1248
- });
1249
- };
1250
- /**
1251
- * Removes a repeater item on the current question.
1252
- * Current question must be of type Repeater.
1253
- * @param index The index at which to remove.
1254
- * @returns A promise which resolves when the repeater item has been removed.
1255
- * @author Will Poulson
1256
- */
1257
- QuestionnaireService.prototype.removeRepeaterItem = function (index) {
1258
- var _this = this;
1259
- return new Promise(function (resolve, reject) {
1260
- if (_this.currentQuestion.data.type !== QuestionType.Repeater) {
1261
- return reject("Current question isn't a repeater");
1262
- }
1263
- if (!(_this.currentQuestion.formControl instanceof FormArray)) {
1264
- return reject("The current questions control isn't of type FormArray");
1265
- }
1266
- _this.currentQuestion.formControl.removeAt(index);
1267
- return resolve();
1268
- });
1269
- };
1270
- /**
1271
- * Returns the display text for a given repeater item
1272
- * @param index The index at which to get the label for.
1273
- * @returns A string which is the display text.
1274
- * @author Will Poulson
1275
- */
1276
- QuestionnaireService.prototype.getRepeaterItemLabel = function (index) {
1277
- var e_4, _a;
1278
- if (!this.currentQuestion.formControl || !(this.currentQuestion.formControl instanceof FormArray)) {
1279
- return '';
1280
- }
1281
- var repeaterItem = this.currentQuestion.formControl.controls[index];
1282
- if (!repeaterItem) {
1283
- return '';
1284
- }
1285
- var template = this.currentQuestion.data.repeaterDisplayName;
1286
- var splitTemplate = template.split(' ');
1287
- var displayArray = [];
1288
- try {
1289
- for (var splitTemplate_1 = __values(splitTemplate), splitTemplate_1_1 = splitTemplate_1.next(); !splitTemplate_1_1.done; splitTemplate_1_1 = splitTemplate_1.next()) {
1290
- var templateItem = splitTemplate_1_1.value;
1291
- if (templateItem.match(/\[.*?\]/)) {
1292
- var path = templateItem.replace(/[\[\]']+/g, '');
1293
- var item = repeaterItem.get(path);
1294
- var value = (item && item.value ? item.value : '?');
1295
- displayArray.push(value);
1296
- }
1297
- else {
1298
- displayArray.push(templateItem);
1299
- }
1300
- }
1301
- }
1302
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
1303
- finally {
1304
- try {
1305
- if (splitTemplate_1_1 && !splitTemplate_1_1.done && (_a = splitTemplate_1.return)) _a.call(splitTemplate_1);
1306
- }
1307
- finally { if (e_4) throw e_4.error; }
1308
- }
1309
- var display = displayArray.join(' ');
1310
- return display;
1311
- };
1312
- QuestionnaireService.ɵfac = function QuestionnaireService_Factory(t) { return new (t || QuestionnaireService)(ɵɵinject(FormConstructorService), ɵɵinject('options')); };
1313
- QuestionnaireService.ɵprov = ɵɵdefineInjectable({ token: QuestionnaireService, factory: QuestionnaireService.ɵfac, providedIn: 'root' });
1314
- return QuestionnaireService;
1315
- }());
1316
- /*@__PURE__*/ (function () { ɵsetClassMetadata(QuestionnaireService, [{
1317
- type: Injectable,
1318
- args: [{
1319
- providedIn: 'root'
1320
- }]
1321
- }], function () { return [{ type: FormConstructorService }, { type: undefined, decorators: [{
1322
- type: Inject,
1323
- args: ['options']
1324
- }] }]; }, null); })();
1325
-
1326
- var GgQuestionnaireV2Module = /** @class */ (function () {
1327
- function GgQuestionnaireV2Module() {
1328
- }
1329
- GgQuestionnaireV2Module.forRoot = function (options) {
1330
- return {
1331
- ngModule: GgQuestionnaireV2Module,
1332
- providers: [
1333
- QuestionnaireService,
1334
- {
1335
- provide: 'options',
1336
- useValue: options
1337
- }
1338
- ]
1339
- };
1340
- };
1341
- GgQuestionnaireV2Module.ɵmod = ɵɵdefineNgModule({ type: GgQuestionnaireV2Module });
1342
- GgQuestionnaireV2Module.ɵinj = ɵɵdefineInjector({ factory: function GgQuestionnaireV2Module_Factory(t) { return new (t || GgQuestionnaireV2Module)(); }, providers: [
1343
- FormConstructorService,
1344
- FilterService
1345
- ], imports: [[
1346
- FormsModule,
1347
- ReactiveFormsModule
1348
- ]] });
1349
- return GgQuestionnaireV2Module;
1350
- }());
1351
- (function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵɵsetNgModuleScope(GgQuestionnaireV2Module, { imports: [FormsModule,
1352
- ReactiveFormsModule] }); })();
1353
- /*@__PURE__*/ (function () { ɵsetClassMetadata(GgQuestionnaireV2Module, [{
1354
- type: NgModule,
1355
- args: [{
1356
- declarations: [],
1357
- providers: [
1358
- FormConstructorService,
1359
- FilterService
1360
- ],
1361
- imports: [
1362
- FormsModule,
1363
- ReactiveFormsModule
1364
- ],
1365
- }]
1366
- }], null, null); })();
1367
-
1368
- var InputType;
1369
- (function (InputType) {
1370
- InputType["Text"] = "text";
1371
- InputType["Number"] = "number";
1372
- InputType["Email"] = "email";
1373
- InputType["Telephone"] = "tel";
1374
- InputType["Password"] = "password";
1375
- InputType["Color"] = "color";
1376
- InputType["Date"] = "date";
1377
- InputType["DateTimeLocal"] = "datetime-local";
1378
- InputType["Time"] = "time";
1379
- InputType["Month"] = "month";
1380
- InputType["Week"] = "week";
1381
- })(InputType || (InputType = {}));
1382
-
1383
- var RepeaterQuestionType;
1384
- (function (RepeaterQuestionType) {
1385
- RepeaterQuestionType["Input"] = "input";
1386
- RepeaterQuestionType["Select"] = "select";
1387
- RepeaterQuestionType["Upload"] = "upload";
1388
- })(RepeaterQuestionType || (RepeaterQuestionType = {}));
1389
-
1390
- // Services
1391
-
1392
- /**
1393
- * Generated bundle index. Do not edit.
1394
- */
1395
-
1396
- export { ConditionRelationship, ConditionType, GgQuestionnaireV2Module, InputType, QuestionType, QuestionnaireService, RepeaterQuestionType, ValidationType };
1397
- //# sourceMappingURL=glowgreen-gg-questionnaire-v2.js.map