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, 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
  private errorMessages: ErrorMessagesComponent;
34
  properties: EnvProperties;
35

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

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

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

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

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

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

    
155
  public navigateToError() {
156
    this._router.navigate(['/error'],{queryParams: {'page': this._router.url}});
157
  }
158

    
159
  public navigateTo(stakeholder: string, topic: string, category: string = null, subcategory: string = null) {
160
    let url = 'monitor/' + stakeholder+ '/' + topic + ((category)?('/'
161
      + category):'') + ((subcategory)?('/' + subcategory):'');
162
    return this._router.navigate([url]);
163
  }
164

    
165
  public quote(param: string): string {
166
    return StringUtils.quote(param);
167
  }
168

    
169
  public ngOnDestroy() {
170
    if (this.piwiksub) {
171
      this.piwiksub.unsubscribe();
172
    }
173
  }
174
}
(3-3/4)