Project

General

Profile

1
import {Component, Input} from '@angular/core';
2
import {Router} from '@angular/router';
3
import {ContextsService} from './service/contexts.service';
4
import {ClaimContext, ClaimEntity} from './claimHelper.class';
5
import {Session} from '../../login/utils/helper.class';
6
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
7
import {EnvProperties} from '../../utils/properties/env-properties';
8

    
9
declare var UIkit: any;
10

    
11
@Component({
12
  selector: 'claim-contexts-search-form',
13
  templateUrl: 'claimContextSearchForm.component.html'
14

    
15
})
16
export class ClaimContextSearchFormComponent {
17
  @Input() public results:ClaimEntity[];
18
  @Input() public sources;
19
  @Input() public properties: EnvProperties;
20
  @Input() communityId: string = null;
21
  @Input() public inlineClaim: boolean = false;
22

    
23
  public selectedCommunityId: string = "0";
24
  public selectedCategoryId: string = "0";
25
  public query = '';
26
  public communities: any;
27
  public selectedCommunityLabel: string = "Community:";
28

    
29
  public categories: any = [];
30
  public concepts = [];
31
  public conceptsClass = [];
32
  public conceptsClassDisplay = [];
33
  public conceptsCategoryLoading = [];
34
  public warningMessage = "";
35
  public infoMessage = "";
36
  public loading: boolean = false;
37
  public error: boolean = false;
38
  @Input() localStoragePrefix: string = "";
39

    
40
  keyword = "";
41

    
42
  ngOnInit() {
43
    this.getCommunities();
44
  }
45

    
46
  constructor(private _contextService: ContextsService, private router: Router) {
47

    
48
  }
49

    
50
  getCommunityClass(community) {
51
    let addclass = "";
52
    if (this.isSelected(community.id)) {
53
      addclass += " contextlabelSelected ";
54
    } else {
55
      addclass += " contextlabelNotSelected ";
56
    }
57
    if (this.keyword.length > 0) {
58
      if (community.label.toLowerCase().indexOf(this.keyword.toLowerCase()) == -1) {
59
        addclass += " fadeOut "
60
      }
61
    }
62
    return addclass + 'uk-label';
63
  }
64

    
65
  select(community) {
66
    this.selectedCommunityId = community.id;
67
    this.selectedCommunityLabel = community.label;
68
    this.getCategories();
69
    if (this.isSelected(community.id)) {
70

    
71
    } else {
72
      this.addNewContext(community.label, null, {'id': community.id, 'label': community.label});
73
    }
74
  }
75

    
76
  isSelected(id): boolean {
77
    for (let _i = 0; _i < this.results.length; _i++) {
78
      let item = this.results[_i];
79
      if (item.id == id) {
80
        return true;
81
        // this.warningMessage = "Concept already in selected list";
82
      }
83
    }
84
    return false;
85
  }
86

    
87
  addNewContext(community, category, concept, notify = true) {
88
    if (this.results.length > 50) {
89
      UIkit.notification({
90
        message: 'Your basket exceeds the number of allowed concepts (50)',
91
        status: 'warning',
92
        timeout: 1500,
93
        pos: 'top-center'
94
      });
95
      return;
96
    }
97
    const entity: ClaimEntity = new ClaimEntity() ;
98
    entity.type = "community";
99

    
100
    entity.context = {community: community, category: category, concept: concept};
101
    entity.id = entity.context.concept.id;
102
    this.warningMessage = "";
103
    if (!this.isSelected(entity.id)) {
104
      this.results.push(entity);
105
      localStorage.setItem(this.localStoragePrefix, JSON.stringify(this.results));
106
    }
107

    
108
  }
109

    
110
  getCommunities() {
111
    if (!Session.isLoggedIn()) {
112
      this.saveStateAndRedirectLogin();
113

    
114
    } else {
115
      this.loading = true;
116
      this._contextService.getPublicCommunities(this.properties.contextsAPI).subscribe(
117
        data => {
118
          this.communities = data;
119
          if (this.communities.length > 0) {
120
            this.communities.sort((n1, n2) => n1.label > n2.label);
121
          }
122
          this.loading = false;
123
          if (this.communityId != null) {
124
            //preselect community
125
            this.selectedCommunityId = this.communityId;
126
            for (let i = 0; i < this.communities.length; i++) {
127
              if (this.communities[i].id == this.selectedCommunityId) {
128
                this.selectedCommunityLabel = this.communities[i].label;
129
                break;
130
              }
131
            }
132
            this.addNewContext(this.selectedCommunityLabel, null, {
133
              'id': this.selectedCommunityId,
134
              'label': this.selectedCommunityLabel
135
            }, false)
136

    
137
          }
138
        },
139
        err => {
140
          //console.log(err);
141
          ClaimContextSearchFormComponent.handleError("Error getting communities", err);
142
          this.loading = false;
143
          this.error = true;
144
        }
145
      );
146
    }
147
  }
148

    
149
  getCategories() {
150
    this.loading = true;
151
    // this.categories=[];
152
    if (this.selectedCommunityId != '0') {
153
      if (!Session.isLoggedIn()) {
154
        this.saveStateAndRedirectLogin();
155

    
156
      } else {
157
        if (this.categories[this.selectedCommunityId]) {
158
          this.loading = false;
159
          return;
160
        }
161
        this._contextService.getCategories(this.selectedCommunityId, this.properties.contextsAPI).subscribe(
162
          data => {
163

    
164
            this.categories[this.selectedCommunityId] = data;
165
            this.concepts = [];
166
            if (this.query !== "") {
167
              const event = {value: ""};
168
              event.value = this.query;
169
            }
170
            this.loading = false;
171
          },
172
          err => {
173
            //console.log(err);
174
            ClaimContextSearchFormComponent.handleError("Error getting categories for community with id: " + this.selectedCommunityId, err);
175
            this.loading = false;
176
          }
177
        );
178
      }
179
    }
180
  }
181
/*
182
  getConcepts() {
183
    this.loading = true;
184
    if (this.selectedCategoryId != '0') {
185
      if (!Session.isLoggedIn()) {
186
        this.saveStateAndRedirectLogin();
187
      } else {
188
        this.concepts = [];
189

    
190
        this._contextService.getConcepts(this.selectedCategoryId, "", true, this.properties.contextsAPI).subscribe(
191
          data => {
192

    
193
            this.concepts = data;
194
            for (var i = 0; i < data.length; i++) {
195
              if (data[i].hasSubConcept == true) {
196
                this.getSubConcepts(data[i].id);
197
              }
198
            }
199
            this.addCommunityInConcepts();
200
            if (this.query !== "") {
201
              var event = {value: ""};
202
              event.value = this.query;
203
              //  this.filter(event);
204
            }
205
            this.loading = false;
206
          },
207
          err => {
208
            //console.log(err);
209
            this.handleError("Error getting concepts for category with id: " + this.selectedCategoryId, err);
210
            this.loading = false;
211
          }
212
        );
213
      }
214
    } else {
215
      this.concepts = [];
216
      this.loading = false;
217
    }
218
  }*/
219
/*
220
  getSubConcepts(conceptId) {
221
    this.loading = true;
222
    if (this.selectedCategoryId != '0') {
223
      if (!Session.isLoggedIn()) {
224
        this.saveStateAndRedirectLogin();
225
      } else {
226
        this._contextService.getSubConcepts(conceptId, "", true, this.properties.contextsAPI).subscribe(
227
          data => {
228
            for (var i = 0; i < data.length; i++) {
229
              this.concepts.push(data[i]);
230
            }
231

    
232
            if (this.query !== "") {
233
              var event = {value: ""};
234
              event.value = this.query;
235
              //  this.filter(event);
236
            }
237
            this.loading = false;
238
          },
239
          err => {
240
            //console.log(err);
241
            this.handleError("Error getting subconcepts for concept with id: " + conceptId, err);
242
            this.loading = false;
243
          }
244
        );
245
      }
246
    } else {
247
      this.concepts = [];
248
      this.loading = false;
249
    }
250
  }*/
251

    
252
  displaySubcategory(id) {
253
    if (this.conceptsClassDisplay[id] != null) {
254
      this.conceptsClassDisplay[id] = !this.conceptsClassDisplay[id];
255

    
256
    } else {
257
      this.conceptsClassDisplay[id] = true;
258
    }
259
  }
260

    
261
  browseConcepts(categoryId) {
262
    if (!Session.isLoggedIn()) {
263
      this.saveStateAndRedirectLogin();
264
    } else {
265
      if (this.conceptsClass[categoryId] != null) {
266
        this.conceptsClassDisplay[categoryId] = !this.conceptsClassDisplay[categoryId];
267
        return;
268
      } else {
269
        this.conceptsClassDisplay[categoryId] = true;
270
      }
271
      this.conceptsClass[categoryId] = [];
272
      this.conceptsCategoryLoading[categoryId] = true;
273
      this._contextService.getConcepts(categoryId, "", false, this.properties.contextsAPI).subscribe(
274
        data => {
275
          // var concepts = data;
276
          this.conceptsClass[categoryId] = [];
277
          for (let i = 0; i < data.length; i++) {
278
            if (data[i].hasSubConcept == true) {
279
              this.browseSubConcepts(categoryId, data[i].id);
280
            } else {
281
              this.conceptsClass[categoryId].push(data[i]);
282
            }
283
          }
284
          this.conceptsCategoryLoading[categoryId] = false;
285
        },
286
        err => {
287
          //console.log(err);
288
          ClaimContextSearchFormComponent.handleError("Error getting concepts for category with id: " + this.selectedCategoryId, err);
289
          this.conceptsCategoryLoading[categoryId] = false;
290
        }
291
      );
292
    }
293

    
294
  }
295

    
296
  browseSubConcepts(categoryId, conceptId) {
297

    
298
    this.conceptsCategoryLoading[categoryId] = true;
299
    this._contextService.getSubConcepts(conceptId, "", false, this.properties.contextsAPI).subscribe(
300
      data => {
301
        const concepts = data[0];
302
        this.conceptsClass[categoryId].push(concepts);
303
        this.conceptsCategoryLoading[categoryId] = false;
304
      },
305
      err => {
306
        //console.log(err);
307
        ClaimContextSearchFormComponent.handleError("Error getting subconcepts for concept with id: " + conceptId, err);
308
        this.conceptsCategoryLoading[categoryId] = false;
309
      }
310
    );
311

    
312

    
313
  }
314

    
315
  // communityChanged(){
316
  //   this.warningMessage = "";
317
  //   this.infoMessage = "";
318
  //   for(var i = 0; i< this.communities.length; i++){
319
  //     if(this.communities[i].id==this.selectedCommunityId){
320
  //       this.selectedCommunityLabel = this.communities[i].label;
321
  //       break;
322
  //     }
323
  //   }
324
  //   this.selectedCategoryId = "0";
325
  //   this.selectedCategoryLabel="Select Category:";
326
  //   if(this.selectedCommunityId != "0"){
327
  //     this.getCategories();
328
  //   }
329
  // }
330
  // categoryChanged(){
331
  //   this.warningMessage = "";
332
  //   this.infoMessage = "";
333
  //   for(var i = 0; i< this.categories.length; i++){
334
  //     if(this.categories[i].id==this.selectedCategoryId){
335
  //       this.selectedCategoryLabel = this.categories[i].label;
336
  //       break;
337
  //     }
338
  //   }
339
  //     this.getConcepts();
340
  // }
341
/*  addCommunityInConcepts() {
342
    this.concepts.push({"id": this.selectedCommunityId, "label": this.selectedCommunityLabel});
343
    // if(this.autocomplete){
344
    //   this.autocomplete.updateList(this.concepts);
345
    // }
346
  }*/
347

    
348
  saveStateAndRedirectLogin() {
349
    //  if(this.projects != null){
350
    //   localStorage.setItem(this.localStoragePrefix + "projects", JSON.stringify(this.projects));
351
    // }
352
    // localStorage.setItem(this.localStoragePrefix + "contexts", JSON.stringify(this.selectedList));
353
    if (this.results != null) {
354
      localStorage.setItem(this.localStoragePrefix + "results", JSON.stringify(this.results));
355
    }
356
    if (this.sources != null) {
357
      localStorage.setItem(this.localStoragePrefix + "sources", JSON.stringify(this.sources));
358
    }
359

    
360
    this.router.navigate(['/user-info'], {
361
      queryParams: {
362
        "errorCode": LoginErrorCodes.NOT_VALID,
363
        "redirectUrl": this.router.url
364
      }
365
    });
366
  }
367

    
368
  private static handleError(message: string, error) {
369
    console.error("Claim context search form (component): " + message, error);
370
  }
371
}
(3-3/15)