1
|
import {Component, ElementRef, Input, Output, EventEmitter, OnChanges, SimpleChange} from '@angular/core';
|
2
|
import {Value} from '../../searchPages/searchUtils/searchHelperClasses.class';
|
3
|
import {ISVocabulariesService} from './ISVocabularies.service';
|
4
|
import {RefineFieldResultsService} from '../../services/refineFieldResults.service';
|
5
|
import{EnvProperties} from '../../utils/properties/env-properties';
|
6
|
|
7
|
//Usage example
|
8
|
//<static-autocomplete [(filtered)] =filtered [(selected)] =selected placeHolderMessage = "Search for countries" title = "Countries:" (keywordChange)="keywordChanged($event)"></static-autocomplete>
|
9
|
|
10
|
@Component({
|
11
|
selector: 'static-autocomplete',
|
12
|
host: {
|
13
|
'(document:click)': 'handleClick($event)',
|
14
|
},
|
15
|
template: `
|
16
|
<span class="custom-autocomplete uk-width-1-1">
|
17
|
<span *ngIf = "showSelected && selectedValue != ''">
|
18
|
<span class="uk-alert-default" data-uk-alert="" *ngFor="let item of selected" [title]="showItem(item)"> <span >{{showItem(item)}} </span>
|
19
|
<span (click)="remove(item)" aria-hidden="true" title="Remove selection" > <span class="clickable uk-icon">
|
20
|
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="close" ratio="1"><path fill="none" stroke="#000" stroke-width="1.06" d="M16,16 L4,4"></path><path fill="none" stroke="#000" stroke-width="1.06" d="M16,4 L4,16"></path></svg>
|
21
|
</span> </span>
|
22
|
</span>
|
23
|
</span>
|
24
|
<input *ngIf = "showInput " type="text" class="auto-complete-input validate filter-input input-sm form-control -width-small " [placeholder]=placeHolderMessage [(ngModel)]=keyword (keyup)=filter() >
|
25
|
<!--span [style.display]="showLoading ? 'inline' : 'none'" class="uk-alert uk-alert-primary" data-uk-alert=""> <i class="uk-icon-spinner"></i> Loading... </span>
|
26
|
<span *ngIf="warningMessage.length > 0" class="uk-alert uk-alert-warning" data-uk-alert=""> {{warningMessage}} <a href="" class="uk-alert-close uk-close"></a></span-->
|
27
|
<div *ngIf="focus && showInput" class="uk-dropdown" aria-expanded="true" style="display:block" >
|
28
|
<ul class="uk-nav uk-nav-autocomplete uk-autocomplete-results" >
|
29
|
<li>
|
30
|
<span [style.display]="showLoading ? 'inline' : 'none'" class="uk-alert uk-alert-primary" data-uk-alert=""> <i class="uk-icon-spinner"></i> Loading... </span>
|
31
|
<span *ngIf="filtered.length > 0" > {{results | number}} results found:</span>
|
32
|
<span *ngIf="filtered.length == 0" class="uk-alert uk-alert-primary" data-uk-alert=""> No results found</span>
|
33
|
</li>
|
34
|
<li *ngFor=" let item of filtered">
|
35
|
<a (click)="select(item)" [title]="showItem(item)" style="text-overflow: ellipsis; ">{{showItem(item)}}</a>
|
36
|
</li>
|
37
|
</ul>
|
38
|
</div>
|
39
|
</span>
|
40
|
|
41
|
`
|
42
|
})
|
43
|
export class StaticAutoCompleteComponent implements OnChanges{
|
44
|
@Input() placeHolderMessage = "Search for entries";
|
45
|
@Input() title = "Autocomplete";
|
46
|
@Output() addItem = new EventEmitter(); // when selected list changes update parent component
|
47
|
@Output() selectedValueChanged = new EventEmitter(); // when changed a method for filtering will be called
|
48
|
@Output() listUpdated = new EventEmitter(); // when changed a method for filtering will be called
|
49
|
@Input() public list = []; // the entries resulted after filtering function
|
50
|
@Input() public filtered = []; // the entries resulted after filtering function
|
51
|
@Input() public selected = []; // the entries selected from user
|
52
|
@Input() public keywordlimit = 3; // the minimum length of keyword
|
53
|
@Input() public showSelected = true; // the minimum length of keyword
|
54
|
@Input() public multipleSelections:boolean = true;
|
55
|
@Input() public allowDuplicates:boolean = false;
|
56
|
@Input() public selectedValue:string = '';
|
57
|
@Input() public vocabularyId:string ;
|
58
|
@Input() public fieldName:string ;
|
59
|
@Input() public entityName:string ;
|
60
|
@Input() public fieldId:string ;
|
61
|
@Input() properties:EnvProperties;
|
62
|
@Input() public keyword = '';
|
63
|
@Input() public type = 'search' //search, result, context, project
|
64
|
public warningMessage = "";
|
65
|
public infoMessage = "";
|
66
|
public showLoading:boolean = false;
|
67
|
public tries = 0;
|
68
|
public showInput = true;
|
69
|
public sub;
|
70
|
public done = false;
|
71
|
public results = 0;
|
72
|
public focus:boolean = false;
|
73
|
public currentFieldId: string ;
|
74
|
constructor ( private _vocabulariesService: ISVocabulariesService,private _refineService: RefineFieldResultsService, private myElement: ElementRef) {
|
75
|
this.currentFieldId=this.fieldId;
|
76
|
|
77
|
}
|
78
|
ngOnDestroy(){
|
79
|
if(this.sub && this.sub != undefined){
|
80
|
this.sub.unsubscribe();
|
81
|
}
|
82
|
}
|
83
|
|
84
|
ngOnChanges(changes: {[propKey: string]: SimpleChange}) {
|
85
|
if(this.currentFieldId!=this.fieldId){ //this is going to be called when
|
86
|
this.currentFieldId=this.fieldId;
|
87
|
this.initialize();
|
88
|
}
|
89
|
}
|
90
|
private initialize(){
|
91
|
|
92
|
this.showInput = true;
|
93
|
if(this.list == undefined || this.list.length == 0){
|
94
|
this.showLoading = true;
|
95
|
|
96
|
if(this.vocabularyId){
|
97
|
// this.list = this._vocabulariesService.getVocabularyByType(this.vocabularyId, this.entityName);
|
98
|
// this.afterListFetchedActions();
|
99
|
this.sub = this._vocabulariesService.getVocabularyByType(this.vocabularyId, this.entityName, this.properties).subscribe(
|
100
|
data => {
|
101
|
this.list = data;
|
102
|
this.afterListFetchedActions();
|
103
|
|
104
|
},
|
105
|
err => {
|
106
|
//console.log(err);
|
107
|
this.handleError("Error getting vocabulary with id: "+this.vocabularyId+" for "+this.entityName, err);
|
108
|
this.warningMessage = "An Error occured..."
|
109
|
}
|
110
|
);
|
111
|
}else if(this.fieldName && this.entityName){
|
112
|
this.sub = this._refineService.getRefineFieldResultsByFieldName(this.fieldName,this.entityName, this.properties).subscribe(
|
113
|
data => {
|
114
|
this.list = data;
|
115
|
this.afterListFetchedActions();
|
116
|
|
117
|
},
|
118
|
err => {
|
119
|
//console.log(err);
|
120
|
this.handleError("Error getting results for refine field: "+this.fieldName+" for "+this.entityName, err);
|
121
|
this.warningMessage = "An Error occured..."
|
122
|
}
|
123
|
);
|
124
|
}else{
|
125
|
this.showLoading = false;
|
126
|
|
127
|
}
|
128
|
}else{
|
129
|
this.afterListFetchedActions();
|
130
|
}
|
131
|
|
132
|
}
|
133
|
public updateList(list){ // used in claim context autocomplete
|
134
|
this.list = list;
|
135
|
this.afterListFetchedActions()
|
136
|
}
|
137
|
private afterListFetchedActions(){
|
138
|
this.showLoading = false;
|
139
|
this.getSelectedNameFromGivenId();
|
140
|
this.listUpdated.emit({
|
141
|
value: this.list
|
142
|
});
|
143
|
if(this.list == null || this.list.length == 0 ){
|
144
|
this.warningMessage = "No results available";
|
145
|
return;
|
146
|
}
|
147
|
this.done = true;
|
148
|
if(this.keyword != ""){
|
149
|
this.filter();
|
150
|
}
|
151
|
|
152
|
}
|
153
|
filter() {
|
154
|
this.focus = true;
|
155
|
if(this.done){
|
156
|
this.infoMessage = "";
|
157
|
this.filtered = [];
|
158
|
if(this.keyword == ""){
|
159
|
var cut = 10;
|
160
|
if(this.list.length < 5){
|
161
|
cut = this.list.length;
|
162
|
}
|
163
|
this.results = this.list.length;
|
164
|
this.filtered =this.list.slice(0, cut);
|
165
|
this.tries = 0;
|
166
|
this.warningMessage = "";
|
167
|
// } else if(this.keyword && this.keyword.length < this.keywordlimit){
|
168
|
// this.tries++;
|
169
|
// if(this.tries == this.keywordlimit -1 ){
|
170
|
// this.warningMessage = "Type at least " + this.keywordlimit + " characters";
|
171
|
// this.tries = 0;
|
172
|
// }
|
173
|
}else{
|
174
|
this.tries = 0;
|
175
|
this.warningMessage = "";
|
176
|
this.filtered = this.list.filter(function(el){
|
177
|
return el.label.toLowerCase().indexOf(this.keyword.toLowerCase()) > -1;
|
178
|
}.bind(this));
|
179
|
var cut = 10;
|
180
|
if(this.filtered .length < 5){
|
181
|
cut = this.list.length;
|
182
|
}
|
183
|
this.results = this.filtered.length;
|
184
|
this.filtered =this.filtered.slice(0, cut);
|
185
|
}
|
186
|
}
|
187
|
}
|
188
|
remove(item:any){
|
189
|
var index:number =this.checkIfExists(item,this.selected);
|
190
|
if (index > -1) {
|
191
|
this.selected.splice(index, 1);
|
192
|
}
|
193
|
if(!this.multipleSelections && this.selected.length == 0 ){
|
194
|
this.showInput = true;
|
195
|
this.selectedValue = "";
|
196
|
this.selectedValueChanged.emit({
|
197
|
value: this.selectedValue
|
198
|
});
|
199
|
|
200
|
|
201
|
}
|
202
|
}
|
203
|
select(item:any){
|
204
|
// console.log("select"+this.selected.length + item.id + " "+ item.label);
|
205
|
|
206
|
if(this.multipleSelections){
|
207
|
var index:number =this.checkIfExists(item,this.selected);
|
208
|
if (index > -1 && !this.allowDuplicates) {
|
209
|
this.keyword = "";
|
210
|
this.filtered.splice(0, this.filtered.length);
|
211
|
return;
|
212
|
}
|
213
|
else{
|
214
|
this.selected.push(item);
|
215
|
this.keyword = "";
|
216
|
this.filtered.splice(0, this.filtered.length);
|
217
|
this.addItem.emit({
|
218
|
value: item
|
219
|
});
|
220
|
}
|
221
|
}else{
|
222
|
this.selected.splice(0, this.selected.length);
|
223
|
this.selected.push(item);
|
224
|
this.filtered.splice(0, this.filtered.length);
|
225
|
this.keyword = "";
|
226
|
this.showInput = false;
|
227
|
this.selectedValue = item.id;
|
228
|
this.selectedValueChanged.emit({
|
229
|
value: this.selectedValue
|
230
|
});
|
231
|
|
232
|
}
|
233
|
|
234
|
}
|
235
|
private checkIfExists(item:any,list):number{
|
236
|
|
237
|
if(item.concept && item.concept.id ){
|
238
|
|
239
|
for (var _i = 0; _i < list.length; _i++) {
|
240
|
let itemInList = list[_i];
|
241
|
if(item.concept.id == itemInList.concept.id){
|
242
|
return _i;
|
243
|
}
|
244
|
}
|
245
|
}else if(item.id){
|
246
|
for (var _i = 0; _i < list.length; _i++) {
|
247
|
let itemInList = list[_i];
|
248
|
if(item.id == itemInList.id){
|
249
|
return _i;
|
250
|
}
|
251
|
}
|
252
|
}
|
253
|
return -1;
|
254
|
|
255
|
}
|
256
|
showItem(item:any):string{
|
257
|
|
258
|
if (item.name){ //search
|
259
|
return item.name;
|
260
|
}else if( item.concept && item.concept.label){ //context
|
261
|
return item.concept.label;
|
262
|
}else if (item.label){ //simple
|
263
|
return item.label;
|
264
|
}
|
265
|
|
266
|
}
|
267
|
truncate(str:string, size:number):string{
|
268
|
if(str == null){return "";}
|
269
|
return (str.length > size)?str.substr(0,size)+'...':str;
|
270
|
}
|
271
|
private getSelectedNameFromGivenId(){
|
272
|
if(this.list == null ){
|
273
|
return;
|
274
|
}
|
275
|
this.showInput = true;
|
276
|
for( var i = 0; i < this.list.length; i++){
|
277
|
if(this.list[i].id == this.selectedValue){
|
278
|
this.selectedValue = this.list[i].label;
|
279
|
this.selected.push(this.list[i]);
|
280
|
this.showInput = false;
|
281
|
return;
|
282
|
|
283
|
}
|
284
|
}
|
285
|
}
|
286
|
|
287
|
handleClick(event){
|
288
|
var clickedComponent = event.target;
|
289
|
var inside = false;
|
290
|
do {
|
291
|
if (clickedComponent === this.myElement.nativeElement) {
|
292
|
inside = true;
|
293
|
}
|
294
|
clickedComponent = clickedComponent.parentNode;
|
295
|
} while (clickedComponent);
|
296
|
if(!inside){
|
297
|
this.focus =false;
|
298
|
this.filtered.splice(0, this.filtered.length);
|
299
|
}
|
300
|
}
|
301
|
|
302
|
private handleError(message: string, error) {
|
303
|
console.error("Static Autocomplete (component): "+message, error);
|
304
|
}
|
305
|
}
|