1 import { AfterViewInit, Component, EventEmitter, Output, ViewChild } from '@angular/core';
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4 import { TableColumnProp } from '@swimlane/ngx-datatable';
5 import * as _ from 'lodash';
7 import { ActionLabelsI18n } from '../../../../shared/constants/app.constants';
8 import { Icons } from '../../../../shared/enum/icons.enum';
9 import { CdFormBuilder } from '../../../../shared/forms/cd-form-builder';
10 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
11 import { CdTableColumnFiltersChange } from '../../../../shared/models/cd-table-column-filters-change';
12 import { InventoryDevice } from '../../inventory/inventory-devices/inventory-device.model';
13 import { InventoryDevicesComponent } from '../../inventory/inventory-devices/inventory-devices.component';
16 selector: 'cd-osd-devices-selection-modal',
17 templateUrl: './osd-devices-selection-modal.component.html',
18 styleUrls: ['./osd-devices-selection-modal.component.scss']
20 export class OsdDevicesSelectionModalComponent implements AfterViewInit {
21 @ViewChild('inventoryDevices')
22 inventoryDevices: InventoryDevicesComponent;
25 submitAction = new EventEmitter<CdTableColumnFiltersChange>();
28 filterColumns: TableColumnProp[] = [];
32 formGroup: CdFormGroup;
35 devices: InventoryDevice[] = [];
36 filteredDevices: InventoryDevice[] = [];
38 event: CdTableColumnFiltersChange;
40 requiredFilters: string[] = [];
43 private formBuilder: CdFormBuilder,
44 public activeModal: NgbActiveModal,
45 public actionLabels: ActionLabelsI18n
47 this.action = actionLabels.ADD;
52 // At least one filter other than hostname is required
53 // Extract the name from table columns for i18n strings
54 const cols = _.filter(this.inventoryDevices.columns, (col) => {
55 return this.filterColumns.includes(col.prop) && col.prop !== 'hostname';
57 // Fixes 'ExpressionChangedAfterItHasBeenCheckedError'
59 this.requiredFilters = _.map(cols, 'name');
64 this.formGroup = this.formBuilder.group({});
67 onFilterChange(event: CdTableColumnFiltersChange) {
69 this.canSubmit = false;
70 if (_.isEmpty(event.filters)) {
71 // filters are cleared
72 this.filteredDevices = [];
73 this.event = undefined;
75 // at least one filter is required (except hostname)
76 const filters = event.filters.filter((filter) => {
77 return filter.prop !== 'hostname';
79 this.canSubmit = !_.isEmpty(filters);
80 this.filteredDevices = event.data;
81 this.capacity = _.sumBy(this.filteredDevices, 'sys_api.size');
87 this.submitAction.emit(this.event);
88 this.activeModal.close();