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" [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
        citationData.id = this.id;
53
        if(this.result.types != undefined && this.result.types.length > 0 && this.result.types[0]){
54
          citationData.type = this.result.types[0].toLowerCase();
55
        }else if(this.result.type != undefined ){
56
          citationData.type = this.result.type.toLowerCase();
57
        }
58
        if(this.result.title && this.result.title.name){
59
          citationData.title = this.result.title.name;
60
        }
61
        if(this.result.journal && this.result.journal.journal){
62
          citationData["container-title"] = this.result.journal.journal;
63
        }
64
        if(this.result.publisher){
65
          citationData.publisher = this.result.publisher;
66
        }
67
        if( this.result.authors){
68
          citationData.author = [];
69
          var max_length = (this.result.authors.length > 10)?10:this.result.authors.length;
70
          for (var i =0 ;i <  max_length; i++){
71
            if(this.result.authors[i] && this.result.authors[i].indexOf(", ") !== -1){
72
              citationData.author.push({given:this.result.authors[i].split(", ")[0], family:this.result.authors[i].split(", ")[1], 'parse-names':true});
73
            }else{
74
              citationData.author.push({given:"", family:this.result.authors[i], 'parse-names':true});
75
            }
76
            // citationData.authors.push(this.result.authors[i]);
77
          }
78
        }
79
        if(this.result.dateofacceptance  != undefined){
80
          citationData.issued = {};
81
          var date:string = (this.result.dateofacceptance)+""; // transform to string in case it is an integer
82
          var dateArray:string[]  = (date && (date).indexOf('-') !== -1)?date.split('-'):[date];
83
          if(dateArray.length < 3){
84
            // dateArray.push[1];
85
            // dateArray.push[1];
86
          }
87
           citationData.issued={"date-parts":[[""+dateArray[0],dateArray[1], dateArray[2]]]};
88
           if(this.result.date ){
89
             citationData.date = this.result.date ;
90
           }
91
      }
92

    
93

    
94
    this.data = citationData;
95
    // console.log(this.data);
96

    
97
  }
98
  styleChanged(){
99
    this.updateCitation();
100

    
101
  }
102
  updateCitation(){
103
    var Sys =
104
      function Sys(lang, data){
105
     this.lang = lang;
106
     this.data = data;
107
       this.changeName = function (name) {
108
           this.lastName = name;
109
       };
110
       this.retrieveLocale= function (lang){
111

    
112
         return this.lang;
113
       }
114
       this.retrieveItem= function(id){
115
         console.log("retrieve id:" + id);
116
         return this.data;
117
       }
118
    };
119

    
120
    var citeproc = new CSL.Engine(new Sys(this.citation.locale,  this.data ), this.citation[this.selectedStyle]);
121
    citeproc.updateItems([this.data.id]);
122
    this.citationText = citeproc.makeBibliography();
123
    this.citationText = ((this.citationText != null) && (this.citationText.length > 1) && (this.citationText[1].length > 0)) ? this.citationText[1][0] : '';
124

    
125
  }
126

    
127
}
(2-2/3)