Project

General

Profile

1
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
2
import {OpenAireJsonldConverterService} from './service/open-aire-jsonld-converter.service';
3
import {JsonldDocumentSerializerService} from './service/jsonld-document-serializer.service';
4
import {DomSanitizer} from '@angular/platform-browser';
5

    
6
@Component({
7
  selector: 'schema2jsonld',
8
  template: `
9
    <div *ngIf="html" [innerHTML]="html"></div>
10
  `
11
})
12
export class Schema2jsonldComponent implements OnInit, OnChanges {
13
  @Input() data; // for project, organization, datasource
14
  @Input() URL;
15
  @Input() logoURL; // for home, search
16
  @Input() otherURL; //for project, datasource
17
  @Input() name;
18
  @Input() searchAction = false;
19
  @Input() type = 'result';
20
  @Input() description = null;
21
  @Input() searchActionRoute = '/search/find/';
22
  public json;
23
  public html;
24
  
25
  constructor(private documentParser: OpenAireJsonldConverterService,
26
              private documentSerializer: JsonldDocumentSerializerService, private sanitizer: DomSanitizer) {
27
    
28
  }
29
  
30
  ngOnChanges(changes: SimpleChanges): void {
31
    if (changes.description) {
32
      this.createJson();
33
    }
34
  }
35
  
36
  ngOnInit() {
37
    this.createJson();
38
  }
39
  
40
  getSafeHTML(value) {
41
    let json = JSON.stringify(value, null, 2);
42
    let html = '<script type="application/ld+json">' + json + '</script>';
43
    return this.sanitizer.bypassSecurityTrustHtml(html);
44
  };
45
  
46
  createJson() {
47
    var docOvject;
48
    if (this.type == 'project') {
49
      docOvject = this.documentParser.convertProject(this.data, this.URL);
50
      this.json = this.documentSerializer.serializeOrganization(docOvject);
51
    } else if (this.type == 'organization') {
52
      docOvject = this.documentParser.convertOrganization(this.data, this.URL, this.description);
53
      this.json = this.documentSerializer.serializeOrganization(docOvject);
54
    } else if (this.type == 'datasource') {
55
      docOvject = this.documentParser.convertDatasource(this.data, this.URL, this.otherURL);
56
      this.json = this.documentSerializer.serializeOrganization(docOvject);
57
    } else if (this.type == 'home') {
58
      this.json = this.documentParser.createHome(this.name, this.URL, this.logoURL, this.description, this.searchActionRoute);
59
    } else if (this.type == 'search') {
60
      this.json = this.documentParser.createSearchPage(this.name, this.URL, this.logoURL, this.searchAction, this.description, this.searchActionRoute);
61
    } else if (this.type == 'result') {
62
      docOvject = this.documentParser.convertResult(this.data, this.URL);
63
      this.json = this.documentSerializer.serializeDataset(docOvject);
64
    } else {
65
      this.json = this.documentParser.createSimplePage(this.name, this.URL, this.description);
66
    }
67
    this.html = this.getSafeHTML(this.json);
68
  }
69
}
(1-1/2)