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
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]="url">
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
      <ul class="uk-list uk-list-divider">
41
        <li *ngFor="let annotation of annotations.slice(0, visibleAnnotations)" uk-grid class="uk-flex uk-flex-top uk-margin-remove-left">
42
          <div [ngClass]="annotation.type" class="uk-width-auto">{{annotation.type}}</div>
43
          <div class="uk-width-auto">{{annotation.text}}</div>
44
          <ul class="uk-width-expand uk-list uk-margin-remove-top" *ngIf="annotation.urls">
45
            <li *ngFor="let url of annotation.urls.slice(0, urlSize)"><a [href]="url" target="_blank">{{url}}</a></li>
46
            <li *ngIf="urlSize < annotation.urls.length"><a (click)="urlSize = annotation.urls.length">+ {{annotation.urls.length - urlSize}} more</a></li>
47
          </ul>
48
        </li>
49
      </ul>
50
      <div *ngIf="visibleAnnotations < annotations.length" class="uk-margin-medium-top uk-text-center">
51
        <button class="uk-button uk-button-default" (click)="visibleAnnotations = (visibleAnnotations + annotationSize)">Load More</button>
52
      </div>
53
    </div>
54
    <div [class.uk-hidden]="!visible">
55
      <div class="widget-container" cdkDrag>
56
        <!--Close button, should be bound to hide the widget-->
57
        <button type="button" class="close" aria-label="Close" (click)="toggleAnnotation($event)">
58
          <span aria-hidden="true">×</span>
59
        </button>
60
        <!--The glorious iframe with the running app!-->
61
        <iframe id="b2note_iframe" name="b2note_iframe" class="b2note-iframe">
62
        </iframe>
63
      </div>
64
    </div>`,
65
  styleUrls: ['annotation.css']
66
})
67
export class AnnotationComponent implements OnInit, OnDestroy {
68
  
69
  @Input()
70
  public landingInfo: ResultLandingInfo = null;
71
  @Input()
72
  public id: string = null;
73
  @Input()
74
  public properties: EnvProperties;
75
  public url: string = null;
76
  public pid: string = null;
77
  public keywords: string[] = [];
78
  public visible: boolean = false;
79
  public annotations: Annotation[] = [];
80
  public annotationSize: number = 10;
81
  public visibleAnnotations: number;
82
  public urlSize: number = 3;
83
  
84
  constructor(private annotationService: AnnotationService) {
85
  }
86
  
87
  ngOnInit(): void {
88
    this.visibleAnnotations = this.annotationSize;
89
    if (typeof window !== "undefined") {
90
      let id = this.id;
91
      this.url = window.location.href;
92
      if (this.landingInfo.deletedByInferenceIds) {
93
        id = this.landingInfo.deletedByInferenceIds[0];
94
        this.url = this.url.replace(this.id, id);
95
      }
96
      if (this.landingInfo.identifiers && this.landingInfo.identifiers.size > 0) {
97
        if (this.landingInfo.identifiers.get('doi')) {
98
          this.pid = this.landingInfo.identifiers.get('doi')[0];
99
        } else {
100
          const key: string = this.landingInfo.identifiers.keys().next().value;
101
          if (key) {
102
            this.pid = this.landingInfo.identifiers.get(key)[0];
103
          }
104
        }
105
      } else {
106
        this.pid = id;
107
      }
108
      this.annotationService.getAllAnnotations(this.properties, this.url).subscribe(annotations => {
109
        this.annotations = annotations;
110
      });
111
    }
112
  }
113
  
114
  ngOnDestroy(): void {
115
  }
116
  
117
  public toggleAnnotation(event) {
118
    if (this.visible) {
119
      event.preventDefault();
120
    }
121
    this.visible = !this.visible;
122
  }
123
}
(1-1/4)