Project

General

Profile

1
import {Component, ViewChild, ViewChildren, QueryList, Input, ViewEncapsulation} from '@angular/core';
2
import {Location}                    from '@angular/common';
3
import {ActivatedRoute, Params}      from '@angular/router';
4
import {Title, Meta}                 from '@angular/platform-browser';
5

    
6
import {DataTableDirective}          from 'angular-datatables';
7
import {Observable}                  from 'rxjs/Observable';
8
import { Subject }                   from 'rxjs/Subject';
9

    
10
import{EnvProperties}                from '../../utils/properties/env-properties';
11
import {ErrorCodes}                  from '../../utils/properties/errorCodes';
12

    
13
import {ClaimsDatatablePipe}         from '../../utils/pipes/claimsDatatable.pipe';
14

    
15
import {Session}                     from '../../login/utils/helper.class';
16
import {RouterHelper}                from '../../utils/routerHelper.class';
17

    
18
import {ModalSelect}                 from '../../utils/modal/selectModal.component';
19
import {ModalLoading}                from '../../utils/modal/loading.component';
20

    
21
import {ClaimsByTokenService}        from './claimsByToken.service';
22

    
23
@Component({
24
    selector: 'claims-project-manager',
25
    templateUrl: 'claimsByToken.component.html',
26
    styles: [`
27
      #table1_info, #table1_paginate,  #table1_length,  #table1_filter,
28
      #table2_info, #table2_paginate,  #table2_length,  #table2_filter{
29
        display: none;
30
      }
31

    
32
    `],
33
    encapsulation: ViewEncapsulation.None // this used in order styles to work
34

    
35
})
36
export class ClaimsByTokenComponent {
37
  public token: string = "";
38
  public sub: any;
39
  public project: any;
40
  private claims:any = [];
41
  public pending_claims: any = [];
42
  public curated_claims: any = [];
43
  public selectedRight_PendingMode: Set<string>;
44
  public selectedWrong_PendingMode: Set<string>;
45
  public selectedRight_CuratedMode: Set<string>;
46
  public selectedWrong_CuratedMode: Set<string>;
47
  public editable: Set<number>;
48
  public contact_person: string[] = ["Konstantina", "Argiro", "Katerina"];
49

    
50
  private errorCodes: ErrorCodes;
51
  public pending_status: number;
52
  public curated_status: number;
53

    
54
  // when 'valid' show proper claims, when 'invalid' show no matched entry-wanna retry
55
  public accessStatus: string;// = "empty";
56

    
57
  public mode: string = "pending";
58
  public showTables: boolean = true;
59
  public rowsOnPage = 5;
60
  public sortOrder = "asc";
61
  public keyword1:string = "";
62
  public keyword2:string = "";
63

    
64
  public activePendingPage:any = {page: 1};
65
  public totalPendingResults:any = {count: 0};
66
  public activeCuratedPage:any = {page: 1};
67
  public totalCuratedResults:any = {count: 0};
68

    
69
  dtTrigger: Subject<any>[] = [];
70
  private triggered: boolean = false;
71

    
72
  dtOptions: DataTables.Settings[] = [];
73
  @ViewChildren(DataTableDirective)
74
  dtElements: QueryList<any>;
75
  //@ViewChild("table1") table1: DataTableDirective;
76
  //@ViewChild("table2") table2: DataTableDirective;
77

    
78
  @ViewChild (ModalSelect) selectModal : ModalSelect;
79
  @ViewChild (ModalLoading) loading : ModalLoading ;
80

    
81
  properties:EnvProperties;
82

    
83
  public routerHelper:RouterHelper = new RouterHelper();
84

    
85
  constructor (private route: ActivatedRoute,
86
               private claimsByTokenService: ClaimsByTokenService,
87
               private _meta: Meta, private _title: Title) {
88
      this.errorCodes = new ErrorCodes();
89
      this.pending_status = this.errorCodes.LOADING;
90
      this.curated_status = this.errorCodes.LOADING;
91
  }
92
  ngOnInit() {
93
    this.route.data
94
      .subscribe((data: { envSpecific: EnvProperties }) => {
95
         this.properties = data.envSpecific;
96

    
97
      });
98
    this.sub =  this.route.queryParams.subscribe(params => {
99
        this.mode = "pending";
100
        this.token = params['token'];
101
        this.selectedRight_PendingMode = new Set<string>();
102
        this.selectedWrong_PendingMode = new Set<string>();
103
        this.selectedRight_CuratedMode = new Set<string>();
104
        this.selectedWrong_CuratedMode = new Set<string>();
105
        this.editable = new Set<number>();
106
        this.validateJWTandToken();
107
        this.updateTitle("Claims For Project Managers");
108
      }
109
    );
110

    
111
    this.dtOptions[0] = {
112
      //"paging": false,
113
      //"searching": false,
114
      //"lengthChange": false,
115
      "pageLength": this.rowsOnPage,
116
      "columnDefs": [ {
117
            "type": "date",
118
            "targets": 2
119
        } ],
120
      "order": [[ 2, 'desc' ]]
121
      //"pagingType": 'full_numbers',
122
      /*"language": {
123
          "search": "",
124
          "searchPlaceholder": "Search projects..."
125
      }*/
126
    };
127
console.info("configure dtOptions1: ",this.dtOptions[0]);
128

    
129
    this.dtOptions[1] = {
130
      "pageLength": this.rowsOnPage,
131
      "columnDefs": [ {
132
            "type": "date",
133
            "targets": [2,4]
134
        } ],
135
      "order": [[ 4, 'desc' ]]
136
    };
137

    
138
console.info("configure dtOptions2: ",this.dtOptions[1]);
139

    
140
    this.dtTrigger[0] = new Subject<any>();
141
    this.dtTrigger[1] = new Subject<any>();
142
  }
143

    
144
  ngAfterViewInit(): void {
145
     $.fn['dataTable'].ext.search.push((settings, data, dataIndex) => {
146
        if(settings.sTableId == 'table1') {
147
          if (this.filterData(data, this.keyword1)) {
148
            return true;
149
          }
150
          return false;
151
        } else if(settings.sTableId == 'table2') {
152
          if (this.filterData(data, this.keyword2)) {
153
            return true;
154
          }
155
          return false;
156
        }
157
      });
158
  }
159

    
160
  ngOnDestroy(): void {
161
    $.fn['dataTable'].ext.search.pop();
162
    // Do not forget to unsubscribe the event
163
    this.dtTrigger[0].unsubscribe();
164
    this.dtTrigger[1].unsubscribe();
165
  }
166

    
167
  /*
168
  Trigger a table draw in order to get the initial filtering
169
  */
170
  triggerInitialLoad(){
171
      this.triggered = true;
172
      console.info("triggerInitialLoad");
173
      setTimeout(function(){
174
        /*var table1 = <any>$('#table1').DataTable();
175
        table1.page( 0 ).draw( false );
176

    
177
        var table2 = <any>$('#table2').DataTable();
178
        table2.page( 0 ).draw( false );*/
179
      }, 500);
180
      setTimeout(() => {
181
        this.dtTrigger[0].next();
182
        this.dtTrigger[1].next();
183
      });
184

    
185
  }
186

    
187
  rerender(): void {
188
    console.info("RERENDER");
189
    this.dtElements.forEach((dtElement: DataTableDirective, index: number) => {
190
      dtElement.dtInstance.then((dtInstance: any) => {
191
        // Destroy the table first
192
        dtInstance.destroy();
193

    
194
        // Call the dtTrigger to rerender again
195
        this.dtTrigger[index].next();
196
      });
197
    });
198
  }
199

    
200
  filterData(row: any, query: string) {
201
    let returnValue: boolean = false;
202

    
203
    if(query) {
204
      for(var i=0; i <3; i++){
205
        var r= this.filterQuery(row[i], query);
206
        if(r) {
207
          returnValue = true;
208
          break;
209
        }
210
      }
211

    
212
      if(!returnValue) {
213
        return false;
214
      }
215
    }
216

    
217
    return true;
218
  }
219

    
220
  filterQuery(data, query){
221
    if(data.toLowerCase().indexOf(query.toLowerCase()) > -1){
222
      return true;
223
    }else{
224
      return false;
225
    }
226
  }
227

    
228
refreshTable(page:number, whichTable: string) {
229

    
230
  if(whichTable == "pending") {
231
    var table = $('#table1').DataTable();
232
    table.page( page - 1  ).draw( false );
233

    
234
    var info = table.page.info();
235
    console.info("records: "+info.recordsDisplay);
236

    
237
    this.activePendingPage.page = page;//$event.value;
238
    this.totalPendingResults.count = info.recordsDisplay;
239
  } else if(whichTable == 'curated') {
240
    var table = $('#table2').DataTable();
241
    table.page( page - 1  ).draw( false );
242

    
243
    var info = table.page.info();
244
    console.info("records: "+info.recordsDisplay);
245

    
246
    this.activeCuratedPage.page = page;//$event.value;
247
    this.totalCuratedResults.count = info.recordsDisplay;
248
  }
249

    
250
  //table.mfActivePage=$event.value;
251
  //table.setPage(table.mfActivePage, this.rowsOnPage);
252
}
253

    
254
  validateJWTandToken() {
255

    
256
    if(this.token) {
257
      this.pending_status = this.errorCodes.LOADING;
258
      this.curated_status = this.errorCodes.LOADING;
259

    
260
      this.showTables = false;
261
      this.pending_claims = [];
262
      this.curated_claims = [];
263

    
264
      this.activePendingPage.page = 1;
265
      this.totalPendingResults.count = 0;
266
      this.activeCuratedPage.page = 1;
267
      this.totalCuratedResults.count = 0;
268

    
269
      this.claimsByTokenService.getClaims(this.token,  this.properties.claimsAPIURL).subscribe(
270
          data => {
271
            //this.closeLoading();
272
            this.accessStatus = "valid";
273
              //console.info(data);
274
              this.claims = data.data;
275
              for(let claim of this.claims) {
276
                if(claim.targetType == "project") {
277
                  this.project = claim.target;
278
                } else {
279
                  this.project = claim.source;
280
                }
281
                if(claim.curatedBy) {
282
                  this.curated_claims.push(claim);
283
                } else {
284
                  this.pending_claims.push(claim);
285
                }
286
              }
287

    
288
              this.totalPendingResults.count = this.pending_claims.length;
289
              this.totalCuratedResults.count = this.curated_claims.length;
290

    
291
              this.updateTitle("Claims For Project Managers - "+this.project.name);
292
              this.showTables = true;
293

    
294
              if(!this.triggered) {
295
                console.info("initial load");
296
                this.triggerInitialLoad();
297
              } else {
298
                console.info("rerender");
299
                var table1 = $('#table1').DataTable();
300
                table1.clear();
301

    
302
                var table2 = $('#table2').DataTable();
303
                table2.clear();
304

    
305
                this.rerender();
306
              }
307

    
308
              this.pending_status = this.errorCodes.DONE;
309
              this.curated_status = this.errorCodes.DONE;
310
          },
311
          err => {
312
            if(err.status == '404') {
313
              this.pending_status = this.errorCodes.NOT_FOUND;
314
              this.curated_status = this.errorCodes.NOT_FOUND;
315
            } else if(err.status == '500') {
316
              this.pending_status = this.errorCodes.ERROR;
317
              this.curated_status = this.errorCodes.ERROR;
318
            } else {
319
              this.pending_status = this.errorCodes.NOT_AVAILABLE;
320
              this.curated_status = this.errorCodes.NOT_AVAILABLE;
321
            }
322
            this.showTables = true;
323

    
324
            if(!this.triggered) {
325
              this.triggerInitialLoad();
326
            } else {
327
              var table1 = $('#table1').DataTable();
328
              table1.clear();
329

    
330
              var table2 = $('#table2').DataTable();
331
              table2.clear();
332

    
333
              this.rerender();
334
            }
335

    
336
            this.accessStatus = "invalid";
337
            console.log(err);
338
          }
339
        );
340
      } else {
341
        this.accessStatus = "invalid";
342
      }
343
  }
344

    
345
  selectApprove(id:string, event, mode: string) {
346
    var value = event.currentTarget.checked;
347
    if(value){
348
      if(mode == "pending") {
349
        this.selectedRight_PendingMode.add(id);
350
        this.selectedWrong_PendingMode.delete(id);
351
      } else {
352
        this.selectedRight_CuratedMode.add(id);
353
        this.selectedWrong_CuratedMode.delete(id);
354
      }
355
    }else{
356
      if(mode == "pending") {
357
        this.selectedRight_PendingMode.delete(id);
358
      }
359
      // } else {
360
      //   this.selectedRight_CuratedMode.delete(id);
361
      //   this.selectedWrong_CuratedMode.add(id);
362
      // }
363
    }
364
  }
365

    
366
  selectDisapprove(id:string, event, mode: string) {
367
    var value = event.currentTarget.checked;
368
    if(value){
369
      if(mode == "pending") {
370
        this.selectedWrong_PendingMode.add(id);
371
        this.selectedRight_PendingMode.delete(id);
372
      } else {
373
        this.selectedWrong_CuratedMode.add(id);
374
        this.selectedRight_CuratedMode.delete(id);
375
      }
376
    }else{
377
      if(mode == "pending") {
378
        this.selectedWrong_PendingMode.delete(id);
379
      }
380
      // } else {
381
      //   this.selectedWrong_CuratedMode.delete(id);
382
      //   this.selectedRight_CuratedMode.add(id);
383
      // }
384
    }
385
  }
386

    
387
  isSelected(id:string, set:Set<string>) {
388
    return set.has(id);
389
  }
390
/*
391
  isSelectedWrong(id:string) {
392
    return this.selectedWrong.has(id);
393
  }
394
*/
395
  isRight_CuratedMode(claim: any) {
396
    if(this.isSelected(claim.id, this.selectedRight_CuratedMode)) {
397
      return true;
398
    } else if(claim.approved == true && !this.isSelected(claim.id, this.selectedWrong_CuratedMode)) {
399
      return true;
400
    }
401

    
402
    return false;
403
  }
404

    
405
  isWrong_CuratedMode(claim: any) {
406
    if(this.isSelected(claim.id, this.selectedWrong_CuratedMode)) {
407
      return true;
408
    } else if(claim.approved == false && !this.isSelected(claim.id, this.selectedRight_CuratedMode)) {
409
      return true;
410
    }
411

    
412
    return false;
413
  }
414

    
415
  cancelEditOfCuration(claim: any) {
416
    console.info("cancelEditOfCuration - approved:"+claim.approved);
417
    if(claim.approved) {
418
      //this.selectedRight_CuratedMode.add(claim.id);
419
      this.selectedWrong_CuratedMode.delete(claim.id);
420
    } else {
421
      this.selectedRight_CuratedMode.delete(claim.id);
422
      //this.selectedWrong_CuratedMode.add(claim.id);
423
    }
424
  }
425

    
426
  saveEdited(claim: any, editable_index: number) {
427
    this.curated_status = this.errorCodes.LOADING;
428

    
429
    let approved: boolean = this.isRight_CuratedMode(claim);
430

    
431
    if(approved == claim.approved) {
432
      this.selectedRight_CuratedMode.delete(claim.id);
433
      this.selectedWrong_CuratedMode.delete(claim.id);
434
      this.editable.delete(editable_index);
435

    
436
      this.curated_status = this.errorCodes.DONE;
437
    } else {
438
      let claimCurationInfo: {"id": string, "approved": boolean} = {"id": claim.id, "approved": approved};
439

    
440
      this.claimsByTokenService.updateClaimCuration(claimCurationInfo, this.properties.claimsAPIURL).subscribe(
441
          data => {
442
            console.info(data);
443
            this.selectedRight_CuratedMode.delete(claim.id);
444
            this.selectedWrong_CuratedMode.delete(claim.id);
445
            this.editable.delete(editable_index);
446

    
447
            this.validateJWTandToken();
448
          },
449
          err => {
450
            console.log(err);
451
            this.curated_status = this.errorCodes.NOT_SAVED;
452
          }
453
        );
454
    }
455
  }
456

    
457
  saveChanges() {
458
    this.pending_status = this.errorCodes.LOADING;
459
    //this.openLoading();
460
    console.info("Changes Saved!, right-wrong", this.selectedRight_PendingMode, this.selectedWrong_PendingMode);
461
    this.claimsByTokenService.updateClaimsCuration(this.selectedRight_PendingMode, this.selectedWrong_PendingMode, this.properties.claimsAPIURL).subscribe(
462
        data => {
463
          //this.closeLoading();
464
          console.info(data);
465
          this.mode = "curated";
466
          this.clearCheckboxes();
467
          this.validateJWTandToken();
468
        },
469
        err => {
470
          //this.closeLoading();
471
          console.log(err);
472
          this.pending_status = this.errorCodes.NOT_SAVED;
473
        }
474
      );
475
  }
476

    
477
  clearCheckboxes() {
478
    this.pending_status = this.errorCodes.LOADING;
479
    this.selectedRight_PendingMode.clear();
480
    this.selectedWrong_PendingMode.clear();
481
    this.pending_status = this.errorCodes.DONE;
482
  }
483

    
484
  public openLoading(){
485
    if(this.loading){
486
      this.loading.open();
487
    }
488
  }
489
  public closeLoading(){
490
    if(this.loading){
491
      this.loading.close();
492
    }
493
  }
494

    
495
  curatorSelected(selected: string) {
496
    console.info("selected curator: "+selected);
497
  }
498

    
499
  public openSelect(){
500
    if(this.selectModal){
501
      this.selectModal.open();
502
    }
503
  }
504

    
505
  public setMessageSelect(message: string){
506
    if(this.selectModal){
507
      this.selectModal.message = message;
508
    }
509
  }
510

    
511
  public setOptionsSelect(options: string[]){
512
    if(this.selectModal){
513
      this.selectModal.options = options;
514
    }
515
  }
516

    
517
  totalPages(totalResults: number): number {
518
      let totalPages:any = totalResults/(this.rowsOnPage);
519
      if(!(Number.isInteger(totalPages))) {
520
          totalPages = (parseInt(totalPages, 10) + 1);
521
      }
522
      return totalPages;
523
  }
524

    
525
  updateTitle(title:string){
526
    var _prefix ="OpenAIRE | ";
527
    var _title = _prefix + ((title.length> 50 ) ?title.substring(0,50):title);
528
    this._meta.updateTag({content:_title},"property='og:title'");
529
    this._title.setTitle(_title);
530

    
531
  }
532

    
533
}
(2-2/4)