2020-05-26 12:16:03 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
Inject,
|
|
|
|
Input,
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
RendererFactory2,
|
|
|
|
ViewEncapsulation
|
|
|
|
} from '@angular/core';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {Citation, CitationData} from './citation.class';
|
2019-12-04 14:32:36 +01:00
|
|
|
import {ResultLandingInfo} from "../../../utils/entities/resultLandingInfo";
|
2020-05-26 12:16:03 +02:00
|
|
|
import {DOCUMENT} from "@angular/common";
|
2020-12-23 16:25:11 +01:00
|
|
|
import {EnvProperties} from "../../../utils/properties/env-properties";
|
|
|
|
import {properties} from "../../../../../environments/environment";
|
2021-02-10 13:50:02 +01:00
|
|
|
import {PiwikService} from "../../../utils/piwik/piwik.service";
|
2020-05-22 16:51:28 +02:00
|
|
|
|
2020-12-23 16:25:11 +01:00
|
|
|
declare var Cite: any;
|
|
|
|
// Based on https://citation.js.org/api/tutorial-getting_started.html browser release
|
2020-05-26 12:16:03 +02:00
|
|
|
//<citeThis ></citeThis>
|
2017-12-19 13:53:46 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'citeThis',
|
|
|
|
template: `
|
2020-05-22 16:51:28 +02:00
|
|
|
<div class="uk-padding">
|
|
|
|
<mat-form-field class="matSelectionFormField uk-width-1-1">
|
|
|
|
<mat-label>Select a citation style</mat-label>
|
2020-08-12 16:25:43 +02:00
|
|
|
<mat-select class="matSelection" id="citations" name="citeselect" [(ngModel)]="selectedStyle"
|
2020-05-22 16:51:28 +02:00
|
|
|
(ngModelChange)="styleChanged()"
|
2020-08-12 16:25:43 +02:00
|
|
|
[disableOptionCentering]="true"
|
2020-05-22 16:51:28 +02:00
|
|
|
panelClass="matSelectionPanel">
|
|
|
|
<mat-option *ngFor=" let style of this.citation.templates let i = index"
|
|
|
|
[value]="style">{{style}}</mat-option>
|
|
|
|
</mat-select>
|
|
|
|
</mat-form-field>
|
2021-02-01 10:46:24 +01:00
|
|
|
<div *ngIf="selectedStyle" class="box-content uk-grid uk-margin-auto ">
|
|
|
|
<div id="citation" class=" uk-overflow-auto uk-padding-small" [innerHTML]=citationText></div>
|
|
|
|
<div class="uk-width-1-1">
|
|
|
|
<button class="clipboard_btn uk-button uk-button-primary uk-button-small copy uk-icon uk-float-right"
|
|
|
|
data-clipboard-target="#citation" title="Copy to clipboard">
|
|
|
|
<span class="custom-icon custom-copy-white"></span>
|
|
|
|
<!-- <span class="uk-margin-small-left">COPY</span>-->
|
|
|
|
</button>
|
|
|
|
</div>
|
2020-05-22 16:51:28 +02:00
|
|
|
</div>
|
2021-02-01 10:46:24 +01:00
|
|
|
<div class="uk-margin-top">
|
2020-12-23 16:25:11 +01:00
|
|
|
<mat-form-field class="matSelectionFormField uk-width-1-1">
|
|
|
|
<mat-label>Or select file format to download</mat-label>
|
|
|
|
<mat-select class="matSelection" id="fileformats" name="fileformatsSelect" [(ngModel)]="selectedFormat"
|
|
|
|
(ngModelChange)="formatChanged()"
|
|
|
|
[disableOptionCentering]="true"
|
|
|
|
panelClass="matSelectionPanel">
|
|
|
|
<mat-option *ngFor=" let format of citation.fileFormats let i = index"
|
|
|
|
[value]="format">{{format + " (" +citation.fileSuffix[i] + ")"}}</mat-option>
|
|
|
|
</mat-select>
|
|
|
|
</mat-form-field>
|
2021-02-01 10:46:24 +01:00
|
|
|
<button *ngIf="selectedFormat" class=" uk-button uk-button-primary uk-button-small uk-margin-small-top uk-icon uk-float-right"
|
2020-12-23 16:25:11 +01:00
|
|
|
title="Copy to clipboard" [disabled]="!selectedFormat" (click)="downloadFile()">
|
|
|
|
<span class="custom-icon " uk-icon="download"></span>
|
|
|
|
<span class="uk-margin-small-left">Download</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
2020-05-20 22:34:29 +02:00
|
|
|
</div>
|
2017-12-19 13:53:46 +01:00
|
|
|
`
|
|
|
|
})
|
2020-05-22 16:51:28 +02:00
|
|
|
export class CiteThisComponent implements OnInit, OnDestroy {
|
2020-05-25 13:12:16 +02:00
|
|
|
public selectedStyle: string = null;
|
2020-12-23 16:25:11 +01:00
|
|
|
public selectedFormat: string = null;
|
|
|
|
public fileText: string;
|
|
|
|
properties:EnvProperties;
|
2020-05-22 16:51:28 +02:00
|
|
|
public citationText: string;
|
|
|
|
public citation: Citation = new Citation();
|
2019-12-04 14:32:36 +01:00
|
|
|
@Input() result: ResultLandingInfo;
|
2017-12-19 13:53:46 +01:00
|
|
|
@Input() id: string;
|
2020-05-22 16:51:28 +02:00
|
|
|
@Input() type: string = "article";
|
2021-02-10 13:50:02 +01:00
|
|
|
@Input() piwikSiteId;
|
2020-12-23 16:25:11 +01:00
|
|
|
public data:CitationData;
|
2020-05-20 22:34:29 +02:00
|
|
|
public clipboard;
|
2020-11-11 15:43:13 +01:00
|
|
|
timeout;
|
2020-12-23 16:25:11 +01:00
|
|
|
cite;citeConfig;
|
2021-02-10 13:50:02 +01:00
|
|
|
sub;
|
|
|
|
constructor( @Inject(DOCUMENT) private document, private rendererFactory: RendererFactory2, private _piwikService: PiwikService){
|
2020-05-26 12:16:03 +02:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
ngOnInit() {
|
2020-12-23 16:25:11 +01:00
|
|
|
this.properties = properties;
|
2020-05-26 12:16:03 +02:00
|
|
|
try{
|
2020-12-23 16:25:11 +01:00
|
|
|
if (!this.document.getElementById('citationjs')) {
|
|
|
|
const renderer = this.rendererFactory.createRenderer(this.document, {
|
|
|
|
id: '-1',
|
|
|
|
encapsulation: ViewEncapsulation.None,
|
|
|
|
styles: [],
|
|
|
|
data: {}
|
|
|
|
});
|
|
|
|
const head = this.document.head;
|
|
|
|
if (head === null) {
|
|
|
|
throw new Error('<head> not found within DOCUMENT.');
|
|
|
|
}
|
|
|
|
const script = renderer.createElement('script');
|
|
|
|
renderer.setAttribute(script, "id", "citationjs");
|
|
|
|
renderer.setAttribute(script, "src", "assets/common-assets/citation-0.4.0-9.min.js");
|
|
|
|
renderer.setAttribute(script, "type", "text/javascript");
|
|
|
|
renderer.appendChild(head, script);
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
const script2 = renderer.createElement('script');
|
2021-02-01 10:46:24 +01:00
|
|
|
renderer.setAttribute(script2, "id", "citationjs2");
|
2020-12-23 16:25:11 +01:00
|
|
|
renderer.setAttribute(script2, "type", "text/javascript");
|
|
|
|
renderer.setProperty(script2, "text","const Cite = require('citation-js')");
|
|
|
|
renderer.appendChild(head, script2);
|
2021-02-01 10:46:24 +01:00
|
|
|
}, 500);
|
2020-05-26 12:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-05-20 14:56:21 +02:00
|
|
|
|
2020-05-20 22:34:29 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
delete this.clipboard;
|
2020-11-11 15:43:13 +01:00
|
|
|
clearTimeout(this.timeout);
|
2020-05-20 22:34:29 +02:00
|
|
|
}
|
2021-02-01 10:46:24 +01:00
|
|
|
private initiate(){
|
|
|
|
this.citeConfig = Cite.plugins.config.get('csl');
|
|
|
|
this.cite = new Cite();
|
|
|
|
this.parseData();
|
|
|
|
this.createClipboard();
|
|
|
|
}
|
2020-05-20 22:34:29 +02:00
|
|
|
private createClipboard() {
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
delete this.clipboard;
|
|
|
|
let Clipboard;
|
|
|
|
Clipboard = require('clipboard');
|
|
|
|
this.clipboard = new Clipboard('.clipboard_btn');
|
|
|
|
}
|
2020-05-20 14:56:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:51:28 +02:00
|
|
|
parseData() {
|
|
|
|
var citationData: CitationData = new CitationData();
|
|
|
|
|
|
|
|
if (this.result.identifiers && Array.from(this.result.identifiers.keys()).length > 0) {
|
|
|
|
var keys = Array.from(this.result.identifiers.keys());
|
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
if (keys[i] == "doi") {
|
|
|
|
var ids = this.result.identifiers.get(keys[i]);
|
|
|
|
for (var j = 0; j < ids.length; j++) {
|
|
|
|
citationData.DOI = ids[j];
|
|
|
|
break;
|
2018-05-03 16:05:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 16:51:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
citationData.id = this.id;
|
2020-12-23 16:25:11 +01:00
|
|
|
citationData.type = this.mapTypes();
|
2020-05-22 16:51:28 +02:00
|
|
|
if (this.result.title) {
|
|
|
|
citationData.title = this.result.title;
|
|
|
|
}
|
|
|
|
if (this.result.publisher) {
|
|
|
|
citationData.publisher = this.result.publisher;
|
|
|
|
}
|
|
|
|
if (this.result.authors) {
|
|
|
|
citationData.author = [];
|
|
|
|
var max_length = (this.result.authors.length > 10) ? 10 : this.result.authors.length;
|
|
|
|
for (var i = 0; i < max_length; i++) {
|
|
|
|
if (this.result.authors[i] && this.result.authors[i].fullName && this.result.authors[i].fullName.indexOf(", ") !== -1) {
|
|
|
|
citationData.author.push({
|
|
|
|
given: this.result.authors[i].fullName.split(", ")[1],
|
|
|
|
family: this.result.authors[i].fullName.split(", ")[0],
|
|
|
|
'parse-names': true
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
citationData.author.push({given: "", family: this.result.authors[i].fullName, 'parse-names': true});
|
|
|
|
}
|
|
|
|
// citationData.authors.push(this.result.authors[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.result.dateofacceptance != undefined) {
|
|
|
|
citationData.issued = {};
|
|
|
|
var date: string = (this.result.dateofacceptance.getFullYear()) + ""; // transform to string in case it is an integer
|
|
|
|
var dateArray: string[] = (date && (date).indexOf('-') !== -1) ? [date.split('-')[0]] : [date];
|
|
|
|
if (dateArray.length < 3) {
|
|
|
|
// dateArray.push[1];
|
|
|
|
// dateArray.push[1];
|
|
|
|
}
|
|
|
|
citationData.issued = {"date-parts": [["" + dateArray[0]]]};
|
|
|
|
if (this.result.date) {
|
|
|
|
citationData.date = this.result.date;
|
|
|
|
}
|
|
|
|
if (this.result.journal) {
|
|
|
|
if (this.result.journal.journal) {
|
|
|
|
citationData["container-title"] = this.result.journal.journal;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-05-22 16:51:28 +02:00
|
|
|
if (this.result.journal.issn) {
|
|
|
|
citationData.ISSN = this.result.journal.issn;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-05-22 16:51:28 +02:00
|
|
|
if (this.result.journal.issue) {
|
|
|
|
citationData.issue = this.result.journal.issue;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-05-22 16:51:28 +02:00
|
|
|
citationData.type = "article-journal"; // in case of APA volume and pages appear only in specific types not just article
|
|
|
|
if (this.result.journal.volume) {
|
|
|
|
citationData.volume = this.result.journal.volume;
|
|
|
|
}
|
|
|
|
if (this.result.journal["start_page"] && this.result.journal["end_page"]) {
|
|
|
|
citationData.page = this.result.journal["start_page"] + "-" + this.result.journal["end_page"];
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 16:51:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
this.data = citationData;
|
2020-12-23 16:25:11 +01:00
|
|
|
this.cite.set(this.data);
|
2020-05-22 16:51:28 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-12-23 16:25:11 +01:00
|
|
|
private mapTypes(){
|
|
|
|
if (this.result.types != undefined && this.result.types.length > 0) {
|
|
|
|
if(this.result.types.indexOf("Article") != -1) {
|
|
|
|
return "article";
|
|
|
|
}else if(this.result.types.indexOf("Conference object") != -1){
|
|
|
|
return "paper-conference";
|
|
|
|
}else if( this.result.types[0]){
|
|
|
|
return this.result.types[0].toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2020-05-22 16:51:28 +02:00
|
|
|
styleChanged() {
|
2020-12-23 16:25:11 +01:00
|
|
|
this.citationText = this.get(this.selectedStyle);
|
2020-05-22 16:51:28 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-12-23 16:25:11 +01:00
|
|
|
formatChanged() {
|
|
|
|
this.fileText = this.get(this.selectedFormat, false);
|
|
|
|
}
|
|
|
|
downloadFile(){
|
|
|
|
if (typeof document !== 'undefined') {
|
|
|
|
var url = window.URL.createObjectURL(new Blob([this.fileText]));
|
|
|
|
var a = window.document.createElement('a');
|
|
|
|
window.document.body.appendChild(a);
|
|
|
|
a.setAttribute('style', 'display: none');
|
|
|
|
a.href = url;
|
|
|
|
a.download = this.selectedFormat + this.citation.fileSuffix[this.citation.fileFormats.indexOf(this.selectedFormat)];
|
|
|
|
a.click();
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
a.remove(); // remove the element
|
2021-02-10 13:50:02 +01:00
|
|
|
if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
|
|
|
|
this.sub = this._piwikService.trackDownload(this.properties, "DownloadCitation"+this.selectedFormat, this.piwikSiteId).subscribe();
|
|
|
|
}
|
2020-12-23 16:25:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(style:string, isBibliography:boolean = true){
|
2020-05-26 12:16:03 +02:00
|
|
|
try {
|
2020-12-23 16:25:11 +01:00
|
|
|
if (!isBibliography && style != "bibtex") {
|
|
|
|
return this.getFormat(style);
|
|
|
|
} else {
|
|
|
|
return this.getBibliography(style);
|
|
|
|
}
|
2020-05-26 12:16:03 +02:00
|
|
|
}catch (e) {
|
2020-12-23 16:25:11 +01:00
|
|
|
console.error(e);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Supports formats bibtex, bibtxt, ris
|
|
|
|
* @param format
|
|
|
|
*/
|
|
|
|
getFormat(format:string) {
|
2021-02-01 10:46:24 +01:00
|
|
|
if(!this.cite){
|
|
|
|
this.initiate();
|
|
|
|
}
|
2020-12-23 16:25:11 +01:00
|
|
|
return this.cite.format(format, {
|
|
|
|
format: 'text'
|
|
|
|
});
|
|
|
|
}
|
2020-05-26 12:16:03 +02:00
|
|
|
|
2020-12-23 16:25:11 +01:00
|
|
|
/**
|
|
|
|
* Based on this and citeproc library https://citation.js.org/api/tutorial-plugins.html
|
|
|
|
* if the template doesn;t exist add the CSL xml and the library will produce the citation
|
|
|
|
* @param template
|
|
|
|
*/
|
|
|
|
|
|
|
|
getBibliography(template:string) {
|
2021-02-01 10:46:24 +01:00
|
|
|
if(!this.cite || !this.citeConfig ){
|
|
|
|
this.initiate();
|
|
|
|
}
|
2020-12-23 16:25:11 +01:00
|
|
|
if (!this.citeConfig.templates.get(template)) {
|
|
|
|
this.citeConfig.templates.add(template, this.citation[template]);
|
2020-05-26 12:16:03 +02:00
|
|
|
}
|
2020-12-23 16:25:11 +01:00
|
|
|
return this.cite.format('bibliography', {
|
|
|
|
format: 'text',
|
|
|
|
template: template,
|
|
|
|
lang: 'en-US'
|
|
|
|
});
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|