import { Directive , ElementRef , HostListener , Input , OnInit } from '@angular/core' ; import { NgControl } from '@angular/forms' ; @ Directive ( { selector : '[obNumberFormat]' , exportAs : 'obNumberFormat' , host : { class : 'ob-number-format' } } ) export class ObNumberFormatDirective implements OnInit { @ Input ( ) decimals = 2 ; @ Input ( ) persistent = true ; private changed = false ; private focused = false ; constructor ( private readonly ngControl , private readonly el ) { } @ HostListener ( 'blur' ) onBlur ( ) { this . focused = false ; const value = ObNumberFormatDirective . toFixedNumber ( this . ngControl . value , this . decimals ) ; if ( this . persistent ) { this . changed = true ; this . ngControl . reset ( value ) ; } else { this . el . nativeElement . value = value ; } } @ HostListener ( 'focus' ) onFocus ( ) { this . focused = true ; if ( ! this . persistent ) { this . ngControl . reset ( this . ngControl . value ) ; } } ngOnInit ( ) { this . ngControl . valueChanges . subscribe ( ( value ) : void => { if ( this . changed || this . focused || isNaN ( value ) ) { this . changed = false ; return ; } this . setValue ( value ) ; } ) ; this . setValue ( this . ngControl . value ) ; } private static toFixedNumber ( number , decimals ) { const pow = 10 ** decimals ; return + ( Math . round ( number * pow ) / pow ) ; } private setValue ( value ) { const fixedValue = ObNumberFormatDirective . toFixedNumber ( value , this . decimals ) ; if ( this . persistent ) { this . changed = true ; this . ngControl . reset ( fixedValue ) ; } else { this . el . nativeElement . value = fixedValue ; } } }