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.stakeholderService.getStakeholder(params['stakeholder']).subscribe(stakeholder => {
64
              this.stakeholder = stakeholder;
65
              this.seoService.createLinkForCanonicalURL(url, false);
66
              this._meta.updateTag({content: url}, "property='og:url'");
67
              var description = "Monitor Dashboard | " + this.stakeholder.index_name;
68
              var title = "Monitor Dashboard | " + this.stakeholder.index_shortName;
69
              this._meta.updateTag({content: description}, "name='description'");
70
              this._meta.updateTag({content: description}, "property='og:description'");
71
              this._meta.updateTag({content: title}, "property='og:title'");
72
              this._title.setTitle(title);
73
              if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
74
                this.piwiksub = this._piwikService.trackView(this.properties, title, this.properties.piwikSiteId).subscribe();
75
              }
76
              //this.getDivContents();
77
              this.getPageContents();
78
              this.status = this.errorCodes.DONE;
79
              this.setView(params);
80
            }, error => {
81
              this.navigateToError();
82
            })
83
          } else {
84
            this.setView(params);
85
          }
86
        });
87
      });
88
  }
89

    
90
  private getPageContents() {
91
    this.helper.getPageHelpContents(this._router.url, this.properties, 'monitor').subscribe(contents => {
92
      this.pageContents = contents;
93
    })
94
  }
95

    
96
  private getDivContents() {
97
    this.helper.getDivHelpContents(this._router.url, this.properties, 'monitor').subscribe(contents => {
98
      this.divContents = contents;
99
    })
100
  }
101

    
102
  private setView(params: Params) {
103
    if (params && params['topic']) {
104
      this.activeTopic = this.stakeholder.topics.filter(topic => topic.alias === decodeURIComponent(params['topic']))[0];
105
      if (this.activeTopic) {
106
        if (params['category']) {
107
          this.activeCategory = this.activeTopic.categories.filter(category =>
108
            (category.alias === params['category']) && category.isPublic && category.isActive)[0];
109
        } else {
110
          let category: Category = this.activeTopic.categories[0];
111
          this.navigateTo(this.stakeholder.alias, this.activeTopic.alias, category.alias);
112
          return;
113
        }
114
        if (this.activeCategory) {
115
          if (params['subCategory']) {
116
            this.activeSubCategory = this.activeCategory.subCategories.filter(subCategory =>
117
              (subCategory.alias === params['subCategory'] && subCategory.isPublic && subCategory.isActive))[0];
118
          } else {
119
            this.activeSubCategory = this.activeCategory.subCategories.filter(subCategory =>
120
              subCategory.isPublic && subCategory.isActive)[0];
121
          }
122
          if (this.activeSubCategory) {
123
            this.setSideBar();
124
            this.setIndicators();
125
            return;
126
          }
127
        }
128
      }
129
      this.navigateToError();
130
    } else {
131
      let topic: Topic = this.stakeholder.topics[0];
132
      let category: Category = topic.categories.filter(category => category.isPublic && category.isActive)[0];
133
      this.navigateTo(this.stakeholder.alias, topic.alias, category.alias);
134
    }
135
  }
136

    
137
  private setSideBar() {
138
    this.sideMenuItems = [];
139
    this.activeTopic.categories.forEach(category => {
140
      if (category.isPublic && category.isActive) {
141
        let rootItem: MenuItem = new MenuItem(category.alias, category.name, null, (
142
          '/monitor/' + this.stakeholder.alias + '/' + this.activeTopic.alias + '/' + category.alias),
143
          false, null, null, null);
144
        let items: RootMenuItem[] = [];
145
        category.subCategories.forEach(subCategory => {
146
          if (subCategory.alias != null && subCategory.isPublic && subCategory.isActive) {
147
            items.push({
148
              items: [],
149
              rootItem: new MenuItem(subCategory.alias, subCategory.name, null, (
150
                '/monitor/' + this.stakeholder.alias + '/' + this.activeTopic.alias + '/' + category.alias + '/' + subCategory.alias),
151
                false, null, null, null)
152
            });
153
          }
154
        });
155
        this.sideMenuItems.push({
156
          rootItem: rootItem,
157
          items: items,
158
          ukIcon: null
159
        });
160
      }
161
    });
162
  }
163

    
164
  private setIndicators() {
165
    this.activeSubCategory.numbers.forEach((number, index) => {
166
      this.statisticsService.getNumbers(number.indicatorPaths[0].source, number.indicatorPaths[0].url).subscribe(response => {
167
        number.indicatorPaths[0].jsonPath.forEach(jsonPath => {
168
          response = response[jsonPath];
169
        });
170
        this.numberResults.set(index, response);
171
      });
172
    });
173
    this.activeSubCategory.charts.forEach((chart, index) => {
174
      if (chart.indicatorPaths.length > 0) {
175
        this.chartsActiveType.set(index, chart.indicatorPaths[0]);
176
      }
177
    });
178
  }
179

    
180
  public getUrlByStakeHolder(indicatorPath: IndicatorPath) {
181
    return this.sanitizer.
182
    bypassSecurityTrustResourceUrl(this.statisticsService.getChartUrl(indicatorPath.source, indicatorPath.url));
183
  }
184

    
185
  public setActiveChart(index, type: string) {
186
    this.chartsActiveType.set(index, this.activeSubCategory.charts[index].indicatorPaths.filter(indicatorPath => indicatorPath.type === type)[0]);
187
  }
188

    
189
  private navigateToError() {
190
    this._router.navigate(['/error'], {queryParams: {'page': this._router.url}});
191
  }
192

    
193
  public navigateTo(stakeholder: string, topic: string, category: string = null, subcategory: string = null) {
194
    let url = 'monitor/' + stakeholder + '/' + topic + ((category) ? ('/'
195
      + category) : '') + ((subcategory) ? ('/' + subcategory) : '');
196
    return this._router.navigate([url]);
197
  }
198

    
199
  public quote(param: string): string {
200
    return StringUtils.quote(param);
201
  }
202

    
203
  public ngOnDestroy() {
204
    if (this.piwiksub) {
205
      this.piwiksub.unsubscribe();
206
    }
207
  }
208
}
(3-3/4)