import {Component, Input,Output, ElementRef, EventEmitter} from '@angular/core';
import {JSONP_PROVIDERS} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import { RouteParams, ROUTER_DIRECTIVES} from '@angular/router-deprecated';
import {ContextsService} from '../../services/contexts.service';
@Component({
selector: 'claim-contexts',
template: `
0" class="alert alert-warning" role="alert">{{warningMessage}}
0" class="alert alert-info" role="alert">{{infoMessage}}
`,
providers:[ ContextsService ]
})
export class ClaimContextComponent {
ngOnInit() {
this.getCommunities();
}
public query = '';
public filteredList = [];
@Input() public selectedList ;
public elementRef;
@Output() contextsChange = new EventEmitter();
public communities:string[];
@Input() public selectedCommunityId:string = "0";
selectedCommunityLabel:string = "Community:";
@Output() cselectedCommunityChange = new EventEmitter();
public categories:string[];
@Input() public selectedCategoryId:string ="0";
selectedCategoryLabel:string ="Category:";
@Output() selectedCategoryChange = new EventEmitter();
public concepts:string[];
public warningMessage = "";
public infoMessage = "";
constructor(private _contextService: ContextsService,myElement: ElementRef) {
this.elementRef = myElement;
}
filter() {
this.warningMessage = "";
this.infoMessage = "";
if(this.selectedCommunityId == "0"){
this.warningMessage = "Please select Community";
}else if (this.query !== ""){
this.warningMessage = "";
this.infoMessage = "";
if(this.selectedCategoryId == "0"){
this.warningMessage = "Specify category for more concepts";
}
this.filteredList = this.concepts.filter(function(el){
return el.label.toLowerCase().indexOf(this.query.toLowerCase()) > -1;
}.bind(this));
if(this.filteredList.length == 0 ){
this.infoMessage = "No results found";
}
}else{
this.filteredList = [];
}
}
select(item){
this.query = "";
this.filteredList = [];
var context= { community: this.selectedCommunityLabel, category: this.selectedCategoryLabel, concept: item };
var found:boolean = false;
this.warningMessage = "";
for (var _i = 0; _i < this.selectedList.length; _i++) {
let item = this.selectedList[_i];
if(item.concept.id == context.concept.id){
found=true;
this.warningMessage = "Concept already in selected list";
}
}
if (!found) {
this.selectedList.push(context);
this.contextsChange.emit({
value: this.selectedList
});
}
// var index:number =this.selectedList.indexOf(context);
// if (index == -1) {
// this.selectedList.push(context);
// this.contextsChange.emit({
// value: this.selectedList
// });
// }
}
// remove(item){
// var index:number =this.selectedList.indexOf(item);
// if (index > -1) {
// this.selectedList.splice(index, 1);
// }
// this.contextsChange.emit({
// value: this.selectedList
// });
// }
handleClick(event){
var clickedComponent = event.target;
var inside = false;
do {
if (clickedComponent === this.elementRef.nativeElement) {
inside = true;
}
clickedComponent = clickedComponent.parentNode;
} while (clickedComponent);
if(!inside){
this.filteredList = [];
}
}
getCommunities () {
this._contextService.getCommunities().subscribe(
data => {
this.communities = data.communities;
// var concept= ["{id: this.communities['id'], label: this.communities['label'] }"];
// this.filteredList.push(concept);
// this.concepts.push(concept);
},
err => console.error(err)
);
}
getCategories () {
this.categories=[];
if(this.selectedCommunityId != '0'){
this._contextService.getCategories(this.selectedCommunityId).subscribe(
data => {
this.categories = data.category;
this.concepts = [];
this.filteredList = [];
if (this.query !== ""){
this.filter();
}
},
err => console.error(err)
);
}
}
getConcepts () {
if(this.selectedCategoryId != '0'){
this._contextService.getConcepts(this.selectedCategoryId, "").subscribe(
data => {
this.concepts = data.concept;
if (this.query !== ""){
this.filter();
}
},
err => console.error(err)
);
}else{
this.concepts=[];
}
}
communityChanged(communityId:string, communityLabel:string){
this.warningMessage = "";
this.infoMessage = "";
this.selectedCommunityId= communityId;
this.selectedCommunityLabel= communityLabel;
this.getCategories();
}
categoryChanged(categoryId:string, categoryLabel:string){
this.warningMessage = "";
this.infoMessage = "";
this.selectedCategoryId = categoryId;
this.selectedCategoryLabel = categoryLabel;
this.getConcepts();
}
}