Project

General

Profile

1 61381 k.triantaf
import {Component, Input} from '@angular/core';
2
import {Router} from '@angular/router';
3
import {ContextsService} from './service/contexts.service';
4
import {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
import {Subscriber} from "rxjs";
9
10
declare var UIkit: any;
11
12
@Component({
13
  selector: 'claim-contexts-search-form',
14
  templateUrl: 'claimContextSearchForm.component.html'
15
16
})
17
export class ClaimContextSearchFormComponent {
18
  @Input() public results:ClaimEntity[];
19
  @Input() public sources;
20
  @Input() public properties: EnvProperties;
21
  @Input() communityId: string = null;
22
  @Input() public inlineClaim: boolean = false;
23
  @Input() basketLimit;
24
25
  public selectedCommunityId: string = "0";
26
  public selectedCategoryId: string = "0";
27
  public query = '';
28
  public communities: any;
29
  public selectedCommunityLabel: string = "Community:";
30
31
  public categories: any = [];
32
  public concepts = [];
33
  public conceptsClass = [];
34
  public conceptsClassDisplay = [];
35
  public conceptsCategoryLoading = [];
36
  public warningMessage = "";
37
  public infoMessage = "";
38
  public loading: boolean = false;
39
  public error: boolean = false;
40
  @Input() localStoragePrefix: string = "";
41
42
  keyword = "";
43
  subscriptions = [];
44
  ngOnInit() {
45
    this.getCommunities();
46
  }
47
  ngOnDestroy() {
48
    this.subscriptions.forEach(subscription => {
49
      if (subscription instanceof Subscriber) {
50
        subscription.unsubscribe();
51
      }
52
    });
53
  }
54
  constructor(private _contextService: ContextsService, private router: Router) {
55
56
  }
57
58
  getCommunityClass(community) {
59
    let addclass = "";
60
    if (this.isSelected(community.id)) {
61
      addclass += " contextlabelSelected ";
62
    } else {
63
      addclass += " contextlabelNotSelected ";
64
    }
65
    if (this.keyword.length > 0) {
66
      if (community.label.toLowerCase().indexOf(this.keyword.toLowerCase()) == -1) {
67
        addclass += " fadeOut "
68
      }
69
    }
70
    return addclass + 'uk-label';
71
  }
72
73
  select(community) {
74
    this.selectedCommunityId = community.id;
75
    this.selectedCommunityLabel = community.label;
76
    this.getCategories();
77
    if (this.isSelected(community.id)) {
78
79
    } else {
80
      this.addNewContext(community.label, null, {'id': community.id, 'label': community.label});
81
    }
82
  }
83
84
  isSelected(id): boolean {
85
    for (let _i = 0; _i < this.results.length; _i++) {
86
      let item = this.results[_i];
87
      if (item.id == id) {
88
        return true;
89
        // this.warningMessage = "Concept already in selected list";
90
      }
91
    }
92
    return false;
93
  }
94
95
  addNewContext(community, category, concept, notify = true) {
96
    // if (this.results.length > 50) {
97
    //   UIkit.notification({
98
    //     message: 'Your basket exceeds the number of allowed concepts (50)',
99
    //     status: 'warning',
100
    //     timeout: 1500,
101
    //     pos: 'top-center'
102
    //   });
103
    //   return;
104
    // }
105
    const entity: ClaimEntity = new ClaimEntity() ;
106
    entity.type = "community";
107
108
    entity.context = {community: community, category: category, concept: concept};
109
    entity.id = entity.context.concept.id;
110
    this.warningMessage = "";
111
    if (!this.isSelected(entity.id)) {
112
      this.results.push(entity);
113
      localStorage.setItem(this.localStoragePrefix, JSON.stringify(this.results));
114
    }
115
116
  }
117
118
  getCommunities() {
119
    if (!Session.isLoggedIn()) {
120
      this.saveStateAndRedirectLogin();
121
122
    } else {
123
      this.loading = true;
124
      this.subscriptions.push(this._contextService.getPublicCommunitiesByState().subscribe(
125
        data => {
126
          this.communities = data;
127
          if (this.communities.length > 0) {
128
            this.communities.sort((n1, n2) => n1.label > n2.label);
129
          }
130
          this.loading = false;
131
          if (this.communityId != null) {
132
            //preselect community
133
            this.selectedCommunityId = this.communityId;
134
            for (let i = 0; i < this.communities.length; i++) {
135
              if (this.communities[i].id == this.selectedCommunityId) {
136
                this.selectedCommunityLabel = this.communities[i].label;
137
                break;
138
              }
139
            }
140
            this.addNewContext(this.selectedCommunityLabel, null, {
141
              'id': this.selectedCommunityId,
142
              'label': this.selectedCommunityLabel
143
            }, false)
144
145
          }
146
        },
147
        err => {
148
          //console.log(err);
149
          ClaimContextSearchFormComponent.handleError("Error getting communities", err);
150
          this.loading = false;
151
          this.error = true;
152
        }
153
      ));
154
    }
155
  }
156
157
  getCategories() {
158
    this.loading = true;
159
    // this.categories=[];
160
    if (this.selectedCommunityId != '0') {
161
      if (!Session.isLoggedIn()) {
162
        this.saveStateAndRedirectLogin();
163
164
165
      } else {
166
        if (this.categories[this.selectedCommunityId]) {
167
          this.loading = false;
168
          return;
169
        }
170
        this.subscriptions.push(this._contextService.getCategories(this.selectedCommunityId, this.properties.contextsAPI).subscribe(
171
          data => {
172
173
            this.categories[this.selectedCommunityId] = data;
174
            this.concepts = [];
175
            if (this.query !== "") {
176
              const event = {value: ""};
177
              event.value = this.query;
178
            }
179
            this.loading = false;
180
          },
181
          err => {
182
            //console.log(err);
183
            ClaimContextSearchFormComponent.handleError("Error getting categories for community with id: " + this.selectedCommunityId, err);
184
            this.loading = false;
185
          }
186
        ));
187
      }
188
    }
189
  }
190
  /*
191
    getConcepts() {
192
      this.loading = true;
193
      if (this.selectedCategoryId != '0') {
194
        if (!Session.isLoggedIn()) {
195
          this.saveStateAndRedirectLogin();
196
        } else {
197
          this.concepts = [];
198
199
          this._contextService.getConcepts(this.selectedCategoryId, "", true, this.properties.contextsAPI).subscribe(
200
            data => {
201
202
              this.concepts = data;
203
              for (var i = 0; i < data.length; i++) {
204
                if (data[i].hasSubConcept == true) {
205
                  this.getSubConcepts(data[i].id);
206
                }
207
              }
208
              this.addCommunityInConcepts();
209
              if (this.query !== "") {
210
                var event = {value: ""};
211
                event.value = this.query;
212
                //  this.filter(event);
213
              }
214
              this.loading = false;
215
            },
216
            err => {
217
              //console.log(err);
218
              this.handleError("Error getting concepts for category with id: " + this.selectedCategoryId, err);
219
              this.loading = false;
220
            }
221
          );
222
        }
223
      } else {
224
        this.concepts = [];
225
        this.loading = false;
226
      }
227
    }*/
228
  /*
229
    getSubConcepts(conceptId) {
230
      this.loading = true;
231
      if (this.selectedCategoryId != '0') {
232
        if (!Session.isLoggedIn()) {
233
          this.saveStateAndRedirectLogin();
234
        } else {
235
          this._contextService.getSubConcepts(conceptId, "", true, this.properties.contextsAPI).subscribe(
236
            data => {
237
              for (var i = 0; i < data.length; i++) {
238
                this.concepts.push(data[i]);
239
              }
240
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.handleError("Error getting subconcepts for concept with id: " + conceptId, err);
251
              this.loading = false;
252
            }
253
          );
254
        }
255
      } else {
256
        this.concepts = [];
257
        this.loading = false;
258
      }
259
    }*/
260
261
  displaySubcategory(id) {
262
    if (this.conceptsClassDisplay[id] != null) {
263
      this.conceptsClassDisplay[id] = !this.conceptsClassDisplay[id];
264
265
    } else {
266
      this.conceptsClassDisplay[id] = true;
267
    }
268
  }
269
270
  browseConcepts(categoryId) {
271
    if (!Session.isLoggedIn()) {
272
      this.saveStateAndRedirectLogin();
273
    } else {
274
      if (this.conceptsClass[categoryId] != null) {
275
        this.conceptsClassDisplay[categoryId] = !this.conceptsClassDisplay[categoryId];
276
        return;
277
      } else {
278
        this.conceptsClassDisplay[categoryId] = true;
279
      }
280
      this.conceptsClass[categoryId] = [];
281
      this.conceptsCategoryLoading[categoryId] = true;
282
      this.subscriptions.push(this._contextService.getConcepts(categoryId, "", false, this.properties.contextsAPI).subscribe(
283
        data => {
284
          // var concepts = data;
285
          this.conceptsClass[categoryId] = [];
286
          for (let i = 0; i < data.length; i++) {
287
            if (data[i].hasSubConcept == true) {
288
              this.browseSubConcepts(categoryId, data[i].id);
289
            } else {
290
              this.conceptsClass[categoryId].push(data[i]);
291
            }
292
          }
293
          this.conceptsCategoryLoading[categoryId] = false;
294
        },
295
        err => {
296
          //console.log(err);
297
          ClaimContextSearchFormComponent.handleError("Error getting concepts for category with id: " + this.selectedCategoryId, err);
298
          this.conceptsCategoryLoading[categoryId] = false;
299
        }
300
      ));
301
    }
302
303
  }
304
305
  browseSubConcepts(categoryId, conceptId) {
306
307
    this.conceptsCategoryLoading[categoryId] = true;
308
    this.subscriptions.push(this._contextService.getSubConcepts(conceptId, "", false, this.properties.contextsAPI).subscribe(
309
      data => {
310
        const concepts = data[0];
311
        this.conceptsClass[categoryId].push(concepts);
312
        this.conceptsCategoryLoading[categoryId] = false;
313
      },
314
      err => {
315
        //console.log(err);
316
        ClaimContextSearchFormComponent.handleError("Error getting subconcepts for concept with id: " + conceptId, err);
317
        this.conceptsCategoryLoading[categoryId] = false;
318
      }
319
    ));
320
321
322
  }
323
324
  // communityChanged(){
325
  //   this.warningMessage = "";
326
  //   this.infoMessage = "";
327
  //   for(var i = 0; i< this.communities.length; i++){
328
  //     if(this.communities[i].id==this.selectedCommunityId){
329
  //       this.selectedCommunityLabel = this.communities[i].label;
330
  //       break;
331
  //     }
332
  //   }
333
  //   this.selectedCategoryId = "0";
334
  //   this.selectedCategoryLabel="Select Category:";
335
  //   if(this.selectedCommunityId != "0"){
336
  //     this.getCategories();
337
  //   }
338
  // }
339
  // categoryChanged(){
340
  //   this.warningMessage = "";
341
  //   this.infoMessage = "";
342
  //   for(var i = 0; i< this.categories.length; i++){
343
  //     if(this.categories[i].id==this.selectedCategoryId){
344
  //       this.selectedCategoryLabel = this.categories[i].label;
345
  //       break;
346
  //     }
347
  //   }
348
  //     this.getConcepts();
349
  // }
350
  /*  addCommunityInConcepts() {
351
      this.concepts.push({"id": this.selectedCommunityId, "label": this.selectedCommunityLabel});
352
      // if(this.autocomplete){
353
      //   this.autocomplete.updateList(this.concepts);
354
      // }
355
    }*/
356
357
  saveStateAndRedirectLogin() {
358
    //  if(this.projects != null){
359
    //   localStorage.setItem(this.localStoragePrefix + "projects", JSON.stringify(this.projects));
360
    // }
361
    // localStorage.setItem(this.localStoragePrefix + "contexts", JSON.stringify(this.selectedList));
362
    if (this.results != null) {
363
      localStorage.setItem(this.localStoragePrefix + "results", JSON.stringify(this.results));
364
    }
365
    if (this.sources != null) {
366
      localStorage.setItem(this.localStoragePrefix + "sources", JSON.stringify(this.sources));
367
    }
368
369
    this.router.navigate(['/user-info'], {
370
      queryParams: {
371
        "errorCode": LoginErrorCodes.NOT_VALID,
372
        "redirectUrl": this.router.url
373
      }
374
    });
375
  }
376
377
  private static handleError(message: string, error) {
378
    console.error("Claim context search form (component): " + message, error);
379
  }
380
}