Project

General

Profile

1
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
//Usage example
5
//<static-autocomplete [(filtered)] =filtered [(selected)] =selected placeHolderMessage = "Search for countries" title = "Countries:" (keywordChange)="keywordChanged($event)"></static-autocomplete>
6

    
7
@Component({
8
    selector: 'static-autocomplete',
9
    styleUrls:  ['autoComplete.component.css'],
10
    template: `
11
  <div class="bs-docs-grid">
12
    <div class = "row-fluid  form-inline auto-complete-box panel panel-default">
13
      <div class="panel-heading">{{title}}</div>
14
      <div class="panel-body">
15
        <span *ngIf = "showSelected">
16
          <span class="row-fluid show-grid auto-complete-choice" *ngFor="let item of selected" >
17
            <span >{{showItem(item)}} </span>
18
            <span (click)="remove(item)" aria-hidden="true" class=" remove glyphicon glyphicon-remove"></span>
19
          </span>
20
        </span>
21
        <input  type="text" class="auto-complete-input validate filter-input input-sm form-control  " [placeholder]=placeHolderMessage [(ngModel)]=keyword (keyup)=filter()   >
22
        <div class="suggestions" *ngIf="filtered.length > 0">
23
              <ul class="list-group" >
24
                  <li  class="list-group-item"   *ngFor=" let item of filtered">
25
                      <a (click)="select(item)">{{showItem(item)}}</a>
26
                  </li>
27
              </ul>
28
        </div>
29
        <div *ngIf="warningMessage.length > 0" class="alert alert-warning row-fluid " role="alert">{{warningMessage}}</div>
30
        <div *ngIf="filtered.length == 0 && keyword.length >=3 " class="alert alert-info row-fluid " role="alert">No results Found</div>
31
      </div>
32
    </div>
33
  </div>
34

    
35
        `
36
})
37
export class StaticAutocompleteComponent {
38
    @Input() placeHolderMessage = "Search for entries";
39
    @Input() title = "Autocomplete";
40
    @Output() keywordChange = new EventEmitter(); // when changed  a method for filtering will be called
41
    @Output() addNew = new EventEmitter(); // when changed  a method for filtering will be called
42

    
43
    @Input() public filtered = []; // the entries resulted after filtering function
44
    @Input() public selected = []; // the entries selected from user
45
    @Input() public keywordlimit = 3; // the minimum length of keyword
46
    @Input() public showSelected = true; // the minimum length of keyword
47

    
48
    @Input() public keyword = '';
49
    @Input() public type = 'search' //search, result, context, project
50
    private warningMessage = "";
51
    private infoMessage = "";
52

    
53
    private tries = 0;
54

    
55
    constructor () {
56
      console.info("Type"+this.type);
57
    }
58
    filter() {
59
      this.infoMessage = "";
60
      this.filtered = [];
61
      if(this.keyword == ""){
62
        this.tries = 0;
63
        this.warningMessage = "";
64
      } else if(this.keyword && this.keyword.length < this.keywordlimit){
65
        this.tries++;
66
        if(this.tries == this.keywordlimit -1 ){
67
          this.warningMessage = "Type at least " + this.keywordlimit + " characters";
68
          this.tries = 0;
69
        }
70
      }else{
71
        this.tries = 0;
72
        this.warningMessage = "";
73
        this.keywordChange.emit({
74
           value: this.keyword
75
         });
76

    
77
      }
78
    }
79
    remove(item:any){
80
      var index:number =this.checkIfExists(item,this.selected);
81
       if (index > -1) {
82
          this.selected.splice(index, 1);
83
      }
84

    
85
    }
86
    select(item:any){
87

    
88
        var index:number =this.checkIfExists(item,this.selected);
89
         if (index > -1) {
90
            this.keyword = "";
91
            this.filtered.splice(0, this.filtered.length);
92
            return;
93
        }
94
        else{
95
            this.selected.push(item);
96
            this.keyword = "";
97
            this.filtered.splice(0, this.filtered.length);
98
        }
99
    }
100
    private checkIfExists(item:any,list):number{
101

    
102
       if(item.concept && item.concept.id ){
103
        console.log("context");
104

    
105
        for (var _i = 0; _i < list.length; _i++) {
106
            let itemInList = list[_i];
107
            if(item.concept.id == itemInList.concept.id){
108
                 return _i;
109
            }
110
         }
111
      }else if(item.id){
112
        for (var _i = 0; _i < list.length; _i++) {
113
            let itemInList = list[_i];
114
             if(item.id == itemInList.id){
115
                 return _i;
116
            }
117
         }
118
      }
119
      return -1;
120

    
121
    }
122
    showItem(item:any):string{
123

    
124
     if (item.name){ //search
125
         return item.name;
126
      }else if( item.concept && item.concept.label){ //context
127
         return item.concept.label;
128
      }else if (item.label){ //simple
129
         return item.label;
130
      }
131

    
132
    }
133

    
134

    
135
    // handleClick(event){
136
    //  var clickedComponent = event.target;
137
    //  var inside = false;
138
    //  do {
139
    //      if (clickedComponent === this.elementRef.nativeElement) {
140
    //          inside = true;
141
    //      }
142
    //     clickedComponent = clickedComponent.parentNode;
143
    //  } while (clickedComponent);
144
    //   if(!inside){
145
    //       this.filteredList = [];
146
    //   }
147
    // }
148

    
149
}
(11-11/12)