Project

General

Profile

1
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
2
import {Annotation, AnnotationService} from "./annotation.service";
3
import {ResultLandingInfo} from "../../utils/entities/resultLandingInfo";
4
import {EnvProperties} from "../../utils/properties/env-properties";
5

    
6
@Component({
7
  selector: 'b2note',
8
  template: `
9
    <div class="sideInfoTitle uk-margin-small-bottom">Annotations</div>
10
    <div class="b2note">
11
      <form ngNoForm *ngIf="pid"
12
            action="https://b2note-dev.bsc.es/widget/"
13
            method="post"
14
            target="b2note_iframe"
15
            class="uk-padding-small uk-padding-remove-vertical">
16
        <!--URL of the annotated record-->
17
        <input
18
            type="hidden"
19
            name="recordurl_tofeed"
20
            [value]="url">
21
        <!--URL of the record contents for downloading-->
22
        <input
23
            type="hidden"
24
            name="subject_tofeed"
25
            [value]="pid">
26
        <!--PID of the annotated record-->
27
        <input
28
            type="hidden"
29
            name="pid_tofeed"
30
            [value]="pid">
31
        <!--URL of the record contents for downloading-->
32
        <button class="uk-flex uk-flex-middle"
33
                (click)="toggleAnnotation($event)"
34
                type="submit"
35
                title="Click to annotate this page using B2Note.">
36
          <img src="assets/common-assets/b2note.png" width="48" height="24">
37
          <span>add annotation</span>
38
        </button>
39
      </form>
40
      <div *ngIf="!pid">
41
        <button class="uk-flex uk-flex-middle disabled" title="Annotations are only available for resources with a PID (persistent identifier) like DOI, handle, PMID">
42
          <img src="assets/common-assets/b2note.png" width="48" height="24">
43
          <span>add annotation</span>
44
        </button>
45
      </div>
46
      <ul *ngIf="annotations" class="uk-list uk-list-divider">
47
        <li *ngFor="let annotation of annotations.slice(0, visibleAnnotations)" uk-grid class="uk-flex uk-flex-top uk-margin-remove-left">
48
          <div [ngClass]="annotation.type" class="uk-width-auto">{{annotation.type}}</div>
49
          <div [class.uk-width-1-3]="annotation.urls" [class.uk-width-1-6@s]="annotation.urls">{{annotation.text}}</div>
50
          <ul class="uk-width-expand uk-list uk-margin-remove-top" *ngIf="annotation.urls">
51
            <li *ngFor="let url of annotation.urls.slice(0, annotation.urlSize)"><a [href]="url" target="_blank">{{url}}</a></li>
52
            <li *ngIf="annotation.urlSize < annotation.urls.length"><a (click)="annotation.urlSize = annotation.urls.length">+ {{annotation.urls.length - annotation.urlSize}} more</a></li>
53
          </ul>
54
        </li>
55
      </ul>
56
      <div *ngIf="visibleAnnotations < annotations.length" class="uk-margin-medium-top uk-text-center">
57
        <button class="uk-button uk-button-primary" (click)="visibleAnnotations = (visibleAnnotations + annotationSize)">Load More</button>
58
      </div>
59
    </div>
60
    <div [class.uk-hidden]="!visible">
61
      <div class="widget-container" cdkDrag>
62
        <!--Close button, should be bound to hide the widget-->
63
        <button type="button" class="close" aria-label="Close" (click)="toggleAnnotation($event)">
64
          <span aria-hidden="true">×</span>
65
        </button>
66
        <!--The glorious iframe with the running app!-->
67
        <iframe id="b2note_iframe" name="b2note_iframe" class="b2note-iframe">
68
        </iframe>
69
      </div>
70
    </div>`,
71
  styleUrls: ['annotation.css']
72
})
73
export class AnnotationComponent implements OnInit, OnDestroy {
74
  
75
  @Input()
76
  public landingInfo: ResultLandingInfo = null;
77
  @Input()
78
  public id: string = null;
79
  @Input()
80
  public properties: EnvProperties;
81
  public url: string = null;
82
  public pid: string = null;
83
  public keywords: string[] = [];
84
  public visible: boolean = false;
85
  public annotations: Annotation[] = [];
86
  public annotationSize: number = 3;
87
  public visibleAnnotations: number;
88
  
89
  constructor(private annotationService: AnnotationService) {
90
  }
91
  
92
  ngOnInit(): void {
93
    this.visibleAnnotations = this.annotationSize;
94
    if (typeof window !== "undefined") {
95
      let id = this.id;
96
      this.url = window.location.href;
97
      if (this.landingInfo.deletedByInferenceIds) {
98
        id = this.landingInfo.deletedByInferenceIds[0];
99
        this.url = this.url.replace(this.id, id);
100
      }
101
      if (this.landingInfo.identifiers && this.landingInfo.identifiers.size > 0) {
102
        if (this.landingInfo.identifiers.get('doi')) {
103
          this.pid = this.landingInfo.identifiers.get('doi')[0];
104
        } else {
105
          const key: string = this.landingInfo.identifiers.keys().next().value;
106
          if (key) {
107
            this.pid = this.landingInfo.identifiers.get(key)[0];
108
          }
109
        }
110
      }
111
      if(this.pid) {
112
        this.annotationService.getAllAnnotations(this.properties, this.pid).subscribe(annotations => {
113
          this.annotations = annotations;
114
          this.annotations.forEach(annotation => {
115
            annotation.urlSize = 3;
116
          });
117
        });
118
      }
119
    }
120
  }
121
  
122
  public getTypeName(): string {
123
    if (this.landingInfo.resultType === "dataset") {
124
      return "research data";
125
    } else if (this.landingInfo.resultType === "other") {
126
      return "research product";
127
    } else {
128
      return this.landingInfo.resultType;
129
    }
130
  }
131
  
132
  ngOnDestroy(): void {
133
  }
134
  
135
  public toggleAnnotation(event) {
136
    if (this.visible) {
137
      event.preventDefault();
138
    }
139
    this.visible = !this.visible;
140
  }
141
}
(1-1/4)