Project

General

Profile

1
import {
2
  Component,
3
  ElementRef,
4
  Inject,
5
  Input,
6
  OnDestroy,
7
  OnInit,
8
  RendererFactory2,
9
  ViewEncapsulation
10
} from '@angular/core';
11
import {ActivatedRoute} from '@angular/router';
12
import {Citation, CitationData} from './citation.class';
13
import {ResultLandingInfo} from "../../../utils/entities/resultLandingInfo";
14
import {DOCUMENT} from "@angular/common";
15

    
16
declare var CSL: any;
17
declare var Sys: any;
18
declare var UIkit: any;
19

    
20
//<citeThis ></citeThis>
21
@Component({
22
  selector: 'citeThis',
23
  template: `
24
    <div class="uk-padding">
25
      <mat-form-field class="matSelectionFormField uk-width-1-1">
26
        <mat-label>Select a citation style</mat-label>
27
        <mat-select class="matSelection" id="citations" name="citeselect" [(ngModel)]="selectedStyle"
28
                    (ngModelChange)="styleChanged()"
29
                    [disableOptionCentering]="true"
30
                    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
      <div *ngIf="selectedStyle">
36
        <div id="citation" class="box-content uk-margin-small-top uk-overflow-auto uk-padding-small" [innerHTML]=citationText></div>
37
        <button class="clipboard_btn uk-button uk-button-primary uk-button-small copy uk-margin-small-top uk-icon uk-float-right"
38
            data-clipboard-target="#citation" title="Copy to clipboard">
39
          <span class="custom-icon custom-copy-white"></span>
40
          <span class="uk-margin-small-left">COPY</span>
41
        </button>
42
      </div>
43
    </div>
44
  `
45
})
46
export class CiteThisComponent implements OnInit, OnDestroy {
47
  private sub: any;
48
  public selectedStyle: string = null;
49
  public citationText: string;
50
  public citation: Citation = new Citation();
51
  // public cite: any;
52
  @Input() result: ResultLandingInfo;
53
  @Input() id: string;
54
  @Input() type: string = "article";
55
  public citeproc;
56
  public data;
57
  public clipboard;
58
  constructor( @Inject(DOCUMENT) private document, private rendererFactory: RendererFactory2){
59

    
60

    
61
}
62
  ngOnInit() {
63
    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
      this.parseData();
83
      this.selectedStyle = this.citation.templates[0];
84
      this.updateCitation();
85
      this.createClipboard();
86
    }, 800);
87

    
88
  }catch (e) {
89
    console.error(e)
90
  }
91
  }
92
  
93
  ngOnDestroy() {
94
    delete this.clipboard;
95
  }
96
  
97
  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
  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
          }
118
        }
119
      }
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
        }
144
        // 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
        }
163
        if (this.result.journal.issn) {
164
          citationData.ISSN = this.result.journal.issn;
165
        }
166
        if (this.result.journal.issue) {
167
          citationData.issue = this.result.journal.issue;
168
        }
169
        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
      }
177
    }
178
    
179
    
180
    this.data = citationData;
181
    // console.log(this.data);
182
    
183
  }
184
  
185
  styleChanged() {
186
    this.updateCitation();
187
    
188
  }
189
  
190
  updateCitation() {
191
    var Sys =
192
      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
    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
    
215
  }
216
  
217
}
(2-2/3)