module Sfx { export interface ITab { name : string ; refresh ? : ( messageHandler ? : IResponseMessageHandler ) => angular . IPromise < any > ; superscriptClass ? : string ; superscriptInHtml ? : ( ) => string ; } export interface ITabs { [ id ] : ITab ; } export interface IControllerBase { refresh ( messageHandler ? ) : angular . IPromise < any > ; } export class ControllerBase implements IControllerBase { public routes : RoutesService ; public message : MessageService ; public telemetry : TelemetryService ; public authSvc : AuthenticationService ; public settings : SettingsService ; public data : DataService ; public storage : StorageService ; public $location : angular . ILocationService ; public $q : angular . IQService ; public $route : ng . route . IRouteService ; constructor ( protected $injector ) { this . routes = this . $injector . get < RoutesService > ( "routes" ) ; this . message = this . $injector . get < MessageService > ( "message" ) ; this . telemetry = this . $injector . get < TelemetryService > ( "telemetry" ) ; this . settings = this . $injector . get < SettingsService > ( "settings" ) ; this . data = this . $injector . get < DataService > ( "data" ) ; this . authSvc = this . $injector . get < AuthenticationService > ( "auth" ) ; this . storage = this . $injector . get < StorageService > ( "storage" ) ; this . $location = this . $injector . get < angular . ILocationService > ( "$location" ) ; this . $q = this . $injector . get < angular . IQService > ( "$q" ) ; this . $route = this . $injector . get < ng . route . IRouteService > ( "$route" ) ; let controllerManager = this . $injector . get < ControllerManagerService > ( "controllerManager" ) ; controllerManager . registerController ( this ) ; } public get routeParams ( ) : any { return this . $route . current . params ; } public refresh ( messageHandler ? ) { return this . $q . when ( true ) ; } } export class ControllerWithResolver extends ControllerBase { public valueResolver : ValueResolver = new ValueResolver ( ) ; constructor ( $injector ) { super ( $injector ) ; } } export class TabController extends ControllerWithResolver { private static LastVisitedTabId : string ; private static LastVisitedTemplateUrl : any ; public activeTabId : string ; public basePath : string ; private refreshingPromise : angular . IPromise < any > ; public get hasTabs ( ) : boolean { return _ . size ( this . tabs ) > 0 ; } private get firstTabId ( ) : string { return _ . keys ( this . tabs ) [ 0 ] ; } constructor ( $injector , public tabs = { } ) { super ( $injector ) ; this . setActiveTab ( ) ; } public navigateToTab ( tabId ) { TabController . LastVisitedTabId = null ; TabController . LastVisitedTemplateUrl = null ; this . $location . path ( this . getTabViewPath ( tabId ) ) ; } public isActiveTab ( tabId ) { return this . activeTabId === tabId ; } public refresh ( messageHandler ? ) { if ( ! this . authSvc . isApplicationBootstrapped ) { return this . $q . reject ( ) ; } if ( ! this . refreshingPromise ) { this . refreshingPromise = this . refreshCommon ( messageHandler ) . then ( ( ) => { if ( this . hasTabs && this . tabs [ this . activeTabId ] && this . tabs [ this . activeTabId ] . refresh ) { return this . tabs [ this . activeTabId ] . refresh ( messageHandler ) ; } } ) . finally ( ( ) => { this . refreshingPromise = null ; } ) ; } return this . refreshingPromise ; } protected refreshCommon ( messageHandler ? ) { return this . $q . when ( true ) ; } private setActiveTab ( ) { this . basePath = this . getBasePath ( ) ; let tabId = this . routeParams . tabId ; if ( tabId ) { this . activeTabId = tabId ; } else if ( this . hasTabs ) { if ( TabController . LastVisitedTabId && TabController . LastVisitedTabId !== this . firstTabId && this . $route . current . templateUrl === TabController . LastVisitedTemplateUrl && this . tabs [ TabController . LastVisitedTabId ] ) { this . navigateToTab ( TabController . LastVisitedTabId ) ; } else { this . activeTabId = this . firstTabId ; } } TabController . LastVisitedTabId = this . activeTabId ; TabController . LastVisitedTemplateUrl = this . $route . current . templateUrl ; this . telemetry . trackPageView ( ) ; } private getBasePath ( ) { return _ . trimEnd ( this . $location . path ( ) . replace ( /\/tab\/.*/ , "" ) , "/" ) ; } private getTabViewPath ( tabId ) { if ( tabId === this . firstTabId ) { return this . basePath ; } return this . basePath + "/tab/" + tabId ; } } export class MainViewController extends TabController { private clusterTree : ClusterTreeService ; constructor ( $injector , tabs ? ) { super ( $injector , tabs ) ; this . clusterTree = $injector . get < ClusterTreeService > ( "clusterTree" ) ; let controllerManager = $injector . get < ControllerManagerService > ( "controllerManager" ) ; controllerManager . registerMainController ( this ) ; this . switchThemeIfDetected ( ) ; } protected selectTreeNode ( path ) { return this . clusterTree . selectTreeNode ( path , true ) ; } private switchThemeIfDetected ( ) { if ( this . routeParams . theme_source && this . routeParams . theme_name ) { this . telemetry . trackEvent ( "Theme external source: " + this . routeParams . theme_source + "/" + this . routeParams . theme_name ) ; if ( ! this . storage . isDefined ( Constants . ThemeNameStorageKey ) ) { let themeService = this . $injector . get < ThemeService > ( "theme" ) ; themeService . resolveAndChangeToTheme ( this . routeParams . theme_source , this . routeParams . theme_name ) ; } this . $location . search ( Constants . ThemeSourceQueryStringName , null ) ; this . $location . search ( Constants . ThemeNameQueryStringName , null ) ; } } } }