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