Project

General

Profile

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 {MenuItem, RootMenuItem, SideMenuItem} from "../openaireLibrary/sharedComponents/menu";
16
import {StatisticsService} from "../utils/services/statistics.service";
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 sideMenuItems: SideMenuItem[] = 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
    this.sideMenuItems = [];
146
    this.activeTopic.categories.forEach(category => {
147
      if (category.isPublic && category.isActive) {
148
        let rootItem: MenuItem = new MenuItem(category.alias, category.name, null, (
149
          '/monitor/' + this.stakeholder.alias + '/' + this.activeTopic.alias + '/' + category.alias),
150
          false, null, null, null);
151
        let items: RootMenuItem[] = [];
152
        category.subCategories.forEach(subCategory => {
153
          if (subCategory.alias != null && subCategory.isPublic && subCategory.isActive) {
154
            items.push({
155
              items: [],
156
              rootItem: new MenuItem(subCategory.alias, subCategory.name, null, (
157
                '/monitor/' + this.stakeholder.alias + '/' + this.activeTopic.alias + '/' + category.alias + '/' + subCategory.alias),
158
                false, null, null, null)
159
            });
160
          }
161
        });
162
        this.sideMenuItems.push({
163
          rootItem: rootItem,
164
          items: items,
165
          ukIcon: null
166
        });
167
      }
168
    });
169
  }
170

    
171
  private setIndicators() {
172
    let urls: Map<string, number[]> = new Map<string, number[]>();
173
    this.activeSubCategory.numbers.forEach((number, index) => {
174
      if(number.isActive && number.isPublic) {
175
        const pair = JSON.stringify([number.indicatorPaths[0].source, number.indicatorPaths[0].url]);
176
        const indexes = urls.get(pair) ? urls.get(pair) : [];
177
        indexes.push(index);
178
        urls.set(pair, indexes);
179
      }
180
    });
181
    urls.forEach((indexes, pair) => {
182
      pair = JSON.parse(pair);
183
      this.statisticsService.getNumbers(pair[0], pair[1]).subscribe( response => {
184
        indexes.forEach( index => {
185
          let result = JSON.parse(JSON.stringify(response));
186
          this.activeSubCategory.numbers[index].indicatorPaths[0].jsonPath.forEach(jsonPath => {
187
            result = result[jsonPath];
188
          });
189
          this.numberResults.set(index, result);
190
        });
191
      })
192
    });
193
    this.activeSubCategory.charts.forEach((chart, index) => {
194
      if (chart.indicatorPaths.length > 0) {
195
        this.chartsActiveType.set(index, chart.indicatorPaths[0]);
196
      }
197
    });
198
  }
199

    
200
  public getUrlByStakeHolder(indicatorPath: IndicatorPath) {
201
    return this.sanitizer.
202
    bypassSecurityTrustResourceUrl(this.statisticsService.getChartUrl(indicatorPath.source, indicatorPath.url));
203
  }
204

    
205
  public setActiveChart(index, type: string) {
206
    this.chartsActiveType.set(index, this.activeSubCategory.charts[index].indicatorPaths.filter(indicatorPath => indicatorPath.type === type)[0]);
207
  }
208

    
209
  private navigateToError() {
210
    this._router.navigate(['/error'], {queryParams: {'page': this._router.url}});
211
  }
212

    
213
  public navigateTo(stakeholder: string, topic: string, category: string = null, subcategory: string = null) {
214
    let url = 'monitor/' + stakeholder + '/' + topic + ((category) ? ('/'
215
      + category) : '') + ((subcategory) ? ('/' + subcategory) : '');
216
    return this._router.navigate([url]);
217
  }
218

    
219
  public quote(param: string): string {
220
    return StringUtils.quote(param);
221
  }
222

    
223
  public ngOnDestroy() {
224
    if (this.piwiksub) {
225
      this.piwiksub.unsubscribe();
226
    }
227
  }
228
}
(3-3/4)