1
|
import {Component, OnDestroy, OnInit, ViewChild} from "@angular/core";
|
2
|
import {StakeholderService} from "../openaireLibrary/monitor/services/stakeholder.service";
|
3
|
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
|
4
|
import {Stakeholder, Visibility} from "../openaireLibrary/monitor/entities/stakeholder";
|
5
|
import {Subscriber, zip} from "rxjs";
|
6
|
import {StakeholderUtils} from "../utils/indicator-utils";
|
7
|
import {FormBuilder, FormGroup} from "@angular/forms";
|
8
|
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
|
9
|
import {Option} from "../openaireLibrary/sharedComponents/input/input.component";
|
10
|
import {Title} from "@angular/platform-browser";
|
11
|
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
|
12
|
import {Session} from "../openaireLibrary/login/utils/helper.class";
|
13
|
import {EditStakeholderComponent} from "../general/edit-stakeholder/edit-stakeholder.component";
|
14
|
import {properties} from "../../environments/environment";
|
15
|
import {ActivatedRoute} from "@angular/router";
|
16
|
|
17
|
type Tab = 'all' | 'templates'| 'profiles';
|
18
|
|
19
|
declare var UIkit;
|
20
|
|
21
|
@Component({
|
22
|
selector: 'home',
|
23
|
templateUrl: "./manageStakeholders.component.html",
|
24
|
styleUrls: ['manageStakeholders.component.css']
|
25
|
})
|
26
|
export class ManageStakeholdersComponent implements OnInit, OnDestroy {
|
27
|
|
28
|
public properties: EnvProperties;
|
29
|
public loading: boolean = true;
|
30
|
public stakeholderUtils: StakeholderUtils = new StakeholderUtils();
|
31
|
public defaultStakeholders: Stakeholder[];
|
32
|
public stakeholders: Stakeholder[];
|
33
|
public alias: string[];
|
34
|
public stakeholder: Stakeholder;
|
35
|
public index: number;
|
36
|
public user = null;
|
37
|
/**
|
38
|
* Filtered Stakeholders
|
39
|
*/
|
40
|
public displayDefaultStakeholders: Stakeholder[];
|
41
|
public displayStakeholders: Stakeholder[];
|
42
|
/**
|
43
|
* Top filters
|
44
|
*/
|
45
|
public filters: FormGroup;
|
46
|
public all: Option = {
|
47
|
value: 'all',
|
48
|
label: 'All'
|
49
|
};
|
50
|
|
51
|
public callback: Function;
|
52
|
|
53
|
/**
|
54
|
* Grid or List View
|
55
|
*/
|
56
|
private subscriptions: any[] = [];
|
57
|
@ViewChild('editStakeholderModal', { static: true }) editStakeholderModal: AlertModal;
|
58
|
@ViewChild('deleteStakeholderModal', { static: true }) deleteStakeholderModal: AlertModal;
|
59
|
@ViewChild('editStakeholderComponent', { static: true }) editStakeholderComponent: EditStakeholderComponent;
|
60
|
|
61
|
tab: Tab = 'all';
|
62
|
|
63
|
constructor(private stakeholderService: StakeholderService,
|
64
|
private userManagementService: UserManagementService,
|
65
|
private route: ActivatedRoute,
|
66
|
private title: Title,
|
67
|
private fb: FormBuilder) {
|
68
|
}
|
69
|
|
70
|
ngOnInit(): void {
|
71
|
this.subscriptions.push(this.route.fragment.subscribe((fragment: Tab) => {
|
72
|
if (this.isTab(fragment)) {
|
73
|
this.tab = fragment;
|
74
|
} else {
|
75
|
this.tab = 'all';
|
76
|
}
|
77
|
}));
|
78
|
this.buildFilters();
|
79
|
this.properties = properties;
|
80
|
this.title.setTitle('Manage profiles');
|
81
|
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
|
82
|
this.user = user;
|
83
|
}));
|
84
|
let data = zip(
|
85
|
this.stakeholderService.getDefaultStakeholders(this.properties.monitorServiceAPIURL),
|
86
|
this.stakeholderService.getMyStakeholders(this.properties.monitorServiceAPIURL),
|
87
|
this.stakeholderService.getAlias(this.properties.monitorServiceAPIURL)
|
88
|
);
|
89
|
this.subscriptions.push(data.subscribe(res => {
|
90
|
this.defaultStakeholders = res[0];
|
91
|
this.stakeholders = res[1];
|
92
|
this.displayDefaultStakeholders = res[0];
|
93
|
this.displayStakeholders = res[1];
|
94
|
this.alias = res[2];
|
95
|
this.loading = false;
|
96
|
}));
|
97
|
|
98
|
this.subscriptions.push(UIkit.util.on(document, 'hidden', '#edit_modal', (): void => {
|
99
|
this.editStakeholderComponent.removePhoto();
|
100
|
}));
|
101
|
}
|
102
|
|
103
|
ngOnDestroy(): void {
|
104
|
this.subscriptions.forEach(value => {
|
105
|
if (value instanceof Subscriber) {
|
106
|
value.unsubscribe();
|
107
|
} else if (value instanceof Function) {
|
108
|
value();
|
109
|
}
|
110
|
});
|
111
|
}
|
112
|
|
113
|
hide(element: any) {
|
114
|
UIkit.dropdown(element).hide();
|
115
|
}
|
116
|
|
117
|
|
118
|
private buildFilters() {
|
119
|
this.filters = this.fb.group({
|
120
|
status: this.fb.control('all'),
|
121
|
keyword: this.fb.control('')
|
122
|
});
|
123
|
this.subscriptions.push(this.filters.get('status').valueChanges.subscribe(value => {
|
124
|
this.onStatusChange(value);
|
125
|
}));
|
126
|
this.subscriptions.push(this.filters.get('keyword').valueChanges.subscribe(value => {
|
127
|
this.onKeywordChange(value);
|
128
|
}));
|
129
|
}
|
130
|
|
131
|
onStatusChange(value) {
|
132
|
this.displayDefaultStakeholders = this.filterStatus(this.defaultStakeholders, value);
|
133
|
this.displayStakeholders = this.filterStatus(this.stakeholders, value);
|
134
|
}
|
135
|
|
136
|
onKeywordChange(value) {
|
137
|
this.displayDefaultStakeholders = this.filterByKeyword(this.defaultStakeholders, value);
|
138
|
this.displayStakeholders = this.filterByKeyword(this.stakeholders, value);
|
139
|
}
|
140
|
|
141
|
private filterStatus(stakeholders: Stakeholder[], value): Stakeholder[] {
|
142
|
if (value === 'all') {
|
143
|
return stakeholders;
|
144
|
} else {
|
145
|
return stakeholders.filter(stakeholder => stakeholder.visibility == value);
|
146
|
}
|
147
|
}
|
148
|
|
149
|
private filterByKeyword(stakeholders: Stakeholder[], value): Stakeholder[] {
|
150
|
if (value === null || value === '') {
|
151
|
return stakeholders;
|
152
|
} else {
|
153
|
return stakeholders.filter(stakeholder =>
|
154
|
stakeholder.name && stakeholder.name.toLowerCase().includes(value.toLowerCase()) ||
|
155
|
stakeholder.type && stakeholder.type.toLowerCase().includes(value.toLowerCase()) ||
|
156
|
stakeholder.index_id && stakeholder.index_id.toLowerCase().includes(value.toLowerCase()) ||
|
157
|
stakeholder.index_shortName && stakeholder.index_shortName.toLowerCase().includes(value.toLowerCase()) ||
|
158
|
stakeholder.index_name && stakeholder.index_name.toLowerCase().includes(value.toLowerCase())
|
159
|
);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
public editStakeholder(stakeholder: Stakeholder = null, isDefault: boolean = false) {
|
164
|
if (isDefault) {
|
165
|
this.index = (stakeholder) ? this.defaultStakeholders.findIndex(value => value._id === stakeholder._id) : -1;
|
166
|
} else {
|
167
|
this.index = (stakeholder) ? this.stakeholders.findIndex(value => value._id === stakeholder._id) : -1;
|
168
|
}
|
169
|
if (!stakeholder) {
|
170
|
this.stakeholder = new Stakeholder(null, null, null,
|
171
|
null, null, null, null, null);
|
172
|
} else {
|
173
|
this.stakeholder = stakeholder;
|
174
|
}
|
175
|
this.editStakeholderComponent.init(this.stakeholder, this.alias, this.defaultStakeholders, isDefault, this.index === -1);
|
176
|
if (this.index !== -1) {
|
177
|
this.callback = (stakeholder: Stakeholder) => {
|
178
|
if (stakeholder.defaultId == null) {
|
179
|
this.defaultStakeholders[this.index] = stakeholder;
|
180
|
} else {
|
181
|
this.stakeholders[this.index] = stakeholder;
|
182
|
}
|
183
|
};
|
184
|
this.editStakeholderModal.okButtonText = 'Save Changes';
|
185
|
} else {
|
186
|
this.callback = (stakeholder: Stakeholder) => {
|
187
|
if (stakeholder.defaultId === null) {
|
188
|
this.defaultStakeholders.push(stakeholder);
|
189
|
} else {
|
190
|
this.stakeholders.push(stakeholder);
|
191
|
}
|
192
|
};
|
193
|
this.editStakeholderModal.okButtonText = 'Create';
|
194
|
}
|
195
|
this.editStakeholderModal.cancelButtonText = 'Cancel';
|
196
|
this.editStakeholderModal.okButtonLeft = false;
|
197
|
this.editStakeholderModal.alertMessage = false;
|
198
|
this.editStakeholderModal.open();
|
199
|
}
|
200
|
|
201
|
public deleteStakeholderOpen(stakeholder: Stakeholder) {
|
202
|
this.stakeholder = stakeholder;
|
203
|
this.deleteStakeholderModal.alertTitle = 'Delete ' + this.stakeholder.index_name;
|
204
|
this.deleteStakeholderModal.cancelButtonText = 'No';
|
205
|
this.deleteStakeholderModal.okButtonText = 'Yes';
|
206
|
this.deleteStakeholderModal.message = 'This stakeholder will permanently be deleted. Are you sure you want to proceed?';
|
207
|
this.deleteStakeholderModal.open();
|
208
|
}
|
209
|
|
210
|
public deleteStakeholder() {
|
211
|
if (!this.stakeholder.defaultId) {
|
212
|
this.index = (this.stakeholder) ? this.defaultStakeholders.findIndex(value => value._id === this.stakeholder._id) : -1;
|
213
|
} else {
|
214
|
this.index = (this.stakeholder) ? this.stakeholders.findIndex(value => value._id === this.stakeholder._id) : -1;
|
215
|
}
|
216
|
this.subscriptions.push(this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, [this.stakeholder._id]).subscribe(() => {
|
217
|
UIkit.notification(this.stakeholder.name+ ' has been <b>successfully deleted</b>', {
|
218
|
status: 'success',
|
219
|
timeout: 6000,
|
220
|
pos: 'bottom-right'
|
221
|
});
|
222
|
if (!this.stakeholder.defaultId) {
|
223
|
this.defaultStakeholders.splice(this.index, 1);
|
224
|
} else {
|
225
|
this.stakeholders.splice(this.index, 1);
|
226
|
}
|
227
|
}, error => {
|
228
|
UIkit.notification('An error has occurred. Please try again later', {
|
229
|
status: 'danger',
|
230
|
timeout: 6000,
|
231
|
pos: 'bottom-right'
|
232
|
});
|
233
|
}));
|
234
|
}
|
235
|
|
236
|
changeStakeholderStatus(stakeholder: Stakeholder, visibility: Visibility) {
|
237
|
let path = [
|
238
|
stakeholder._id
|
239
|
];
|
240
|
this.subscriptions.push(this.stakeholderService.changeVisibility(this.properties.monitorServiceAPIURL, path, visibility).subscribe(visibility => {
|
241
|
stakeholder.visibility = visibility;
|
242
|
UIkit.notification(stakeholder.name+ '\'s status has been <b>successfully changed</b> to ' + stakeholder.visibility.toLowerCase(), {
|
243
|
status: 'success',
|
244
|
timeout: 6000,
|
245
|
pos: 'bottom-right'
|
246
|
});
|
247
|
}, error => {
|
248
|
UIkit.notification('An error has occurred. Please try again later', {
|
249
|
status: 'danger',
|
250
|
timeout: 6000,
|
251
|
pos: 'bottom-right'
|
252
|
});
|
253
|
}));
|
254
|
}
|
255
|
|
256
|
public isManager(): boolean {
|
257
|
return this.isCurator() || (Session.isKindOfMonitorManager(this.user));
|
258
|
}
|
259
|
|
260
|
public isProfileManager(stakeholder: Stakeholder): boolean {
|
261
|
return this.isCurator() || (Session.isManager(stakeholder.type, stakeholder.alias, this.user));
|
262
|
}
|
263
|
|
264
|
public isCurator(): boolean {
|
265
|
return this.isAdmin() || Session.isMonitorCurator(this.user);
|
266
|
}
|
267
|
|
268
|
public isAdmin(): boolean {
|
269
|
return Session.isPortalAdministrator(this.user);
|
270
|
}
|
271
|
|
272
|
private isTab(tab: Tab): boolean {
|
273
|
switch (tab) {
|
274
|
case "all":
|
275
|
return true;
|
276
|
case "profiles":
|
277
|
return true;
|
278
|
case "templates":
|
279
|
return true;
|
280
|
default:
|
281
|
return false;
|
282
|
}
|
283
|
}
|
284
|
}
|