2020-07-21 14:41:08 +02:00
|
|
|
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
|
2020-03-16 14:09:46 +01:00
|
|
|
import {OpenAireJsonldConverterService} from './service/open-aire-jsonld-converter.service';
|
|
|
|
import {JsonldDocumentSerializerService} from './service/jsonld-document-serializer.service';
|
2021-07-14 13:19:57 +02:00
|
|
|
import {DomSanitizer} from '@angular/platform-browser';
|
2020-03-16 14:09:46 +01:00
|
|
|
|
2018-11-12 16:36:20 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'schema2jsonld',
|
|
|
|
template: `
|
2021-06-04 10:18:01 +02:00
|
|
|
<div *ngIf="html" [innerHTML]="html"></div>
|
2018-11-12 16:36:20 +01:00
|
|
|
`
|
|
|
|
})
|
2021-07-14 13:19:57 +02:00
|
|
|
export class Schema2jsonldComponent implements OnInit, OnChanges {
|
2020-03-16 14:09:46 +01:00
|
|
|
@Input() data; // for project, organization, datasource
|
|
|
|
@Input() URL;
|
|
|
|
@Input() logoURL; // for home, search
|
2022-05-05 19:29:55 +02:00
|
|
|
@Input() otherURL; //for datasource
|
2020-03-16 14:09:46 +01:00
|
|
|
@Input() name;
|
2020-08-10 17:14:48 +02:00
|
|
|
@Input() searchAction = false;
|
2021-07-14 13:19:57 +02:00
|
|
|
@Input() type = 'result';
|
2020-08-10 17:14:48 +02:00
|
|
|
@Input() description = null;
|
2021-07-14 13:19:57 +02:00
|
|
|
@Input() searchActionRoute = '/search/find/';
|
2020-03-16 14:09:46 +01:00
|
|
|
public json;
|
2021-06-04 10:18:01 +02:00
|
|
|
public html;
|
2021-07-14 13:19:57 +02:00
|
|
|
|
2020-03-16 14:09:46 +01:00
|
|
|
constructor(private documentParser: OpenAireJsonldConverterService,
|
2021-06-04 10:18:01 +02:00
|
|
|
private documentSerializer: JsonldDocumentSerializerService, private sanitizer: DomSanitizer) {
|
2020-03-16 14:09:46 +01:00
|
|
|
|
2018-11-12 16:36:20 +01:00
|
|
|
}
|
2021-07-14 13:19:57 +02:00
|
|
|
|
2020-07-21 14:41:08 +02:00
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
if (changes.description) {
|
|
|
|
this.createJson();
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 13:19:57 +02:00
|
|
|
|
2018-11-12 16:36:20 +01:00
|
|
|
ngOnInit() {
|
2020-07-21 14:41:08 +02:00
|
|
|
this.createJson();
|
|
|
|
}
|
2021-07-14 13:19:57 +02:00
|
|
|
|
2021-06-04 10:18:01 +02:00
|
|
|
getSafeHTML(value) {
|
|
|
|
let json = JSON.stringify(value, null, 2);
|
2021-07-14 13:19:57 +02:00
|
|
|
let html = '<script type="application/ld+json">' + json + '</script>';
|
2021-06-04 10:18:01 +02:00
|
|
|
return this.sanitizer.bypassSecurityTrustHtml(html);
|
|
|
|
};
|
2021-07-14 13:19:57 +02:00
|
|
|
|
|
|
|
createJson() {
|
2020-03-16 14:09:46 +01:00
|
|
|
var docOvject;
|
|
|
|
if (this.type == 'project') {
|
2020-07-21 14:41:08 +02:00
|
|
|
docOvject = this.documentParser.convertProject(this.data, this.URL);
|
2020-03-16 14:09:46 +01:00
|
|
|
this.json = this.documentSerializer.serializeOrganization(docOvject);
|
|
|
|
} else if (this.type == 'organization') {
|
2020-07-21 14:41:08 +02:00
|
|
|
docOvject = this.documentParser.convertOrganization(this.data, this.URL, this.description);
|
2020-03-16 14:09:46 +01:00
|
|
|
this.json = this.documentSerializer.serializeOrganization(docOvject);
|
|
|
|
} else if (this.type == 'datasource') {
|
|
|
|
docOvject = this.documentParser.convertDatasource(this.data, this.URL, this.otherURL);
|
|
|
|
this.json = this.documentSerializer.serializeOrganization(docOvject);
|
|
|
|
} else if (this.type == 'home') {
|
2020-08-10 17:14:48 +02:00
|
|
|
this.json = this.documentParser.createHome(this.name, this.URL, this.logoURL, this.description, this.searchActionRoute);
|
2020-03-16 14:09:46 +01:00
|
|
|
} else if (this.type == 'search') {
|
2021-07-14 13:19:57 +02:00
|
|
|
this.json = this.documentParser.createSearchPage(this.name, this.URL, this.logoURL, this.searchAction, this.description, this.searchActionRoute);
|
2020-03-16 14:09:46 +01:00
|
|
|
} else if (this.type == 'result') {
|
|
|
|
docOvject = this.documentParser.convertResult(this.data, this.URL);
|
|
|
|
this.json = this.documentSerializer.serializeDataset(docOvject);
|
|
|
|
} else {
|
2020-07-15 15:59:09 +02:00
|
|
|
this.json = this.documentParser.createSimplePage(this.name, this.URL, this.description);
|
2020-03-16 14:09:46 +01:00
|
|
|
}
|
2021-06-04 10:18:01 +02:00
|
|
|
this.html = this.getSafeHTML(this.json);
|
2018-11-12 16:36:20 +01:00
|
|
|
}
|
|
|
|
}
|