import { ICommandPalette , IPaletteItem } from '@jupyterlab/apputils' ; import { ObservableMap } from '@jupyterlab/observables' ; import { DOMWidgetModel , ISerializers , WidgetModel , } from '@jupyter-widgets/base' ; import { IDisposable } from '@lumino/disposable' ; import { MODULE_NAME , MODULE_VERSION } from '../version' ; /**
 * The model for a command palette.
 */ export class CommandPaletteModel extends WidgetModel { /**
     * The default attributes.
     */ defaults ( ) { return { ... super . defaults ( ) , _model_name : CommandPaletteModel . model_name , _model_module : CommandPaletteModel . model_module , _model_module_version : CommandPaletteModel . model_module_version , _items : [ ] , } ; } param attributes param options initialize ( attributes , options ) { this . _palette = CommandPaletteModel . palette ; super . initialize ( attributes , options ) ; this . on ( 'msg:custom' , this . _onMessage . bind ( this ) ) ; const items = this . get ( '_items' ) ; items . forEach ( ( item ) => this . _addItem ( item ) ) ; } param msg private _onMessage ( msg ) { switch ( msg . func ) { case 'addItem' : { this . _addItem ( msg . payload ) ; const items = this . get ( '_items' ) ; this . set ( '_items' , items . concat ( msg . payload ) ) ; this . save_changes ( ) ; break ; } default : break ; } } param options private _addItem ( options ) { if ( ! this . _palette ) { return ; } const { id , category , args , rank } = options ; const itemId = `${ id }-${ category }` ; if ( Private . customItems . has ( itemId ) ) { return ; } const item = this . _palette . addItem ( { command : id , category , args , rank } ) ; Private . customItems . set ( itemId , item ) ; } static serializers : ISerializers = { ... DOMWidgetModel . serializers , } ; static model_name = 'CommandPaletteModel' ; 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 _palette : ICommandPalette ; static palette : ICommandPalette ; } /**
 * A namespace for private data
 */ namespace Private { export const customItems = new ObservableMap < IDisposable > ( ) ; }