import { Component , EventEmitter , Input , OnInit , Output } from '@angular/core' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { TableState } from '../../table.types' ; @ Component ( { selector : 'table-state' , templateUrl : './table-state.component.html' , styleUrls : [ './table-state.component.scss' ] } ) export class TableStateComponent { /**
     * Current possible table states
     */ @ Input ( ) public tableStates : Array < TableState > = [ ] ; /**
     * Current selected state.
     */ @ Input ( ) public currentState : TableState ; /**
     * State selection event emitter.
     */ @ Output ( ) public stateSelect : EventEmitter < TableState > = new EventEmitter < TableState > ( ) ; /**
     * State save event emitter.
     */ @ Output ( ) public stateSave : EventEmitter < string > = new EventEmitter < string > ( ) ; /**
     * State update event emitter.
     */ @ Output ( ) public stateUpdate : EventEmitter < TableState > = new EventEmitter < TableState > ( ) ; /**
     * State delete event emitter.
     */ @ Output ( ) public stateDelete : EventEmitter < TableState > = new EventEmitter < TableState > ( ) ; /**
     * State reset event emitter.
     */ @ Output ( ) public stateReset : EventEmitter < void > = new EventEmitter < void > ( ) ; /**
     * Name form validation
     */ public form : FormGroup ; constructor ( ) { this . form = new FormGroup ( { name : new FormControl ( '' , Validators . required ) } ) ; } /**
     * Get the control of the settings.
     */ public get nameControl ( ) : FormControl { return this . form . get ( 'name' ) as FormControl ; } param state public selectState ( state ) { this . stateSelect . emit ( state ) ; } /**
     * Reset to a existing state.
     */ public resetState ( ) { this . stateReset . emit ( ) ; } param name param event public saveState ( name , event ) { event . stopPropagation ( ) ; this . stateSave . emit ( name ) ; this . nameControl . setValue ( '' ) ; } param state param event public updateState ( state , event ) { event . stopPropagation ( ) ; this . stateUpdate . emit ( state ) ; } param state param event public deleteState ( state , event ) { event . stopPropagation ( ) ; const index = this . tableStates . indexOf ( state , 0 ) ; if ( index > - 1 ) { this . tableStates . splice ( index , 1 ) ; } this . stateDelete . emit ( state ) ; } }