-
Notifications
You must be signed in to change notification settings - Fork 74
/
expression-model-list.component.ts
140 lines (111 loc) · 4.67 KB
/
expression-model-list.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {Component, forwardRef, Input, ViewEncapsulation} from "@angular/core";
import {ControlValueAccessor, FormControl, FormGroup, NG_VALUE_ACCESSOR, Validators} from "@angular/forms";
import {SBDraft2ExpressionModel} from "cwlts/models/d2sb";
import {GuidService} from "../../../services/guid.service";
import {CustomValidators} from "../../../validators/custom.validator";
import {noop} from "../../../lib/utils.lib";
import {DirectiveBase} from "../../../util/directive-base/directive-base";
import {ModalService} from "../../../ui/modal-old/modal.service";
@Component({
encapsulation: ViewEncapsulation.None,
selector: "expression-model-list",
providers: [
{provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => ExpressionModelListComponent), multi: true}
],
template: `
<form *ngIf="form" [formGroup]="form">
<ol *ngIf="formList.length > 0" class="list-unstyled">
<li *ngFor="let item of formList"
class="removable-form-control">
<ct-expression-input
[context]="context"
[formControl]="form.controls[item.id]"
[readonly]="readonly">
</ct-expression-input>
<div *ngIf="!readonly" class="remove-icon clickable" (click)="removeExpressionModel(item)">
<i [ct-tooltip]="'Delete'" class="fa fa-trash text-hover-danger"></i>
</div>
</li>
</ol>
<ct-blank-tool-state *ngIf="!formList.length"
[title]="emptyListText"
[buttonText]="addButtonText"
[readonly]="readonly"
(buttonClick)="addExpressionModel()">
</ct-blank-tool-state>
<button type="button" *ngIf="formList.length && !readonly" class="btn btn-link add-btn-link no-underline-hover"
(click)="addExpressionModel()">
<i class="fa fa-plus"></i> {{addButtonText}}
</button>
</form>
`
})
export class ExpressionModelListComponent extends DirectiveBase implements ControlValueAccessor {
@Input()
public readonly = false;
@Input()
public addButtonText: string = "";
@Input()
public emptyListText: string = "";
/** Context in which expression should be evaluated */
@Input()
public context: { $job: any } = {$job: {}};
/** List which connects model to forms */
private formList: Array<{ id: string, model: SBDraft2ExpressionModel }> = [];
private onTouched = noop;
private propagateChange = noop;
private input: SBDraft2ExpressionModel[];
private form: FormGroup = new FormGroup({});
constructor(private guidService: GuidService, private modal: ModalService) {
super();
}
writeValue(input: SBDraft2ExpressionModel[]): void {
this.input = input;
this.formList = this.input.map(model => {
return {
id: this.guidService.generate(), model
};
});
this.formList.forEach((item) => {
this.form.addControl(
item.id,
new FormControl(item.model, [Validators.required])
);
});
this.tracked = this.form.valueChanges.subscribe(change => {
const values = Object.keys(change).map(key => change[key]);
this.propagateChange(values);
})
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
private removeExpressionModel(ctrl: { id: string, model: SBDraft2ExpressionModel }): void {
this.modal.confirm({
title: "Really Remove?",
content: `Are you sure that you want to remove this secondary file?`,
cancellationLabel: "No, keep it",
confirmationLabel: "Yes, remove it"
}).then(() => {
this.formList = this.formList.filter(item => item.id !== ctrl.id);
this.form.removeControl(ctrl.id);
this.form.markAsDirty();
}, noop);
}
private addExpressionModel(): void {
const newCmd = {
id: this.guidService.generate(),
model: new SBDraft2ExpressionModel("")
};
this.form.addControl(newCmd.id, new FormControl(newCmd.model, [Validators.required, CustomValidators.cwlModel]));
this.formList.push(newCmd);
this.form.markAsTouched();
}
ngOnDestroy() {
super.ngOnDestroy();
this.formList.forEach(item => this.form.removeControl(item.id));
}
}