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 './claimEntities.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.resultsS.push(context);
105
      this.results.unshift(entity);
106
      localStorage.setItem(this.localStoragePrefix, JSON.stringify(this.results));
107
    }
108

    
109
  }
110

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

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

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

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

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

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

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

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

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

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

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

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

    
295
  }
296

    
297
  browseSubConcepts(categoryId, conceptId) {
298

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

    
313

    
314
  }
315

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

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

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

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