Project

General

Profile

1
import {Component, ElementRef, Input} 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
//<addThis ></addThis>
8
@Component({
9
  selector: 'citeThis',
10
  template: `
11

    
12
  <div class="uk-padding-small">
13
    <mat-select class="matSelection" id="citations" name="citeselect" [(ngModel)]="selectedStyle" (ngModelChange)="styleChanged()"
14
                [disableOptionCentering]="true"
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'" id="citation" class="box-content uk-margin-small-top uk-overflow-auto" [innerHTML]=citationText></div>
20
  </div>
21
  `
22
})
23
export class CiteThisComponent {
24
  private sub:any;
25
  public selectedStyle:string="0";
26
  public citationText:string;
27
  public citation:Citation = new Citation();
28
  // public cite: any;
29
  @Input() result: ResultLandingInfo;
30
  @Input() id: string;
31
  @Input() type: string="article";
32
  public citeproc;
33
  public data;
34

    
35

    
36
  constructor(private route: ActivatedRoute) {
37
    // this.selectedStyle = this.citation.templates[0];
38
  }
39

    
40
  ngOnInit() {
41
    if(typeof window !== 'undefined') {
42
      // this.citeproc = require('./citeproc.js');
43
      // console.log(this.citeproc);
44
      this.parseData();
45
      this.updateCitation();
46
    }
47
  }
48
  parseData(){
49
    var citationData:CitationData = new CitationData();
50

    
51
        if(this.result.identifiers && Array.from(this.result.identifiers.keys()).length > 0){
52
          var keys = Array.from(this.result.identifiers.keys());
53
          for (var i=0; i<keys.length;i++){
54
            if(keys[i]=="doi"){
55
              var ids = this.result.identifiers.get(keys[i]);
56
              for (var j=0; j<ids.length;j++){
57
                citationData.DOI = ids[j];
58
                break;
59
              }
60
            }
61
          }
62
        }
63
        citationData.id = this.id;
64
        if(this.result.types != undefined && this.result.types.length > 0 && this.result.types[0]){
65
          citationData.type = this.result.types[0].toLowerCase();
66
        }
67
        if(this.result.title){
68
          citationData.title = this.result.title;
69
        }
70
        if(this.result.publisher){
71
          citationData.publisher = this.result.publisher;
72
        }
73
        if( this.result.authors){
74
          citationData.author = [];
75
          var max_length = (this.result.authors.length > 10)?10:this.result.authors.length;
76
          for (var i =0 ;i <  max_length; i++){
77
            if(this.result.authors[i] && this.result.authors[i].fullName && this.result.authors[i].fullName.indexOf(", ") !== -1){
78
              citationData.author.push({given:this.result.authors[i].fullName.split(", ")[1], family:this.result.authors[i].fullName.split(", ")[0], 'parse-names':true});
79
            }else{
80
              citationData.author.push({given:"", family:this.result.authors[i].fullName, 'parse-names':true});
81
            }
82
            // citationData.authors.push(this.result.authors[i]);
83
          }
84
        }
85
        if(this.result.dateofacceptance  != undefined){
86
          citationData.issued = {};
87
          var date:string = (this.result.dateofacceptance.getFullYear())+""; // transform to string in case it is an integer
88
          var dateArray:string[]  = (date && (date).indexOf('-') !== -1)?[date.split('-')[0]]:[date];
89
          if(dateArray.length < 3){
90
            // dateArray.push[1];
91
            // dateArray.push[1];
92
          }
93
           citationData.issued={"date-parts":[[""+dateArray[0]]]};
94
           if(this.result.date ){
95
             citationData.date = this.result.date ;
96
           }
97
           if(this.result.journal ){
98
             if(this.result.journal.journal){
99
               citationData["container-title"] = this.result.journal.journal;
100
             }
101
             if(this.result.journal.issn){
102
               citationData.ISSN = this.result.journal.issn;
103
             }
104
             if(this.result.journal.issue){
105
               citationData.issue = this.result.journal.issue;
106
             }
107
             citationData.type = "article-journal"; // in case of APA volume and pages appear only in specific types not just article
108
             if(this.result.journal.volume){
109
               citationData.volume = this.result.journal.volume ;
110
             }
111
             if(this.result.journal["start_page"] && this.result.journal["end_page"]){
112
               citationData.page = this.result.journal["start_page"] +"-" +this.result.journal["end_page"] ;
113
             }
114
           }
115
      }
116

    
117

    
118
    this.data = citationData;
119
    // console.log(this.data);
120

    
121
  }
122
  styleChanged(){
123
    this.updateCitation();
124

    
125
  }
126
  updateCitation(){
127
    var Sys =
128
      function Sys(lang, data){
129
     this.lang = lang;
130
     this.data = data;
131
       this.changeName = function (name) {
132
           this.lastName = name;
133
       };
134
       this.retrieveLocale= function (lang){
135

    
136
         return this.lang;
137
       }
138
       this.retrieveItem= function(id){
139
         return this.data;
140
       }
141
    };
142

    
143
    var citeproc = new CSL.Engine(new Sys(this.citation.locale,  this.data ), this.citation[(this.selectedStyle == "0")?this.citation.templates[0]:this.selectedStyle]);
144
    citeproc.updateItems([this.data.id]);
145
    this.citationText = citeproc.makeBibliography();
146
    this.citationText = ((this.citationText != null) && (this.citationText.length > 1) && (this.citationText[1].length > 0)) ? this.citationText[1][0] : '';
147

    
148
  }
149

    
150
}
(2-2/3)