'use strict' ; import { shims } from '@jupyter-widgets/base' ; import * as jupyterlab from '@jupyter-widgets/jupyterlab-manager' ; import { RenderMimeRegistry , standardRendererFactories } from '@jupyterlab/rendermime' ; import { Kernel } from '@jupyterlab/services' ; import { Widget } from '@lumino/widgets' ; import { DocumentContext } from './documentContext' ; import { requireLoader } from './widgetLoader' ; export const WIDGET_MIMETYPE = 'application/vnd.jupyter.widget-view+json' ; const widgetsRegisteredInRequireJs = [ '@jupyter-widgets/controls' , '@jupyter-widgets/base' , '@jupyter-widgets/output' ] ; export class WidgetManager extends jupyterlab . WidgetManager { public kernel : Kernel . IKernelConnection ; public el : HTMLElement ; constructor ( kernel , el , private readonly scriptLoader ) { super ( new DocumentContext ( kernel ) , new RenderMimeRegistry ( { initialFactories : standardRendererFactories } ) , { saveState : false } ) ; this . kernel = kernel ; this . el = el ; this . rendermime . addFactory ( { safe : false , mimeTypes : [ WIDGET_MIMETYPE ] , createRenderer : ( options ) => new jupyterlab . WidgetRenderer ( options , this ) } , 0 ) ; kernel . registerCommTarget ( this . comm_target_name , async ( comm , msg ) => { const oldComm = new shims . services . Comm ( comm ) ; return this . handle_comm_open ( oldComm , msg ) as Promise < any > ; } ) ; } /**
     * Create a comm.
     */ public async _create_comm ( target_name , model_id , data ? , metadata ? ) { const comm = this . kernel . createComm ( target_name , model_id ) ; if ( data || metadata ) { comm . open ( data , metadata ) ; } return Promise . resolve ( new shims . services . Comm ( comm ) ) ; } /**
     * Get the currently-registered comms.
     */ public _get_comm_info ( ) { return this . kernel . requestCommInfo ( { target_name : this . comm_target_name } ) . then ( ( reply ) => ( reply . content as any ) . comms ) ; } public async display_view ( msg , view , options ) { const widget = await super . display_view ( msg , view , options ) ; const element = options . node ? ( options . node as HTMLElement ) : this . el ; if ( element ) { Widget . attach ( widget , element ) ; } return widget ; } public async restoreWidgets ( ) { } public get onUnhandledIOPubMessage ( ) { return super . onUnhandledIOPubMessage ; } protected async loadClass ( className , moduleName , moduleVersion ) { window . console . log ( `WidgetManager: Loading class ${ className }:${ moduleName }:${ moduleVersion }` ) ; const result = await super . loadClass ( className , moduleName , moduleVersion ) . then ( ( r ) => { this . sendSuccess ( className , moduleName , moduleVersion ) ; return r ; } ) . catch ( async ( originalException ) => { try { const loadModuleFromRequirejs = widgetsRegisteredInRequireJs . includes ( moduleName ) || this . scriptLoader . widgetsRegisteredInRequireJs . has ( moduleName ) ; if ( ! loadModuleFromRequirejs ) { await this . scriptLoader . loadWidgetScript ( moduleName , moduleVersion ) ; } const m = await requireLoader ( moduleName ) ; if ( m && m [ className ] ) { this . sendSuccess ( className , moduleName , moduleVersion ) ; return m [ className ] ; } throw originalException ; } catch ( ex ) { this . sendError ( className , moduleName , moduleVersion , originalException ) ; throw originalException ; } } ) ; return result ; } private sendSuccess ( className , moduleName , moduleVersion ) { try { this . scriptLoader . successHandler ( className , moduleName , moduleVersion ) ; } catch { } } private sendError ( className , moduleName , moduleVersion , originalException ) { try { this . scriptLoader . errorHandler ( className , moduleName , moduleVersion , originalException ) ; } catch { } } }