import {Component, ElementRef, HostListener, Input, OnDestroy, OnInit, ViewChild} from "@angular/core"; import {Annotation, AnnotationService} from "./annotation.service"; import {ResultLandingInfo} from "../../utils/entities/resultLandingInfo"; import {EnvProperties} from "../../utils/properties/env-properties"; import {properties} from "../../../../environments/environment"; import {UserManagementService} from "../../services/user-management.service"; import {COOKIE} from "../../login/utils/helper.class"; import {Subscriber} from "rxjs"; @Component({ selector: 'b2note', template: `
Annotations
`, styleUrls: ['annotation.css'] }) export class AnnotationComponent implements OnInit, OnDestroy { @Input() public landingInfo: ResultLandingInfo = null; @Input() public id: string = null; public properties: EnvProperties = properties; public url: string = null; public pid: string = null; public keywords: string[] = []; public visible: boolean = false; public annotations: Annotation[] = []; public annotationSize: number = 3; public user; public visibleAnnotations: number; public loading: boolean = false; @ViewChild('iframe') iframe: ElementRef; private subscriptions: any[] = []; constructor(private annotationService: AnnotationService, private userManagementService: UserManagementService) { } @HostListener('window:message', ['$event']) public onChange(event) { if (this.properties.b2noteAPIURL.includes(event.origin)) { if (event.data === "B2NOTE loaded") { let token = COOKIE.getCookie('AccessToken'); this.iframe.nativeElement.contentWindow.postMessage({token: token}, this.properties.b2noteAPIURL); } else { this.getAnnotations(); } } } ngOnInit(): void { this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => { this.user = user; })); this.visibleAnnotations = this.annotationSize; if (typeof window !== "undefined") { let id = this.id; this.url = window.location.href; if (this.landingInfo.deletedByInferenceIds) { id = this.landingInfo.deletedByInferenceIds[0]; this.url = this.url.replace(this.id, id); } if (this.landingInfo.identifiers && this.landingInfo.identifiers.size > 0) { if (this.landingInfo.identifiers.get('doi')) { this.pid = this.landingInfo.identifiers.get('doi')[0]; } else { const key: string = this.landingInfo.identifiers.keys().next().value; if (key) { this.pid = this.landingInfo.identifiers.get(key)[0]; } } } if (this.pid) { this.getAnnotations(); } } } private clearSubscriptions() { this.subscriptions.forEach(subscription => { if (subscription instanceof Subscriber) { subscription.unsubscribe(); } }); this.subscriptions = []; } private getAnnotations() { if(!this.annotations || this.annotations.length === 0) { this.loading = true; } this.annotationService.getAllAnnotations(this.properties, this.pid).subscribe(annotations => { this.annotations = annotations; this.annotations.forEach(annotation => { annotation.urlSize = 3; }); this.loading = false; }); } ngOnDestroy(): void { this.clearSubscriptions(); } public toggleAnnotation(event) { if (this.visible) { event.preventDefault(); } this.visible = !this.visible; } }