import { FunctionAppService } from './../shared/services/function-app.service' ; import { BusyStateScopeManager } from './../busy-state/busy-state-scope-manager' ; import { Subject } from 'rxjs/Subject' ; import { PortalService } from './../shared/services/portal.service' ; import { ActivatedRoute , Router } from '@angular/router' ; import { Component , ViewChild , AfterViewInit , OnDestroy } from '@angular/core' ; import { TreeViewInfo } from '../tree-view/models/tree-view-info' ; import { DashboardType } from '../tree-view/models/dashboard-type' ; import { UserService } from '../shared/services/user.service' ; import { GlobalStateService } from '../shared/services/global-state.service' ; import { BusyStateComponent } from '../busy-state/busy-state.component' ; import { ArmSiteDescriptor , ArmFunctionDescriptor } from 'app/shared/resourceDescriptors' ; import { Http } from '@angular/http' ; import { TranslateService } from '@ngx-translate/core' ; import { BroadcastService } from 'app/shared/services/broadcast.service' ; import { LanguageService } from 'app/shared/services/language.service' ; import { ArmService } from 'app/shared/services/arm.service' ; import { ConfigService } from 'app/shared/services/config.service' ; import { AuthzService } from 'app/shared/services/authz.service' ; import { AiService } from 'app/shared/services/ai.service' ; import { FunctionInfo } from 'app/shared/models/function-info' ; import { NavigationStart , Event as RouterEvent , NavigationEnd , NavigationCancel , NavigationError } from '@angular/router' ; import { FunctionService } from 'app/shared/services/function.service' ; @ Component ( { selector : 'main' , templateUrl : './main.component.html' , styleUrls : [ './main.component.scss' ] , } ) export class MainComponent implements AfterViewInit , OnDestroy { public ready = false ; public resourceId : string ; public viewInfo : TreeViewInfo < any > ; public dashboardType : string ; public inIFrame : boolean ; public inTab : boolean ; public selectedFunction : FunctionInfo ; public showTopBar : boolean ; @ ViewChild ( BusyStateComponent ) busyStateComponent : BusyStateComponent ; private _ngUnsubscribe = new Subject ( ) ; private _busyManager : BusyStateScopeManager ; constructor ( private _userService , private _globalStateService , private _portalService , private _broadcastService , private _functionAppService , private _functionService , _ngHttp , _translateService , _armService , _languageService , _authZService , _configService , _aiService , route , router ) { router . events . takeUntil ( this . _ngUnsubscribe ) . subscribe ( ( event ) => { this . _navigationInterceptor ( event ) ; } ) ; this . inIFrame = _userService . inIFrame ; this . inTab = _userService . inTab ; if ( this . inTab ) { this . initializeChildWindow ( _userService , _globalStateService , _ngHttp , _translateService , _broadcastService , _armService , _languageService , _authZService , _configService , _aiService ) ; } this . _setTopBarVisibility ( ) ; } private _setTopBarVisibility ( ) { if ( this . inIFrame ) { this . showTopBar = false ; } else if ( ! this . _globalStateService . showTryView ) { this . showTopBar = true ; } } ngAfterViewInit ( ) { this . _globalStateService . GlobalBusyStateComponent = this . busyStateComponent ; this . _busyManager = new BusyStateScopeManager ( this . _broadcastService , 'dashboard' ) ; this . _userService . getStartupInfo ( ) . first ( ) . subscribe ( info => { this . ready = true ; this . _portalService . sendTimerEvent ( { timerId : 'PortalReady' , timerAction : 'stop' , } ) ; } ) ; } ngOnDestroy ( ) { this . _ngUnsubscribe . next ( ) ; this . _busyManager . clearBusy ( ) ; } private initializeChildWindow ( _userService , _globalStateService , _ngHttp , _translateService , _broadcastService , _armService , _languageService , _authZService , _configService , _aiService ) { this . _userService . getStartupInfo ( ) . takeUntil ( this . _ngUnsubscribe ) . subscribe ( info => { const siteDescriptor = new ArmSiteDescriptor ( info . resourceId ) ; this . _functionAppService . getAppContext ( siteDescriptor . getTrimmedResourceId ( ) ) . mergeMap ( context => this . _functionService . getFunctions ( context . site . id ) ) . subscribe ( functions => { const fnDescriptor = new ArmFunctionDescriptor ( info . resourceId ) ; const targetName = fnDescriptor . name ; const selectedFunctionArm = functions . isSuccessful && functions . result . value . find ( f => f . name === targetName ) ; if ( selectedFunctionArm ) { this . selectedFunction = selectedFunctionArm . properties ; } else { _aiService . trackEvent ( '/main-component/error' , { error : `Failed to find target function` , } ) ; } } ) ; } ) ; } updateViewInfo ( viewInfo ) { if ( ! viewInfo ) { this . viewInfo = viewInfo ; return ; } else if ( viewInfo . dashboardType === DashboardType . none ) { return ; } this . viewInfo = viewInfo ; this . dashboardType = DashboardType [ viewInfo . dashboardType ] ; } public get trialExpired ( ) { return this . _globalStateService . TrialExpired ; } private _navigationInterceptor ( event ) { if ( ! this . _busyManager ) { return ; } if ( event instanceof NavigationStart ) { this . _busyManager . setBusy ( ) ; } else if ( event instanceof NavigationEnd ) { this . _busyManager . clearBusy ( ) ; } else if ( event instanceof NavigationCancel ) { this . _busyManager . clearBusy ( ) ; } else if ( event instanceof NavigationError ) { this . _busyManager . clearBusy ( ) ; } } }