Project

General

Profile

1
import {Component, ElementRef, ViewChild, ViewEncapsulation} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Meta, Title} from '@angular/platform-browser';
4
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
5
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
6
import {StringUtils} from '../openaireLibrary/utils/string-utils.class';
7

    
8
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
9
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
10
import {HelperService} from "../openaireLibrary/utils/helper/helper.service";
11
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
12
import {RefineFieldResultsService} from "../openaireLibrary/services/refineFieldResults.service";
13
import {NumberUtils} from "../openaireLibrary/utils/number-utils.class";
14
import {SearchResearchResultsService} from "../openaireLibrary/services/searchResearchResults.service";
15
import {animate, state, style, transition, trigger} from "@angular/animations";
16
import {StakeholderService} from "../openaireLibrary/monitor/services/stakeholder.service";
17
import {LocalStorageService} from "../openaireLibrary/services/localStorage.service";
18
import {Stakeholder} from "../openaireLibrary/monitor/entities/stakeholder";
19
import {Session, User} from "../openaireLibrary/login/utils/helper.class";
20
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
21

    
22
@Component({
23
  selector: 'home',
24
  templateUrl: 'home.component.html',
25
  styleUrls: ['home.component.css'],
26
  animations: [
27
    trigger('1', [
28
      state('1', style({
29
        opacity: 1
30
      })),
31
      transition('* => *', [
32
        animate('0.3s')
33
      ])
34
    ]),
35
    trigger('2', [
36
      state('2', style({
37
        opacity: 1
38
      })),
39
      transition('* => *', [
40
        animate('0.3s')
41
      ])
42
    ]),
43
    trigger('3', [
44
      state('3', style({
45
        opacity: 1
46
      })),
47
      transition('* => *', [
48
        animate('0.3s')
49
      ])
50
    ])
51
  ]
52
})
53
export class HomeComponent {
54
  public piwiksub: any;
55
  
56
  public pageTitle = "OpenAIRE | Monitor";
57
  public stakeholders: Stakeholder[] = [];
58
  public selected: Stakeholder = null;
59
  public pageContents = null;
60
  public divContents = null;
61
  // Message variables
62
  public status: number;
63
  public loading: boolean = true;
64
  public subscriberErrorMessage: string = "";
65
  public errorCodes: ErrorCodes;
66
  private errorMessages: ErrorMessagesComponent;
67
  
68
  properties: EnvProperties;
69
  public keyword: string = "";
70
  public type: string = null;
71
  
72
  public publicationsSize: any = null;
73
  public datasetsSize: any = null;
74
  public softwareSize: any = null;
75
  public otherSize: any = null;
76
  public fundersSize: any = null;
77
  numberSubs = [];
78
  public state = 1;
79
  private timeouts: any[] = [];
80
  @ViewChild('AlertModal') modal;
81
  public directLink: boolean = true;
82
  private user: User;
83
  
84
  constructor(
85
    private route: ActivatedRoute,
86
    private _router: Router,
87
    private _meta: Meta,
88
    private _title: Title,
89
    private _piwikService: PiwikService,
90
    private _stakeholderService: StakeholderService,
91
    private localStorageService: LocalStorageService,
92
    private userManagementService: UserManagementService,
93
    private helper: HelperService,
94
    private seoService: SEOService,
95
    private _refineFieldResultsService: RefineFieldResultsService, private _searchResearchResultsService: SearchResearchResultsService) {
96
    
97
    
98
    var description = "OpenAIRE - Monitor, Statistics, Search, Funder, EC, European Commision";
99
    var title = "OpenAIRE - Monitor";
100
    
101
    this._meta.updateTag({content: description}, "name='description'");
102
    this._meta.updateTag({content: description}, "property='og:description'");
103
    this._meta.updateTag({content: title}, "property='og:title'");
104
    this._title.setTitle(title);
105
    
106
    this.errorCodes = new ErrorCodes();
107
    this.errorMessages = new ErrorMessagesComponent();
108
    this.status = this.errorCodes.LOADING;
109
  }
110
  
111
  public ngOnInit() {
112
    this.route.data
113
      .subscribe((data: { envSpecific: EnvProperties }) => {
114
        this.properties = data.envSpecific;
115
        var url = this.properties.domain + this.properties.baseLink + this._router.url;
116
        this.seoService.createLinkForCanonicalURL(url, false);
117
        this._meta.updateTag({content: url}, "property='og:url'");
118
        if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
119
          this.piwiksub = this._piwikService.trackView(this.properties, "OpenAIRE Monitor", this.properties.piwikSiteId).subscribe();
120
        }
121
        this.getNumbers();
122
        this.getStakeholders();
123
        // this.createGifs();
124
        //this.getDivContents();
125
        this.getPageContents();
126
        this.localStorageService.get().subscribe(value => {
127
          this.directLink = value;
128
        });
129
        this.userManagementService.getUserInfo().subscribe(user => {
130
          this.user = user;
131
        })
132
      });
133
    if(typeof document != "undefined") {
134
      this.startAnimation();
135
    }
136
  }
137
  
138
  public startAnimation(state = 1) {
139
    this.clearTimeouts();
140
    this.state = state;
141
    this.timeouts.push(setTimeout(() => {
142
      if (this.state < 3) {
143
        this.startAnimation(this.state + 1);
144
      } else {
145
        this.startAnimation();
146
      }
147
    }, 6000));
148
  }
149
  
150
  private clearTimeouts() {
151
    this.timeouts.forEach(timeout => {
152
      clearTimeout(timeout);
153
    });
154
  }
