Project

General

Profile

1
import {Component, Input,Output, EventEmitter, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import { Router} from '@angular/router';
4
import {ContextsService} from './service/contexts.service';
5
import {ClaimContext} from './claimEntities.class';
6
import { StaticAutoCompleteComponent } from '../../utils/staticAutoComplete/staticAutoComplete.component';
7
declare var UIkit:any;
8
import {Session} from '../../login/utils/helper.class';
9
import {ErrorCodes} from '../../login/utils/guardHelper.class';
10

    
11
@Component({
12
    // moduleId: module.id,
13
    selector: 'claim-contexts-search-form',
14
    template: `
15

    
16
          <div class=" uk-animation uk-card uk-card-default uk-padding uk-padding-large uk-padding-remove-left uk-margin-left" >
17
                <!--div uk-grid="" >
18
                 <div class="uk-width-auto@m uk-first-column ">
19
                    <div>
20
                        <select [(ngModel)]="selectedCommunityId"    (ngModelChange)="communityChanged()"  class=" "  name="select_funder"  >
21
                          <option  value="0"  >Select community</option>
22
                          <option *ngFor="let community of communities"     [value]="community.id" >{{community.label}}</option>
23
                        </select>
24
                    </div>
25
                    <div   *ngIf="selectedCommunityId != '0' && categories.length > 0">
26
                          <select  [(ngModel)]="selectedCategoryId"    (ngModelChange)="categoryChanged()" class=" "   name="select_funder"  >
27
                            <option  value="0"  >Select category</option>
28
                            <option  *ngFor="let category of categories"    [value]="category.id" >{{category.label}}</option>
29
                          </select>
30

    
31
                    </div>
32
                 </div>
33
                 <div class="uk-width-expand@m">
34

    
35
                       <static-autocomplete [(list)] = concepts  [allowDuplicates]=true [showSelected]=false [placeHolderMessage] = "'Concept name'" title = "Concepts"  [multipleSelections]=true  (addItem) = "select($event)" > </static-autocomplete>
36
                 </div>
37
                </div-->
38
              <table class="uk-width-1-1 uk-table uk-table-responsive" ><tr>
39
              <tr>
40
              <td >
41
                <select [(ngModel)]="selectedCommunityId"    (ngModelChange)="communityChanged()"  class=" "  name="select_funder"  >
42
                  <option  value="0"  >Select community</option>
43
                  <option *ngFor="let community of communities"     [value]="community.id" >{{community.label}}</option>
44
                </select>
45
                </td>
46
                <td *ngIf="selectedCommunityId != '0' && categories.length > 0">
47
                <select  [(ngModel)]="selectedCategoryId"    (ngModelChange)="categoryChanged()" class=" "   name="select_funder"  >
48
                  <option  value="0"  >Select category</option>
49
                  <option  *ngFor="let category of categories"    [value]="category.id" >{{category.label}}</option>
50
                </select>
51
                </td><td  >
52
              <static-autocomplete [(list)] = concepts  [allowDuplicates]=true [showSelected]=false [placeHolderMessage] = "'Concept name'" title = "Concepts"  [multipleSelections]=true  (addItem) = "select($event)" > </static-autocomplete>
53
                </td></tr>
54

    
55
              </table>
56
              <div *ngIf="loading" class="uk-alert uk-alert-primary" role="alert">Loading communities information...</div>
57
              <div *ngIf="error" class="uk-alert uk-alert-warning" role="alert">An error occured while loading communities...</div>
58
              <div class="uk-margin-left">
59
              <div>Or <a  uk-toggle="target: #browse; animation:  uk-animation-fade"> Browse </a> through categories </div>
60
              <div id="browse" class="uk-card uk-card-default uk-card-body uk-margin-small" hidden>
61
                  <a uk-toggle="target: #browse; animation:  uk-animation-fade" class="uk-float-right"><span class="uk-icon">
62
<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>
63
</span></a>
64
                  <div *ngIf="selectedCommunityId =='0'"  class="uk-alert uk-alert-warning" >Please select community first...</div>
65
                  <div *ngIf="selectedCommunityId !='0'" class="uk-panel uk-panel-scrollable">
66
                    <ul   class="uk-list ">
67
                      <li>
68
                       <span (click)="displaySubcategory(selectedCommunityId)"  >
69
                          <span *ngIf="!conceptsClassDisplay[selectedCommunityId]"   class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="8 5 13 10 8 15"></polygon></svg></span>
70

    
71
                          <span *ngIf="conceptsClassDisplay[selectedCommunityId]"   class="uk-icon">
72
  <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="5 7 15 7 10 12"></polygon></svg></span>{{selectedCommunityLabel}}
73
                        </span>
74
                        <a *ngIf="!isSelected(selectedCommunityId)"  (click)="addNewContext(selectedCommunityLabel,null,{'id':selectedCommunityId, 'label':selectedCommunityLabel})" class="uk-button-default uk-align-right"  class="uk-icon-button uk-icon">
75
  <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="1"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg> </a>
76
                        <ul *ngIf="conceptsClassDisplay[selectedCommunityId]" class="uk-animation-fade" >
77
                        <li *ngFor="let category of categories" >
78
                          <span (click)="browseConcepts(category.id)"  >
79
                          <span *ngIf="!conceptsClassDisplay[category.id]"   class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="8 5 13 10 8 15"></polygon></svg></span>
80

    
81
                          <span *ngIf="conceptsClassDisplay[category.id]"   class="uk-icon">
82
  <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="5 7 15 7 10 12"></polygon></svg></span>
83
                          {{category.label}}</span><a *ngIf="!isSelected(category.id)"  (click)="addNewContext(selectedCommunityLabel,category.label,{'id':category.id, 'label':category.label})" class="uk-button-default uk-align-right"  class="uk-icon-button" >
84
                          <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="1"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg></a>
85
                          <div *ngIf="conceptsCategoryLoading[category.id]" class="uk-alert uk-alert-primary" role="alert">Loading category information...</div>
86

    
87
                          <ul *ngIf="conceptsClass[category.id] && conceptsClassDisplay[category.id]" class="uk-list  uk-animation-fade"  id="ul_{{category.id}}"  >
88
                            <li *ngFor="let concept1 of conceptsClass[category.id]" >
89
                              <span   (click)="displaySubcategory(concept1.id)" >
90
                                <span *ngIf="!conceptsClassDisplay[concept1.id] && concept1.concept"   class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="8 5 13 10 8 15"></polygon></svg></span>
91

    
92
                                <span *ngIf="conceptsClassDisplay[concept1.id]  && concept1.concept"   class="uk-icon">
93
  <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="5 7 15 7 10 12"></polygon></svg></span>
94
                                {{concept1.label}}
95
                               </span>
96
                              <a  *ngIf="!isSelected(concept1.id)" (click)="addNewContext(selectedCommunityLabel,category.label,concept1)" class="uk-button-default uk-align-right"  class="uk-icon-button"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="1"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg></a>
97
                                <ul  *ngIf="concept1.concept &&  conceptsClassDisplay[concept1.id] "  class="uk-animation-fade" >
98
                                  <li *ngFor="let concept2 of concept1.concept"><span   (click)="displaySubcategory(concept2.id)" >
99
                                  <span *ngIf="!conceptsClassDisplay[concept2.id]  && concept2.concept"   class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="8 5 13 10 8 15"></polygon></svg></span>
100

    
101
                                  <span *ngIf="conceptsClassDisplay[concept2.id]  && concept2.concept"   class="uk-icon">
102
  <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="1"> <polygon points="5 7 15 7 10 12"></polygon></svg></span>
103
                                  {{concept2.label}}</span>
104
                                    <a  *ngIf="!isSelected(concept2.id)" (click)="addNewContext(selectedCommunityLabel,category.label,concept2)" class="uk-button-default uk-align-right"  class="uk-icon-button"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="1"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg></a>
105
                                    <ul  *ngIf="concept2.concept &&  conceptsClassDisplay[concept2.id] " class="uk-animation-fade" >
106
                                      <li *ngFor="let concept3 of concept2.concept">{{concept3.label}}
107
                                        <a  *ngIf="!isSelected(concept3.id)" (click)="addNewContext(selectedCommunityLabel,category.label,concept3)" class="uk-button-default uk-align-right"  class="uk-icon-button"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="plus" ratio="1"><rect x="9" y="1" width="1" height="17"></rect><rect x="1" y="9" width="17" height="1"></rect></svg></a>
108
                                      </li>
109
                                    </ul>
110
                                  </li>
111
                                </ul>
112
                            </li>
113
                          </ul>
114
                      </li>
115
                    </ul>
116
                  </ul>
117
                </div>
118
               </div>
119
              </div>
120
              <div *ngIf="warningMessage.length > 0" class="uk-alert uk-alert-warning" role="alert">{{warningMessage}}</div>
121
              <div *ngIf="infoMessage.length > 0" class="uk-alert uk-alert-primary" role="alert">{{infoMessage}}</div>
122
          </div>
123

    
124
        `
125

    
126
})
127
export class ClaimContextSearchFormComponent {
128
// @Input() public inline:boolean = false ; // for claimed started from landing pages
129
public showComponent:boolean = true ; // for claimed started from landing pages
130
@Input() public selectedList;
131
//The following need to be kept in case we have to save the current state
132
@Input() public projects;
133
@Input() public results;
134
@Input() public inlineEntity;
135
public selectedCommunityId:string = "0";
136
public selectedCategoryId:string ="0";
137
// @Output() contextSelected = new EventEmitter();
138

    
139
@ViewChild (StaticAutoCompleteComponent) autocomplete : StaticAutoCompleteComponent ;
140

    
141
public query = '';
142
public filteredList = [];
143
public communities:any;
144
public selectedCommunityLabel:string = "Community:";
145

    
146
public categories:any;
147
public selectedCategoryLabel:string ="Category:";
148
public concepts = [];
149
public conceptsClass = [];
150
public conceptsClassDisplay = [];
151
public conceptsCategoryLoading = [];
152
public warningMessage = "";
153
public infoMessage = "";
154
public loading:boolean = false;
155
public error:boolean = false;
156

    
157
ngOnInit() {
158
  this.getCommunities();
159
}
160
constructor(private _contextService: ContextsService,private router: Router) {
161

    
162
}
163

    
164
select($event){
165
    var item = $event.value;
166
    this.addNewContext( this.selectedCommunityLabel, this.selectedCategoryLabel, item);
167
  }
168
isSelected(id):boolean{
169
  for (var _i = 0; _i < this.selectedList.length; _i++) {
170
      let item = this.selectedList[_i];
171
      if(item.concept.id == id){
172
             return true;
173
            // this.warningMessage = "Concept already in selected list";
174
      }
175
   }
176
   return false;
177
}
178
  addNewContext(community,category,concept){
179
    var context: ClaimContext= { community: community, category: category, concept: concept };
180
    var found:boolean = false;
181
    this.warningMessage = "";
182
    if (!this.isSelected(context.concept.id)) {
183

    
184
      this.selectedList.push(context);
185
          UIkit.notification({
186
              message : 'A new concept is selected.',
187
              status  : 'primary',
188
              timeout : 1000,
189
              pos     : 'top-center'
190
          });
191

    
192
    }else{
193
      UIkit.notification({
194
          message : 'The concept is already on your list.',
195
          status  : 'warning',
196
          timeout : 1000,
197
          pos     : 'top-center'
198
      });
199
    }
200
}
201

    
202
getCommunities ()  {
203
  if(!Session.isValidAndRemove()){
204
    this.saveStateAndRedirectLogin();
205

    
206
  }else{
207
    this.loading = true;
208
    var token=Session.getUserJwt();
209
    this._contextService.getCommunities().subscribe(
210
      data => {
211
        this.communities = data.communities;
212
        this.loading = false;
213
       },
214
      err => {
215
        console.log(err);
216
        this.loading = false;
217
        this.error = true;
218
      }
219
    );
220
  }
221
 }
222
 getCategories ()  {
223
   this.loading = true;
224
   this.categories=[];
225
   if(this.selectedCommunityId != '0'){
226
     if(!Session.isValidAndRemove()){
227
       this.saveStateAndRedirectLogin();
228

    
229
     }else{
230
       var token=Session.getUserJwt();
231
       this._contextService.getCategories(this.selectedCommunityId).subscribe(
232
         data => {
233

    
234
           this.categories = (Array.isArray(data.category))? data.category:[data.category];
235
           this.concepts = [];
236
           this.addCommunityInConcepts();
237
           this.filteredList = [];
238
           if (this.query !== ""){
239
             var event = {value: ""};
240
             event.value = this.query;
241
           }
242
           this.loading = false;
243
          },
244
         err => {
245
           console.log(err);
246
           this.loading = false;
247
         }
248
       );
249
     }
250
   }
251
  }
252
  getConcepts ()  {
253
    this.loading = true;
254
    if(this.selectedCategoryId != '0'){
255
      if(!Session.isValidAndRemove()){
256
        this.saveStateAndRedirectLogin();
257
      }else{
258
        this.concepts = [];
259
        var token=Session.getUserJwt();
260
        this._contextService.getConcepts(this.selectedCategoryId, "",true).subscribe(
261
          data => {
262

    
263
            this.concepts =data;
264
            this.addCommunityInConcepts();
265
            if (this.query !== ""){
266
              var event = {value: ""};
267
              event.value = this.query;
268
              //  this.filter(event);
269
             }
270
             this.loading = false;
271
           },
272
          err => {
273
            console.log(err);
274
            this.loading = false;
275
          }
276
        );
277
      }
278
    }else{
279
      this.concepts=[];
280
      this.loading = false;
281
    }
282
   }
283
   displaySubcategory(id)  {
284
     if(this.conceptsClassDisplay[id] != null){
285
       this.conceptsClassDisplay[id] = !this.conceptsClassDisplay[id];
286

    
287
     }else{
288
       this.conceptsClassDisplay[id] = true;
289
     }
290
   }
291
   browseConcepts (categoryId)  {
292
       if(!Session.isValidAndRemove()){
293
         this.saveStateAndRedirectLogin();
294
       }else{
295
         if(this.conceptsClass[categoryId] != null){
296
           this.conceptsClassDisplay[categoryId] = !this.conceptsClassDisplay[categoryId];
297
           return;
298
         }else{
299
           this.conceptsClassDisplay[categoryId] = true;
300
         }
301
         this.conceptsClass[categoryId] = [];
302
         var token=Session.getUserJwt();
303
         this.conceptsCategoryLoading[categoryId] = true;
304
         this._contextService.getConcepts(categoryId, "",false).subscribe(
305
           data => {
306
            var concepts = (Array.isArray(data))? data:[data];
307
             for(var i=0;i<concepts.length; i++){
308
               console.log("Data"+concepts[i]);
309
               if(concepts[i].id.split("::").length==3){
310
                 this.conceptsClass[categoryId].push(concepts[i]);
311
               }
312
             }
313
             console.log(this.conceptsClass[categoryId]);
314
             this.conceptsCategoryLoading[categoryId] = false;
315
            },
316
           err => {
317
             console.log(err);
318
             this.conceptsCategoryLoading[categoryId] = false;
319
           }
320
         );
321
       }
322

    
323
    }
324
  communityChanged(){
325
    console.log(this.selectedCommunityId +"  ");
326
    this.warningMessage = "";
327
    this.infoMessage = "";
328
    for(var i = 0; i< this.communities.length; i++){
329
      if(this.communities[i].id==this.selectedCommunityId){
330
        this.selectedCommunityLabel = this.communities[i].label;
331
        break;
332
      }
333
    }
334
    this.selectedCategoryId = "0";
335
    this.selectedCategoryLabel="Select Category:";
336
      this.getCategories();
337
  }
338
  categoryChanged(){
339
    this.warningMessage = "";
340
    this.infoMessage = "";
341
    for(var i = 0; i< this.categories.length; i++){
342
      if(this.categories[i].id==this.selectedCategoryId){
343
        this.selectedCategoryLabel = this.categories[i].label;
344
        break;
345
      }
346
    }
347
      this.getConcepts();
348
  }
349
addCommunityInConcepts(){
350
   this.concepts.push({"id":this.selectedCommunityId, "label":this.selectedCommunityLabel});
351
  this.autocomplete.updateList(this.concepts);
352
}
353
saveStateAndRedirectLogin(){
354
  if(this.projects != null){
355
   localStorage.setItem("projects", JSON.stringify(this.projects));
356
 }
357
 localStorage.setItem("contexts", JSON.stringify(this.selectedList));
358
 if(this.results != null){
359
   localStorage.setItem("results", JSON.stringify(this.results));
360
 }
361
 if(this.inlineEntity != null){
362
   localStorage.setItem("inlineEntity", JSON.stringify(this.inlineEntity));
363
 }
364

    
365
  this.router.navigate(['/user-info'], { queryParams: { "errorCode": ErrorCodes.NOT_VALID, "redirectUrl":  this.router.url } });
366
}
367
}
(2-2/11)