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