import { HsButton } from './button.interface' ; import { HsConfig } from './../../config.service' ; import { HsCoreService } from './../core/core.service' ; import { HsEventBusService } from '../core/event-bus.service' ; import { HsLanguageService } from './../language/language.service' ; import { HsLayoutService } from '../layout/layout.service' ; import { HsUtilsService } from '../utils/utils.service' ; import { Injectable } from '@angular/core' ; import { Subject } from 'rxjs' ; @ Injectable ( { providedIn : 'root' , } ) export class HsSidebarService { extraButtons : Array < HsButton > = [ ] ; buttons : Array < HsButton > = [ ] ; sidebarLoad : Subject < void > = new Subject ( ) ; /**
     * If buttons with importancy property exist.
     * If not, don't display expansion +/- icon
     */ unimportantExist = false ; visibleButtons : Array < HsButton > = [ ] ; showUnimportant : boolean ; miniSidebarButton = { title : ( ) => this . HsLanguageService . getTranslation ( 'SIDEBAR.additionalPanels' ) , } ; numberOfUnimportant : number ; importantButtons : HsButton [ ] ; constructor ( public HsLayoutService , public HsConfig , public HsLanguageService , public HsCoreService , public HsEventBusService , public HsUtilsService ) { this . extraButtons = [ ] ; /**
         * List of visible buttons taking into account viewport size
         */ this . visibleButtons = [ ] ; /**
         * List of sidebar buttons
         */ this . buttons = [ ] ; this . HsEventBusService . mainPanelChanges . subscribe ( ( ) => { setTimeout ( ( ) => { this . HsCoreService . updateMapSize ( ) ; } , 550 ) ; } ) ; this . HsEventBusService . layoutLoads . subscribe ( ( ) => { this . setButtonVisibility ( ) ; this . setPanelState ( this . buttons ) ; this . HsEventBusService . layoutResizes . subscribe ( ( ) => { this . setButtonVisibility ( ) ; } ) ; } ) ; } setButtonVisibility ( ) { this . importantButtons = this . buttons . filter ( ( button ) => { return ( button . important != false && this . visibleButtons . includes ( button . panel ) ) ; } ) ; this . numberOfUnimportant = this . buttons . length - this . importantButtons . length ; for ( const button of this . importantButtons ) { button . fits = this . fitsSidebar ( button ) ; } if ( ! this . unimportantExist ) { this . HsLayoutService . minisidebar = this . importantButtons . some ( ( b ) => b . fits == false ) ; } } getButtonTitle ( button ) { return typeof button . title == 'function' ? button . title ( ) : button . title ; } getButtonDescription ( button ) { return typeof button . description == 'function' ? button . description ( ) : button . description ; } param panelName param state setButtonImportancy ( panelName , state ) { const backCompat = { datasource_selector : 'addData' } ; panelName = backCompat [ panelName ] ? backCompat [ panelName ] : panelName ; const button = this . buttons . find ( ( b ) => b . panel == panelName ) ; if ( button ) { button . fits = state ; button . important = state ; } this . unimportantExist = this . buttons . some ( ( b ) => b . important == false ) ; this . HsLayoutService . minisidebar = this . unimportantExist ; } buttonClicked ( button ) { if ( button . click ) { button . click ( ) ; } else { this . HsLayoutService . setMainPanel ( button . panel , true ) ; } } setPanelState ( buttons ) { for ( const button of buttons ) { if ( this . HsLayoutService . getPanelEnableState ( button . panel ) && this . checkConfigurableButtons ( button ) ) { if ( ! this . visibleButtons . includes ( button . panel ) ) { this . visibleButtons . push ( button . panel ) ; button . visible = true ; } } else { button . visible = false ; } } } param { object } button checkConfigurableButtons ( button ) { if ( typeof button . condition == 'undefined' ) { return true ; } else if ( ! this . HsConfig . panelsEnabled ) { return false ; } else { return this . HsConfig . panelsEnabled [ button . panel ] ; } } public param { string } which description description fitsSidebar ( button ) { const dimensionToCheck = window . innerWidth > 767 ? 'clientHeight' : 'clientWidth' ; let maxNumberOfButtons = Math . floor ( this . HsLayoutService . layoutElement [ dimensionToCheck ] / 60 ) ; maxNumberOfButtons = dimensionToCheck == 'clientHeight' ? maxNumberOfButtons - 1 : maxNumberOfButtons ; return ( this . importantButtons . findIndex ( ( btn ) => btn . panel === button . panel ) + 2 <= maxNumberOfButtons ) ; } }