import { ErrorObject } from '../../error/error-model' ; import { EndPoint } from '../../configuration/tab-endpoint/endpoint.model' ; import { EndPointService } from '../../configuration/tab-endpoint/endpoint.service' ; import { GadgetPropertyService } from './gadget-property.service' ; import { RuntimeService } from '../../services/runtime.service' ; import { GadgetInstanceService } from '../../grid/grid.service' ; import { AfterViewInit , ChangeDetectorRef , OnDestroy , OnInit , ViewChild } from '@angular/core' ; import { DynamicFormComponent } from '../../dynamic-form/dynamic-form.component' ; import { OptionsService } from "../../configuration/tab-options/service" ; /**
 * Created by jayhamilton on 6/22/17.
 */ export abstract class GadgetBase implements IGadget , OnDestroy , OnInit , AfterViewInit { @ ViewChild ( DynamicFormComponent ) propertyPageForm : DynamicFormComponent ; title : string ; instanceId : number ; config : any ; gadgetTags : Array < any > ; type { boolean } showControls = false ; type { boolean } inConfig = false ; type { boolean } inRun = false ; type { boolean } actionInitiated = false ; type { boolean } showOperationControls = false ; type { boolean } gadgetHasOperationControls = true ; type { boolean } showConfigurationControl = true ; propertyPages : any [ ] = [ ] ; endpointObject : EndPoint ; errorObject : ErrorObject ; errorExists = false ; globalOptions : any ; constructor ( protected _runtimeService , protected _gadgetInstanceService , protected _propertyService , protected _endPointService , protected changeDetectionRef , protected _optionsService ) { this . _optionsService . listenForGlobalOptionsChanges ( ) . subscribe ( options => { /**
             * This is called when there is a change to the options tab within the configuration modal.
             * The following method needs to also be called when the gadget is initially instantiated.
             * When the gadget is instantiated the option values need to come from the persistent store.
             */ this . updateGadgetWithGlobalOptions ( options ) ; } ) ; this . updateGadgetWithGlobalOptions ( this . _optionsService . getBoardOptions ( ) ) ; } public ngOnInit ( ) { this . toggleConfigMode ( ) ; this . changeDetectionRef . detectChanges ( ) ; } public ngAfterViewInit ( ) { if ( this . propertyPageForm ) { if ( this . propertyPageForm . isPropertyPageValid ) { this . toggleConfigMode ( ) ; this . changeDetectionRef . detectChanges ( ) ; this . showOperationControls = true ; this . changeDetectionRef . detectChanges ( ) ; } else { this . showOperationControls = false ; this . changeDetectionRef . detectChanges ( ) ; } } this . preRun ( ) ; } public initializeState ( ) { } public toggleConfigMode ( ) { if ( ! this . inConfig ) { this . initializeProperties ( ) ; } this . inConfig = ! this . inConfig ; } public initializeProperties ( ) { if ( this . propertyPages . length === 0 && this . config . propertyPages ) { this . _propertyService . setPropertyPagesAndProperties ( this . config . propertyPages , this . propertyPages ) ; } } public abstract run ( ) ; public abstract stop ( ) ; public abstract updateProperties ( updatedProperties ) ; public abstract updateData ( data ) ; public abstract preRun ( ) ; public handleError ( error ) { this . inRun = false ; this . actionInitiated = false ; this . errorExists = true ; this . errorObject = error ; } public initializeRunState ( forceRunState ) { this . errorExists = false ; this . actionInitiated = true ; this . inConfig = false ; if ( forceRunState ) { this . setInRunState ( ) ; } } public setInRunState ( ) { this . inRun = true ; this . actionInitiated = false ; } public setStopState ( longRunningStopAction ) { /**
         *  If the gadget indicates longRunningStopAction then the gadget has to set this value to
         *  false once the operation is complete
         */ this . actionInitiated = longRunningStopAction ; this . inRun = false ; } public remove ( ) { this . _gadgetInstanceService . removeInstance ( this . instanceId ) ; } public showGadgetControls ( enable ) { this . showControls = enable ; } /**
     * called from cell.component after the gadget is created during runtime
     * intanceId, config, title and endpoint are common to all gadgets. Once the gadgets are configured
     * we give them an opportunity to perform an action during the preRun() method. For example,
     * the statistic gadget uses preRun() to make a single call to the endpoint to update its display.
     * */ public configureGadget ( instanceId , config , tags ) { this . instanceId = instanceId ; this . config = config ; this . gadgetTags = tags . slice ( ) ; this . setTitle ( this . getPropFromPropertyPages ( 'title' ) ) ; this . setEndPoint ( this . getPropFromPropertyPages ( 'endpoint' ) ) ; } protected setEndPoint ( endpoint ) { this . _endPointService . getEndPoints ( ) . subscribe ( data => { if ( data [ 'endPoint' ] ) { data [ 'endPoint' ] . forEach ( item => { if ( item . name === endpoint ) { this . endpointObject = item ; } } ) ; } } ) ; } protected getEndPoint ( ) { return this . endpointObject ; } protected setTitle ( title ) { this . title = title ; } protected getPropFromPropertyPages ( prop ) { for ( let x = 0 ; x < this . config . propertyPages . length ; x ++ ) { for ( let i = 0 ; i < this . config . propertyPages [ x ] . properties . length ; i ++ ) { if ( this . config . propertyPages [ x ] . properties [ i ] . key === prop ) { return this . config . propertyPages [ x ] . properties [ i ] . value ; } } } return 'Unknown' ; } public ngOnDestroy ( ) { } public updateGadgetWithGlobalOptions ( options ) { this . globalOptions = Object . assign ( { } , options ) ; } }