Project

General

Profile

1
import {AfterViewInit, ChangeDetectorRef, Component, OnDestroy, OnInit} 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
import {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
18

    
19
@Component({
20
  selector: 'monitor',
21
  templateUrl: 'monitor.component.html',
22
})
23
export class MonitorComponent implements OnInit, OnDestroy {
24
  public piwiksub: any;
25
  public pageContents = null;
26
  public divContents = null;
27
  public status: number;
28
  public loading: boolean = true;
29
  public activeTopic: Topic = null;
30
  public activeCategory: Category = null;
31
  public activeSubCategory: SubCategory = null;
32
  public sideBar: Sidebar = null;
33
  public errorCodes: ErrorCodes;
34
  public stakeholder: Stakeholder;
35
  public numberResults: Map<number, number> = new Map<number, number>();
36
  public chartsActiveType: Map<number, IndicatorPath> = new Map<number, IndicatorPath>();
37
  private errorMessages: ErrorMessagesComponent;
38
  properties: EnvProperties;
39

    
40
  constructor(
41
    private route: ActivatedRoute,
42
    private _router: Router,
43
    private _meta: Meta,
44
    private _title: Title,
45
    private _piwikService: PiwikService,
46
    private helper: HelperService,
47
    private stakeholderService: StakeholderService,
48
    private statisticsService: StatisticsService,
49
    private sideBarService: SideBarService,
50
    private seoService: SEOService,
51
    private cdr: ChangeDetectorRef,
52
    private sanitizer: DomSanitizer) {
53
      this.errorCodes = new ErrorCodes();
54
      this.errorMessages = new ErrorMessagesComponent();
55
      this.status = this.errorCodes.LOADING;
56
  }
57

    
58
  public ngOnInit() {
59
    this.route.data
60
      .subscribe((data: { envSpecific: EnvProperties }) => {
61
        this.route.params.subscribe(params => {
62
          this.properties = data.envSpecific;
63
          var url = data.envSpecific.baseLink + this._router.url;
64
          if (!this.stakeholder || this.stakeholder.index_id !== params['stakeholder']) {
65
            this.status = this.errorCodes.LOADING;
66
            this.numberResults = new Map<number, number>();
67
            this.chartsActiveType = new Map<number, IndicatorPath>();
68
            this.stakeholderService.getStakeholder(params['stakeholder']).subscribe(stakeholder => {
69
              this.stakeholder = stakeholder;
70
              this.seoService.createLinkForCanonicalURL(url, false);
71
              this._meta.updateTag({content: url}, "property='og:url'");
72
              var description = "Monitor Dashboard | " + this.stakeholder.index_name;
73
              var title = "Monitor Dashboard | " + this.stakeholder.index_shortName;
74
              this._meta.updateTag({content: description}, "name='description'");
75
              this._meta.updateTag({content: description}, "property='og:description'");
76
              this._meta.updateTag({content: title}, "property='og:title'");
77
              this._title.setTitle(title);
78
              if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
79
                this.piwiksub = this._piwikService.trackView(this.properties, title, this.properties.piwikSiteId).subscribe();
80
              }
81
              //this.getDivContents();
82
              this.getPageContents();
83
              this.status = this.errorCodes.DONE;
84
              this.setView(params);
85
            }, error => {
86
              this.navigateToError();
87
            })
88
          } else {
89
            this.setView(params);
90
          }
91
        });
92
      });
93
  }
94

    
95
  public get open() {
96
    return this.sideBarService.open;
97
  }
98

    
99
  public toggleOpen(event = null) {
100
    if(!event) {
101
      this.sideBarService.setOpen(!this.open);
102
    } else if(event && event['value'] === true) {
103
      this.sideBarService.setOpen(false);
104
    }
105
  }
106

    
107
  private getPageContents() {
108
    this.helper.getPageHelpContents(this._router.url, this.properties, 'monitor').subscribe(contents => {
109
      this.pageContents = contents;
110
    })
111
  }
112

    
113
  private getDivContents() {
114
    this.helper.getDivHelpContents(this._router.url, this.properties, 'monitor').subscribe(contents => {
115
      this.divContents = contents;
116
    })
117
  }
118

    
119
  private setView(params: Params) {
120
    if (params && params['topic']) {
121
      this.activeTopic = this.stakeholder.topics.filter(topic => topic.alias === decodeURIComponent(params['topic']))[0];
122
      if (this.activeTopic) {
123
        if (params['category']) {
124
          this.activeCategory = this.activeTopic.categories.filter(category =>
125
            (category.alias === params['category']) && category.isPublic && category.isActive)[0];
126
        } else {
127
          let category: Category = this.activeTopic.categories[0];
128
          this.navigateTo(this.stakeholder.alias, this.activeTopic.alias, category.alias);
129
          return;
130
        }
131
        if (this.activeCategory) {
132
          if (params['subCategory']) {
133
            this.activeSubCategory = this.activeCategory.subCategories.filter(subCategory =>
134
              (subCategory.alias === params['subCategory'] && subCategory.isPublic && subCategory.isActive))[0];
135
          } else {
136
            this.activeSubCategory = this.activeCategory.subCategories.filter(subCategory =>
137
              !subCategory.alias && subCategory.isPublic && subCategory.isActive)[0];
138
          }
139
          if (this.activeSubCategory) {
140
            this.setSideBar();
141
            this.setIndicators();
142
            return;
143
          } else {
144
            let subCategory: SubCategory = this.activeCategory.subCategories.filter(subCategory =>
145
              subCategory.isPublic && subCategory.isActive)[0];
146
            this.navigateTo(this.stakeholder.alias, this.activeTopic.alias, this.activeCategory.alias, subCategory.alias);
147
            return;
148
          }
149
        }
150
      }
151
      this.navigateToError();
152
    } else {
153
      let topic: Topic = this.stakeholder.topics[0];
154
      let category: Category = topic.categories.filter(category => category.isPublic && category.isActive)[0];
155
      this.navigateTo(this.stakeholder.alias, topic.alias, category.alias);
156
    }
157
  }
158

    
159
  private setSideBar() {
160
    let items: Item[] = [];
161
    this.activeTopic.categories.forEach(category => {
162
      if (category.isPublic && category.isActive) {
163
        let subItems: Item[] = [];
164
        category.subCategories.forEach(subCategory => {
165
          if (subCategory.alias != null && subCategory.isPublic && subCategory.isActive) {
166
            subItems.push(new Item(subCategory.name, (
167
              '/monitor/' + this.stakeholder.alias + '/' + this.activeTopic.alias + '/' + category.alias + '/' + subCategory.alias),
168
              null, null, false));
169
          }
170
        });
171
        const open = this.activeCategory.alias === category.alias;
172
        items.push(new Item(category.name, (
173
          '/monitor/' + this.stakeholder.alias + '/' + this.activeTopic.alias + '/' + category.alias),
174
          subItems, null, open));
175
      }
176
    });
177
    this.sideBar = new Sidebar(items, null);
178
  }
179

    
180
  private setIndicators() {
181
    let urls: Map<string, number[]> = new Map<string, number[]>();
182
    this.activeSubCategory.numbers.forEach((number, index) => {
183
      if (number.isActive && number.isPublic) {
184
        const pair = JSON.stringify([number.indicatorPaths[0].source, number.indicatorPaths[0].url]);
185
        const indexes = urls.get(pair) ? urls.get(pair) : [];
186
        indexes.push(index);
187
        urls.set(pair, indexes);
188
      }
189
    });
190
    urls.forEach((indexes, pair) => {
191
      pair = JSON.parse(pair);
192
      this.statisticsService.getNumbers(pair[0], pair[1]).subscribe(response => {
193
        indexes.forEach(index => {
194
          let result = JSON.parse(JSON.stringify(response));
195
          this.activeSubCategory.numbers[index].indicatorPaths[0].jsonPath.forEach(jsonPath => {
196
            if (result) {
197
              result = result[jsonPath];
198
            }
199
          });
200
          this.numberResults.set(index, result);
201
        });
202
      })
203
    });
204
    this.activeSubCategory.charts.forEach((chart, index) => {
205
      if (chart.indicatorPaths.length > 0) {
206
        chart.indicatorPaths[0].safeResourceUrl = this.getUrlByStakeHolder(chart.indicatorPaths[0]);
207
        this.chartsActiveType.set(index, chart.indicatorPaths[0]);
208
      }
209
    });
210
    this.cdr.detectChanges();
211
  }
212

    
213
  public getUrlByStakeHolder(indicatorPath: IndicatorPath) {
214
    return this.sanitizer.bypassSecurityTrustResourceUrl(this.statisticsService.getChartUrl(indicatorPath.source, indicatorPath.getFullUrl()));
215
  }
216

    
217
  public setActiveChart(index, type: string) {
218
    let activeChart = this.activeSubCategory.charts[index].indicatorPaths.filter(indicatorPath => indicatorPath.type === type)[0];
219
    activeChart.safeResourceUrl = this.getUrlByStakeHolder(activeChart);
220
    this.chartsActiveType.set(index, activeChart);
221
  }
222

    
223
  private navigateToError() {
224
    this._router.navigate(['/error'], {queryParams: {'page': this._router.url}});
225
  }
226

    
227
  public navigateTo(stakeholder: string, topic: string, category: string = null, subcategory: string = null) {
228
    let url = '/monitor/' + stakeholder + '/' + topic + ((category) ? ('/'
229
      + category) : '') + ((subcategory) ? ('/' + subcategory) : '');
230
    return this._router.navigate([url]);
231
  }
232

    
233
  public quote(param: string): string {
234
    return StringUtils.quote(param);
235
  }
236

    
237
  public ngOnDestroy() {
238
    if (this.piwiksub) {
239
      this.piwiksub.unsubscribe();
240
    }
241
  }
242
}
(3-3/4)