1 |
58780
|
argiro.kok
|
import {
|
2 |
|
|
Component,
|
3 |
|
|
ElementRef,
|
4 |
|
|
Inject,
|
5 |
|
|
Input,
|
6 |
|
|
OnDestroy,
|
7 |
|
|
OnInit,
|
8 |
|
|
RendererFactory2,
|
9 |
|
|
ViewEncapsulation
|
10 |
|
|
} from '@angular/core';
|
11 |
50169
|
argiro.kok
|
import {ActivatedRoute} from '@angular/router';
|
12 |
|
|
import {Citation, CitationData} from './citation.class';
|
13 |
57809
|
argiro.kok
|
import {ResultLandingInfo} from "../../../utils/entities/resultLandingInfo";
|
14 |
58780
|
argiro.kok
|
import {DOCUMENT} from "@angular/common";
|
15 |
58748
|
k.triantaf
|
|
16 |
|
|
declare var CSL: any;
|
17 |
|
|
declare var Sys: any;
|
18 |
58735
|
k.triantaf
|
declare var UIkit: any;
|
19 |
58748
|
k.triantaf
|
|
20 |
58780
|
argiro.kok
|
//<citeThis ></citeThis>
|
21 |
50169
|
argiro.kok
|
@Component({
|
22 |
|
|
selector: 'citeThis',
|
23 |
|
|
template: `
|
24 |
58748
|
k.triantaf
|
<div class="uk-padding">
|
25 |
|
|
<mat-form-field class="matSelectionFormField uk-width-1-1">
|
26 |
|
|
<mat-label>Select a citation style</mat-label>
|
27 |
59257
|
k.triantaf
|
<mat-select class="matSelection" id="citations" name="citeselect" [(ngModel)]="selectedStyle"
|
28 |
58748
|
k.triantaf
|
(ngModelChange)="styleChanged()"
|
29 |
59257
|
k.triantaf
|
[disableOptionCentering]="true"
|
30 |
58748
|
k.triantaf
|
panelClass="matSelectionPanel">
|
31 |
|
|
<mat-option *ngFor=" let style of this.citation.templates let i = index"
|
32 |
|
|
[value]="style">{{style}}</mat-option>
|
33 |
|
|
</mat-select>
|
34 |
|
|
</mat-form-field>
|
35 |
58756
|
k.triantaf
|
<div *ngIf="selectedStyle">
|
36 |
58780
|
argiro.kok
|
<div id="citation" class="box-content uk-margin-small-top uk-overflow-auto uk-padding-small" [innerHTML]=citationText></div>
|
37 |
59169
|
k.triantaf
|
<button class="clipboard_btn uk-button uk-button-primary uk-button-small copy uk-margin-small-top uk-icon uk-float-right"
|
38 |
58748
|
k.triantaf
|
data-clipboard-target="#citation" title="Copy to clipboard">
|
39 |
59169
|
k.triantaf
|
<span class="custom-icon custom-copy-white"></span>
|
40 |
58748
|
k.triantaf
|
<span class="uk-margin-small-left">COPY</span>
|
41 |
|
|
</button>
|
42 |
|
|
</div>
|
43 |
58735
|
k.triantaf
|
</div>
|
44 |
50169
|
argiro.kok
|
`
|
45 |
|
|
})
|
46 |
58748
|
k.triantaf
|
export class CiteThisComponent implements OnInit, OnDestroy {
|
47 |
|
|
private sub: any;
|
48 |
58756
|
k.triantaf
|
public selectedStyle: string = null;
|
49 |
58748
|
k.triantaf
|
public citationText: string;
|
50 |
|
|
public citation: Citation = new Citation();
|
51 |
50169
|
argiro.kok
|
// public cite: any;
|
52 |
57809
|
argiro.kok
|
@Input() result: ResultLandingInfo;
|
53 |
50169
|
argiro.kok
|
@Input() id: string;
|
54 |
58748
|
k.triantaf
|
@Input() type: string = "article";
|
55 |
50169
|
argiro.kok
|
public citeproc;
|
56 |
|
|
public data;
|
57 |
58735
|
k.triantaf
|
public clipboard;
|
58 |
58780
|
argiro.kok
|
constructor( @Inject(DOCUMENT) private document, private rendererFactory: RendererFactory2){
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
}
|
62 |
50169
|
argiro.kok
|
ngOnInit() {
|
63 |
58780
|
argiro.kok
|
try{
|
64 |
|
|
if (!this.document.getElementById('citeThisScript')) {
|
65 |
|
|
const renderer = this.rendererFactory.createRenderer(this.document, {
|
66 |
|
|
id: '-1',
|
67 |
|
|
encapsulation: ViewEncapsulation.None,
|
68 |
|
|
styles: [],
|
69 |
|
|
data: {}
|
70 |
|
|
});
|
71 |
|
|
const head = this.document.head;
|
72 |
|
|
if (head === null) {
|
73 |
|
|
throw new Error('<head> not found within DOCUMENT.');
|
74 |
|
|
}
|
75 |
|
|
const script = renderer.createElement('script');
|
76 |
|
|
renderer.setAttribute(script, "id", "citeThisScript");
|
77 |
|
|
renderer.setAttribute(script, "src", "assets/common-assets/citeproc.js");
|
78 |
|
|
renderer.setAttribute(script, "type", "text/javascript");
|
79 |
|
|
renderer.appendChild(head, script);
|
80 |
|
|
}
|
81 |
|
|
setTimeout(() => {
|
82 |
50169
|
argiro.kok
|
this.parseData();
|
83 |
58756
|
k.triantaf
|
this.selectedStyle = this.citation.templates[0];
|
84 |
50169
|
argiro.kok
|
this.updateCitation();
|
85 |
58735
|
k.triantaf
|
this.createClipboard();
|
86 |
58780
|
argiro.kok
|
}, 800);
|
87 |
|
|
|
88 |
|
|
}catch (e) {
|
89 |
|
|
console.error(e)
|
90 |
50169
|
argiro.kok
|
}
|
91 |
58780
|
argiro.kok
|
}
|
92 |
58731
|
k.triantaf
|
|
93 |
58735
|
k.triantaf
|
ngOnDestroy() {
|
94 |
|
|
delete this.clipboard;
|
95 |
58731
|
k.triantaf
|
}
|
96 |
|
|
|
97 |
58735
|
k.triantaf
|
private createClipboard() {
|
98 |
|
|
if (typeof window !== 'undefined') {
|
99 |
|
|
delete this.clipboard;
|
100 |
|
|
let Clipboard;
|
101 |
|
|
Clipboard = require('clipboard');
|
102 |
|
|
this.clipboard = new Clipboard('.clipboard_btn');
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
|
106 |
58748
|
k.triantaf
|
parseData() {
|
107 |
|
|
var citationData: CitationData = new CitationData();
|
108 |
|
|
|
109 |
|
|
if (this.result.identifiers && Array.from(this.result.identifiers.keys()).length > 0) {
|
110 |
|
|
var keys = Array.from(this.result.identifiers.keys());
|
111 |
|
|
for (var i = 0; i < keys.length; i++) {
|
112 |
|
|
if (keys[i] == "doi") {
|
113 |
|
|
var ids = this.result.identifiers.get(keys[i]);
|
114 |
|
|
for (var j = 0; j < ids.length; j++) {
|
115 |
|
|
citationData.DOI = ids[j];
|
116 |
|
|
break;
|
117 |
51922
|
argiro.kok
|
}
|
118 |
|
|
}
|
119 |
58748
|
k.triantaf
|
}
|
120 |
|
|
}
|
121 |
|
|
citationData.id = this.id;
|
122 |
|
|
if (this.result.types != undefined && this.result.types.length > 0 && this.result.types[0]) {
|
123 |
|
|
citationData.type = this.result.types[0].toLowerCase();
|
124 |
|
|
}
|
125 |
|
|
if (this.result.title) {
|
126 |
|
|
citationData.title = this.result.title;
|
127 |
|
|
}
|
128 |
|
|
if (this.result.publisher) {
|
129 |
|
|
citationData.publisher = this.result.publisher;
|
130 |
|
|
}
|
131 |
|
|
if (this.result.authors) {
|
132 |
|
|
citationData.author = [];
|
133 |
|
|
var max_length = (this.result.authors.length > 10) ? 10 : this.result.authors.length;
|
134 |
|
|
for (var i = 0; i < max_length; i++) {
|
135 |
|
|
if (this.result.authors[i] && this.result.authors[i].fullName && this.result.authors[i].fullName.indexOf(", ") !== -1) {
|
136 |
|
|
citationData.author.push({
|
137 |
|
|
given: this.result.authors[i].fullName.split(", ")[1],
|
138 |
|
|
family: this.result.authors[i].fullName.split(", ")[0],
|
139 |
|
|
'parse-names': true
|
140 |
|
|
});
|
141 |
|
|
} else {
|
142 |
|
|
citationData.author.push({given: "", family: this.result.authors[i].fullName, 'parse-names': true});
|
143 |
50169
|
argiro.kok
|
}
|
144 |
58748
|
k.triantaf
|
// citationData.authors.push(this.result.authors[i]);
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
if (this.result.dateofacceptance != undefined) {
|
148 |
|
|
citationData.issued = {};
|
149 |
|
|
var date: string = (this.result.dateofacceptance.getFullYear()) + ""; // transform to string in case it is an integer
|
150 |
|
|
var dateArray: string[] = (date && (date).indexOf('-') !== -1) ? [date.split('-')[0]] : [date];
|
151 |
|
|
if (dateArray.length < 3) {
|
152 |
|
|
// dateArray.push[1];
|
153 |
|
|
// dateArray.push[1];
|
154 |
|
|
}
|
155 |
|
|
citationData.issued = {"date-parts": [["" + dateArray[0]]]};
|
156 |
|
|
if (this.result.date) {
|
157 |
|
|
citationData.date = this.result.date;
|
158 |
|
|
}
|
159 |
|
|
if (this.result.journal) {
|
160 |
|
|
if (this.result.journal.journal) {
|
161 |
|
|
citationData["container-title"] = this.result.journal.journal;
|
162 |
50169
|
argiro.kok
|
}
|
163 |
58748
|
k.triantaf
|
if (this.result.journal.issn) {
|
164 |
|
|
citationData.ISSN = this.result.journal.issn;
|
165 |
50169
|
argiro.kok
|
}
|
166 |
58748
|
k.triantaf
|
if (this.result.journal.issue) {
|
167 |
|
|
citationData.issue = this.result.journal.issue;
|
168 |
50169
|
argiro.kok
|
}
|
169 |
58748
|
k.triantaf
|
citationData.type = "article-journal"; // in case of APA volume and pages appear only in specific types not just article
|
170 |
|
|
if (this.result.journal.volume) {
|
171 |
|
|
citationData.volume = this.result.journal.volume;
|
172 |
|
|
}
|
173 |
|
|
if (this.result.journal["start_page"] && this.result.journal["end_page"]) {
|
174 |
|
|
citationData.page = this.result.journal["start_page"] + "-" + this.result.journal["end_page"];
|
175 |
|
|
}
|
176 |
50169
|
argiro.kok
|
}
|
177 |
58748
|
k.triantaf
|
}
|
178 |
|
|
|
179 |
|
|
|
180 |
50169
|
argiro.kok
|
this.data = citationData;
|
181 |
|
|
// console.log(this.data);
|
182 |
58748
|
k.triantaf
|
|
183 |
50169
|
argiro.kok
|
}
|
184 |
58748
|
k.triantaf
|
|
185 |
|
|
styleChanged() {
|
186 |
50169
|
argiro.kok
|
this.updateCitation();
|
187 |
58748
|
k.triantaf
|
|
188 |
50169
|
argiro.kok
|
}
|
189 |
58748
|
k.triantaf
|
|
190 |
|
|
updateCitation() {
|
191 |
50169
|
argiro.kok
|
var Sys =
|
192 |
58748
|
k.triantaf
|
function Sys(lang, data) {
|
193 |
|
|
this.lang = lang;
|
194 |
|
|
this.data = data;
|
195 |
|
|
this.changeName = function (name) {
|
196 |
|
|
this.lastName = name;
|
197 |
|
|
};
|
198 |
|
|
this.retrieveLocale = function (lang) {
|
199 |
|
|
|
200 |
|
|
return this.lang;
|
201 |
|
|
}
|
202 |
|
|
this.retrieveItem = function (id) {
|
203 |
|
|
return this.data;
|
204 |
|
|
}
|
205 |
|
|
};
|
206 |
58780
|
argiro.kok
|
try {
|
207 |
|
|
var citeproc = new CSL.Engine(new Sys(this.citation.locale, this.data), this.citation[(this.selectedStyle == "0") ? this.citation.templates[0] : this.selectedStyle]);
|
208 |
|
|
citeproc.updateItems([this.data.id]);
|
209 |
|
|
this.citationText = citeproc.makeBibliography();
|
210 |
|
|
this.citationText = ((this.citationText != null) && (this.citationText.length > 1) && (this.citationText[1].length > 0)) ? this.citationText[1][0] : '';
|
211 |
|
|
}catch (e) {
|
212 |
|
|
|
213 |
|
|
}
|
214 |
58748
|
k.triantaf
|
|
215 |
50169
|
argiro.kok
|
}
|
216 |
58748
|
k.triantaf
|
|
217 |
50169
|
argiro.kok
|
}
|