1
|
import {Component} from '@angular/core';
|
2
|
import {ActivatedRoute, Params, Router} from '@angular/router';
|
3
|
import {DomSanitizer, Meta, Title} from '@angular/platform-browser';
|
4
|
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
|
5
|
|
6
|
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
|
7
|
import {StringUtils} from '../openaireLibrary/utils/string-utils.class';
|
8
|
|
9
|
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
|
10
|
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
|
11
|
import {HelperService} from "../openaireLibrary/utils/helper/helper.service";
|
12
|
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
|
13
|
import {StakeholderService} from "../services/stakeholder.service";
|
14
|
import {Category, IndicatorPath, Stakeholder, SubCategory, Topic} from "../utils/entities/stakeholder";
|
15
|
import {StatisticsService} from "../utils/services/statistics.service";
|
16
|
import {Item, Sidebar} from "../utils/entities/sidebar";
|
17
|
|
18
|
@Component({
|
19
|
selector: 'monitor',
|
20
|
templateUrl: 'monitor.component.html',
|
21
|
})
|
22
|
export class MonitorComponent {
|
23
|
public piwiksub: any;
|
24
|
public pageContents = null;
|
25
|
public divContents = null;
|
26
|
public status: number;
|
27
|
public loading: boolean = true;
|
28
|
public activeTopic: Topic = null;
|
29
|
public activeCategory: Category = null;
|
30
|
public activeSubCategory: SubCategory = null;
|
31
|
public sideBar: Sidebar = null;
|
32
|
public errorCodes: ErrorCodes;
|
33
|
public stakeholder: Stakeholder;
|
34
|
public numberResults: Map<number, number> = new Map<number, number>();
|
35
|
public chartsActiveType: Map<number, IndicatorPath> = new Map<number, IndicatorPath>();
|
36
|
private errorMessages: ErrorMessagesComponent;
|
37
|
properties: EnvProperties;
|
38
|
|
39
|
constructor(
|
40
|
private route: ActivatedRoute,
|
41
|
private _router: Router,
|
42
|
private _meta: Meta,
|
43
|
private _title: Title,
|
44
|
private _piwikService: PiwikService,
|
45
|
private helper: HelperService,
|
46
|
private stakeholderService: StakeholderService,
|
47
|
private statisticsService: StatisticsService,
|
48
|
private seoService: SEOService,
|
49
|
private sanitizer: DomSanitizer) {
|
50
|
this.errorCodes = new ErrorCodes();
|
51
|
this.errorMessages = new ErrorMessagesComponent();
|
52
|
this.status = this.errorCodes.LOADING;
|
53
|
}
|
54
|
|
55
|
public ngOnInit() {
|
56
|
this.route.data
|
57
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
58
|
this.route.params.subscribe(params => {
|
59
|
this.properties = data.envSpecific;
|
60
|
var url = data.envSpecific.baseLink + this._router.url;
|
61
|
if (!this.stakeholder || this.stakeholder.index_id !== params['stakeholder']) {
|
62
|
this.status = this.errorCodes.LOADING;
|
63
|
this.numberResults= new Map<number, number>();
|
64
|
this.chartsActiveType = new Map<number, IndicatorPath>();
|
65
|
this.stakeholderService.getStakeholder(params['stakeholder']).subscribe(stakeholder => {
|
66
|
this.stakeholder = stakeholder;
|
67
|
this.seoService.createLinkForCanonicalURL(url, false);
|
68
|
this._meta.updateTag({content: url}, "property='og:url'");
|
69
|
var description = "Monitor Dashboard | " + this.stakeholder.index_name;
|
70
|
var title = "Monitor Dashboard | " + this.stakeholder.index_shortName;
|
71
|
this._meta.updateTag({content: description}, "name='description'");
|
72
|
this._meta.updateTag({content: description}, "property='og:description'");
|
73
|
this._meta.updateTag({content: title}, "property='og:title'");
|
74
|
this._title.setTitle(title);
|
75
|
if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
|
76
|
this.piwiksub = this._piwikService.trackView(this.properties, title, this.properties.piwikSiteId).subscribe();
|
77
|
}
|
78
|
//this.getDivContents();
|
79
|
this.getPageContents();
|
80
|
this.status = this.errorCodes.DONE;
|
81
|
this.setView(params);
|
82
|
}, error => {
|
83
|
this.navigateToError();
|
84
|
})
|
85
|
} else {
|
86
|
this.setView(params);
|
87
|
}
|
88
|
});
|
89
|
});
|
90
|
}
|
91
|
|
92
|
private getPageContents() {
|
93
|
this.helper.getPageHelpContents(this._router.url, this.properties, 'monitor').subscribe(contents => {
|
94
|
this.pageContents = contents;
|
95
|
})
|
96
|
}
|
97
|
|
98
|
private getDivContents() {
|
99
|
this.helper.getDivHelpContents(this._router.url, this.properties, 'monitor').subscribe(contents => {
|
100
|
this.divContents = contents;
|
101
|
})
|
102
|
}
|
103
|
|
104
|
private setView(params: Params) {
|
105
|
if (params && params['topic']) {
|
106
|
this.activeTopic = this.stakeholder.topics.filter(topic => topic.alias === decodeURIComponent(params['topic']))[0];
|
107
|
if (this.activeTopic) {
|
108
|
if (params['category']) {
|
109
|
this.activeCategory = this.activeTopic.categories.filter(category =>
|
110
|
(category.alias === params['category']) && category.isPublic && category.isActive)[0];
|
111
|
} else {
|
112
|
let category: Category = this.activeTopic.categories[0];
|
113
|
this.navigateTo(this.stakeholder.alias, this.activeTopic.alias, category.alias);
|
114
|
return;
|
115
|
}
|
116
|
if (this.activeCategory) {
|
117
|
if (params['subCategory']) {
|
118
|
this.activeSubCategory = this.activeCategory.subCategories.filter(subCategory =>
|
119
|
(subCategory.alias === params['subCategory'] && subCategory.isPublic && subCategory.isActive))[0];
|
120
|
} else {
|
121
|
this.activeSubCategory = this.activeCategory.subCategories.filter(subCategory =>
|
122
|
!subCategory.alias && subCategory.isPublic && subCategory.isActive)[0];
|
123
|
}
|
124
|
if (this.activeSubCategory) {
|
125
|
this.setSideBar();
|
126
|
this.setIndicators();
|
127
|
return;
|
128
|
} else {
|
129
|
let subCategory: SubCategory = this.activeCategory.subCategories.filter( subCategory =>
|
130
|
subCategory.isPublic && subCategory.isActive)[0];
|
131
|
this.navigateTo(this.stakeholder.alias, this.activeTopic.alias, this.activeCategory.alias, subCategory.alias);
|
132
|
return;
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
this.navigateToError();
|
137
|
} else {
|
138
|
let topic: Topic = this.stakeholder.topics[0];
|
139
|
let category: Category = topic.categories.filter(category => category.isPublic && category.isActive)[0];
|
140
|
this.navigateTo(this.stakeholder.alias, topic.alias, category.alias);
|
141
|
}
|
142
|
}
|
143
|
|
144
|
private setSideBar() {
|
145
|
let items: Item[] = [];
|
146
|
this.activeTopic.categories.forEach(category => {
|
147
|
if (category.isPublic && category.isActive) {
|
148
|
let subItems: Item[] = [];
|
149
|
category.subCategories.forEach(subCategory => {
|
150
|
if (subCategory.alias != null && subCategory.isPublic && subCategory.isActive) {
|
151
|
subItems.push(new Item(subCategory.name, (
|
152
|
'/' + this.stakeholder.alias + '/' + this.activeTopic.alias + '/' + category.alias + '/' + subCategory.alias),
|
153
|
null, null, false));
|
154
|
}
|
155
|
});
|
156
|
const open = this.activeCategory.alias === category.alias;
|
157
|
items.push(new Item(category.name, (
|
158
|
'/' + this.stakeholder.alias + '/' + this.activeTopic.alias + '/' + category.alias),
|
159
|
subItems, null, open));
|
160
|
}
|
161
|
});
|
162
|
this.sideBar = new Sidebar(items);
|
163
|
}
|
164
|
|
165
|
private setIndicators() {
|
166
|
let urls: Map<string, number[]> = new Map<string, number[]>();
|
167
|
this.activeSubCategory.numbers.forEach((number, index) => {
|
168
|
if(number.isActive && number.isPublic) {
|
169
|
const pair = JSON.stringify([number.indicatorPaths[0].source, number.indicatorPaths[0].url]);
|
170
|
const indexes = urls.get(pair) ? urls.get(pair) : [];
|
171
|
indexes.push(index);
|
172
|
urls.set(pair, indexes);
|
173
|
}
|
174
|
});
|
175
|
urls.forEach((indexes, pair) => {
|
176
|
pair = JSON.parse(pair);
|
177
|
this.statisticsService.getNumbers(pair[0], pair[1]).subscribe( response => {
|
178
|
indexes.forEach( index => {
|
179
|
let result = JSON.parse(JSON.stringify(response));
|
180
|
this.activeSubCategory.numbers[index].indicatorPaths[0].jsonPath.forEach(jsonPath => {
|
181
|
result = result[jsonPath];
|
182
|
});
|
183
|
this.numberResults.set(index, result);
|
184
|
});
|
185
|
})
|
186
|
});
|
187
|
this.activeSubCategory.charts.forEach((chart, index) => {
|
188
|
if (chart.indicatorPaths.length > 0) {
|
189
|
this.chartsActiveType.set(index, chart.indicatorPaths[0]);
|
190
|
}
|
191
|
});
|
192
|
}
|
193
|
|
194
|
public getUrlByStakeHolder(indicatorPath: IndicatorPath) {
|
195
|
return this.sanitizer.
|
196
|
bypassSecurityTrustResourceUrl(this.statisticsService.getChartUrl(indicatorPath.source, indicatorPath.url));
|
197
|
}
|
198
|
|
199
|
public setActiveChart(index, type: string) {
|
200
|
this.chartsActiveType.set(index, this.activeSubCategory.charts[index].indicatorPaths.filter(indicatorPath => indicatorPath.type === type)[0]);
|
201
|
}
|
202
|
|
203
|
private navigateToError() {
|
204
|
this._router.navigate(['/error'], {queryParams: {'page': this._router.url}});
|
205
|
}
|
206
|
|
207
|
public navigateTo(stakeholder: string, topic: string, category: string = null, subcategory: string = null) {
|
208
|
let url = '/' + stakeholder + '/' + topic + ((category) ? ('/'
|
209
|
+ category) : '') + ((subcategory) ? ('/' + subcategory) : '');
|
210
|
return this._router.navigate([url]);
|
211
|
}
|
212
|
|
213
|
public quote(param: string): string {
|
214
|
return StringUtils.quote(param);
|
215
|
}
|
216
|
|
217
|
public ngOnDestroy() {
|
218
|
if (this.piwiksub) {
|
219
|
this.piwiksub.unsubscribe();
|
220
|
}
|
221
|
}
|
222
|
}
|