-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathedit-dialog.component.ts
198 lines (172 loc) · 5.31 KB
/
edit-dialog.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { Component, Inject, OnInit } from "@angular/core";
import { MatChipInputEvent } from "@angular/material/chips";
import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material/dialog";
import { FormControl, FormGroup } from "@angular/forms";
import { COMMA, ENTER } from "@angular/cdk/keycodes";
import { Policy } from "shared/sdk";
@Component({
selector: "edit-dialog",
templateUrl: "./edit-dialog.component.html",
styleUrls: ["./edit-dialog.component.scss"],
})
export class EditDialogComponent implements /*OnChanges,*/ OnInit {
data: any;
multiEdit: boolean;
selectedGroups: string[] = [];
public separatorKeysCodes: number[] = [ENTER, COMMA];
form: FormGroup;
selectable = true;
constructor(
private dialogRef: MatDialogRef<EditDialogComponent>,
@Inject(MAT_DIALOG_DATA)
data: { multiSelect: boolean; selectedPolicy: Policy },
) {
this.multiEdit = data.multiSelect;
// clone input, do not mutate
this.data = JSON.parse(JSON.stringify(data));
// should make selected policy null is multi select, need to preserve group?
if (this.multiEdit) {
this.data.selectedPolicy.archiveEmailsToBeNotified = [];
this.data.selectedPolicy.retrieveEmailsToBeNotified = [];
}
this.form = new FormGroup({
manager: new FormControl({
value: null,
disabled: true,
}),
autoArchive: new FormControl({
value: this.getPreFill(data.selectedPolicy.autoArchive, this.multiEdit),
disabled: true,
/*, Validators.required*/
}),
tapeRedundancy: new FormControl({
value: this.getPreFill(
data.selectedPolicy.tapeRedundancy,
this.multiEdit,
),
disabled: true,
}),
autoArchiveDelay: new FormControl({
value: this.getPreFill(
data.selectedPolicy.autoArchiveDelay,
this.multiEdit,
),
disabled: true,
}),
archiveEmailNotification: new FormControl({
value: this.getPreFill(
data.selectedPolicy.archiveEmailNotification,
this.multiEdit,
),
disabled: true,
}),
archiveEmailsToBeNotified: new FormControl({
value: null,
disabled: true,
}),
retrieveEmailNotification: new FormControl({
value: this.getPreFill(
data.selectedPolicy.retrieveEmailNotification,
this.multiEdit,
),
disabled: true,
}),
retrieveEmailsToBeNotified: new FormControl({
value: null, // value is not useable in chiplists
disabled: true,
}),
});
}
getPreFill(field: any, multi: boolean): any {
return field != null && !multi ? field.toString() : null;
}
getPreFillArray(field: any, multi: boolean): any {
return field != null && !multi ? field : null;
}
addRetrieveEmails(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
if ((value || "").trim()) {
this.data.selectedPolicy.retrieveEmailsToBeNotified.push(value.trim());
}
// Reset the input value
if (input) {
input.value = "";
}
}
addArchiveEmails(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
if ((value || "").trim()) {
this.data.selectedPolicy.archiveEmailsToBeNotified.push(value.trim());
}
// Reset the input value
if (input) {
input.value = "";
}
}
addManager(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
if ((value || "").trim()) {
this.data.selectedPolicy.manager.push(value.trim());
}
// Reset the input value
if (input) {
input.value = "";
}
}
removeRetrieveEmail(chip: any): void {
const index =
this.data.selectedPolicy.retrieveEmailsToBeNotified.indexOf(chip);
if (index >= 0) {
this.data.selectedPolicy.retrieveEmailsToBeNotified.splice(index, 1);
}
}
removeArchiveEmail(chip: any): void {
const index =
this.data.selectedPolicy.archiveEmailsToBeNotified.indexOf(chip);
if (index >= 0) {
this.data.selectedPolicy.archiveEmailsToBeNotified.splice(index, 1);
}
}
removeManager(chip: any): void {
const index = this.data.selectedPolicy.manager.indexOf(chip);
if (index >= 0) {
this.data.selectedPolicy.manager.splice(index, 1);
}
}
ngOnInit() {
this.selectedGroups = this.data.selectedGroups;
}
controlClick(control: any) {
this.form.controls[control.ngControl.name].enable();
}
save() {
// dont patch results that are NULL
// handle chip lists
if (this.form.controls.archiveEmailsToBeNotified.enabled) {
this.form.controls.archiveEmailsToBeNotified.setValue(
this.data.selectedPolicy.archiveEmailsToBeNotified,
);
}
if (this.form.controls.retrieveEmailsToBeNotified.enabled) {
this.form.controls.retrieveEmailsToBeNotified.setValue(
this.data.selectedPolicy.retrieveEmailsToBeNotified,
);
}
if (this.form.controls.manager.enabled) {
this.form.controls.manager.setValue(this.data.selectedPolicy.manager);
}
const result = this.form.value;
for (const key in result) {
if (result[key] === null) {
delete result[key];
}
}
this.dialogRef.close(result);
}
close() {
this.dialogRef.close();
}
}