Project

General

Profile

1
import {Component, Input, Output, EventEmitter} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import {ActivatedRoute, Router} from '@angular/router';
4
import {SearchCrossrefService} from '../services/searchCrossref.service';
5
import {Publication} from '../utils/entities/publication';
6
import {PagingFormatter} from '../utils/pagingFormatter.component';
7
import {PublicationTitleFormatter} from '../utils/publicationTitleFormatter.component';
8

    
9
import {Location} from '@angular/common';
10

    
11
@Component({
12
    selector: 'search-all',
13
    template: `
14

    
15

    
16
      <div  *ngIf="claim">
17
        <h3>Selected publications:</h3>
18
          <p *ngFor=" let item of selected "> <publication-title [title]="item.title" [url]="item.URL" > </publication-title> {{item.DOI}} --{{item.publisher}}  <button (click)="remove(item)"  type="button" class="btn btn-default">Remove</button></p>
19
      </div>
20

    
21
      <h1>Search</h1>
22
      <form>
23
        <input #term   [(ngModel)]="keyword" name= "searchAllTerm" />
24
        <button   (click)="search(keyword)"  type="submit" class="btn btn-default">Search</button>
25
        </form>
26
      <div>
27
        <div *ngIf="resultsNum != null && resultsNum > 0">
28
          <p > {{resultsNum   }} Total Results </p>
29
          <paging [currentPage]="page" [totalResults]="resultsNum"  [baseUrl]="'/search?keyword='+keyword" [size]="size"> </paging>
30
        </div>
31
        <div >
32

    
33
            <p *ngFor=" let item of results "> <publication-title [title]="item.title" [url]="item.URL" > </publication-title> {{item.DOI}} --{{item.publisher}} <button *ngIf="claim" (click)="add(item)"  type="button" class="btn btn-default">Select</button> </p>
34

    
35
        </div>
36
      </div>
37

    
38
    `
39
})
40
export class SearchAllComponent {
41
  constructor (private _searchService: SearchCrossrefService,
42
  private route: ActivatedRoute, private _location: Location) {}
43
  ngOnInit() {
44
    console.log("Path: " + this._location.path());
45
    this.sub =  this.route.queryParams.subscribe(params => {
46

    
47
      let page = (params['page']=== undefined)?1:+params['page'];
48
      let size = (params['size']=== undefined)?10:+params['size'];
49
      this.page = page;
50
      this.size = size;
51
      this.keyword = params['keyword'];
52
      if(this.keyword !=null){
53
        this.searchCrossref(this.keyword,this.size,this.page);
54
      }
55
    });
56

    
57
 }
58
   ngOnDestroy() {
59
     this.sub.unsubscribe();
60
   }
61
   sub: any;
62
  page : number;
63
  size:number;
64
  keyword:string;
65
  navigateTo: string = "Search";
66

    
67
  @Input() public claim:boolean = true ; // add selection for claiming
68
  @Input() public publication:boolean = true ; // true for publication/ false for dataset
69
  @Input() public source:string = 'openaire' ; // other values : crossref/ datacite/orcid
70
  @Input() public selected = [] ;
71
  @Output() selectedChange = new EventEmitter();
72
  results:string[];
73
  resultsNum : Observable<number> ;
74
  search(term: string, size : number, page : number) {
75
    // if(this.source == 'crossref' && this.publication){
76
        this.searchCrossref(term,10,1);
77
    // }else if(this.source == 'openaire' && this.publication){
78
    //
79
    // }else if(this.source == 'openaire' && this.publication){
80
    //
81
    // }else if(this.source == 'openaire' && !this.publication){
82
    //
83
    // }else if(this.source == 'orcid' && this.publication){
84
    //
85
    // }else if(this.source == 'datacite' && !this.publication){
86
    //
87
    // }
88
  }
89
  searchCrossref (term: string, size : number, page : number) {
90
    this._searchService.searchCrossrefResults(term, size, page).subscribe(
91
      data => {
92
        this.results = data.items;
93
        this.page=page;
94
        this.resultsNum = data['total-results'];
95
       },
96
      err => console.error(err)
97
    );
98
  }
99
  searchOpenaire (term2: string) {
100
    //this.items = this._searchService.httpSearchOpenaire(term2);
101
  }
102

    
103

    
104

    
105
   add(item){
106
        this.selected.push(item);
107
         console.info("Select:"+this.selected);
108
         for (var i = 0; i < this.selected.length; i++) {
109
             console.log(this.selected[i]);
110
         }
111

    
112

    
113
        var index:number =this.results.indexOf(item);
114
         if (index > -1) {
115
            this.results.splice(index, 1);
116
        }
117
            console.info("Item removed "+this.selected.indexOf(item));
118
            this.selectedChange.emit({
119
             value: this.selected
120
           });
121
   }
122
   remove(item){
123
          var index:number =this.selected.indexOf(item);
124
         if (index > -1) {
125
            this.selected.splice(index, 1);
126
        }
127
        this.selectedChange.emit({
128
         value: this.selected
129
       });
130

    
131
   }
132

    
133
}
    (1-1/1)