import { JupyterFrontEnd , ILabShell } from '@jupyterlab/application' ; import { DOMUtils } from '@jupyterlab/apputils' ; import { ISerializers , WidgetModel , unpack_models , } from '@jupyter-widgets/base' ; import { ArrayExt } from '@lumino/algorithm' ; import { Message , MessageLoop } from '@lumino/messaging' ; import { MODULE_NAME , MODULE_VERSION } from '../version' ; /**
 * The model for a shell.
 */ export class ShellModel extends WidgetModel { /**
     * The default attributes.
     */ defaults ( ) { return { ... super . defaults ( ) , _model_name : ShellModel . model_name , _model_module : ShellModel . model_module , _model_module_version : ShellModel . model_module_version , _widgets : [ ] , } ; } param attributes param options initialize ( attributes , options ) { this . _shell = ShellModel . shell ; this . _labShell = ShellModel . labShell ; super . initialize ( attributes , options ) ; this . on ( 'msg:custom' , this . _onMessage . bind ( this ) ) ; const widgets = this . get ( '_widgets' ) ; widgets . forEach ( ( w ) => this . _add ( w ) ) ; } param payload private async _add ( payload ) { const { serializedWidget , area , args , id } = payload ; const model = await unpack_models ( serializedWidget , this . widget_manager ) ; const view = await this . widget_manager . create_view ( model , { } ) ; const title = await unpack_models ( model . get ( 'title' ) , this . widget_manager ) ; const pWidget = view . pWidget ; pWidget . id = id ?? DOMUtils . createDomID ( ) ; MessageLoop . installMessageHook ( pWidget , ( handler , msg ) => { switch ( msg . type ) { case 'close-request' : { const widgets = this . get ( '_widgets' ) . slice ( ) ; ArrayExt . removeAllWhere ( widgets , ( w ) => w . id === handler . id ) ; this . set ( '_widgets' , widgets ) ; this . save_changes ( ) ; break ; } } return true ; } ) ; const updateTitle = ( ) : void => { pWidget . title . label = title . get ( 'label' ) ; pWidget . title . iconClass = title . get ( 'icon_class' ) ; pWidget . title . closable = title . get ( 'closable' ) ; } ; title . on ( 'change' , updateTitle ) ; updateTitle ( ) ; if ( ( area === 'left' || area === 'right' ) && this . _labShell ) { let handler ; if ( area === 'left' ) { handler = this . _labShell [ '_leftHandler' ] ; } else { handler = this . _labShell [ '_rightHandler' ] ; } handler . sideBar . tabCloseRequested . connect ( ( sender , tab ) => { tab . title . owner . close ( ) ; } ) ; pWidget . addClass ( 'jp-SideAreaWidget' ) ; } this . _shell . add ( pWidget , area , args ) ; return pWidget . id ; } param msg private async _onMessage ( msg ) { switch ( msg . func ) { case 'add' : { const id = await this . _add ( msg . payload ) ; const widgets = this . get ( '_widgets' ) ; this . set ( '_widgets' , widgets . concat ( { ... msg . payload , id , } ) ) ; this . save_changes ( ) ; break ; } case 'expandLeft' : { if ( this . _labShell ) { this . _labShell . expandLeft ( ) ; } break ; } case 'expandRight' : { if ( this . _labShell ) { this . _labShell . expandRight ( ) ; } break ; } default : break ; } } static serializers : ISerializers = { ... WidgetModel . serializers , } ; static model_name = 'ShellModel' ; static model_module = MODULE_NAME ; static model_module_version = MODULE_VERSION ; static view_name : string = null ; static view_module : string = null ; static view_module_version = MODULE_VERSION ; private _shell : JupyterFrontEnd . IShell ; private _labShell : ILabShell ; static shell : JupyterFrontEnd . IShell ; static labShell : ILabShell ; }