Skip to content

Commit

Permalink
Add docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio committed Apr 2, 2020
1 parent 3e3cf14 commit adf6a2f
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 38 deletions.
11 changes: 7 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { MODULE_NAME, MODULE_VERSION } from './version';

const EXTENSION_ID = 'ipylab:plugin';

/**
* The default plugin.
*/
const extension: JupyterFrontEndPlugin<void> = {
id: EXTENSION_ID,
autoStart: true,
Expand All @@ -29,10 +32,10 @@ const extension: JupyterFrontEndPlugin<void> = {
palette: ICommandPalette
): void => {
// add globals
widgetExports.JupyterFrontEndModel._app = app;
widgetExports.ShellModel._shell = shell;
widgetExports.CommandRegistryModel._commands = app.commands;
widgetExports.CommandPaletteModel._palette = palette;
widgetExports.JupyterFrontEndModel.app = app;
widgetExports.ShellModel.shell = shell;
widgetExports.CommandRegistryModel.commands = app.commands;
widgetExports.CommandPaletteModel.palette = palette;

registry.registerWidget({
name: MODULE_NAME,
Expand Down
68 changes: 54 additions & 14 deletions src/widgets/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ISerializers, WidgetModel } from '@jupyter-widgets/base';

import { CommandRegistry } from '@lumino/commands';

import { ReadonlyPartialJSONObject } from '@lumino/coreutils';

import { IDisposable } from '@lumino/disposable';

import { MODULE_NAME, MODULE_VERSION } from '../version';
Expand All @@ -15,6 +17,9 @@ import { MODULE_NAME, MODULE_VERSION } from '../version';
* The model for a command registry.
*/
export class CommandRegistryModel extends WidgetModel {
/**
* The default attributes.
*/
defaults(): any {
return {
...super.defaults(),
Expand All @@ -24,8 +29,14 @@ export class CommandRegistryModel extends WidgetModel {
};
}

/**
* Initialize a CommandRegistryModel instance.
*
* @param attributes The base attributes.
* @param options The initialization options.
*/
initialize(attributes: any, options: any): void {
this.commands = CommandRegistryModel._commands;
this._commands = CommandRegistryModel.commands;
super.initialize(attributes, options);
this.on('msg:custom', this._onMessage.bind(this));
this.on('comm_live_update', () => {
Expand All @@ -38,6 +49,11 @@ export class CommandRegistryModel extends WidgetModel {
this._sendCommandList();
}

/**
* Handle a custom message from the backend.
*
* @param msg The message to handle.
*/
private _onMessage(msg: any): void {
switch (msg.func) {
case 'execute':
Expand All @@ -54,24 +70,42 @@ export class CommandRegistryModel extends WidgetModel {
}
}

/**
* Send the list of commands to the backend.
*/
private _sendCommandList(): void {
this.commands.notifyCommandChanged();
this.set('_commands', this.commands.listCommands());
this._commands.notifyCommandChanged();
this.set('_commands', this._commands.listCommands());
this.save_changes();
}

private _execute(payload: any): void {
const { id, args } = payload;
void this.commands.execute(id, args);
/**
* Execute a command
*
* @param bundle The command bundle.
*/
private _execute(bundle: {
id: string;
args: ReadonlyPartialJSONObject;
}): void {
const { id, args } = bundle;
void this._commands.execute(id, args);
}

private async _addCommand(payload: any): Promise<void> {
const { id, caption, label, iconClass } = payload;
if (this.commands.hasCommand(id)) {
/**
* Add a new command to the command registry.
*
* @param options The command options.
*/
private async _addCommand(
options: CommandRegistry.ICommandOptions & { id: string }
): Promise<void> {
const { id, caption, label, iconClass } = options;
if (this._commands.hasCommand(id)) {
// TODO: handle this?
return;
}
const command = this.commands.addCommand(id, {
const command = this._commands.addCommand(id, {
caption,
label,
iconClass,
Expand All @@ -87,8 +121,13 @@ export class CommandRegistryModel extends WidgetModel {
this._sendCommandList();
}

private _removeCommand(payload: any): void {
const { id } = payload;
/**
* Remove a command from the command registry.
*
* @param bundle The command bundle.
*/
private _removeCommand(bundle: { id: string }): void {
const { id } = bundle;
if (this._customCommands.has(id)) {
this._customCommands.get(id).dispose();
}
Expand All @@ -106,7 +145,8 @@ export class CommandRegistryModel extends WidgetModel {
static view_module: string = null;
static view_module_version = MODULE_VERSION;

private commands: CommandRegistry;
static _commands: CommandRegistry;
private _commands: CommandRegistry;
private _customCommands = new ObservableMap<IDisposable>();

static commands: CommandRegistry;
}
20 changes: 16 additions & 4 deletions src/widgets/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import {

import { MODULE_NAME, MODULE_VERSION } from '../version';

/**
* The model for a JupyterFrontEnd.
*/
export class JupyterFrontEndModel extends WidgetModel {
/**
* The default attributes.
*/
defaults(): any {
return {
...super.defaults(),
Expand All @@ -21,11 +27,17 @@ export class JupyterFrontEndModel extends WidgetModel {
};
}

/**
* Initialize a JupyterFrontEndModel instance.
*
* @param attributes The base attributes.
* @param options The initialization options.
*/
initialize(attributes: any, options: any): void {
this.app = JupyterFrontEndModel._app;
this._app = JupyterFrontEndModel.app;
super.initialize(attributes, options);
this.send({ event: 'lab_ready' }, {});
this.set('version', this.app.version);
this.set('version', this._app.version);
this.save_changes();
}

Expand All @@ -40,6 +52,6 @@ export class JupyterFrontEndModel extends WidgetModel {
static view_module: string = null;
static view_module_version = MODULE_VERSION;

private app: JupyterFrontEnd;
static _app: JupyterFrontEnd;
private _app: JupyterFrontEnd;
static app: JupyterFrontEnd;
}
39 changes: 31 additions & 8 deletions src/widgets/palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import {
} from '@jupyter-widgets/base';

import { MODULE_NAME, MODULE_VERSION } from '../version';
import { ICommandPalette } from '@jupyterlab/apputils';
import { ICommandPalette, IPaletteItem } from '@jupyterlab/apputils';

/**
* The model for a command palette.
*/
export class CommandPaletteModel extends WidgetModel {
/**
* The default attributes.
*/
defaults(): any {
return {
...super.defaults(),
Expand All @@ -20,13 +26,24 @@ export class CommandPaletteModel extends WidgetModel {
};
}

/**
* Initialize a CommandPaletteModel instance.
*
* @param attributes The base attributes.
* @param options The initialization options.
*/
initialize(attributes: any, options: any): void {
this.palette = CommandPaletteModel._palette;
this._palette = CommandPaletteModel.palette;
super.initialize(attributes, options);

this.on('msg:custom', this._onMessage.bind(this));
}

/**
* Handle a custom message from the backend.
*
* @param msg The message to handle.
*/
private _onMessage(msg: any): void {
switch (msg.func) {
case 'addItem':
Expand All @@ -37,13 +54,18 @@ export class CommandPaletteModel extends WidgetModel {
}
}

private _addItem(payload: any): void {
if (!this.palette) {
/**
* Add a new item to the command palette.
*
* @param options The item options.
*/
private _addItem(options: IPaletteItem & { id: string }): void {
if (!this._palette) {
// no-op if no palette
return;
}
const { id, category, args, rank } = payload;
void this.palette.addItem({ command: id, category, args, rank });
const { id, category, args, rank } = options;
void this._palette.addItem({ command: id, category, args, rank });
}

static serializers: ISerializers = {
Expand All @@ -57,6 +79,7 @@ export class CommandPaletteModel extends WidgetModel {
static view_module: string = null;
static view_module_version = MODULE_VERSION;

private palette: ICommandPalette;
static _palette: ICommandPalette;
private _palette: ICommandPalette;

static palette: ICommandPalette;
}
6 changes: 6 additions & 0 deletions src/widgets/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { VBoxModel } from '@jupyter-widgets/controls';

import { MODULE_NAME, MODULE_VERSION } from '../version';

/**
* The model for a panel.
*/
export class PanelModel extends VBoxModel {
/**
* The default attributes.
*/
defaults(): any {
return {
...super.defaults(),
Expand Down
34 changes: 26 additions & 8 deletions src/widgets/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import {

import { MODULE_NAME, MODULE_VERSION } from '../version';

/**
* The model for a shell.
*/
export class ShellModel extends WidgetModel {
/**
* The default attributes.
*/
defaults(): any {
return {
...super.defaults(),
Expand All @@ -21,12 +27,23 @@ export class ShellModel extends WidgetModel {
};
}

/**
* Initialize a ShellModel instance.
*
* @param attributes The base attributes.
* @param options The initialization options.
*/
initialize(attributes: any, options: any): void {
this.shell = ShellModel._shell;
this._shell = ShellModel.shell;
super.initialize(attributes, options);
this.on('msg:custom', this.onMessage.bind(this));
}

/**
* Handle a custom message from the backend.
*
* @param msg The message to handle.
*/
private async onMessage(msg: any): Promise<void> {
switch (msg.func) {
case 'add': {
Expand Down Expand Up @@ -60,9 +77,9 @@ export class ShellModel extends WidgetModel {
if (area === 'left' || area === 'right') {
let handler;
if (area === 'left') {
handler = this.shell['_leftHandler'];
handler = this._shell['_leftHandler'];
} else {
handler = this.shell['_rightHandler'];
handler = this._shell['_rightHandler'];
}

// handle tab closed event
Expand All @@ -72,15 +89,15 @@ export class ShellModel extends WidgetModel {

pWidget.addClass('jp-SideAreaWidget');
}
this.shell.add(pWidget, area, args);
this._shell.add(pWidget, area, args);
break;
}
case 'expandLeft': {
this.shell.expandLeft();
this._shell.expandLeft();
break;
}
case 'expandRight': {
this.shell.expandRight();
this._shell.expandRight();
break;
}
default:
Expand All @@ -99,6 +116,7 @@ export class ShellModel extends WidgetModel {
static view_module: string = null;
static view_module_version = MODULE_VERSION;

private shell: ILabShell;
static _shell: ILabShell;
private _shell: ILabShell;

static shell: ILabShell;
}
Loading

0 comments on commit adf6a2f

Please sign in to comment.