UNPKG

9.29 kBJavaScriptView Raw
1import { Component, Input } from '@angular/core';
2import { FormControl, FormGroup, Validators } from '@angular/forms';
3var DropDownFormSectionComponent = (function () {
4 function DropDownFormSectionComponent() {
5 this.startOpen = false;
6 this.displayAutoTitle = false;
7 this.titleText = '';
8 this.displayAutoDescription = true;
9 this.descriptionText = '';
10 this.editingHint = '';
11 this.expandable = true;
12 this.autoDescriptionText = '';
13 this.iconName = 'help_outline';
14 this.subscriptions = [];
15 this.uniqueIndex = 1;
16 this.autoTitleText = '';
17 }
18 DropDownFormSectionComponent.prototype.ngOnInit = function () {
19 var _this = this;
20 while (this.parentForm.controls.hasOwnProperty(this.titleText + this.uniqueIndex)) {
21 this.uniqueIndex += 1;
22 }
23 this.parentForm.addControl(this.titleText + this.uniqueIndex, this.toFormGroup(this.fields));
24 var form = this.parentForm.controls[this.titleText + this.uniqueIndex];
25 for (var key in form.controls) {
26 if (form.controls.hasOwnProperty(key)) {
27 if (this.prepopulatedData != null && this.prepopulatedData.hasOwnProperty(key)) {
28 form.controls[key].setValue(this.prepopulatedData[key]);
29 }
30 }
31 }
32 this.subscriptions.push(form.statusChanges.subscribe(function (change) {
33 var missingFields = false;
34 for (var control in form.controls) {
35 if (!form.controls[control].value ||
36 (form.controls[control].value.constructor === Array && !form.controls[control].value[0])) {
37 _this.iconName = 'help_outline';
38 missingFields = true;
39 if (change === 'INVALID') {
40 _this.autoTitleText = '* Please fill out all required fields';
41 if (form.controls[control].dirty || form.controls[control].touched) {
42 _this.iconName = 'warning';
43 break;
44 }
45 }
46 else {
47 _this.autoTitleText = 'Optional fields unanswered';
48 }
49 }
50 }
51 if (!missingFields) {
52 _this.iconName = 'check_circle';
53 _this.autoTitleText = 'Complete';
54 }
55 }));
56 if (this.displayAutoDescription) {
57 this.subscriptions.push(form.valueChanges.subscribe(function (changes) {
58 var description = '';
59 for (var key in changes) {
60 if (changes.hasOwnProperty(key) && changes[key] !== null && changes[key].length > 0) {
61 if (changes[key][0].name) {
62 for (var fileKey in changes[key]) {
63 if (changes[key][fileKey] != null) {
64 description += changes[key][fileKey].name;
65 description += ', ';
66 }
67 }
68 }
69 else {
70 description += changes[key];
71 description += ', ';
72 }
73 }
74 }
75 _this.autoDescriptionText = description.substring(0, description.length - 2);
76 }));
77 }
78 form.updateValueAndValidity({ onlySelf: false, emitEvent: true });
79 };
80 DropDownFormSectionComponent.prototype.toFormGroup = function (formFields) {
81 var _this = this;
82 var group = {};
83 formFields.forEach(function (field) {
84 var formControl;
85 if (field.controlType === 'checkbox') {
86 formControl = new FormControl(field.value);
87 }
88 else {
89 formControl = field.required ? new FormControl(field.value, Validators.required) :
90 new FormControl(field.value);
91 }
92 group[field.id] = formControl;
93 _this.subscriptions.push(formControl.valueChanges.subscribe(function (value) { return field.value = value; }));
94 });
95 return new FormGroup(group);
96 };
97 DropDownFormSectionComponent.prototype.ngOnDestroy = function () {
98 this.subscriptions.forEach(function (subscription) { return subscription.unsubscribe(); });
99 };
100 DropDownFormSectionComponent.decorators = [
101 { type: Component, args: [{
102 selector: 'va-dropdown-form-section',
103 template: "\n <mat-card *ngIf=\"!expandable && !(expandable == undefined)\" class=\"not-expandable-card\">\n <mat-icon class=\"not-expandable-icon valid\"> check_circle</mat-icon>\n <mat-card-header *ngIf=\"!displayAutoTitle\" class=\"not-expandable-header\">\n {{ titleText }}\n </mat-card-header>\n <mat-card-header *ngIf=\"displayAutoTitle\" class=\"not-expandable-header valid\">\n <div class=\"title\"> {{ titleText }}</div>\n <div *ngIf=\"titleText && autoTitleText\">&nbsp;</div>\n <ng-container><i> Complete </i></ng-container>\n </mat-card-header>\n <mat-panel-description class=\"not-expandable-description\">\n {{ descriptionText }}\n </mat-panel-description>\n </mat-card>\n <mat-expansion-panel *ngIf=\"expandable || expandable == undefined\" [expanded]=\"startOpen\">\n <mat-expansion-panel-header>\n <div class=\"dropdown-form-header\">\n <mat-icon\n [ngClass]=\"{invalid: iconName=='warning', valid: iconName=='check_circle', question: iconName=='help_outline'}\">\n {{ iconName }}\n </mat-icon>\n <mat-panel-title *ngIf=\"!displayAutoTitle\">\n {{ titleText }}\n </mat-panel-title>\n <mat-panel-title *ngIf=\"displayAutoTitle\"\n [ngClass]=\"{valid: iconName=='check_circle', invalid: iconName=='warning'}\">\n <div class=\"title\"> {{ titleText }}</div>\n <div *ngIf=\"titleText && autoTitleText\"> &nbsp;</div>\n <ng-container><i> {{autoTitleText}} </i></ng-container>\n </mat-panel-title>\n <mat-panel-description *ngIf=\"displayAutoDescription\" [ngClass]=\"{invalid: iconName=='warning'}\">\n {{ autoDescriptionText }}\n </mat-panel-description>\n <mat-panel-description *ngIf=\"!displayAutoDescription\" [ngClass]=\"{invalid: iconName=='warning'}\">\n <i> {{descriptionText}} </i>\n </mat-panel-description>\n </div>\n </mat-expansion-panel-header>\n <div class=\"expansion-panel-body\">\n <va-field *ngFor=\"let field of fields\" [field]=\"field\"\n [form]=\"parentForm.controls[titleText + uniqueIndex]\"></va-field>\n <p *ngIf=\"editingHint != ''\" class=\"editing-hint\"><i>{{ editingHint }}</i></p>\n </div>\n </mat-expansion-panel>\n ",
104 styles: [":host-context(va-dropdown-form-section) { font-size: 14px; } :host-context(va-dropdown-form-section) .expansion-panel-body { margin-top: -10px; display: block; width: 60%; } .mat-expanded { transition: margin 400ms; } .mat-expansion-panel { transition: margin 400ms; } .dropdown-form-header { width: 100%; display: flex; } mat-panel-description { align-self: center; display: flex; flex: inherit; } mat-panel-description.invalid { color: #c62828; } mat-icon { margin-right: 7px; align-self: center; } mat-icon.valid { color: #4caf50; } mat-icon.invalid { color: #c62828; } mat-icon.question { color: #9e9e9e; } mat-panel-description { display: initial; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; } mat-panel-title { align-self: center; flex: none; margin-right: 20px; } mat-panel-title.valid { color: #4caf50; } mat-panel-title.invalid { color: #c62828; } .editing-hint { color: #9e9e9e; } .title { color: #212121; } .not-expandable-card { background: white; cursor: default; font-size: 15px; display: flex; height: 48px; align-items: center; box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12); } .not-expandable-card .not-expandable-icon { margin-left: 24px; } .not-expandable-card .not-expandable-header { display: flex; } .not-expandable-card .not-expandable-description { margin-left: 22px; } .not-expandable-card .valid { color: #4caf50; } "]
105 },] },
106 ];
107 /** @nocollapse */
108 DropDownFormSectionComponent.ctorParameters = function () { return []; };
109 DropDownFormSectionComponent.propDecorators = {
110 'prepopulatedData': [{ type: Input },],
111 'startOpen': [{ type: Input },],
112 'parentForm': [{ type: Input },],
113 'displayAutoTitle': [{ type: Input },],
114 'titleText': [{ type: Input },],
115 'displayAutoDescription': [{ type: Input },],
116 'descriptionText': [{ type: Input },],
117 'fields': [{ type: Input },],
118 'editingHint': [{ type: Input },],
119 'expandable': [{ type: Input },],
120 };
121 return DropDownFormSectionComponent;
122}());
123export { DropDownFormSectionComponent };