Project

General

Profile

1
import {Component, Input, ElementRef} from '@angular/core';
2

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

    
7
                <span *ngIf="countIdentifiers() > 0" class="uk-text-bold">Related identifiers:</span>
8

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

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

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

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

    
36

    
37
    `
38
    })
39

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

    
50
    constructor (private element: ElementRef) {
51
        this.doiURL = "https://dx.doi.org/";
52
        this.pmcURL = "http://europepmc.org/articles/";
53
        this.handleURL = "http://hdl.handle.net/";
54
    }
55

    
56
    ngOnInit() {}
57

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

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

    
86
    public scroll() {
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)