summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/iops.directive.ts
blob: 4faf6916447b175c1ae8f6cc97d7065bffea6fe6 (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
import { Directive, EventEmitter, HostListener, Input, OnInit } from '@angular/core';
import { NgControl } from '@angular/forms';

import { FormatterService } from '../services/formatter.service';

@Directive({
  selector: '[cdIops]'
})
export class IopsDirective implements OnInit {
  @Input()
  ngDataReady: EventEmitter<any>;

  constructor(private formatter: FormatterService, private ngControl: NgControl) {}

  setValue(value: string): void {
    const iops = this.formatter.toIops(value);
    this.ngControl.control.setValue(`${iops} IOPS`);
  }

  ngOnInit(): void {
    this.setValue(this.ngControl.value);
    if (this.ngDataReady) {
      this.ngDataReady.subscribe(() => this.setValue(this.ngControl.value));
    }
  }

  @HostListener('blur', ['$event.target.value'])
  onUpdate(value: string) {
    this.setValue(value);
  }
}