import { ObservableMap } from '@jupyterlab/observables' ; import { ISerializers , WidgetModel } from '@jupyter-widgets/base' ; import { ArrayExt } from '@lumino/algorithm' ; import { CommandRegistry } from '@lumino/commands' ; import { ReadonlyPartialJSONObject } from '@lumino/coreutils' ; import { IDisposable } from '@lumino/disposable' ; import { MODULE_NAME , MODULE_VERSION } from '../version' ; /**
 * The model for a command registry.
 */ export class CommandRegistryModel extends WidgetModel { /**
     * The default attributes.
     */ defaults ( ) { return { ... super . defaults ( ) , _model_name : CommandRegistryModel . model_name , _model_module : CommandRegistryModel . model_module , _model_module_version : CommandRegistryModel . model_module_version , _command_list : [ ] , _commands : [ ] , } ; } param attributes param options initialize ( attributes , options ) { this . _commands = CommandRegistryModel . commands ; super . initialize ( attributes , options ) ; this . on ( 'msg:custom' , this . _onMessage . bind ( this ) ) ; this . on ( 'comm_live_update' , ( ) => { if ( this . comm_live ) { return ; } Private . customCommands . values ( ) . forEach ( ( command ) => command . dispose ( ) ) ; this . _sendCommandList ( ) ; } ) ; const commands = this . get ( '_commands' ) ; commands . forEach ( ( command ) => this . _addCommand ( command ) ) ; this . _sendCommandList ( ) ; } param msg private _onMessage ( msg ) { switch ( msg . func ) { case 'execute' : this . _execute ( msg . payload ) ; break ; case 'addCommand' : { this . _addCommand ( msg . payload ) ; const commands = this . get ( '_commands' ) ; this . set ( '_commands' , commands . concat ( msg . payload ) ) ; this . save_changes ( ) ; break ; } case 'removeCommand' : this . _removeCommand ( msg . payload ) ; break ; default : break ; } } /**
     * Send the list of commands to the backend.
     */ private _sendCommandList ( ) { this . _commands . notifyCommandChanged ( ) ; this . set ( '_command_list' , this . _commands . listCommands ( ) ) ; this . save_changes ( ) ; } param bundle private _execute ( bundle ) { const { id , args } = bundle ; void this . _commands . execute ( id , args ) ; } param options private _addCommand ( options ) { const { id , caption , label , iconClass } = options ; if ( this . _commands . hasCommand ( id ) ) { Private . customCommands . get ( id ) . dispose ( ) ; } const commandEnabled = ( command ) : boolean => { return ! command . isDisposed && ! ! this . comm && this . comm_live ; } ; const command = this . _commands . addCommand ( id , { caption , label , iconClass , execute : ( ) => { if ( ! this . comm_live ) { command . dispose ( ) ; return ; } this . send ( { event : 'execute' , id } , { } ) ; } , isEnabled : ( ) => commandEnabled ( command ) , isVisible : ( ) => commandEnabled ( command ) , } ) ; Private . customCommands . set ( id , command ) ; this . _sendCommandList ( ) ; } param bundle private _removeCommand ( bundle ) { const { id } = bundle ; if ( Private . customCommands . has ( id ) ) { Private . customCommands . get ( id ) . dispose ( ) ; } const commands = this . get ( '_commands' ) . slice ( ) ; ArrayExt . removeAllWhere ( commands , ( w ) => w . id === id ) ; this . set ( '_commands' , commands ) ; this . save_changes ( ) ; this . _sendCommandList ( ) ; } static serializers : ISerializers = { ... WidgetModel . serializers , } ; static model_name = 'CommandRegistryModel' ; 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 _commands : CommandRegistry ; static commands : CommandRegistry ; } /**
 * A namespace for private data
 */ namespace Private { export const customCommands = new ObservableMap < IDisposable > ( ) ; }