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 {SidebarOpenService} from "../library/sidebar/sidebar-open.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 sidebarOpenService: SidebarOpenService,
50
    private seoService: SEOService,
51
    private sanitizer: DomSanitizer) {
52
      this.errorCodes = new ErrorCodes();
53
      this.errorMessages = new ErrorMessagesComponent();
54
      this.status = this.errorCodes.LOADING;
55
  }
56

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

    
94
  public get open() {
95
    return this.sidebarOpenService.open;
96
  }
97

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

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

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

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

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

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

    
210
  public getUrlByStakeHolder(indicatorPath: IndicatorPath) {
211
    return this.sanitizer.bypassSecurityTrustResourceUrl(this.statisticsService.getChartUrl(indicatorPath.source, indicatorPath.url));
212
  }
213

    
214
  public setActiveChart(index, type: string) {
215
    this.chartsActiveType.set(index, this.activeSubCategory.charts[index].indicatorPaths.filter(indicatorPath => indicatorPath.type === type)[0]);
216
  }
217

    
218
  private navigateToError() {
219
    this._router.navigate(['/error'], {queryParams: {'page': this._router.url}});
220
  }
221

    
222
  public navigateTo(stakeholder: string, topic: string, category: string = null, subcategory: string = null) {
223
    let url = '/' + stakeholder + '/' + topic + ((category) ? ('/'
224
      + category) : '') + ((subcategory) ? ('/' + subcategory) : '');
225
    return this._router.navigate([url]);
226
  }
227

    
228
  public quote(param: string): string {
229
    return StringUtils.quote(param);
230
  }
231

    
232
  public ngOnDestroy() {
233
    if (this.piwiksub) {
234
      this.piwiksub.unsubscribe();
235
    }
236
  }
237
}
(3-3/4)