aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/components/searchMembersModal/members-table/elements-table.component.ts
blob: 485a63c43580178431d0660042176c23e68b452a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {Component, Input, OnChanges, Output, SimpleChanges, EventEmitter} from '@angular/core';
import {ElementsTableService} from "./elements-table.service";
import {ModalInformation} from "./element-table-row.model";
import * as _ from 'lodash';
import {Level1Instance} from "../../../models/level1Instance";
import {NgRedux} from "@angular-redux/store";
import {AppState} from "../../../store/reducers";
import {
  deleteGenericModalhelper,
  deleteGenericModalTableDataHelper
} from "../../../storeUtil/utils/global/global.actions";

export class CustomTableColumnDefinition {
  public displayName = '';
  public key : any = '';
  public type? = 'text';
  public filter? = '';
}

@Component({
  selector: 'app-members-table',
  templateUrl: './elements-table.component.html',
  styleUrls: ['./members-table.component.scss']
})

export class ElementsTableComponent implements OnChanges{
  filterValue: string = null;
  allMemberStatusMap = null;
  membersTableService : ElementsTableService;
  headers: CustomTableColumnDefinition[] = [];
  searchQuery = null;

  @Input() modalInformation : ModalInformation;
  @Input() data: Level1Instance[];
  @Output() selectedMembersAmountChange : EventEmitter<number> = new EventEmitter();
  constructor(private _membersTableService : ElementsTableService, private _store : NgRedux<AppState>){
    this.membersTableService = this._membersTableService;
  }

  ngOnChanges(changes: SimpleChanges): void {
    if(_.isNil(this.data)){
      this._membersTableService.resetAll(this.modalInformation.uniqObjectField, this.modalInformation.maxSelectRow);
    }else {
      ElementsTableService.uniqObjectField = this.modalInformation.uniqObjectField;
      this.headers = this.modalInformation.tableHeaders;
      const genericModalHelper = this._store.getState().global.genericModalHelper;
      if(!_.isNil(genericModalHelper) && !_.isNil(genericModalHelper[`${this.modalInformation.type}_TABLE_DATA`]) && !_.isNil(genericModalHelper[`selected${this.modalInformation.type}`])){
        this.updateTablWithDefaultData(this._store.getState().global.genericModalHelper[`${this.modalInformation.type}_TABLE_DATA`]);
      }else {
        this.modalInformation.getElements().subscribe((res)=>{
          this.updateTablWithDefaultData(res);
        });
      }
    }
  }

  updateTablWithDefaultData(tableData) : void{
    this._membersTableService.allElementsStatusMap = this._membersTableService.generateAllMembersStatus(tableData);
    this._membersTableService.filteredMembers = this._membersTableService.sortElementsByName(tableData, "instanceName");
    this._membersTableService.updateAmountsAndCheckAll(this.modalInformation.uniqObjectField, this.modalInformation, this.modalInformation.maxSelectRow);
    this.updateDefaultSelectedRows();
  }
  
  search(searchStr: string): void {
    this.filterValue = searchStr;
    this._membersTableService.filterMembers(this.filterValue, this.modalInformation.type);
  }

  selectItem(item , maxNumberOfRows : number) : void {
    if (maxNumberOfRows === 1) {
      for (let currentItem in this.membersTableService.allElementsStatusMap) {
        if (this.membersTableService.allElementsStatusMap[currentItem].isSelected) {
          this.membersTableService.allElementsStatusMap[currentItem].isSelected = false;
          this.membersTableService.allElementsStatusMap[item[this.membersTableService.staticUniqObjectField]].isSelected = !this.membersTableService.allElementsStatusMap[item[this.membersTableService.staticUniqObjectField]].isSelected;
          return;
        }
      }
      this.membersTableService.allElementsStatusMap[item[this.membersTableService.staticUniqObjectField]].isSelected = !this.membersTableService.allElementsStatusMap[item[this.membersTableService.staticUniqObjectField]].isSelected;
    }
  }

  updateDefaultSelectedRows(): void {
    if(this._store.getState().global.genericModalHelper && this._store.getState().global.genericModalHelper[`selected${this.modalInformation.type}`]){
      const selectedIds = this._store.getState().global.genericModalHelper[`selected${this.modalInformation.type}`];
      for(const id in selectedIds){
        if(!_.isNil(this._membersTableService.allElementsStatusMap[id])){
          this._membersTableService.allElementsStatusMap[id].isSelected = true;
        }
      }
      this._membersTableService.updateAmountsAndCheckAll(this.modalInformation.uniqObjectField, this.modalInformation, this.modalInformation.maxSelectRow);
      this.selectedMembersAmountChange.emit(this._membersTableService.numberOfSelectedRows);
    }
  }

  changeAllCheckboxStatus(status: boolean) : void {
    this._membersTableService.changeAllCheckboxStatus(status);
    this.selectedMembersAmountChange.emit(this._membersTableService.numberOfSelectedRows);
  }


  changeCheckboxStatus(vnfInstanceId: string) : void {
    if (this.modalInformation.maxSelectRow === 1) {
      for (let currentItem in this.membersTableService.allElementsStatusMap) {
        if (this.membersTableService.allElementsStatusMap[currentItem].isSelected) {
          this.membersTableService.allElementsStatusMap[currentItem].isSelected = false;
          this._store.dispatch(deleteGenericModalhelper(`selected${this.modalInformation.type}`, this.membersTableService.allElementsStatusMap[currentItem][this.modalInformation.uniqObjectField]));
          this._store.dispatch(deleteGenericModalTableDataHelper(`${this.modalInformation.type}_TABLE_DATA`));
        }
      }
    }
    this._membersTableService.changeCheckboxStatus(vnfInstanceId, this.data);
    this.selectedMembersAmountChange.emit(this._membersTableService.numberOfSelectedRows);
  }


  getTdInformationItemId(data : {id : string[], value : string[], prefix ?: string}, item) : string {
    let result = item;
    for(const idVal of data.id){
      if(_.isNil(result)) return null;
      result = result[idVal];
    }
    return result;
  }

  getTdInformationItemValue(data : {id : string[], value : string[], prefix ?: string}, item) : string {
    let result = item;
    for(const idVal of data.value){
      if(_.isNil(result)) return null;
      result = result[idVal];
    }
    return !_.isNil(data.prefix) ? data.prefix + result : result;
  }


  getTdListInformationItemValue(data : {id : string[], value : string[], prefix ?: string}, item) : string[] {
    let result = item;

    for(let i = 0 ; i < data.value.length -1 ; i++){
      if(_.isNil(result)) return null;
      result = result[data.value[i]];
    }
    return _.map(result, _.last(data.value));
  }
}