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

    
5
//<addThis ></addThis>
6
@Component({
7
  selector: 'citeThis',
8
  template: `
9

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

    
33
  public data;//= '[ { "id": "Q23571040", "type": "apple", "title": "Correlation of the Base Strengths of Amines 1", "DOI": "10.1021/ja01577a030", "author": [ { "given": "H. K.", "family": "Hall" } ], "issued": [ { "date-parts": [ "1957", "1", "1" ] } ], "container-title": "Journal of the American Chemical Society", "volume": "79", "issue": "20", "page": "5441-5444" } ]';
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.parseData();
43
      // var Cite = require('citation-js');
44
      // this.cite = new Cite(this.data,this.citation.getOptionsBy(this.selectedStyle));
45
      // this.citationText = this.cite.get(this.citation.getOptionsBy(this.selectedStyle));
46
    }
47
  }
48
  parseData(){
49
    var citationData:CitationData = new CitationData();
50

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

    
92

    
93
    this.data = JSON.stringify(citationData);
94
    console.log(this.data);
95

    
96
  }
97
  styleChanged(){
98
    // this.citationText = this.cite.get(this.citation.getOptionsBy(this.selectedStyle));
99

    
100
  }
101

    
102
}
(2-2/3)