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-padding uk-panel  uk-background-muted " >
17
              <div>Search for Communities:</div>
18
              <table>
19
              <tr>
20
              <td >
21
                <select  class="custom-select-mini"  name="select_funder"  >
22
                  <option  value="0" (click)="communityChanged('0','Community:')">Select Community:</option>
23
                  <option *ngFor="let community of communities"   (click)="communityChanged(community.id, community.label)"  [value]="community.id" >{{community.label}}</option>
24
                </select>
25
                </td>
26
                <td *ngIf="selectedCommunityId != '0' && categories.length > 0"><select  class="custom-select-mini"   name="select_funder"  >
27
                  <option  value="0" (click)="categoryChanged('0','Category:')">Select Category:</option>
28
                  <option  *ngFor="let category of categories"  (click)="categoryChanged(category.id, category.label)"  [value]="category.id" >{{category.label}}</option>
29
                </select>
30
                </td><td  >
31
              <static-autocomplete [(list)] = concepts  [allowDuplicates]=true [showSelected]=false [placeHolderMessage] = "'Type keywords...'" title = "Concepts:"  [multipleSelections]=true  (addItem) = "select($event)" > </static-autocomplete>
32
                </td></tr>
33

    
34
              </table>
35
              <div *ngIf="loading" class="uk-alert uk-alert-primary" role="alert">Loading communities information...</div>
36
              <div *ngIf="error" class="uk-alert uk-alert-warning" role="alert">An error occured while loading communities...</div>
37
              <div>Or <a  uk-toggle="target: #browse; animation:  uk-animation-fade"> Browse </a> through categories </div>
38
              <div id="browse" class="uk-card uk-card-default uk-card-body uk-margin-small" hidden>
39
                  <a uk-toggle="target: #browse; animation:  uk-animation-fade" class="uk-float-right"><span class="uk-icon">
40
<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>
41
</span></a>
42
                  <div *ngIf="selectedCommunityId =='0'"  class="uk-alert uk-alert-warning" >Please select community first...</div>
43
                  <ul  *ngIf="selectedCommunityId !='0'" class="uk-list ">
44
                    <li>
45
                     <span (click)="displaySubcategory(selectedCommunityId)"  >
46
                        <span *ngIf="!conceptsClassDisplay[selectedCommunityId]"   class="uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="triangle-down" ratio="1"><polygon points="5 7 15 7 10 12"></polygon></svg></span>
47

    
48
                        <span *ngIf="conceptsClassDisplay[selectedCommunityId]"   class="uk-icon">
49
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="triangle-up" ratio="1"><polygon points="5 13 10 8 15 13"></polygon></svg></span>{{selectedCommunityLabel}}
50
                      </span>
51
                      <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">
52
<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>
53
                      <ul *ngIf="conceptsClassDisplay[selectedCommunityId]" class="uk-animation-fade" >
54
                      <li *ngFor="let category of categories" >
55
                        <span (click)="browseConcepts(category.id)"  >
56
                        <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" icon="triangle-down" ratio="1"><polygon points="5 7 15 7 10 12"></polygon></svg></span>
57

    
58
                        <span *ngIf="conceptsClassDisplay[category.id]"   class="uk-icon">
