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
declare var CSL:any;
5
declare var Sys:any;
6
//<addThis ></addThis>
7
@Component({
8
  selector: 'citeThis',
9
  template: `
10

    
11
  <div class="citationDownloader ">
12
    <dl class="uk-description-list-line">
13
      <!--dt class="title">Cite this {{type}}</dt-->
14
      <dd class="line"  >
15
        <select class="select" id="citations" name="citeselect" [(ngModel)]="selectedStyle" (ngModelChange)="styleChanged()">
16
          <option *ngFor=" let  style of this.citation.templates let i = index" [value]="style">{{style}}</option>
17
        </select>
18
        <div id="citation" class="box-content uk-padding-small" [innerHTML]=citationText></div>
19
      </dd>
20
    </dl>
21
  </div>
22
  `
23
})
24
export class CiteThisComponent {
25
  private sub:any;
26
  public selectedStyle:string;
27
  public citationText:string;
28
  public citation:Citation = new Citation();
29
  // public cite: any;
30
  @Input() result: any;
31
  @Input() id: string;
32
  @Input() type: string="article";
33
  public citeproc;
34
  public data;
35

    
36

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

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

    
52
        if(this.result.identifiers && Array.from(this.result.identifiers.keys()).length > 0){
53
          var keys = Array.from(this.result.identifiers.keys());
54
          for (var i=0; i<keys.length;i++){
55
            if(keys[i]=="doi"){
56
              var ids = this.result.identifiers.get(keys[i]);
57
              for (var j=0; j<ids.length;j++){
58
                citationData.DOI = ids[j];
59
                break;
60
              }
61
            }
62
          }
63
        }
64
        citationData.id = this.id;
65
        if(this.result.types != undefined && this.result.types.length > 0 && this.result.types[0]){
66
          citationData.type = this.result.types[0].toLowerCase();
67
        }else if(this.result.type != undefined ){
68
          citationData.type = this.result.type.toLowerCase();
69
        }
70
        if(this.result.title && this.result.title.name){
71
          citationData.title = this.result.title.name;
72
        }
73
        if(this.result.journal && this.result.journal.journal){
74
          citationData["container-title"] = this.result.journal.journal;
75
        }
76
        if(this.result.publisher){
77
          citationData.publisher = this.result.publisher;
78
        }
79
        if( this.result.authors){
80
          citationData.author = [];
81
          var max_length = (this.result.authors.length > 10)?10:this.result.authors.length;
82
          for (var i =0 ;i <  max_length; i++){
83
            if(this.result.authors[i] && this.result.authors[i].indexOf(", ") !== -1){
84
              citationData.author.push({given:this.result.authors[i].split(", ")[0], family:this.result.authors[i].split(", ")[1], 'parse-names':true});
85
            }else{
86
              citationData.author.push({given:"", family:this.result.authors[i], 'parse-names':true});
87
            }
88
            // citationData.authors.push(this.result.authors[i]);
89
          }
90
        }
91
        if(this.result.dateofacceptance  != undefined){
92
          citationData.issued = {};
93
          var date:string = (this.result.dateofacceptance)+""; // transform to string in case it is an integer
94
          var dateArray:string[]  = (date && (date).indexOf('-') !== -1)?[date.split('-')[0]]:[date];
95
          if(dateArray.length < 3){
96
            // dateArray.push[1];
97
            // dateArray.push[1];
98
          }
99
           citationData.issued={"date-parts":[[""+dateArray[0]]]};
100
           if(this.result.date ){
101
             citationData.date = this.result.date ;
102
           }
103
      }
104

    
105

    
106
    this.data = citationData;
107
    // console.log(this.data);
108

    
109
  }
110
  styleChanged(){
111
    this.updateCitation();
112

    
113
  }
114
  updateCitation(){
115
    var Sys =
116
      function Sys(lang, data){
117
     this.lang = lang;
118
     this.data = data;
119
       this.changeName = function (name) {
120
           this.lastName = name;
121
       };
122
       this.retrieveLocale= function (lang){
123

    
124
         return this.lang;
125
       }
126
       this.retrieveItem= function(id){
127
         console.log("retrieve id:" + id);
128
         return this.data;
129
       }
130
    };
131

    
132
    var citeproc = new CSL.Engine(new Sys(this.citation.locale,  this.data ), this.citation[this.selectedStyle]);
133
    citeproc.updateItems([this.data.id]);
134
    this.citationText = citeproc.makeBibliography();
135
    this.citationText = ((this.citationText != null) && (this.citationText.length > 1) && (this.citationText[1].length > 0)) ? this.citationText[1][0] : '';
136

    
137
  }
138

    
139
}
(2-2/3)