1
|
import {Component, OnDestroy, OnInit} from "@angular/core";
|
2
|
import {StakeholderService} from "../services/stakeholder.service";
|
3
|
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
|
4
|
import {EnvironmentSpecificService} from "../openaireLibrary/utils/properties/environment-specific.service";
|
5
|
import {Stakeholder} from "../utils/entities/stakeholder";
|
6
|
import {Subscriber, zip} from "rxjs";
|
7
|
import {IndicatorUtils, Option} from "../utils/indicator-utils";
|
8
|
import {FormBuilder, FormGroup} from "@angular/forms";
|
9
|
|
10
|
@Component({
|
11
|
selector: 'home',
|
12
|
templateUrl: "./home.component.html"
|
13
|
})
|
14
|
export class HomeComponent implements OnInit, OnDestroy {
|
15
|
|
16
|
public properties: EnvProperties;
|
17
|
public loading: boolean = true;
|
18
|
public indicatorUtils: IndicatorUtils = new IndicatorUtils();
|
19
|
public defaultStakeholders: Stakeholder[];
|
20
|
public stakeholders: Stakeholder[];
|
21
|
public stakeholder: FormGroup;
|
22
|
/**
|
23
|
* Filtered Stakeholders
|
24
|
*/
|
25
|
public displayDefaultStakeholders: Stakeholder[];
|
26
|
public displayStakeholders: Stakeholder[];
|
27
|
/**
|
28
|
* Top filters
|
29
|
*/
|
30
|
public filters: FormGroup;
|
31
|
public all: Option = {
|
32
|
value: 'all',
|
33
|
label: 'All'
|
34
|
};
|
35
|
|
36
|
/**
|
37
|
* Grid or List View
|
38
|
*/
|
39
|
public grid: boolean = true;
|
40
|
private subscriptions: any[] = [];
|
41
|
|
42
|
constructor(private stakeholderService: StakeholderService,
|
43
|
private propertiesService: EnvironmentSpecificService,
|
44
|
private fb: FormBuilder) {
|
45
|
}
|
46
|
|
47
|
ngOnInit(): void {
|
48
|
this.buildFilters();
|
49
|
this.propertiesService.loadEnvironment()
|
50
|
.then(properties => {
|
51
|
this.properties = properties;
|
52
|
let data = zip(
|
53
|
this.stakeholderService.getDefaultStakeholders(this.properties.monitorServiceAPIURL),
|
54
|
this.stakeholderService.getStakeholders(this.properties.monitorServiceAPIURL)
|
55
|
);
|
56
|
data.subscribe(res => {
|
57
|
this.defaultStakeholders = res[0];
|
58
|
this.stakeholders = res[1];
|
59
|
this.displayDefaultStakeholders = res[0];
|
60
|
this.displayStakeholders = res[1];
|
61
|
this.loading = false;
|
62
|
});
|
63
|
});
|
64
|
}
|
65
|
|
66
|
ngOnDestroy(): void {
|
67
|
this.subscriptions.forEach(value => {
|
68
|
if (value instanceof Subscriber) {
|
69
|
value.unsubscribe();
|
70
|
}
|
71
|
});
|
72
|
}
|
73
|
|
74
|
public changeGrid(value) {
|
75
|
this.grid = value;
|
76
|
}
|
77
|
|
78
|
private buildFilters() {
|
79
|
this.filters = this.fb.group({
|
80
|
privacy: this.fb.control('all'),
|
81
|
status: this.fb.control('all'),
|
82
|
keyword: this.fb.control('')
|
83
|
});
|
84
|
this.subscriptions.push(this.filters.get('privacy').valueChanges.subscribe(value => {
|
85
|
this.onPrivacyChange(value);
|
86
|
}));
|
87
|
this.subscriptions.push(this.filters.get('status').valueChanges.subscribe(value => {
|
88
|
this.onStatusChange(value);
|
89
|
}));
|
90
|
this.subscriptions.push(this.filters.get('keyword').valueChanges.subscribe(value => {
|
91
|
this.onKeywordChange(value);
|
92
|
}));
|
93
|
}
|
94
|
|
95
|
onPrivacyChange(value) {
|
96
|
this.displayDefaultStakeholders = this.filterPrivacy(this.defaultStakeholders, value);
|
97
|
this.displayStakeholders = this.filterPrivacy(this.stakeholders, value);
|
98
|
}
|
99
|
|
100
|
onStatusChange(value) {
|
101
|
this.displayDefaultStakeholders = this.filterStatus(this.defaultStakeholders, value);
|
102
|
this.displayStakeholders = this.filterStatus(this.stakeholders, value);
|
103
|
}
|
104
|
|
105
|
onKeywordChange(value) {
|
106
|
this.displayDefaultStakeholders = this.filterByKeyword(this.defaultStakeholders, value);
|
107
|
this.displayStakeholders = this.filterByKeyword(this.stakeholders, value);
|
108
|
}
|
109
|
|
110
|
|
111
|
private filterPrivacy(stakeholders: Stakeholder[], value): Stakeholder[] {
|
112
|
if (value === 'all') {
|
113
|
return stakeholders;
|
114
|
} else {
|
115
|
return stakeholders.filter(stakeholder => stakeholder.isPublic === value);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
private filterStatus(stakeholders: Stakeholder[], value): Stakeholder[] {
|
120
|
if (value === 'all') {
|
121
|
return stakeholders;
|
122
|
} else {
|
123
|
return stakeholders.filter(stakeholder => stakeholder.isActive === value);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
private filterByKeyword(stakeholders: Stakeholder[], value): Stakeholder[] {
|
128
|
if (value === null || value === '') {
|
129
|
return stakeholders;
|
130
|
} else {
|
131
|
return stakeholders.filter(stakeholder => (
|
132
|
stakeholder.index_id && stakeholder.index_id.toLowerCase().includes(value.toLowerCase())) ||
|
133
|
stakeholder.index_shortName && stakeholder.index_shortName.toLowerCase().includes(value.toLowerCase()) ||
|
134
|
stakeholder.index_name && stakeholder.index_name.toLowerCase().includes(value.toLowerCase())
|
135
|
);
|
136
|
}
|
137
|
}
|
138
|
|
139
|
public editStakeholder(index = -1) {
|
140
|
/*let stakeholder = this.d
|
141
|
this.fb.group({
|
142
|
_id: this.fb.control()
|
143
|
})*/
|
144
|
}
|
145
|
}
|