forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess-control-array-form.component.ts
More file actions
170 lines (139 loc) · 4.3 KB
/
access-control-array-form.component.ts
File metadata and controls
170 lines (139 loc) · 4.3 KB
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
import {
Component,
Input,
OnInit,
ViewChild,
} from '@angular/core';
import {
FormsModule,
NgForm,
} from '@angular/forms';
import { NgbDatepickerModule } from '@ng-bootstrap/ng-bootstrap';
import { TranslateModule } from '@ngx-translate/core';
import { AccessesConditionOption } from '../../../core/config/models/config-accesses-conditions-options.model';
import { BtnDisabledDirective } from '../../btn-disabled.directive';
import { dateToISOFormat } from '../../date.util';
import { ToDatePipe } from './to-date.pipe';
@Component({
selector: 'ds-access-control-array-form',
templateUrl: './access-control-array-form.component.html',
styleUrls: ['./access-control-array-form.component.scss'],
exportAs: 'accessControlArrayForm',
standalone: true,
imports: [FormsModule, NgbDatepickerModule, TranslateModule, ToDatePipe, BtnDisabledDirective],
})
export class AccessControlArrayFormComponent implements OnInit {
@Input() dropdownOptions: AccessesConditionOption[] = [];
@Input() mode!: 'add' | 'replace';
@Input() type!: 'item' | 'bitstream';
@ViewChild('ngForm', { static: true }) ngForm!: NgForm;
form: { accessControls: AccessControlItem[] } = {
accessControls: [emptyAccessControlItem()], // Start with one empty access control item
};
formDisabled = true;
ngOnInit(): void {
this.disable(); // Disable the form by default
}
get allControlsAreEmpty() {
return this.form.accessControls
.every(x => x.itemName === null || x.itemName === '');
}
get showWarning() {
return this.mode === 'replace' && this.allControlsAreEmpty && !this.formDisabled;
}
/**
* Add a new access control item to the form.
* Start and end date are disabled by default.
* @param itemName The name of the item to add
*/
addAccessControlItem(itemName: string = null) {
this.form.accessControls = [
...this.form.accessControls,
{ ...emptyAccessControlItem(), itemName },
];
}
/**
* Remove an access control item from the form.
* @param ngModelGroup
* @param index
*/
removeAccessControlItem(id: number) {
this.form.accessControls = this.form.accessControls.filter(item => item.id !== id);
}
/**
* Get the value of the form.
* This will be used to read the form value from the parent component.
* @return The form value
*/
getValue() {
return this.form.accessControls
.filter(x => x.itemName !== null && x.itemName !== '')
.map(x => ({
name: x.itemName,
startDate: (x.startDate ? dateToISOFormat(x.startDate) : null),
endDate: (x.endDate ? dateToISOFormat(x.endDate) : null),
}));
}
/**
* Set the value of the form from the parent component.
*/
reset() {
this.form.accessControls = [];
// Add an empty access control item by default
this.addAccessControlItem();
this.disable();
}
/**
* Disable the form.
* This will be used to disable the form from the parent component.
*/
disable = () => {
this.ngForm.form.disable();
this.formDisabled = true;
};
/**
* Enable the form.
* This will be used to enable the form from the parent component.
*/
enable = () => {
this.ngForm.form.enable();
this.formDisabled = false;
};
accessControlChanged(control: AccessControlItem, selectedItem: string) {
const item = this.dropdownOptions
.find((x) => x.name === selectedItem);
control.startDate = null;
control.endDate = null;
control.hasStartDate = item?.hasStartDate || false;
control.hasEndDate = item?.hasEndDate || false;
control.maxStartDate = item?.maxStartDate || null;
control.maxEndDate = item?.maxEndDate || null;
}
trackById(index: number, item: AccessControlItem) {
return item.id;
}
isValid() {
return this.ngForm.valid;
}
}
export interface AccessControlItem {
id: number; // will be used only locally
itemName: string | null;
hasStartDate?: boolean;
startDate: string | null;
maxStartDate?: string | null;
hasEndDate?: boolean;
endDate: string | null;
maxEndDate?: string | null;
}
const emptyAccessControlItem = (): AccessControlItem => ({
id: randomID(),
itemName: null,
startDate: null,
hasStartDate: false,
maxStartDate: null,
endDate: null,
hasEndDate: false,
maxEndDate: null,
});
const randomID = () => Math.floor(Math.random() * 1000000);