import { Component, ElementRef, Inject, Input, OnDestroy, OnInit, RendererFactory2, ViewEncapsulation } from '@angular/core'; import {ActivatedRoute} from '@angular/router'; import {Citation, CitationData} from './citation.class'; import {ResultLandingInfo} from "../../../utils/entities/resultLandingInfo"; import {DOCUMENT} from "@angular/common"; declare var CSL: any; declare var Sys: any; declare var UIkit: any; // @Component({ selector: 'citeThis', template: `
Select a citation style {{style}}
` }) export class CiteThisComponent implements OnInit, OnDestroy { private sub: any; public selectedStyle: string = null; public citationText: string; public citation: Citation = new Citation(); // public cite: any; @Input() result: ResultLandingInfo; @Input() id: string; @Input() type: string = "article"; public citeproc; public data; public clipboard; constructor( @Inject(DOCUMENT) private document, private rendererFactory: RendererFactory2){ } ngOnInit() { try{ if (!this.document.getElementById('citeThisScript')) { 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(' not found within DOCUMENT.'); } const script = renderer.createElement('script'); renderer.setAttribute(script, "id", "citeThisScript"); renderer.setAttribute(script, "src", "assets/common-assets/citeproc.js"); renderer.setAttribute(script, "type", "text/javascript"); renderer.appendChild(head, script); } setTimeout(() => { this.parseData(); this.selectedStyle = this.citation.templates[0]; this.updateCitation(); this.createClipboard(); }, 800); }catch (e) { console.error(e) } } ngOnDestroy() { delete this.clipboard; } private createClipboard() { if (typeof window !== 'undefined') { delete this.clipboard; let Clipboard; Clipboard = require('clipboard'); this.clipboard = new Clipboard('.clipboard_btn'); } } 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; } } } } citationData.id = this.id; if (this.result.types != undefined && this.result.types.length > 0 && this.result.types[0]) { citationData.type = this.result.types[0].toLowerCase(); } 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; } if (this.result.journal.issn) { citationData.ISSN = this.result.journal.issn; } if (this.result.journal.issue) { citationData.issue = this.result.journal.issue; } 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"]; } } } this.data = citationData; // console.log(this.data); } styleChanged() { this.updateCitation(); } updateCitation() { var Sys = function Sys(lang, data) { this.lang = lang; this.data = data; this.changeName = function (name) { this.lastName = name; }; this.retrieveLocale = function (lang) { return this.lang; } this.retrieveItem = function (id) { return this.data; } }; try { var citeproc = new CSL.Engine(new Sys(this.citation.locale, this.data), this.citation[(this.selectedStyle == "0") ? this.citation.templates[0] : this.selectedStyle]); citeproc.updateItems([this.data.id]); this.citationText = citeproc.makeBibliography(); this.citationText = ((this.citationText != null) && (this.citationText.length > 1) && (this.citationText[1].length > 0)) ? this.citationText[1][0] : ''; }catch (e) { } } }