app/src/commons/component/nic-dropdown.component.ts
2018-09-12 15:44:45 +09:00

85 lines
2.3 KiB
TypeScript

import { Component, Input, OnInit, EventEmitter, Output, ViewChild } from '@angular/core';
import { of } from 'rxjs';
import { catchError, map, take } from 'rxjs/operators';
import { ProbeService } from '../service/probe.service';
import { Interface } from '@overflow/model/net/nic';
import { SelectItem } from 'primeng/primeng';
import { Zone } from '@overflow/model/discovery';
import { toMetaIPType, MetaIPTypeEnum } from '@overflow/model/meta';
import { DropdownPanelComponent } from '@overflow/commons/ui/component/primeng';
@Component({
selector: 'app-nic-dropdown',
templateUrl: './nic-dropdown.component.html',
styleUrls: ['./nic-dropdown.component.scss'],
})
export class NicDropdownComponent implements OnInit {
@Input() blockTarget: any;
@Output() select = new EventEmitter<Zone>();
@ViewChild('panel') panel: DropdownPanelComponent;
nics: SelectItem[] = [];
selected;
constructor(
private probeService: ProbeService,
) {
}
ngOnInit(): void {
this.probeService.call<Interface>('MachineService.Interfaces').pipe(
map((ifaces: Interface[]) => {
ifaces.forEach(iface => {
console.log(iface);
iface.addresses.forEach(address => {
if (address.metaIPType.key !== toMetaIPType(MetaIPTypeEnum.V4).key) {
return;
}
this.nics.push({
label: iface.iface + ' (' + address.network + ')',
value: {
name: iface.iface,
metaIPType: address.metaIPType,
mac: iface.mac,
address: address.address,
network: address.network,
},
disabled: address.metaIPType.key !== toMetaIPType(MetaIPTypeEnum.V4).key ? true : false,
});
});
});
if (this.nics.length > 0) {
this.nicSelected(this.nics[0].value);
}
}),
catchError(error => {
console.log(error);
return of();
}),
take(1),
).subscribe();
}
nicSelected(nic: SelectItem) {
this.selected = nic;
const zone: Zone = {
network: this.selected.network,
iface: this.selected.name,
metaIPType: toMetaIPType(MetaIPTypeEnum.V4),
address: this.selected.address,
mac: this.selected.mac,
};
this.select.emit(zone);
this.panel.hide();
}
}