Project

General

Profile

1
import {Component, Input,Output, EventEmitter, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import {ContextsService} from '../../services/contexts.service';
4
import {ClaimContext} from '../../utils/entities/claimEntities.class';
5
import { StaticAutoCompleteComponent } from '../../utils/staticAutoComplete.component';
6

    
7
@Component({
8
    // moduleId: module.id,
9
    selector: 'claim-contexts',
10
    template: `
11
        <div class="panel-body"  *ngIf="!inline">
12

    
13
          <div class="input-group" >
14
            <div class=" input-group-btn">
15
                <button class="btn btn-success dropdown-toggle" type="button" id="communityDropDown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
16
                  {{selectedCommunityLabel}}
17

    
18
                </button>
19
                <ul class="dropdown-menu" aria-labelledby="communityDropDown">
20
                  <li  (click)="communityChanged('0','Community:')"><a  >Community:</a></li>
21
                  <li *ngIf="communities"  (click)="communityChanged(communities.id, communities.label)"  ><a  >{{communities.label}}</a></li>
22

    
23
                </ul>
24
              </div>
25
              <div class="input-group-btn ">
26
                <button class="btn btn-success dropdown-toggle" type="button" id="categoryDropDown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
27
                  {{selectedCategoryLabel}}
28

    
29
                </button>
30
                <ul class="dropdown-menu" aria-labelledby="categoryDropDown">
31
                  <li  (click)="categoryChanged('0','Category:')"><a  >Category:</a></li>
32
                  <li *ngFor="let category of categories"  (click)="categoryChanged(category.id, category.label)"  ><a  >{{category.label}}</a></li>
33

    
34
                </ul>
35
              </div>
36
              <static-autocomplete [(list)] = concepts  [allowDuplicates]=true [showSelected]=false [placeHolderMessage] = "'Search for Concepts'" title = "Concepts:"  [multipleSelections]=true  (addItem) = "select($event)" > </static-autocomplete>
37
          </div>
38
         </div>
39
        <div class="panel-body"   *ngIf=" inline && showComponent ">
40
          <div class=" form-horizontal">
41
          <div    class=" form-group ">
42
              <button class="btn btn-xs btn-success dropdown-toggle" type="button" id="communityDropDown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
43
                {{selectedCommunityLabel}}
44

    
45
              </button>
46
              <ul   class="dropdown-menu custom-hidden-dropdown-menu" aria-labelledby="communityDropDown">
47
                <li  (click)="communityChanged('0','Community:')"><a  >Community:</a></li>
48
                <li *ngIf="communities"  (click)="communityChanged(communities.id, communities.label)"  ><a  >{{communities.label}}</a></li>
49

    
50
              </ul>
51
            </div>
52
            <div    class="form-group ">
53
              <button class="btn btn-xs btn-success dropdown-toggle" type="button" id="categoryDropDown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
54
                {{selectedCategoryLabel}}
55

    
56
              </button>
57
              <ul class="dropdown-menu custom-hidden-dropdown-menu" aria-labelledby="categoryDropDown">
58
                <li  (click)="categoryChanged('0','Category:')"><a  >Category:</a></li>
59
                <li *ngFor="let category of categories"  (click)="categoryChanged(category.id, category.label)"  ><a  >{{category.label}}</a></li>
60

    
61
              </ul>
62
            </div>
63
            <static-autocomplete [(list)] = concepts  [allowDuplicates]=true [showSelected]=false [placeHolderMessage] = "'Search for Concepts'" title = "Concepts:"  [multipleSelections]=true  (addItem) = "select($event)" > </static-autocomplete>
64

    
65
          </div>
66
        </div>
67

    
68
     <div *ngIf="warningMessage.length > 0" class="alert alert-warning" role="alert">{{warningMessage}}</div>
69
     <div *ngIf="infoMessage.length > 0" class="alert alert-info" role="alert">{{infoMessage}}</div>
70
        `
71

    
72
})
73
export class ClaimContextComponent {
74
@Input() public inline:boolean = false ; // for claimed started from landing pages
75
@Input() public showComponent:boolean = true ; // for claimed started from landing pages
76
@Input() public selectedList = [];
77
@Input() public selectedCommunityId:string = "0";
78
@Input() public selectedCategoryId:string ="0";
79
@Output() cselectedCommunityChange = new EventEmitter();
80
@Output() selectedCategoryChange = new EventEmitter();
81
@ViewChild (StaticAutoCompleteComponent) autocomplete : StaticAutoCompleteComponent ;
82

    
83
public query = '';
84
public filteredList = [];
85
public communities:string[];
86
public selectedCommunityLabel:string = "Community:";
87

    
88
public categories:string[];
89
public selectedCategoryLabel:string ="Category:";
90
public concepts = [];
91
public warningMessage = "";
92
public infoMessage = "";
93

    
94
ngOnInit() {
95
  this.getCommunities();
96
}
97
constructor(private _contextService: ContextsService) {
98

    
99
}
100

    
101
select($event){
102
    var item = $event.value;
103

    
104
    var context: ClaimContext= { community: this.selectedCommunityLabel, category: this.selectedCategoryLabel, concept: item };
105
    var found:boolean = false;
106
    this.warningMessage = "";
107
    for (var _i = 0; _i < this.selectedList.length; _i++) {
108
        let item = this.selectedList[_i];
109
        if(item.concept.id == context.concept.id){
110
              found=true;
111
              // this.warningMessage = "Concept already in selected list";
112
        }
113
     }
114
    if (!found) {
115
      this.selectedList.push(context);
116
    }
117
}
118

    
119
getCommunities ()  {
120
  this._contextService.getCommunities().subscribe(
121
    data => {
122
      this.communities = data.communities;
123
     },
124
    err => console.log(err)
125
  );
126
 }
127
 getCategories ()  {
128
   this.categories=[];
129
   if(this.selectedCommunityId != '0'){
130
     this._contextService.getCategories(this.selectedCommunityId).subscribe(
131
       data => {
132
         this.categories = data.category;
133
         this.concepts = [];
134
         this.filteredList = [];
135
         if (this.query !== ""){
136
           var event = {value: ""};
137
           event.value = this.query;
138
         }
139

    
140
        },
141
       err => console.log(err)
142
     );
143
   }
144
  }
145
  getConcepts ()  {
146
    if(this.selectedCategoryId != '0'){
147
      this._contextService.getConcepts(this.selectedCategoryId, "").subscribe(
148
        data => {
149
          this.concepts = data;
150
          this.autocomplete.updateList(this.concepts);
151
          if (this.query !== ""){
152
            var event = {value: ""};
153
            event.value = this.query;
154
            //  this.filter(event);
155
           }
156
         },
157
        err => console.log(err)
158
      );
159
    }else{
160
      this.concepts=[];
161
    }
162
   }
163
  communityChanged(communityId:string, communityLabel:string){
164
    this.warningMessage = "";
165
    this.infoMessage = "";
166
    if(this.selectedCommunityId != communityId){
167
      this.selectedCommunityId = communityId;
168
      this.selectedCommunityLabel = communityLabel;
169
      this.getCategories();
170
    }
171

    
172
  }
173
  categoryChanged(categoryId:string, categoryLabel:string){
174
    this.warningMessage = "";
175
    this.infoMessage = "";
176
    if(this.selectedCategoryId != categoryId){
177
      this.selectedCategoryId = categoryId;
178
      this.selectedCategoryLabel = categoryLabel;
179
      this.getConcepts();
180
    }
181

    
182
  }
183

    
184
}
(1-1/8)