Project

General

Profile

1

    
2
import {switchMap, distinctUntilChanged, debounceTime} from 'rxjs/operators';
3
import {Component, ElementRef, Input, Output, EventEmitter, OnChanges, SimpleChange} from '@angular/core';
4
import {Observable, Subject} from 'rxjs';
5
import {Value} from '../../searchPages/searchUtils/searchHelperClasses.class';
6
import {EntitiesSearchService} from './entitySearch.service';
7
import{EnvProperties} from '../properties/env-properties';
8
import {StringUtils} from "../string-utils.class";
9

    
10

    
11
//Usage example
12
//<static-autocomplete [(filtered)] =filtered [(selected)] =selected placeHolderMessage = "Search for countries" title = "Countries:" (keywordChange)="keywordChanged($event)"></static-autocomplete>
13

    
14
@Component({
15
    selector: 'entities-autocomplete',
16
     host: {
17
       '(document:click)': 'handleClick($event)',
18
   },
19
    template: `
20

    
21
        <div class="custom-autocomplete">
22
          <div   *ngIf = "showSelected && selectedValue != ''">
23
            <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)" >
24
              <div class="uk-width-expand uk-padding-remove-left" >{{showItem(item)}} </div>
25
            <div (click)="remove(item)" aria-hidden="true" title="Remove selection" class="uk-padding-remove-left"> <span class="clickable uk-icon">
26
<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>
27
</span> </div>
28
          </div>
29
          </div>
30
          <input *ngIf = "showInput" type="text" class="auto-complete-input validate filter-input input-sm form-control uk-input " [placeholder]=placeHolderMessage [(ngModel)]=keyword (keyup)=search()  (blur)="keyword = ''"  >
31
          <!--span [style.display]="showLoading ? 'inline' : 'none'" class="uk-alert uk-alert-primary" data-uk-alert=""> <i class="uk-icon-spinner"></i> Loading... </span>
32
          <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-->
33
          <div *ngIf="focus && showInput" class="uk-dropdown" aria-expanded="true" style="display:block" >
34
                <ul class="uk-nav uk-nav-autocomplete uk-autocomplete-results" >
35
                      <li>
36
                          <span *ngIf="!_search.ready" class="uk-alert uk-alert-primary" data-uk-alert=""> <i class="uk-icon-spinner"></i> Loading..... </span>
37
                          <span *ngIf="warningMessage.length > 0" class="uk-alert uk-alert-warning" data-uk-alert="">{{warningMessage}}</span>
38
                          <span *ngIf="results > 0"  >  {{results | number}} results found:</span>
39
                          <!--span *ngIf="results == 0 && !showLoading" class="uk-alert uk-alert-primary" data-uk-alert=""> No results found</span-->
40
                      </li>
41
                       <li      *ngFor=" let item of filtered | async">
42
                        <a *ngIf= "item.id !=-1 && item.id !=-2" (click)="select(item)" [title]="showItem(item)"  style="text-overflow: ellipsis; ">{{showItem(item)}}</a>
43
                          <span *ngIf= "item.id ==-1"  class="uk-alert uk-alert-primary" data-uk-alert=""> No results found</span>
44
                          <span *ngIf= "item.id ==-2"  class="uk-alert uk-alert-warning" data-uk-alert=""> An error occured</span>
45
                     </li>
46
                </ul>
47

    
48
          </div>
49

    
50
      </div>
51

    
52
        `
53
})
54
export class EntitiesAutocompleteComponent {
55
    @Input() placeHolderMessage = "Search for entries";
56
    @Input() title = "Autocomplete";
57
    @Output() addItem = new EventEmitter(); // when selected list  changes update parent component
58
    @Output() selectedValueChanged = new EventEmitter(); // when changed  a method for filtering will be called
59
    @Output() updateValueLabel = new EventEmitter(); //when the value is id sends  an event to update the value (for meta tags)
60
    @Input() public list = []; // the entries resulted after filtering function
61
    @Input() public selected = []; // the entries selected from user
62
    @Input() public keywordlimit = 3; // the minimum length of keyword
63
    @Input() public showSelected = true; // the minimum length of keyword
64
    @Input() public multipleSelections:boolean = true;
65
    @Input() public allowDuplicates:boolean = false;
66
    @Input() public selectedValue:string = '';
67
    @Input() public keyword = '';
68
    @Input() public fieldId:string ;
69
    @Input() public properties:EnvProperties ;
70
    public currentFieldId: string ;
71
    public currentFunderId: string ;
72
    public warningMessage = "";
73
    public infoMessage = "";
74
    public tries = 0;
75
    public showInput = true;
76
    public sub;
77
    public done = false;
78
    public showLoading:boolean = false;
79
    public searchTermStream = new Subject<string>();
80
    filtered: Observable<{}> ;
81
    // public numFilteredResults:number = 0;
82

    
83
    @Input() public funderId:string;
84
    @Input() public entityType:string ;
85
    @Input() public depositType:string ;
86
    public results = 0;
87
    public focus:boolean  = false;
88
    constructor (public _search:EntitiesSearchService, private myElement: ElementRef) {
89
      this.currentFieldId=this.fieldId;
90
      this.currentFunderId=this.funderId;
91
      this.initialize();
92
    }
93

    
94
    ngOnChanges(changes: {[propKey: string]: SimpleChange}) {
95
         if(this.currentFieldId!=this.fieldId){ //this is going to be called when
96
          this.currentFieldId=this.fieldId;
97
          this.initialize();
98
        }else if(this.currentFunderId!=this.funderId){
99
          this.currentFunderId=this.funderId;
100
          this.initialize();
101
        }
102
    }
103
    private initialize(){
104

    
105
      this.showInput = true;
106
      /* if(this.entityType == "project" && this.funderId ){
107
        this.filtered  = this.searchTermStream.pipe(
108
        debounceTime(300),distinctUntilChanged(),
109
        switchMap((term: string) =>    {
110
          //console.log("funder"+this.funderId);
111
            var results = this._search.searchProjectsByFunder(term, (this.funderId == "0"?"":encodeURIComponent(this.funderId)), this.properties);
112
            this.showLoading = false;
113
             this.results = results.length;
114
            return   results;
115
        }),);
116
      }else */
117

    
118
        if(this.entityType == "organization" && this.depositType ){
119
         this.filtered  = this.searchTermStream.pipe(
120
        debounceTime(300),distinctUntilChanged(),
121
        switchMap((term: string) => {
122
          var results = this._search.searchByDepositType(term, this.depositType, this.properties);
123
          this.showLoading = false;
124
          this.results = results.length;
125
          return   results;
126
      }),);
127

    
128
      }else{
129

    
130
        this.filtered  = this.searchTermStream.pipe(
131
        debounceTime(300),
132
        distinctUntilChanged(),
133
        switchMap((term: string) => {
134
          var results = this._search.searchByType(StringUtils.URIEncode(term), this.entityType, this.properties);
135
          this.showLoading = false;
136
          this.results = results.length;
137
          return   results;
138
      }),);
139

    
140
        this.getSelectedNameFromGivenId();
141
      }
142

    
143
    }
144
    ngOnDestroy(){
145
      if(this.sub && this.sub != undefined){
146
        this.sub.unsubscribe();
147
      }
148
    }
149

    
150
    search() {
151
      this.infoMessage = "";
152
      if(this.keyword == ""){
153
        this.tries = 0;
154
        this.warningMessage = "";
155
      } else if(this.keyword && this.keyword.length < this.keywordlimit){
156
        this.tries++;
157
        if(this.tries == this.keywordlimit -1 ){
158
          this.warningMessage = "Type at least " + this.keywordlimit + " characters";
159
          this.tries = 0;
160
        }
161
      }else{
162

    
163
        this.tries = 0;
164
        this.warningMessage = "";
165
        this.searchTermStream.next(this.keyword);
166
        // if(this.numFilteredResults ==0){
167
          this.showLoading = true;
168
          this.focus = true;
169
        // }
170
      }
171

    
172
    }
173

    
174
    remove(item:any){
175
      var index:number =this.checkIfExists(item,this.selected);
176
       if (index > -1) {
177
          this.selected.splice(index, 1);
178
      }
179
      if(!this.multipleSelections && this.selected.length == 0 ){
180
        this.showInput = true;
181
        this.selectedValue = "";
182
        this.selectedValueChanged.emit({
183
            value: this.selectedValue
184
        });
185
        this.updateValueLabel.emit({
186
          value:""
187
        });
188

    
189

    
190
      }
191
    }
192
    select(item:any){
193
        if(this.multipleSelections){
194
          var index:number =this.checkIfExists(item,this.selected);
195
           if (index > -1 && !this.allowDuplicates) {
196
              // this.keyword = "";
197
              // this.filtered.splice(0, this.filtered.length);
198
              this.focus=false;
199
              return;
200
          }
201
          else{
202
              this.selected.push(item);
203
              // this.keyword = "";
204
              // this.filtered.splice(0, this.filtered.length);
205
              this.addItem.emit({
206
                  value: item
207
              });
208
              this.focus=false;
209
          }
210
      }else{
211
        this.selected.splice(0, this.selected.length);
212
        this.selected.push(item);
213
        // this.filtered.splice(0, this.filtered.length);
214
        this.keyword = "";
215
        this.showInput = false;
216
        this.selectedValue = item.id;
217
        this.selectedValueChanged.emit({
218
            value: this.selectedValue
219
        });
220
        this.updateValueLabel.emit({
221
          value:this.showItem(item)
222
        });
223
        this.focus=false;
224

    
225
      }
226

    
227
    }
228
    private checkIfExists(item:any,list):number{
229

    
230
       if(item.concept && item.concept.id ){
231

    
232
        for (var _i = 0; _i < list.length; _i++) {
233
            let itemInList = list[_i];
234
            if(item.concept.id == itemInList.concept.id){
235
                 return _i;
236
            }
237
         }
238
      }else if(item.id){
239
        for (var _i = 0; _i < list.length; _i++) {
240
            let itemInList = list[_i];
241
             if(item.id == itemInList.id){
242
                 return _i;
243
            }
244
         }
245
      }
246
      return -1;
247

    
248
    }
249
    showItem(item:any):string{
250
      //console.log(item);
251
      if(!item){
252
        return "[No title available]"
253
      }
254
      else if (item.projectName || item.projectAcronym || item.code){ //project
255
          return ((item.projectAcronym)?"["+item.projectAcronym+"] ":"")+item.projectName+((item.code)?" ("+item.code+")":"");
256
       }else if (item.name){ //search
257
         return item.name;
258
      }else if( item.concept && item.concept.label){ //context
259
         return item.concept.label;
260
      }else if (item.label){ //simple
261
         return item.label;
262
      }
263

    
264
    }
265
    truncate(str:string, size:number):string{
266
      if(str == null){return "";}
267
      return (str.length > size)?str.substr(0,size)+'...':str;
268
    }
269
    private getSelectedNameFromGivenId(){
270
      this.showInput = true;
271
      if(this.selectedValue && this.selectedValue.length > 0 ){
272

    
273
      console.log(this.selectedValue+" "+this.entityType )
274
      this.sub = this._search.fetchByType(this.selectedValue,this.entityType, this.properties).subscribe(
275
        data => {
276
          this.selected.push( data[0]);
277
          this.updateValueLabel.emit({
278
            value:this.showItem(this.selected[0])
279
          });
280
          this.showInput = false;
281
         },
282
        err => {
283
          //console.log("An error occured"));
284
          this.handleError("Error getting results of type: "+this.entityType+" with id: "+this.selectedValue, err);
285
        }
286
      );
287
    }
288
  }
289

    
290
    handleClick(event){
291
     var clickedComponent = event.target;
292
     var inside = false;
293
     do {
294
         if (clickedComponent === this.myElement.nativeElement) {
295
             inside = true;
296
         }
297
        clickedComponent = clickedComponent.parentNode;
298
     } while (clickedComponent);
299
      if(!inside){
300
        this.keyword = "";
301
        // this.numFilteredResults = 0;
302
        this.searchTermStream.next(this.keyword);
303
        this.focus=false;
304
      }
305
  }
306

    
307
  private handleError(message: string, error) {
308
      console.error("Autocomplete (component): "+message, error);
309
  }
310
}
(1-1/4)