summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/components/searchMembersModal/members-table/elements-table.service.ts
blob: bd7f3979dfc2786f6cca63e2a3bacb5ab261540c (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import {Injectable} from "@angular/core";;
import {NgRedux} from "@angular-redux/store";
import {AppState} from "../../../store/reducers";
import {DataFilterPipe} from "../../../pipes/dataFilter/data-filter.pipe";
import {ElementTableRowModel, ModalInformation} from "./element-table-row.model";
import {Level1Instance} from "../../../models/level1Instance";
import * as _ from 'lodash';
import {Subject} from "rxjs";
import {CustomTableColumnDefinition} from "./elements-table.component";
import {
  deleteGenericModalhelper,
  deleteGenericModalTableDataHelper,
  updateGenericModalhelper, updateGenericModalTableDataHelper
} from "../../../storeUtil/utils/global/global.actions";

@Injectable()
export class ElementsTableService {
  allElementsStatusMap : { [key:string]: ElementTableRowModel; };
  filteredMembers :  any[];
  allCheckboxAreSelected : boolean;
  numberOfNotHideRows : number;
  numberOfSelectedRows : number;
  numberOfSelectedAndNotHideRows : number;
  numberOfNotSelectedAndNotHideRows : number;
  maxSelectedRow : number;
  modalInformation : ModalInformation;

  static uniqObjectField : string;
  static changeFnTableDataTrigger : Subject<any> = new Subject();
  static changeModalInformationDataTrigger : Subject<{modalInformation, selectedRowsIds}> = new Subject();
  static selectRowsTrigger : Subject<string[]> = new Subject();

  get staticUniqObjectField() { return ElementsTableService.uniqObjectField; }

  constructor(private _store: NgRedux<AppState>, private dataFilter: DataFilterPipe){
    this.resetAll(ElementsTableService.uniqObjectField, this.maxSelectedRow);
  }

  updateAmountsAndCheckAll = (uniqObjectField: string, modalInformation : ModalInformation, maxSelectedRow? : number) : void => {
    this.maxSelectedRow = maxSelectedRow;
    this.modalInformation = modalInformation;
    ElementsTableService.uniqObjectField = uniqObjectField;
    this.numberOfSelectedRows = this.calculateSelectedRows();
    this.numberOfNotHideRows = this.calculateNotHideRows();
    this.numberOfSelectedAndNotHideRows = this.calculateSelectedAndNotHide();
    this.numberOfNotSelectedAndNotHideRows = this.calculateNotSelectedAndNotHide();
    this.allCheckboxAreSelected = this.numberOfNotHideRows > 0 && ((this.numberOfNotHideRows === this.numberOfSelectedAndNotHideRows) ||  (this.numberOfSelectedAndNotHideRows  === this.maxSelectedRow));
  };

  resetAll = (uniqObjectField: string, maxSelectedRow? : number) : void => {
    this.allElementsStatusMap = {};
    this.filteredMembers = [];
    this.numberOfSelectedRows = 0;
    this.numberOfNotHideRows = 0;
    this.numberOfSelectedAndNotHideRows = 0;
    this.numberOfNotSelectedAndNotHideRows = 0;
    this.allCheckboxAreSelected = false;
    this.maxSelectedRow = maxSelectedRow;
    ElementsTableService.uniqObjectField = uniqObjectField;
  };

  changeAllCheckboxStatus = (status : boolean) : void =>{
    for(const member of this.filteredMembers){
        this.allElementsStatusMap[member[this.modalInformation.uniqObjectField]].isSelected = status;
        if(status){
          this._store.dispatch(updateGenericModalhelper(`selected${this.modalInformation.type}`, this.allElementsStatusMap[member[this.modalInformation.uniqObjectField]], this.modalInformation.uniqObjectField));
        }else {
          this._store.dispatch(deleteGenericModalhelper(`selected${this.modalInformation.type}`,this.allElementsStatusMap[member[this.modalInformation.uniqObjectField]][this.modalInformation.uniqObjectField]));
        }
    }
    this.updateAmountsAndCheckAll(ElementsTableService.uniqObjectField, this.modalInformation, this.maxSelectedRow);
  };

  changeCheckboxStatus = (vnfInstanceId : string, tableData) : void => {
    if(_.isNil(this.allElementsStatusMap[vnfInstanceId].isSelected)){
      this.allElementsStatusMap[vnfInstanceId].isSelected = true;
      this._store.dispatch(updateGenericModalhelper(`selected${this.modalInformation.type}`, this.allElementsStatusMap[vnfInstanceId], this.modalInformation.uniqObjectField));
      this._store.dispatch(updateGenericModalTableDataHelper(`${this.modalInformation.type}_TABLE_DATA`, tableData));
    }else {
      this.allElementsStatusMap[vnfInstanceId].isSelected = !this.allElementsStatusMap[vnfInstanceId].isSelected;
      if(this.allElementsStatusMap[vnfInstanceId].isSelected){
        this._store.dispatch(updateGenericModalhelper(`selected${this.modalInformation.type}`, this.allElementsStatusMap[vnfInstanceId], this.modalInformation.uniqObjectField));
        this._store.dispatch(updateGenericModalTableDataHelper(`${this.modalInformation.type}_TABLE_DATA`, tableData));
      }else {
        this._store.dispatch(deleteGenericModalhelper(`selected${this.modalInformation.type}`, this.modalInformation.uniqObjectField));
        this._store.dispatch(deleteGenericModalhelper(`selected${this.modalInformation.type}`, vnfInstanceId));

        this._store.dispatch(deleteGenericModalTableDataHelper(`${this.modalInformation.type}_TABLE_DATA`));
      }
    }

    this.updateAmountsAndCheckAll(ElementsTableService.uniqObjectField, this.modalInformation,  this.maxSelectedRow);
  };

  filterMembers(searchStr: string, type :string): void {
    const keys: string[][] =  this.getDataKeys(type);
    const types :string[] = this.getDataType(type);
    this.filteredMembers = this.dataFilter.transform(_.values(this.allElementsStatusMap), searchStr || '', keys, types);
    this.updateAmountsAndCheckAll(ElementsTableService.uniqObjectField, this.modalInformation, this.maxSelectedRow);
  }

  /**************************************************
   generate elements data for select/ unselect rows
   **************************************************/
   generateAllMembersStatus(tableData : Level1Instance[]) : { [key:string]: ElementTableRowModel; }{
    tableData.map((item) => {
      item['isSelected'] = false
    });
    return _.keyBy(tableData as ElementTableRowModel[],this.staticUniqObjectField);
  }

   sortElementsByName(list : Level1Instance[], keyName : string) :Level1Instance[]{
    if(!_.isNil(list) && !_.isNil(keyName)) {
      return list.sort(function(itemA, itemB) { return itemA[keyName]- itemB[keyName];})
    }
    return [];
  }

  /********************************
   table columns headers and key's
   ********************************/
  static getHeaders(type: string) : CustomTableColumnDefinition[] {
    return  [
      {displayName: `${type} instance name`, key: ['instanceName']},
      {displayName: `${type} version`, key: ['modelInfo', 'modelVersion']},
      {displayName: `${type} model name`, key: ['modelInfo', 'modelName']},
      {displayName: 'Prov Status', key: ['provStatus']},
      {displayName: 'Service instance name', key: ['serviceInstanceName']},
      {displayName: 'Cloud Region', key: ['lcpCloudRegionId']},
      {displayName: 'Tenant Name', key: ['tenantName']}
    ];
  }

  getDataKeys(type: string): string[][]{
    const headers = (!_.isNil(this.modalInformation) && !_.isNil(this.modalInformation.tableHeaders)) ? this.modalInformation.tableHeaders : ElementsTableService.getHeaders(type);
    return headers.map((header)=> header.key).concat([[ElementsTableService.uniqObjectField]],[['serviceInstanceId']]);
  }

  getDataType(type: string): string[]{
    const headers = (!_.isNil(this.modalInformation) && !_.isNil(this.modalInformation.tableHeaders)) ? this.modalInformation.tableHeaders : ElementsTableService.getHeaders(type);
    return headers.map((header)=> header.type);

  }

  /*************************************************************************************
   calculate the number of selected vnf members - include not visible and visible rows
   @allElementsStatusMap: current vnf member status
   *************************************************************************************/
  calculateSelectedRows() : number {
    const flatObject = _.values(this.allElementsStatusMap);
    return  _.filter(flatObject, (item) => { if (item.isSelected) return item }).length;
  }

  /************************************************
   calculate the number of display vnf members
   @allElementsStatusMap: current vnf member status
   ************************************************/
  calculateNotHideRows() : number {
    return  this.filteredMembers ? this.filteredMembers.length : 0;
  }

  /************************************************
   calculate the number of display vnf members
   @allElementsStatusMap: current vnf member status
   ************************************************/
  calculateSelectedAndNotHide() : number {
    return  _.filter(this.filteredMembers, (item) => { if ( this.allElementsStatusMap[item[ElementsTableService.uniqObjectField]].isSelected) return item }).length;
  }

  calculateNotSelectedAndNotHide() : number {
    return  _.filter(this.filteredMembers, (item) => { if ( !this.allElementsStatusMap[item[ElementsTableService.uniqObjectField]].isSelected) return item }).length;
  }


  isRowDisabled(currentRowIsSelected : boolean, maxSelectRow?: number) : boolean {
    return _.isNil(maxSelectRow) || currentRowIsSelected || maxSelectRow === 1 ? false : maxSelectRow <= this.calculateSelectedRows();
  }

  isCheckAllDisabled(maxSelectRow?: number) : boolean{
    if(_.isNil(maxSelectRow)) return false;
    else {
      return this.numberOfNotSelectedAndNotHideRows  > maxSelectRow;
    }
  }


}