Project

General

Profile

1
import {Component, Input, ElementRef} from '@angular/core';
2
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
3

    
4
@Component({
5
    selector: 'showIdentifiers',
6
    template: `
7

    
8
                <span *ngIf="showAll && countIdentifiers() > pageSize">
9
                    <a (click)="showAll = !showAll;">View less identifiers</a>
10
                </span>
11

    
12
                <ng-container *ngFor="let key of getKeys(identifiers) let i=index">
13
                  <ng-container *ngIf="countSizeOfPreviousIdentifiers(i) < pageSize">
14
                    <ng-container *ngFor="let item of identifiers.get(key) let j=index">
15
                      <span *ngIf="(sizeOfPreviousIdentifiers + j) < pageSize || showAll" class="custom-external custom-icon">
16
                        <a *ngIf="key=='doi'" href="{{doiURL}}{{item}}" target="_blank">{{key}}: {{item}}</a><a
17
                        *ngIf="key=='pmc'" href="{{pmcURL}}{{item}}" target="_blank">{{key}}: {{item}}</a><a
18
                        *ngIf="key=='handle'" href="{{handleURL}}{{item}}" target="_blank">{{key}}: {{item}}</a></span><span
19

    
20
                        *ngIf="((sizeOfPreviousIdentifiers + j) < pageSize || showAll) && ((sizeOfPreviousIdentifiers + j) < countIdentifiers()-1)">{{", "}}</span>
21
                      <span *ngIf="!showAll && (sizeOfPreviousIdentifiers + j)==pageSize"> ... </span>
22
                    </ng-container>
23
                  </ng-container>
24
                </ng-container>
25

    
26
                <span *ngIf="!showAll && countIdentifiers() > pageSize">
27
                    <a (click)="showAll = !showAll;">
28
                        view all {{countIdentifiers()}} identifiers
29
                    </a>
30
                </span>
31
                <span *ngIf="showAll && countIdentifiers() > pageSize">
32
                    <a (click)="showAll = !showAll; scroll()">View less identifiers</a>
33
                </span>
34

    
35

    
36
    `
37
    })
38

    
39
export class ShowIdentifiersComponent {
40
    @Input() identifiers: Map<string, string[]>;
41
    public doiURL: string;
42
    public pmcURL: string;
43
    public handleURL: string;
44
    public showAll: boolean = false;
45
    public sizeOfIdentifiers: number = -1;
46
    public sizeOfPreviousIdentifiers: number = -1;
47
    public pageSize: number = 10;
48

    
49
    constructor (private element: ElementRef) {
50
        this.doiURL = OpenaireProperties.getDoiURL();
51
        this.pmcURL = OpenaireProperties.getPmcURL();
52
        this.handleURL = OpenaireProperties.getHandleURL();
53
    }
54

    
55
    ngOnInit() {}
56

    
57
    public countIdentifiers(): number {
58
      if(this.sizeOfIdentifiers < 0) {
59
        let num: number = 0;
60
        if(this.identifiers != undefined) {
61
            this.identifiers.forEach(function (value, key, map) {
62
                num += value.length;
63
            });
64
        }
65
        this.sizeOfIdentifiers = num;
66
      }
67
      return this.sizeOfIdentifiers;
68
    }
69

    
70
    public countSizeOfPreviousIdentifiers(index: number): number {
71
      let num: number = 0;
72
      let i: number = 0;
73
      if(this.identifiers != undefined) {
74
        this.identifiers.forEach(function (value, key, map) {
75
          if(i < index) {
76
            num += value.length;
77
          }
78
          i++;
79
        });
80
      }
81
      this.sizeOfPreviousIdentifiers= num;
82
      return num;
83
    }
84

    
85
    public scroll() {
86
      console.info("scroll into view");
87
      if (typeof document !== 'undefined') {
88
         this.element.nativeElement.scrollIntoView();
89
      }
90
    }
91
    public getKeys( map) {
92
      return Array.from(map.keys());
93
    }
94
}
(13-13/17)