Project

General

Profile

1
import {Component} from '@angular/core';
2
import {ActivatedRoute, Params, Router} from '@angular/router';
3
import {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, Indicator, Stakeholder, SubCategory, Topic} from "../utils/entities/stakeholder";
15
import {MenuItem, RootMenuItem, SideMenuItem} from "../openaireLibrary/sharedComponents/menu";
16

    
17
@Component({
18
  selector: 'monitor',
19
  templateUrl: 'monitor.component.html',
20
})
21
export class MonitorComponent {
22
  public piwiksub: any;
23
  public pageContents = null;
24
  public divContents = null;
25
  public status: number;
26
  public loading: boolean = true;
27
  public activeTopic: Topic = null;
28
  public activeCategory: Category = null;
29
  public activeSubCategory: SubCategory = null;
30
  public sideMenuItems: SideMenuItem[] = null;
31
  public errorCodes: ErrorCodes;
32
  public stakeholder: Stakeholder;
33
  public numbers: Indicator[] = [];
34
  public charts: Indicator[] = [];
35
  private errorMessages: ErrorMessagesComponent;
36
  properties: EnvProperties;
37

    
38
  constructor(
39
    private route: ActivatedRoute,
40
    private _router: Router,
41
    private _meta: Meta,
42
    private _title: Title,
43
    private _piwikService: PiwikService,
44
    private helper: HelperService,
45
    private stakeholderService: StakeholderService,
46
    private seoService: SEOService) {
47
    this.errorCodes = new ErrorCodes();
48
    this.errorMessages = new ErrorMessagesComponent();
49
    this.status = this.errorCodes.LOADING;
50
  }
51

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

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

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

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

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

    
158
  private setIndicators() {
159
    this.stakeholderService.getIndicators(this.activeSubCategory.numbers).subscribe( numbers => {
160
      this.numbers = numbers;
161
    });
162
    this.stakeholderService.getIndicators(this.activeSubCategory.charts).subscribe( charts => {
163
      this.charts = charts;
164
    });
165
  }
166

    
167
  public navigateToError() {
168
    this._router.navigate(['/error'],{queryParams: {'page': this._router.url}});
169
  }
170

    
171
  public navigateTo(stakeholder: string, topic: string, category: string = null, subcategory: string = null) {
172
    let url = 'monitor/' + stakeholder+ '/' + topic + ((category)?('/'
173
      + category):'') + ((subcategory)?('/' + subcategory):'');
174
    return this._router.navigate([url]);
175
  }
176

    
177
  public quote(param: string): string {
178
    return StringUtils.quote(param);
179
  }
180

    
181
  public ngOnDestroy() {
182
    if (this.piwiksub) {
183
      this.piwiksub.unsubscribe();
184
    }
185
  }
186
}
(3-3/4)