155
  
156
  private getPageContents() {
157
    this.helper.getPageHelpContents(this.properties, 'monitor', this._router.url).subscribe(contents => {
158
      this.pageContents = contents;
159
    })
160
  }
161
  
162
  private getDivContents() {
163
    this.helper.getDivHelpContents(this.properties, 'monitor', this._router.url).subscribe(contents => {
164
      this.divContents = contents;
165
    })
166
  }
167
  
168
  public get stakeholdersNumber(): number {
169
    if(this.type === null) {
170
      return this.stakeholders.length;
171
    } else {
172
      return this.stakeholders.filter(stakeholder => stakeholder.type === this.type).length;
173
    }
174
  }
175
  
176
  getNumbers() {
177
    this.numberSubs.push(this._refineFieldResultsService.getRefineFieldsResultsByEntityName(["funder"], "project", this.properties).subscribe(
178
      data => {
179
        if (data[1].length > 0 && data[1][0].filterId == "funder" && data[1][0].values) {
180
          this.fundersSize = NumberUtils.roundNumber(data[1][0].values.length);
181
        }
182
      },
183
      err => {
184
        //console.log(err);
185
        this.handleError("Error getting 'funder' field results of projects", err);
186
      }));
187
    
188
    this.numberSubs.push(this._searchResearchResultsService.numOfSearchResults("publication", "", this.properties).subscribe(
189
      data => {
190
        if (data && data > 0) {
191
          this.publicationsSize = NumberUtils.roundNumber(data);
192
        }
193
      },
194
      err => {
195
        //console.log(err);
196
        this.handleError("Error getting number of publications", err);
197
      }
198
    ));
199
    
200
    this.numberSubs.push(this._searchResearchResultsService.numOfSearchResults("dataset", "", this.properties).subscribe(
201
      data => {
202
        if (data && data > 0) {
203
          this.datasetsSize = NumberUtils.roundNumber(data);
204
        }
205
      },
206
      err => {
207
        //console.log(err);
208
        this.handleError("Error getting number of research data", err);
209
      }
210
    ));
211
    
212
    this.numberSubs.push(this._searchResearchResultsService.numOfSearchResults("software", "", this.properties).subscribe(
213
      data => {
214
        if (data && data > 0) {
215
          this.softwareSize = NumberUtils.roundNumber(data);
216
        }
217
      },
218
      err => {
219
        this.handleError("Error getting number of software data", err);
220
      }
221
    ));
222
    
223
    this.numberSubs.push(this._searchResearchResultsService.numOfSearchResults("other", "", this.properties).subscribe(
224
      data => {
225
        if (data && data > 0) {
226
          this.otherSize = NumberUtils.roundNumber(data);
227
        }
228
      },
229
      err => {
230
        this.handleError("Error getting number of software data", err);
231
      }
232
    ));
233
    
234
    
235
  }
236
  
237
  public getStakeholders() {
238
    this.loading = true;
239
    this.status = this.errorCodes.LOADING;
240
    this.subscriberErrorMessage = "";
241
    
242
    
243
    this._stakeholderService.getStakeholders(this.properties.monitorServiceAPIURL).subscribe(
244
      stakeholders => {
245
        if (!stakeholders || stakeholders.length == 0) {
246
          this.status = this.errorCodes.NONE;
247
        } else {
248
          this.stakeholders = stakeholders.filter(stakeholder => stakeholder.isActive && (stakeholder.isPublic || this.isStakeholderManager()));
249
        }
250
        this.loading = false;
251
      },
252
      error => {
253
        this.status = this.handleError("Error getting funders", error);
254
        this.loading = false;
255
      }
256
    );
257
  }
258
  
259
  private isStakeholderManager() {
260
    return Session.isPortalAdministrator(this.user) || Session.isMonitorCurator(this.user) || Session.isCommunityCurator(this.user);
261
  }
262
  
263
  public confirmModalOpen(result: Stakeholder) {
264
    this.selected = result;
265
    this.modal.cancelButton = true;
266
    this.modal.okButton = true;
267
    this.modal.alertTitle = 'You are going to visit ' + result.name + ' Monitor Dashboard';
268
    this.modal.alertMessage = false;
269
    this.modal.okButtonLeft = false;
270
    this.modal.okButtonText = 'Yes';
271
    this.modal.cancelButtonText = 'No';
272
    this.modal.choice = true;
273
    this.modal.open();
274
  }
275
  
276
  public getStakeholderPageUrl(stakeholder: Stakeholder) {
277
    return this.properties.domain + this.properties.baseLink + '/dashboard/' + stakeholder.alias;
278
  }
279
  
280
  public goToPage(data: any) {
281
    if (data.value == true) {
282
      let url = this.getStakeholderPageUrl(this.selected);
283
      this.localStorageService.setCommunityDirectLink(data.choice);
284
      window.open(url, '_blank');
285
    }
286
  }
287
  
288
  public quote(param: string): string {
289
    return StringUtils.quote(param);
290
  }
291
  
292
  public ngOnDestroy() {
293
    if (this.piwiksub) {
294
      this.piwiksub.unsubscribe();
295
    }
296
    this.clearTimeouts();
297
  }
298
  
299
  private handleError(message: string, error): number {
300
    var code = "";
301
    if (!error.status) {
302
      var error = error.json();
303
      code = error.code;
304
    } else {
305
      code = error.status;
306
    }
307
    
308
    console.error("Home Component: " + message, error);
309
    
310
    return this.errorMessages.getErrorCode(code);
311
  }
312
}
(4-4/5)