Project

General

Profile

1
import {Component, EventEmitter, Input, Output, SimpleChanges} from '@angular/core';
2
import {ActivatedRoute} from "@angular/router";
3
import {Subscriber} from "rxjs";
4
import {SearchResult} from "../../utils/entities/searchResult";
5
import {EnvProperties} from "../../utils/properties/env-properties";
6
import {RouterHelper} from "../../utils/routerHelper.class";
7
import {ErrorCodes} from "../../utils/properties/errorCodes";
8
import {ResultPreview} from "../../utils/result-preview/result-preview";
9
import {properties} from "../../../../environments/environment";
10
import {Session} from "../../login/utils/helper.class";
11
import {OrcidService} from "../orcid.service";
12

    
13
@Component({
14
  selector: 'my-orcid-result',
15
  templateUrl:'searchMyOrcidResults.component.html'
16
})
17

    
18
export class searcMyOrcidResultsComponent {
19
  @Input() results: SearchResult[];
20
  @Input() status: number;
21
  @Input() type: string;
22
  @Input() properties:EnvProperties;
23

    
24
  @Input() previewResults:ResultPreview[];
25

    
26
  public urlParam: string;
27
  public linkToAdvancedSearchPage: string;
28
  public errorCodes:ErrorCodes = new ErrorCodes();
29
  public routerHelper:RouterHelper = new RouterHelper();
30
  public errorMessage: string = "No results found";
31
  @Output() pageChange  = new EventEmitter();
32
  @Input() currentPage: number = 0;
33
  @Input() totalResults: number;
34
  @Input() resultsPerPage: number = 5;
35

    
36
  sub;
37
  constructor (private  route: ActivatedRoute, private orcidService: OrcidService) {}
38
  ngOnDestroy() {
39
    if (this.sub instanceof Subscriber) {
40
      this.sub.unsubscribe();
41
    }
42
  }
43
  ngOnInit() {
44
    if(this.type == "publication") {
45
      this.linkToAdvancedSearchPage = this.properties.searchLinkToAdvancedPublications;
46
      this.urlParam = "articleId";
47
    } else if(this.type == "dataset") {
48
      this.linkToAdvancedSearchPage = this.properties.searchLinkToAdvancedDatasets;
49
      this.urlParam = "datasetId";
50
    } else if(this.type == "software") {
51
      this.linkToAdvancedSearchPage = this.properties.searchLinkToAdvancedSoftware;
52
      this.urlParam = "softwareId";
53
    } else if(this.type == "other") {
54
      this.linkToAdvancedSearchPage = this.properties.searchLinkToAdvancedOrps;
55
      this.urlParam = "orpId";
56
    }
57

    
58
    this.properties = properties;
59

    
60
  }
61

    
62
  // ngOnChanges(changes: SimpleChanges): void {
63
  //   if (changes.results) {
64
  //     this.initialize();
65
  //   }
66
  // }
67

    
68
  public  quote(params: string):string {
69
    return '"'+params+'"';
70
  }
71
  public getResultPreview(result: SearchResult): ResultPreview {
72
    return ResultPreview.searchResultConvert(result, (result.entityType)?result.entityType:this.type);
73
  }
74

    
75
  initialize() {
76
    this.previewResults = [];
77
    for (let result of this.results) {
78
      this.previewResults.push(this.getResultPreview(result));
79
    }
80

    
81
    if (Session.isLoggedIn() && this.results && this.results.length > 0) {
82
      this.orcidService.getLocalWorksByPids(this.previewResults.map(
83
        previewResult => {
84
          if (previewResult.identifiers) {
85
            let pidsArray: string[] = [];
86
            for (let key of Array.from(previewResult.identifiers.keys())) {
87
              pidsArray = pidsArray.concat(previewResult.identifiers.get(key));
88
            }
89
            return pidsArray;//.join();
90
          }
91
        })).subscribe(
92
        works => {
93
          for (let i = 0; i < this.previewResults.length; i++) {
94
            if (this.previewResults[i].identifiers) {
95
              this.previewResults[i].orcidPutCodes = [];
96
              this.previewResults[i].orcidUpdateDates = [];
97

    
98
              works[i].forEach(work => {
99
                this.previewResults[i].orcidPutCodes.push(work['putCode']);
100
                this.previewResults[i].orcidUpdateDates.push(work['creationDate']);
101
              });
102
              // this.previewResults[i].orcidPutCodes = works[i].map(work => work['putCode']);
103
              // this.previewResults[i].orcidUpdateDates = works[i]
104
              // console.debug(i, this.previewResults[i].orcidPutCodes);
105
            }
106
          }
107
        }, error => {
108

    
109
        }
110
      );
111
    }
112
  }
113

    
114
  public pageChanged($event) {
115
    this.pageChange.emit($event);
116
  }
117

    
118
  public totalPages(totalResults: number): number {
119
    let totalPages:any = totalResults/this.resultsPerPage;
120
    if(!(Number.isInteger(totalPages))) {
121
      totalPages = (parseInt(totalPages, this.resultsPerPage) + 1);
122
    }
123
    return totalPages;
124
  }
125
}
(4-4/5)