1
|
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
2
|
import {ActivatedRoute, Router} from '@angular/router';
|
3
|
import {Title} from '@angular/platform-browser';
|
4
|
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
|
5
|
|
6
|
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
|
7
|
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
|
8
|
import {Stakeholder, Topic} from "../utils/entities/stakeholder";
|
9
|
import {StakeholderService} from "../services/stakeholder.service";
|
10
|
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
|
11
|
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
|
12
|
import {Subscriber} from "rxjs";
|
13
|
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
|
14
|
import {LayoutService} from "../openaireLibrary/dashboard/sharedComponents/sidebar/layout.service";
|
15
|
import {StakeholderUtils} from "../utils/indicator-utils";
|
16
|
import {IDeactivateComponent} from "../openaireLibrary/utils/can-exit.guard";
|
17
|
|
18
|
declare var UIkit;
|
19
|
|
20
|
@Component({
|
21
|
selector: 'stakeholder',
|
22
|
templateUrl: './stakeholder.component.html',
|
23
|
})
|
24
|
export class StakeholderComponent implements OnInit, OnDestroy, IDeactivateComponent {
|
25
|
public subscriptions: any[] = [];
|
26
|
public loading: boolean = true;
|
27
|
public errorCodes: ErrorCodes;
|
28
|
public stakeholder: Stakeholder;
|
29
|
public analysisOpen: boolean = true;
|
30
|
private errorMessages: ErrorMessagesComponent;
|
31
|
public form: FormGroup;
|
32
|
public stakeholderUtils: StakeholderUtils = new StakeholderUtils();
|
33
|
public index: number = -1;
|
34
|
properties: EnvProperties;
|
35
|
|
36
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
37
|
@ViewChild('editModal') editModal: AlertModal;
|
38
|
|
39
|
constructor(
|
40
|
private route: ActivatedRoute,
|
41
|
private router: Router,
|
42
|
private title: Title,
|
43
|
private layoutService: LayoutService,
|
44
|
private fb: FormBuilder,
|
45
|
private stakeholderService: StakeholderService) {
|
46
|
this.errorCodes = new ErrorCodes();
|
47
|
this.errorMessages = new ErrorMessagesComponent();
|
48
|
}
|
49
|
|
50
|
public ngOnInit() {
|
51
|
this.route.data
|
52
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
53
|
this.properties = data.envSpecific;
|
54
|
this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
|
55
|
if (stakeholder) {
|
56
|
this.stakeholder = HelperFunctions.copy(stakeholder);
|
57
|
this.form = null;
|
58
|
this.title.setTitle(stakeholder.index_name);
|
59
|
}
|
60
|
},
|
61
|
err => {
|
62
|
console.error("Error getting stakeholder: ", err);
|
63
|
if(err.status == 404) {
|
64
|
//{queryParams: {"page": this.location.path(true), "page_type": "dataprovider"}}
|
65
|
this.router.navigate(['/error'], );
|
66
|
}
|
67
|
|
68
|
}));
|
69
|
});
|
70
|
}
|
71
|
|
72
|
public ngOnDestroy() {
|
73
|
}
|
74
|
|
75
|
canExit():boolean {
|
76
|
this.subscriptions.forEach(value => {
|
77
|
if (value instanceof Subscriber) {
|
78
|
value.unsubscribe();
|
79
|
}
|
80
|
});
|
81
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
82
|
return true;
|
83
|
}
|
84
|
|
85
|
hide(element: any) {
|
86
|
UIkit.dropdown(element).hide();
|
87
|
}
|
88
|
|
89
|
get open(): boolean {
|
90
|
return this.layoutService.open;
|
91
|
}
|
92
|
|
93
|
private buildTopic(topic: Topic) {
|
94
|
let topics = this.stakeholder.topics.filter(element => element._id !== topic._id);
|
95
|
this.form = this.fb.group({
|
96
|
_id: this.fb.control(topic._id),
|
97
|
name: this.fb.control(topic.name, Validators.required),
|
98
|
description: this.fb.control(topic.description),
|
99
|
alias: this.fb.control(topic.alias, [
|
100
|
Validators.required,
|
101
|
this.stakeholderUtils.aliasValidator(topics)
|
102
|
]
|
103
|
),
|
104
|
isActive: this.fb.control(topic.isActive),
|
105
|
isPublic: this.fb.control(topic.isPublic),
|
106
|
defaultId: this.fb.control(topic.defaultId),
|
107
|
categories: this.fb.control(topic.categories)
|
108
|
});
|
109
|
this.subscriptions.push(this.form.get('name').valueChanges.subscribe(value => {
|
110
|
let i = 1;
|
111
|
value = this.stakeholderUtils.generateAlias(value);
|
112
|
this.form.controls['alias'].setValue(value);
|
113
|
while (this.form.get('alias').invalid) {
|
114
|
this.form.controls['alias'].setValue(value + i);
|
115
|
i++;
|
116
|
}
|
117
|
}));
|
118
|
}
|
119
|
|
120
|
public editTopicOpen(index = -1) {
|
121
|
this.index = index;
|
122
|
if (index === -1) {
|
123
|
this.buildTopic(new Topic(null, null, null, true, true));
|
124
|
} else {
|
125
|
this.buildTopic(this.stakeholder.topics[index]);
|
126
|
}
|
127
|
this.editModal.cancelButtonText = 'Cancel';
|
128
|
this.editModal.okButtonLeft = false;
|
129
|
this.editModal.alertMessage = false;
|
130
|
if (index === -1) {
|
131
|
this.editModal.alertTitle = 'Create a new topic';
|
132
|
this.editModal.okButtonText = 'Create';
|
133
|
} else {
|
134
|
this.editModal.alertTitle = 'Edit topic\'s information ';
|
135
|
this.editModal.okButtonText = 'Save';
|
136
|
}
|
137
|
this.editModal.open();
|
138
|
}
|
139
|
|
140
|
public saveTopic(index = -1) {
|
141
|
if (!this.form.invalid) {
|
142
|
if (index === -1) {
|
143
|
this.save('Topic has been successfully created');
|
144
|
} else {
|
145
|
this.save('Topic has been successfully saved', index);
|
146
|
}
|
147
|
}
|
148
|
}
|
149
|
|
150
|
public deleteTopicOpen(index: number) {
|
151
|
this.index = index;
|
152
|
this.deleteModal.alertTitle = 'Delete topic';
|
153
|
this.deleteModal.cancelButtonText = 'No';
|
154
|
this.deleteModal.okButtonText = 'Yes';
|
155
|
this.deleteModal.open();
|
156
|
}
|
157
|
|
158
|
private save(message: string, index: number = -1) {
|
159
|
let path = [this.stakeholder._id];
|
160
|
this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.form.value, path).subscribe(topic => {
|
161
|
if (index === -1) {
|
162
|
this.stakeholder.topics.push(topic);
|
163
|
} else {
|
164
|
this.stakeholder.topics[index] = topic;
|
165
|
}
|
166
|
UIkit.notification(message, {
|
167
|
status: 'success',
|
168
|
timeout: 3000,
|
169
|
pos: 'top-left'
|
170
|
});
|
171
|
}, error => {
|
172
|
UIkit.notification(error.error.message, {
|
173
|
status: 'danger',
|
174
|
timeout: 3000,
|
175
|
pos: 'top-left'
|
176
|
});
|
177
|
});
|
178
|
}
|
179
|
|
180
|
deleteTopic() {
|
181
|
let path = [
|
182
|
this.stakeholder._id,
|
183
|
this.stakeholder.topics[this.index]._id
|
184
|
];
|
185
|
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => {
|
186
|
this.stakeholder.topics.splice(this.index, 1);
|
187
|
this.index = -1;
|
188
|
UIkit.notification('Topic has been successfully deleted', {
|
189
|
status: 'success',
|
190
|
timeout: 3000,
|
191
|
pos: 'top-left'
|
192
|
});
|
193
|
}, error => {
|
194
|
UIkit.notification(error.error.message, {
|
195
|
status: 'danger',
|
196
|
timeout: 3000,
|
197
|
pos: 'top-left'
|
198
|
});
|
199
|
});
|
200
|
}
|
201
|
|
202
|
toggleTopicStatus(topic: Topic) {
|
203
|
let path = [
|
204
|
this.stakeholder._id,
|
205
|
topic._id
|
206
|
];
|
207
|
this.stakeholderService.toggleStatus(this.properties.monitorServiceAPIURL, path).subscribe(isActive => {
|
208
|
topic.isActive = isActive;
|
209
|
UIkit.notification('Topic has been successfully changed to ' + (isActive ? 'active' : 'inactive'), {
|
210
|
status: 'success',
|
211
|
timeout: 3000,
|
212
|
pos: 'top-left'
|
213
|
});
|
214
|
}, error => {
|
215
|
UIkit.notification(error.error.message, {
|
216
|
status: 'danger',
|
217
|
timeout: 3000,
|
218
|
pos: 'top-left'
|
219
|
});
|
220
|
});
|
221
|
}
|
222
|
|
223
|
toggleTopicAccess(topic: Topic) {
|
224
|
let path = [
|
225
|
this.stakeholder._id,
|
226
|
topic._id
|
227
|
];
|
228
|
this.stakeholderService.toggleAccess(this.properties.monitorServiceAPIURL, path).subscribe(isPublic => {
|
229
|
topic.isPublic = isPublic;
|
230
|
UIkit.notification('Topic has been successfully changed to ' + (isPublic?'public':'private'), {
|
231
|
status: 'success',
|
232
|
timeout: 3000,
|
233
|
pos: 'top-left'
|
234
|
});
|
235
|
}, error => {
|
236
|
UIkit.notification(error.error.message, {
|
237
|
status: 'danger',
|
238
|
timeout: 3000,
|
239
|
pos: 'top-left'
|
240
|
});
|
241
|
});
|
242
|
}
|
243
|
}
|