Project

General

Profile

1
import {Component, ElementRef, Input, OnDestroy, OnInit} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {Citation, CitationData} from './citation.class';
4
import {ResultLandingInfo} from "../../../utils/entities/resultLandingInfo";
5
declare var CSL:any;
6
declare var Sys:any;
7
declare var UIkit: any;
8
//<addThis ></addThis>
9
@Component({
10
  selector: 'citeThis',
11
  template: `
12
  <div class="uk-padding">
13
    <mat-select #matSelect class="matSelection" id="citations" name="citeselect" [(ngModel)]="selectedStyle" (ngModelChange)="styleChanged()"
14
                [disableOptionCentering]="true" modal-select [matSelect]="matSelect"
15
                panelClass="matSelectionPanel">
16
      <mat-option value="0">select a citation style</mat-option>
17
      <mat-option *ngFor=" let  style of this.citation.templates let i = index" [value]="style">{{style}}</mat-option>
18
    </mat-select>
19
    <div *ngIf="selectedStyle!='0'">
20
      <div id="citation" class="box-content uk-margin-small-top uk-overflow-auto" [innerHTML]=citationText></div>
21
      <button class="clipboard_btn uk-button uk-button-small uk-button-secondary uk-margin-small-top uk-icon uk-float-right" data-clipboard-target="#citation" title="Copy to clipboard">
22
        <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="copy" ratio="1">
23
          <rect fill="none" stroke="#000" x="3.5" y="2.5" width="12" height="16"></rect>
24
          <polyline fill="none" stroke="#000" points="5 0.5 17.5 0.5 17.5 17"></polyline>
25
        </svg>
26
        <span class="uk-margin-small-left">COPY</span>
27
      </button>
28
    </div>
29
  </div>
30
  `
31
})
32
export class CiteThisComponent implements OnInit, OnDestroy{
33
  private sub:any;
34
  public selectedStyle:string="0";
35
  public citationText:string;
36
  public citation:Citation = new Citation();
37
  // public cite: any;
38
  @Input() result: ResultLandingInfo;
39
  @Input() id: string;
40
  @Input() type: string="article";
41
  public citeproc;
42
  public data;
43
  public clipboard;
44
  
45
  ngOnInit() {
46
    if(typeof window !== 'undefined') {
47
      // this.citeproc = require('./citeproc.js');
48
      // console.log(this.citeproc);
49
      this.parseData();
50
      this.updateCitation();
51
      this.createClipboard();
52
    }
53
  }
54
  
55
  ngOnDestroy() {
56
    delete this.clipboard;
57
  }
58
  
59
  private createClipboard() {
60
    if (typeof window !== 'undefined') {
61
      delete this.clipboard;
62
      let Clipboard;
63
      Clipboard = require('clipboard');
64
      this.clipboard = new Clipboard('.clipboard_btn');
65
    }
66
  }
67
  
68
  parseData(){
69
    var citationData:CitationData = new CitationData();
70

    
71
        if(this.result.identifiers && Array.from(this.result.identifiers.keys()).length > 0){
72
          var keys = Array.from(this.result.identifiers.keys());
73
          for (var i=0; i<keys.length;i++){
74
            if(keys[i]=="doi"){
75
              var ids = this.result.identifiers.get(keys[i]);
76
              for (var j=0; j<ids.length;j++){
77
                citationData.DOI = ids[j];
78
                break;
79
              }
80
            }
81
          }
82
        }
83
        citationData.id = this.id;
84
        if(this.result.types != undefined && this.result.types.length > 0 && this.result.types[0]){
85
          citationData.type = this.result.types[0].toLowerCase();
86
        }
87
        if(this.result.title){
88
          citationData.title = this.result.title;
89
        }
90
        if(this.result.publisher){
91
          citationData.publisher = this.result.publisher;
92
        }
93
        if( this.result.authors){
94
          citationData.author = [];
95
          var max_length = (this.result.authors.length > 10)?10:this.result.authors.length;
96
          for (var i =0 ;i <  max_length; i++){
97
            if(this.result.authors[i] && this.result.authors[i].fullName && this.result.authors[i].fullName.indexOf(", ") !== -1){
98
              citationData.author.push({given:this.result.authors[i].fullName.split(", ")[1], family:this.result.authors[i].fullName.split(", ")[0], 'parse-names':true});
99
            }else{
100
              citationData.author.push({given:"", family:this.result.authors[i].fullName, 'parse-names':true});
101
            }
102
            // citationData.authors.push(this.result.authors[i]);
103
          }
104
        }
105
        if(this.result.dateofacceptance  != undefined){
106
          citationData.issued = {};
107
          var date:string = (this.result.dateofacceptance.getFullYear())+""; // transform to string in case it is an integer
108
          var dateArray:string[]  = (date && (date).indexOf('-') !== -1)?[date.split('-')[0]]:[date];
109
          if(dateArray.length < 3){
110
            // dateArray.push[1];
111
            // dateArray.push[1];
112
          }
113
           citationData.issued={"date-parts":[[""+dateArray[0]]]};
114
           if(this.result.date ){
115
             citationData.date = this.result.date ;
116
           }
117
           if(this.result.journal ){
118
             if(this.result.journal.journal){
119
               citationData["container-title"] = this.result.journal.journal;
120
             }
121
             if(this.result.journal.issn){
122
               citationData.ISSN = this.result.journal.issn;
123
             }
124
             if(this.result.journal.issue){
125
               citationData.issue = this.result.journal.issue;
126
             }
127
             citationData.type = "article-journal"; // in case of APA volume and pages appear only in specific types not just article
128
             if(this.result.journal.volume){
129
               citationData.volume = this.result.journal.volume ;
130
             }
131
             if(this.result.journal["start_page"] && this.result.journal["end_page"]){
132
               citationData.page = this.result.journal["start_page"] +"-" +this.result.journal["end_page"] ;
133
             }
134
           }
135
      }
136

    
137

    
138
    this.data = citationData;
139
    // console.log(this.data);
140

    
141
  }
142
  styleChanged(){
143
    this.updateCitation();
144

    
145
  }
146
  updateCitation(){
147
    var Sys =
148
      function Sys(lang, data){
149
     this.lang = lang;
150
     this.data = data;
151
       this.changeName = function (name) {
152
           this.lastName = name;
153
       };
154
       this.retrieveLocale= function (lang){
155

    
156
         return this.lang;
157
       }
158
       this.retrieveItem= function(id){
159
         return this.data;
160
       }
161
    };
162

    
163
    var citeproc = new CSL.Engine(new Sys(this.citation.locale,  this.data ), this.citation[(this.selectedStyle == "0")?this.citation.templates[0]:this.selectedStyle]);
164
    citeproc.updateItems([this.data.id]);
165
    this.citationText = citeproc.makeBibliography();
166
    this.citationText = ((this.citationText != null) && (this.citationText.length > 1) && (this.citationText[1].length > 0)) ? this.citationText[1][0] : '';
167

    
168
  }
169

    
170
}
(2-2/3)