1 |
44262
|
argiro.kok
|
import {Component, ElementRef, Input, Output, EventEmitter} from '@angular/core';
|
2 |
|
|
import {Value} from '../searchPages/searchUtils/searchHelperClasses.class';
|
3 |
|
|
import {ClaimResult, ClaimContext, ClaimProject} from './entities/claimEntities.class';
|
4 |
|
|
import {Observable} from 'rxjs/Observable';
|
5 |
|
|
import { Subject } from 'rxjs/Subject';
|
6 |
|
|
import {OpenaireProjectsService} from '../services/openaireProjects.service';
|
7 |
|
|
|
8 |
|
|
//Usage example
|
9 |
|
|
//<dynamic-autocomplete [(filtered)] =filtered [(selected)] =selected placeHolderMessage = "Search for countries" title = "Countries:" (keywordChange)="keywordChanged($event)"></dynamic-autocomplete>
|
10 |
|
|
|
11 |
|
|
@Component({
|
12 |
|
|
selector: 'dynamic-autocomplete',
|
13 |
|
|
styleUrls: ['autoComplete.component.css'],
|
14 |
|
|
template: `
|
15 |
44343
|
argiro.kok
|
<!--div class="bs-docs-grid">
|
16 |
44262
|
argiro.kok
|
<div class = "row-fluid form-inline auto-complete-box panel panel-default">
|
17 |
|
|
<div class="panel-heading">{{title}}</div>
|
18 |
44343
|
argiro.kok
|
<div class="panel-body"-->
|
19 |
|
|
<div class="custom-autocomplete">
|
20 |
44262
|
argiro.kok
|
<span *ngIf = "showSelected">
|
21 |
|
|
<span class="row-fluid show-grid auto-complete-choice" *ngFor="let item of selected" >
|
22 |
|
|
<span >{{showItem(item)}} </span>
|
23 |
|
|
<span (click)="remove(item)" aria-hidden="true" class=" remove glyphicon glyphicon-remove"></span>
|
24 |
|
|
</span>
|
25 |
|
|
</span>
|
26 |
|
|
<input type="text" class="auto-complete-input validate filter-input input-sm form-control " [placeholder]=placeHolderMessage [(ngModel)]=keyword (keyup)=search() >
|
27 |
44343
|
argiro.kok
|
<div class="suggestions" >
|
28 |
44262
|
argiro.kok
|
<ul class="list-group" >
|
29 |
|
|
<li class="list-group-item" *ngFor=" let item of filtered | async">
|
30 |
|
|
<a (click)="select(item)">{{showItem(item)}}</a>
|
31 |
|
|
</li>
|
32 |
|
|
</ul>
|
33 |
|
|
</div>
|
34 |
44343
|
argiro.kok
|
<div class="messages">
|
35 |
|
|
<div *ngIf="warningMessage.length > 0" class="alert alert-warning row-fluid " role="alert">{{warningMessage}}</div>
|
36 |
|
|
<div *ngIf="filtered.length == 0 && keyword.length >=3 " class="alert alert-info row-fluid " role="alert">No results Found</div>
|
37 |
|
|
</div>
|
38 |
44262
|
argiro.kok
|
</div>
|
39 |
44343
|
argiro.kok
|
|
40 |
|
|
<!--/div>
|
41 |
44262
|
argiro.kok
|
</div>
|
42 |
44343
|
argiro.kok
|
</div-->
|
43 |
|
|
`
|
44 |
44262
|
argiro.kok
|
})
|
45 |
|
|
export class DynamicAutocompleteComponent {
|
46 |
|
|
@Input() placeHolderMessage = "Search for entries";
|
47 |
|
|
@Input() title = "Autocomplete";
|
48 |
|
|
@Input() public selected = []; // the entries selected from user
|
49 |
|
|
@Input() public keywordlimit = 3; // the minimum length of keyword
|
50 |
|
|
@Input() public showSelected = true; // the minimum length of keyword
|
51 |
|
|
@Input() public keyword = '';
|
52 |
|
|
@Input() public type = 'search' //search, result, context, project
|
53 |
44343
|
argiro.kok
|
// @Output() keywordChange = new EventEmitter(); // when changed a method for filtering will be called
|
54 |
|
|
// @Output() addNew = new EventEmitter(); // when changed a method for filtering will be called
|
55 |
44262
|
argiro.kok
|
// @Input() public selectedFunderId = "";
|
56 |
44343
|
argiro.kok
|
@Input() searchTermStream = new Subject<string>();
|
57 |
|
|
@Input() filtered: Observable<{}> = this.searchTermStream
|
58 |
|
|
.debounceTime(300).distinctUntilChanged()
|
59 |
|
|
.switchMap((term: string) => this.service(term));
|
60 |
|
|
|
61 |
|
|
|
62 |
44262
|
argiro.kok
|
private warningMessage = "";
|
63 |
|
|
private infoMessage = "";
|
64 |
|
|
|
65 |
|
|
private tries = 0;
|
66 |
|
|
|
67 |
|
|
constructor(private _projectService: OpenaireProjectsService,myElement: ElementRef) {
|
68 |
|
|
// this.elementRef = myElement;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
44343
|
argiro.kok
|
service(term) {
|
72 |
|
|
console.info("In service: "+ projects);
|
73 |
|
|
|
74 |
|
|
var projects = this._projectService.searchForProjectsObs(term, "");
|
75 |
|
|
console.info("Results: "+ projects);
|
76 |
|
|
for( var i = 0 ; i<projects.length ; i++){
|
77 |
|
|
|
78 |
|
|
}
|
79 |
|
|
return projects;
|
80 |
|
|
}
|
81 |
44262
|
argiro.kok
|
search() {
|
82 |
|
|
console.info("heeere "+this.keyword );
|
83 |
|
|
this.infoMessage = "";
|
84 |
|
|
// this.filtered = [];
|
85 |
|
|
if(this.keyword == ""){
|
86 |
|
|
this.tries = 0;
|
87 |
|
|
this.warningMessage = "";
|
88 |
|
|
} else if(this.keyword && this.keyword.length < this.keywordlimit){
|
89 |
|
|
this.tries++;
|
90 |
|
|
if(this.tries == this.keywordlimit -1 ){
|
91 |
|
|
this.warningMessage = "Type at least " + this.keywordlimit + " characters";
|
92 |
|
|
this.tries = 0;
|
93 |
|
|
}
|
94 |
|
|
}else{
|
95 |
|
|
console.info("doo the search "+this.keyword );
|
96 |
|
|
|
97 |
|
|
this.tries = 0;
|
98 |
|
|
this.warningMessage = "";
|
99 |
|
|
this.searchTermStream.next(this.keyword);
|
100 |
|
|
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
remove(item:any){
|
106 |
|
|
var index:number =this.checkIfExists(item,this.selected);
|
107 |
|
|
if (index > -1) {
|
108 |
|
|
this.selected.splice(index, 1);
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
}
|
112 |
|
|
select(item:any){
|
113 |
|
|
|
114 |
|
|
var index:number =this.checkIfExists(item,this.selected);
|
115 |
|
|
if (index > -1) {
|
116 |
|
|
this.keyword = "";
|
117 |
|
|
// this.filtered.splice(0, this.filtered.length);
|
118 |
|
|
return;
|
119 |
|
|
}
|
120 |
|
|
else{
|
121 |
|
|
this.selected.push(item);
|
122 |
|
|
this.keyword = "";
|
123 |
|
|
// this.filtered.splice(0, this.filtered.length);
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
private checkIfExists(item:any,list):number{
|
127 |
|
|
console.log("type "+this.type);
|
128 |
|
|
|
129 |
|
|
if(this.type == "search"){
|
130 |
|
|
console.log("Value");
|
131 |
|
|
}else if(this.type == "context"){
|
132 |
|
|
console.log("context");
|
133 |
|
|
|
134 |
|
|
for (var _i = 0; _i < list.length; _i++) {
|
135 |
|
|
let itemInList = list[_i];
|
136 |
|
|
console.log("itemiL "+itemInList.concept.id);
|
137 |
|
|
console.log("item "+item.concept.id);
|
138 |
|
|
|
139 |
|
|
if(item.concept.id == itemInList.concept.id){
|
140 |
|
|
return _i;
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
}else if(this.type == "project"){
|
144 |
|
|
console.log("project");
|
145 |
|
|
}else {
|
146 |
|
|
console.log("no----");
|
147 |
|
|
}
|
148 |
|
|
return -1;
|
149 |
|
|
|
150 |
|
|
}
|
151 |
|
|
showItem(item:any):string{
|
152 |
|
|
|
153 |
|
|
if (item.name){ //search
|
154 |
|
|
return item.name;
|
155 |
|
|
}else if( item.concept && item.concept.label){ //context
|
156 |
|
|
return item.concept.label;
|
157 |
|
|
}else if (item.label){ //simple
|
158 |
|
|
return item.label;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
// handleClick(event){
|
165 |
|
|
// var clickedComponent = event.target;
|
166 |
|
|
// var inside = false;
|
167 |
|
|
// do {
|
168 |
|
|
// if (clickedComponent === this.elementRef.nativeElement) {
|
169 |
|
|
// inside = true;
|
170 |
|
|
// }
|
171 |
|
|
// clickedComponent = clickedComponent.parentNode;
|
172 |
|
|
// } while (clickedComponent);
|
173 |
|
|
// if(!inside){
|
174 |
|
|
// this.filteredList = [];
|
175 |
|
|
// }
|
176 |
|
|
// }
|
177 |
|
|
|
178 |
|
|
}
|