1
|
import {Component, ElementRef, Input, Output, EventEmitter} from '@angular/core';
|
2
|
import {Value} from '../searchPages/searchUtils/searchHelperClasses.class';
|
3
|
import {ISVocabulariesService} from '../services/ISVocabularies.service';
|
4
|
import {RefineFieldResultsService} from '../services/refineFieldResults.service';
|
5
|
//Usage example
|
6
|
//<static-autocomplete [(filtered)] =filtered [(selected)] =selected placeHolderMessage = "Search for countries" title = "Countries:" (keywordChange)="keywordChanged($event)"></static-autocomplete>
|
7
|
|
8
|
@Component({
|
9
|
selector: 'static-autocomplete',
|
10
|
styleUrls: ['autoComplete.component.css'],
|
11
|
host: {
|
12
|
'(document:click)': 'handleClick($event)',
|
13
|
},
|
14
|
template: `
|
15
|
<div class="custom-autocomplete">
|
16
|
<div *ngIf = "showSelected && selectedValue != ''">
|
17
|
<div class="row-fluid show-grid auto-complete-choice" *ngFor="let item of selected" >
|
18
|
<span >{{showItem(item)}} </span>
|
19
|
<span (click)="remove(item)" aria-hidden="true" title="Remove selection" class=" remove glyphicon glyphicon-remove"></span>
|
20
|
</div>
|
21
|
</div>
|
22
|
<input *ngIf = "showInput" type="text" class="auto-complete-input validate filter-input input-sm form-control " [placeholder]=placeHolderMessage [(ngModel)]=keyword (keyup)=filter() >
|
23
|
<div class="suggestions" *ngIf="filtered.length > 0">
|
24
|
<ul class="list-group" >
|
25
|
<li class="list-group-item" >
|
26
|
Select:
|
27
|
</li>
|
28
|
<li class="list-group-item" *ngFor=" let item of filtered">
|
29
|
<a (click)="select(item)">{{showItem(item)}}</a>
|
30
|
</li>
|
31
|
</ul>
|
32
|
</div>
|
33
|
<div class="messages">
|
34
|
<div *ngIf="showLoading" class="alert alert-info row-fluid " role="alert">Loading... <span class="glyphicon glyphicon-repeat" aria-hidden="true"></span></div>
|
35
|
<div *ngIf="warningMessage.length > 0" class="alert alert-warning row-fluid " role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>{{warningMessage}}</div>
|
36
|
<div *ngIf="filtered.length == 0 && keyword.length >=3 " class="alert alert-info row-fluid " role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>No results found</div>
|
37
|
</div>
|
38
|
</div>
|
39
|
|
40
|
`
|
41
|
})
|
42
|
export class StaticAutoCompleteComponent {
|
43
|
@Input() placeHolderMessage = "Search for entries";
|
44
|
@Input() title = "Autocomplete";
|
45
|
@Output() addItem = new EventEmitter(); // when selected list changes update parent component
|
46
|
@Output() selectedValueChanged = new EventEmitter(); // when changed a method for filtering will be called
|
47
|
@Output() listUpdated = new EventEmitter(); // when changed a method for filtering will be called
|
48
|
@Input() public list = []; // the entries resulted after filtering function
|
49
|
@Input() public filtered = []; // the entries resulted after filtering function
|
50
|
@Input() public selected = []; // the entries selected from user
|
51
|
@Input() public keywordlimit = 3; // the minimum length of keyword
|
52
|
@Input() public showSelected = true; // the minimum length of keyword
|
53
|
@Input() public multipleSelections:boolean = true;
|
54
|
@Input() public allowDuplicates:boolean = false;
|
55
|
@Input() public selectedValue:string = '';
|
56
|
@Input() public vocabularyId:string ;
|
57
|
@Input() public fieldName:string ;
|
58
|
@Input() public entityName:string ;
|
59
|
|
60
|
@Input() public keyword = '';
|
61
|
@Input() public type = 'search' //search, result, context, project
|
62
|
public warningMessage = "";
|
63
|
public infoMessage = "";
|
64
|
public showLoading:boolean = false;
|
65
|
public tries = 0;
|
66
|
public showInput = true;
|
67
|
public sub;
|
68
|
public done = false;
|
69
|
constructor ( private _vocabulariesService: ISVocabulariesService,private _refineService: RefineFieldResultsService, private myElement: ElementRef) {
|
70
|
}
|
71
|
ngOnDestroy(){
|
72
|
if(this.sub && this.sub != undefined){
|
73
|
this.sub.unsubscribe();
|
74
|
}
|
75
|
}
|
76
|
ngOnInit () {
|
77
|
if(this.list == undefined || this.list.length == 0){
|
78
|
this.showLoading = true;
|
79
|
|
80
|
if(this.vocabularyId){
|
81
|
this.list = this._vocabulariesService.getVocabularyByType(this.vocabularyId, this.entityName);
|
82
|
this.afterListFetchedActions();
|
83
|
}else if(this.fieldName && this.entityName){
|
84
|
this.list = this._refineService.getRefineFieldResultsByFieldName(this.fieldName,this.entityName);
|
85
|
this.sub = this._refineService.getRefineFieldResultsByFieldName(this.fieldName,this.entityName).subscribe(
|
86
|
data => {
|
87
|
this.list = data;
|
88
|
this.afterListFetchedActions();
|
89
|
|
90
|
},
|
91
|
err => {
|
92
|
console.error(err);
|
93
|
this.warningMessage = "An Error occured..."
|
94
|
}
|
95
|
);
|
96
|
}else{
|
97
|
this.showLoading = false;
|
98
|
|
99
|
}
|
100
|
}else{
|
101
|
this.afterListFetchedActions();
|
102
|
}
|
103
|
|
104
|
}
|
105
|
public updateList(list){ // used in claim context autocomplete
|
106
|
this.list = list;
|
107
|
this.afterListFetchedActions()
|
108
|
}
|
109
|
private afterListFetchedActions(){
|
110
|
this.showLoading = false;
|
111
|
this.getSelectedNameFromGivenId();
|
112
|
this.listUpdated.emit({
|
113
|
value: this.list
|
114
|
});
|
115
|
if(this.list == null || this.list.length == 0 ){
|
116
|
this.warningMessage = "There are no results";
|
117
|
return;
|
118
|
}
|
119
|
this.done = true;
|
120
|
if(this.keyword != ""){
|
121
|
this.filter();
|
122
|
}
|
123
|
|
124
|
}
|
125
|
filter() {
|
126
|
if(this.done){
|
127
|
this.infoMessage = "";
|
128
|
this.filtered = [];
|
129
|
if(this.keyword == ""){
|
130
|
var cut = 5;
|
131
|
if(this.list.length < 5){
|
132
|
cut = this.list.length;
|
133
|
}
|
134
|
this.filtered =this.list.slice(0, cut);
|
135
|
this.tries = 0;
|
136
|
this.warningMessage = "";
|
137
|
// } else if(this.keyword && this.keyword.length < this.keywordlimit){
|
138
|
// this.tries++;
|
139
|
// if(this.tries == this.keywordlimit -1 ){
|
140
|
// this.warningMessage = "Type at least " + this.keywordlimit + " characters";
|
141
|
// this.tries = 0;
|
142
|
// }
|
143
|
}else{
|
144
|
this.tries = 0;
|
145
|
this.warningMessage = "";
|
146
|
this.filtered = this.list.filter(function(el){
|
147
|
return el.label.toLowerCase().indexOf(this.keyword.toLowerCase()) > -1;
|
148
|
}.bind(this));
|
149
|
|
150
|
}
|
151
|
}
|
152
|
}
|
153
|
remove(item:any){
|
154
|
var index:number =this.checkIfExists(item,this.selected);
|
155
|
if (index > -1) {
|
156
|
this.selected.splice(index, 1);
|
157
|
}
|
158
|
if(!this.multipleSelections && this.selected.length == 0 ){
|
159
|
this.showInput = true;
|
160
|
this.selectedValue = "";
|
161
|
this.selectedValueChanged.emit({
|
162
|
value: this.selectedValue
|
163
|
});
|
164
|
|
165
|
|
166
|
}
|
167
|
}
|
168
|
select(item:any){
|
169
|
// console.log("select"+this.selected.length + item.id + " "+ item.label);
|
170
|
|
171
|
if(this.multipleSelections){
|
172
|
var index:number =this.checkIfExists(item,this.selected);
|
173
|
if (index > -1 && !this.allowDuplicates) {
|
174
|
this.keyword = "";
|
175
|
this.filtered.splice(0, this.filtered.length);
|
176
|
return;
|
177
|
}
|
178
|
else{
|
179
|
this.selected.push(item);
|
180
|
this.keyword = "";
|
181
|
this.filtered.splice(0, this.filtered.length);
|
182
|
this.addItem.emit({
|
183
|
value: item
|
184
|
});
|
185
|
}
|
186
|
}else{
|
187
|
this.selected.splice(0, this.selected.length);
|
188
|
this.selected.push(item);
|
189
|
this.filtered.splice(0, this.filtered.length);
|
190
|
this.keyword = "";
|
191
|
this.showInput = false;
|
192
|
this.selectedValue = item.id;
|
193
|
this.selectedValueChanged.emit({
|
194
|
value: this.selectedValue
|
195
|
});
|
196
|
|
197
|
}
|
198
|
console.log("selected"+this.selected.length );
|
199
|
|
200
|
}
|
201
|
private checkIfExists(item:any,list):number{
|
202
|
|
203
|
if(item.concept && item.concept.id ){
|
204
|
|
205
|
for (var _i = 0; _i < list.length; _i++) {
|
206
|
let itemInList = list[_i];
|
207
|
if(item.concept.id == itemInList.concept.id){
|
208
|
return _i;
|
209
|
}
|
210
|
}
|
211
|
}else if(item.id){
|
212
|
for (var _i = 0; _i < list.length; _i++) {
|
213
|
let itemInList = list[_i];
|
214
|
if(item.id == itemInList.id){
|
215
|
return _i;
|
216
|
}
|
217
|
}
|
218
|
}
|
219
|
return -1;
|
220
|
|
221
|
}
|
222
|
showItem(item:any):string{
|
223
|
|
224
|
if (item.name){ //search
|
225
|
return item.name;
|
226
|
}else if( item.concept && item.concept.label){ //context
|
227
|
return item.concept.label;
|
228
|
}else if (item.label){ //simple
|
229
|
return item.label;
|
230
|
}
|
231
|
|
232
|
}
|
233
|
private getSelectedNameFromGivenId(){
|
234
|
if(this.list == null ){
|
235
|
return;
|
236
|
}
|
237
|
for( var i = 0; i < this.list.length; i++){
|
238
|
if(this.list[i].id == this.selectedValue){
|
239
|
this.selectedValue = this.list[i].label;
|
240
|
this.selected.push(this.list[i]);
|
241
|
this.showInput = false;
|
242
|
}
|
243
|
}
|
244
|
}
|
245
|
|
246
|
handleClick(event){
|
247
|
var clickedComponent = event.target;
|
248
|
var inside = false;
|
249
|
do {
|
250
|
if (clickedComponent === this.myElement.nativeElement) {
|
251
|
inside = true;
|
252
|
}
|
253
|
clickedComponent = clickedComponent.parentNode;
|
254
|
} while (clickedComponent);
|
255
|
if(!inside){
|
256
|
this.filtered.splice(0, this.filtered.length);;
|
257
|
}
|
258
|
}
|
259
|
|
260
|
}
|