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