import { action , computed , observable , makeObservable } from 'mobx' ; import type { ITab , ITabContainer } from '@cloudbeaver/core-ui' ; import { Entity , IServiceInjector , MixinProvider } from '@cloudbeaver/core-di' ; import { ITabContainerEntity , TabContainerToken } from './TabContainerToken' ; import { TabEntity } from './TabEntity' ; export class TabContainerEntity extends Entity implements ITabContainer , ITabContainerEntity { get tabs ( ) : ITab [ ] { return this . tabsOrder . map ( id => { const tabEntity = this . getTabEntity ( id ) ; return tabEntity . getViewModel ( ) ; } ) ; } get currentTabId ( ) : string | null { return this . _activeTabId ; } private _activeTabId : string | null = null ; private tabsOrder = observable . array < string > ( ) ; constructor ( providers , id ? ) { super ( providers , id ) ; makeObservable < TabContainerEntity , '_activeTabId' > ( this , { tabs : computed , _activeTabId : observable , addTabEntity : action , closeTab : action , activateTab : action , addTabsAfter : action , } ) ; this . addMixin ( TabContainerToken , this ) ; } addTabEntity ( tabEntity , desiredTabPosition ? ) { this . addChild ( tabEntity ) ; if ( desiredTabPosition === undefined || desiredTabPosition < 0 ) { this . tabsOrder . push ( tabEntity . id ) ; } else { this . tabsOrder . spliceWithArray ( desiredTabPosition , 0 , [ tabEntity . id ] ) ; } } closeTab ( tabId ) { const tabModel = this . getTabEntity ( tabId ) . getTabModel ( ) ; if ( tabModel . onClose ) { tabModel . onClose ( ) ; } this . removeChild ( tabId ) ; this . tabsOrder . remove ( tabId ) ; } activateTab ( tabId ) { if ( tabId === null ) { this . _activeTabId = null ; return ; } const tabModel = this . getTabEntity ( tabId ) . getTabModel ( ) ; if ( tabModel . onActivate ) { tabModel . onActivate ( ) ; } this . _activeTabId = tabId ; } getTabServiceInjector ( tabId ) { const tabEntity = this . getTabEntity ( tabId ) ; return tabEntity . getServiceInjector ( ) ; } addTabsAfter ( tabEntities , tabId ) { tabEntities . forEach ( ( tabEntity , index ) => { const previousTabId = index === 0 ? tabId : tabEntities [ index - 1 ] . id ; const previousTabIndex = this . tabsOrder . findIndex ( id => id === previousTabId ) ; this . addTabEntity ( tabEntity , previousTabIndex ) ; } ) ; } private getTabEntity ( tabId ) { const entity = this . children . get ( tabId ) ; if ( entity instanceof TabEntity ) { return entity ; } throw Error ( `Tab entity is not found: ${ tabId }` ) ; } }