59
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="triangle-up" ratio="1"><polygon points="5 13 10 8 15 13"></polygon></svg></span>
60
                        {{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" >
61
                        <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>
62
                        <div *ngIf="conceptsCategoryLoading[category.id]" class="uk-alert uk-alert-primary" role="alert">Loading category information...</div>
63

    
64
                        <ul *ngIf="conceptsClass[category.id] && conceptsClassDisplay[category.id]" class="uk-list  uk-animation-fade"  id="ul_{{category.id}}"  >
65
                          <li *ngFor="let concept1 of conceptsClass[category.id]" >
66
                            <span   (click)="displaySubcategory(concept1.id)" >
67
                              <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" icon="triangle-down" ratio="1"><polygon points="5 7 15 7 10 12"></polygon></svg></span>
68

    
69
                              <span *ngIf="conceptsClassDisplay[concept1.id]  && concept1.concept"   class="uk-icon">
70
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="triangle-up" ratio="1"><polygon points="5 13 10 8 15 13"></polygon></svg></span>
71
                              {{concept1.label}}
72
                             </span>
73
                            <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>
74
                              <ul  *ngIf="concept1.concept &&  conceptsClassDisplay[concept1.id] "  class="uk-animation-fade" >
75
                                <li *ngFor="let concept2 of concept1.concept"><span   (click)="displaySubcategory(concept2.id)" >
76
                                <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" icon="triangle-down" ratio="1"><polygon points="5 7 15 7 10 12"></polygon></svg></span>
77

    
78
                                <span *ngIf="conceptsClassDisplay[concept2.id]  && concept2.concept"   class="uk-icon">
79
<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="triangle-up" ratio="1"><polygon points="5 13 10 8 15 13"></polygon></svg></span>
80
                                {{concept2.label}}</span>
81
                                  <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>
82
                                  <ul  *ngIf="concept2.concept &&  conceptsClassDisplay[concept2.id] " class="uk-animation-fade" >
83
                                    <li *ngFor="let concept3 of concept2.concept">{{concept3.label}}
84
                                      <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>
85
                                    </li>
86
                                  </ul>
87
                                </li>
88
                              </ul>
89
                          </li>
90
                        </ul>
91
                    </li>
92
                  </ul>
93
                </ul>
94
               </div>
95
              <div *ngIf="warningMessage.length > 0" class="uk-alert uk-alert-warning" role="alert">{{warningMessage}}</div>
96
              <div *ngIf="infoMessage.length > 0" class="uk-alert uk-alert-primary" role="alert">{{infoMessage}}</div>
97
          </div>
98

    
99
        `
100

    
101
})
102
export class ClaimContextSearchFormComponent {
103
// @Input() public inline:boolean = false ; // for claimed started from landing pages
104
public showComponent:boolean = true ; // for claimed started from landing pages
105
@Input() public selectedList;
106
//The following need to be kept in case we have to save the current state
107
@Input() public projects;
108
@Input() public results;
109
@Input() public inlineEntity;
110
public selectedCommunityId:string = "0";
111
public selectedCategoryId:string ="0";
112
// @Output() contextSelected = new EventEmitter();
113

    
114
@ViewChild (StaticAutoCompleteComponent) autocomplete : StaticAutoCompleteComponent ;
115

    
116
public query = '';
117
public filteredList = [];
118
public communities:string[];
119
public selectedCommunityLabel:string = "Community:";
120

    
121
public categories:string[];
122
public selectedCategoryLabel:string ="Category:";
123
public concepts = [];
124
public conceptsClass = [];
125
public conceptsClassDisplay = [];
126
public conceptsCategoryLoading = [];
127
public warningMessage = "";
128
public infoMessage = "";
129
public loading:boolean = false;
130
public error:boolean = false;
131

    
132
ngOnInit() {
133
  this.getCommunities();
134
}
135
constructor(private _contextService: ContextsService,private router: Router) {
136

    
137
}
138

    
139
select($event){
140
    var item = $event.value;
141
    this.addNewContext( this.selectedCommunityLabel, this.selectedCategoryLabel, item);
142
  }
143
isSelected(id):boolean{
144
  for (var _i = 0; _i < this.selectedList.length; _i++) {
145
      let item = this.selectedList[_i];
146
      if(item.concept.id == id){
147
             return true;
148
            // this.warningMessage = "Concept already in selected list";
149
      }
150
   }
151
   return false;
152
}
153
  addNewContext(community,category,concept){
154
    var context: ClaimContext= { community: community, category: category, concept: concept };
155
    var found:boolean = false;
156
    this.warningMessage = "";
157
    if (!this.isSelected(context.concept.id)) {
158

    
159
      this.selectedList.push(context);
160
          UIkit.notification({
161
              message : 'A context is selected.',
162
              status  : 'info',
163
              timeout : 1000,
164
              pos     : 'top-center'
165
          });
166

    
167
    }else{
168
      UIkit.notification({
169
          message : 'The context is already on your list.',
170
          status  : 'warning',
171
          timeout : 1000,
172
          pos     : 'top-center'
173
      });
174
    }
175
}
176

    
177
getCommunities ()  {
178
  if(!Session.isValidAndRemove()){
179
    this.saveStateAndRedirectLogin();
180

    
181
  }else{
182
    this.loading = true;
183
    var token=Session.getUserJwt();
184
    this._contextService.getCommunities().subscribe(
185
      data => {
186
        this.communities = data.communities;
187
        console.log(this.communities);
188
        this.loading = false;
189
       },
190
      err => {
191
        console.log(err);
192
        this.loading = false;
193
        this.error = true;
194
      }
195
    );
196
  }
197
 }
198
 getCategories ()  {
199
   console.log(" Getting Categories... ");
200
   this.loading = true;
201
   this.categories=[];
202
   if(this.selectedCommunityId != '0'){
203
     if(!Session.isValidAndRemove()){
204
       this.saveStateAndRedirectLogin();
205

    
206
     }else{
207
       var token=Session.getUserJwt();
208
       this._contextService.getCategories(this.selectedCommunityId).subscribe(
209
         data => {
210
           this.categories = data.category;
211
           this.concepts = [];
212
           this.addCommunityInConcepts();
213
           this.filteredList = [];
214
           if (this.query !== ""){
215
             var event = {value: ""};
216
             event.value = this.query;
217
           }
218
           this.loading = false;
219
          },
220
         err => {
221
           console.log(err);
222
           this.loading = false;
223
         }
224
       );
225
     }
226
   }
227
  }
228
  getConcepts ()  {
229
    this.loading = true;
230
    if(this.selectedCategoryId != '0'){
231
      if(!Session.isValidAndRemove()){
232
        this.saveStateAndRedirectLogin();
233
      }else{
234
        this.concepts = [];
235
        this.addCommunityInConcepts();
236
        var token=Session.getUserJwt();
237
        this._contextService.getConcepts(this.selectedCategoryId, "",true).subscribe(
238
          data => {
239
            this.concepts = data;
240
            this.addCommunityInConcepts();
241
            if (this.query !== ""){
242
              var event = {value: ""};
243
              event.value = this.query;
244
              //  this.filter(event);
245
             }
246
             this.loading = false;
247
           },
248
          err => {
249
            console.log(err);
250
            this.loading = false;
251
          }
252
        );
253
      }
254
    }else{
255
      this.concepts=[];
256
      this.loading = false;
257
    }
258
   }
259
   displaySubcategory(id)  {
260
     if(this.conceptsClassDisplay[id] != null){
261
       this.conceptsClassDisplay[id] = !this.conceptsClassDisplay[id];
262

    
263
     }else{
264
       this.conceptsClassDisplay[id] = true;
265
     }
266
     console.log("displaySubcategory: "+id+" value:"+ this.conceptsClassDisplay[id]);
267
   }
268
   browseConcepts (categoryId)  {
269
       if(!Session.isValidAndRemove()){
270
         this.saveStateAndRedirectLogin();
271
       }else{
272
         if(this.conceptsClass[categoryId] != null){
273
           this.conceptsClassDisplay[categoryId] = !this.conceptsClassDisplay[categoryId];
274
           return;
275
         }else{
276
           this.conceptsClassDisplay[categoryId] = true;
277
         }
278
         this.conceptsClass[categoryId] = [];
279
         var token=Session.getUserJwt();
280
         this.conceptsCategoryLoading[categoryId] = true;
281
         this._contextService.getConcepts(categoryId, "",false).subscribe(
282
           data => {
283
             for(var i=0;i<data.length; i++){
284
               console.log("Data"+data[i]);
285
               if(data[i].id.split("::").length==3){
286
                 this.conceptsClass[categoryId].push(data[i]);
287
               }
288
             }
289
             console.log(this.conceptsClass[categoryId]);
290
             this.conceptsCategoryLoading[categoryId] = false;
291
            },
292
           err => {
293
             console.log(err);
294
             this.conceptsCategoryLoading[categoryId] = false;
295
           }
296
         );
297
       }
298

    
299
    }
300
  communityChanged(communityId:string, communityLabel:string){
301
    console.log(communityId +"  "+communityLabel);
302
    console.log(this.selectedCommunityId +"  ");
303
    this.warningMessage = "";
304
    this.infoMessage = "";
305
    if(this.selectedCommunityId != communityId){
306
      this.selectedCommunityId = communityId;
307
      this.selectedCommunityLabel = communityLabel;
308
      this.getCategories();
309
    }
310

    
311
  }
312
  categoryChanged(categoryId:string, categoryLabel:string){
313
    this.warningMessage = "";
314
    this.infoMessage = "";
315
    if(this.selectedCategoryId != categoryId){
316
      this.selectedCategoryId = categoryId;
317
      this.selectedCategoryLabel = categoryLabel;
318
      this.getConcepts();
319
    }
320

    
321
  }
322
addCommunityInConcepts(){
323
   this.concepts.push({"id":this.selectedCommunityId, "label":this.selectedCommunityLabel});
324
  this.autocomplete.updateList(this.concepts);
325
}
326
saveStateAndRedirectLogin(){
327
  if(this.projects != null){
328
   localStorage.setItem("projects", JSON.stringify(this.projects));
329
 }
330
 localStorage.setItem("contexts", JSON.stringify(this.selectedList));
331
 if(this.results != null){
332
   localStorage.setItem("results", JSON.stringify(this.results));
333
 }
334
 if(this.inlineEntity != null){
335
   localStorage.setItem("inlineEntity", JSON.stringify(this.inlineEntity));
336
 }
337

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