explore-services/src/app/linking/claimPublication/claimPublication.component.ts

206 lines
6.2 KiB
TypeScript

import {Component, Input, Output, EventEmitter} from '@angular/core';
import {JSONP_PROVIDERS} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import { RouteParams, RouteConfig, ROUTER_DIRECTIVES, Router } from '@angular/router-deprecated';
import {SearchCrossrefService} from '../../services/searchCrossref.service';
import {SearchOrcidService} from '../../services/searchOrcid.service';
import {Publication} from '../../entities/publication';
import {pagingFormatterNoLoad} from '../../common/pagingFormatterNoLoad.component';
import {PublicationTitleFormatter} from '../../common/publicationTitleFormatter.component';
@Component({
selector: 'claim-publication',
directives: [...ROUTER_DIRECTIVES,pagingFormatterNoLoad, PublicationTitleFormatter],
templateUrl: 'src/app/linking/claimPublication/claimPublication.component.html',
providers:[JSONP_PROVIDERS, SearchCrossrefService, SearchOrcidService]
})
export class ClaimPublicationComponent {
constructor (private _searchCrossrefService: SearchCrossrefService,private _searchOrcidService: SearchOrcidService,
private _routeParams: RouteParams) {}
ngOnInit() {
let page = +this._routeParams.get('page');
let size = +this._routeParams.get('size');
this.page = ( page <= 0 ) ? 1 : page;
this.size = ( size <= 0 ) ? 10 : size;
if(this.keyword !=null && this.keyword.length > 0){
this.search(this.keyword,this.size,this.page);
}
}
page : number = 1;
size:number = 10;
@Input() public keyword:string = "";
navigateTo: string = "Search";
source: string = "crossref";
type : string = "publication";
@Input() public select:boolean = true ;
@Input() public selectedPublications = [] ;
@Output() publicationsChange = new EventEmitter();
crossrefResults=[];
crossrefResultsNum : Observable<number> ;
orcidResults: string[];
orcidResultsNum: number ;
totalPages: number;
orcidResultsToShow: string[];
authorId: string;
authorGivenName: string;
authorFamilyName: string;
authorIds: string[];
authorGivenNames: string[];
authorFamilyNames: string[];
authorsNum : number ;
searchOrcid (term: string) {
this.authorIds = new Array<string>();
this.authorGivenNames = new Array<string>();
this.authorFamilyNames = new Array<string>();
this.getOrcidAuthor(term);
console.info('searchOrcid in searchOrcid file');
}
readData(data: any) {
this.authorIds.push(data[2].path);
if(data[0] != null) {
this.authorGivenNames.push(data[0].value);
} else {
this.authorGivenNames.push("");
}
if(data[1] != null) {
this.authorFamilyNames.push(data[1].value);
} else {
this.authorFamilyNames.push("");
}
}
getOrcidAuthor (term: string) {
this.orcidResultsNum = null;
this._searchOrcidService.searchOrcidAuthor(term).subscribe(
data => {
if(data[2] != null) {
this.readData(data);
this.getOrcidResultsById(0);
}
},
err => this.errorHandler(err, term)
);
}
errorHandler(err: any, term: string) {
if(err.status == 404){
this.getOrcidAuthors(term);
} else {
console.error(err.status);
}
}
getOrcidAuthors (term: string) {
this.orcidResultsNum = null;
this._searchOrcidService.searchOrcidAuthors(term).subscribe(
data => {
if(data[2] != null) {
this.readData(data);
this.getOrcidResultsById(0);
}
},
err => console.error(err)
);
}
getOrcidResultsById (index:number) {
let id = this.authorIds[index];
console.info("getOrcidResultsById: "+id);
this._searchOrcidService.searchOrcidPublications(id).subscribe(
data => {
if(data != null) {
this.orcidResults=data['orcid-work'];
this.orcidResultsNum = data['orcid-work'].length;
this.page = 1;
if((this.orcidResultsNum % this.size) == 0){
this.totalPages=parseInt(''+(this.orcidResultsNum/this.size));
} else{
this.totalPages=parseInt(''+(this.orcidResultsNum/this.size+1));
}
this.orcidResultsToShow = this.orcidResults.slice(0,10);
} else {
this.orcidResultsNum = 0;
this.totalPages=0;
}
this.authorGivenName = this.authorGivenNames[index];
this.authorFamilyName = this.authorFamilyNames[index];
this.authorId = id;
},
err => console.error(err)
);
console.info("totalPages = " +this.totalPages);
}
search(term: string, size : number, page : number){
this.getCrossrefResults(term,size,page);
this.searchOrcid(term);
}
getCrossrefResults (term: string, size : number, page : number) {
this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
data => {
this.crossrefResults = data.items;
this.page=page;
this.crossrefResultsNum = data['total-results'];
},
err => console.error(err)
);
}
add(item, itemSource, itemType, itemUrl, itemTitle){
var result ={id: item.DOI, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate:''};
console.info("Add result:"+result.id+" "+result.source+" "+item);
this.selectedPublications.push(result);
var index:number =this.crossrefResults.indexOf(item);
if (index > -1) {
this.crossrefResults.splice(index, 1);
}
this.publicationsChange.emit({
value: this.selectedPublications
});
}
remove(item){
var index:number =this.selectedPublications.indexOf(item);
if (index > -1) {
this.selectedPublications.splice(index, 1);
this.publicationsChange.emit({
value: this.selectedPublications
});
}
}
pageChange($event) {
this.page=$event.value;
this.crossrefResults=[];
this.getCrossrefResults(this.keyword,this.size,this.page);
}
orcidPageChange($event) {
this.page=$event.value;
this.orcidResultsToShow=[];
this.orcidResultsToShow = this.orcidResults.slice(($event.value-1)*this.size, $event.value*this.size);
}
}