Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Add buttons to switch to classic lab and retro
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio committed Sep 3, 2021
1 parent a396892 commit 3f49eea
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 179 deletions.
3 changes: 3 additions & 0 deletions packages/lab-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@
"@jupyterlab/docregistry": "^3.1.8",
"@jupyterlab/mainmenu": "^3.1.8",
"@jupyterlab/notebook": "^3.1.8",
"@jupyterlab/translation": "^3.1.8",
"@jupyterlab/ui-components": "^3.1.8",
"@lumino/commands": "^1.15.0",
"@lumino/disposable": "^1.7.0",
"@retrolab/application": "^0.3.3",
"@retrolab/ui-components": "^0.3.3"
},
"devDependencies": {
Expand Down
160 changes: 152 additions & 8 deletions packages/lab-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,172 @@
// Distributed under the terms of the Modified BSD License.

import {
ILabShell,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ICommandPalette } from '@jupyterlab/apputils';

import { CommandToolbarButton, ICommandPalette } from '@jupyterlab/apputils';

import { PageConfig } from '@jupyterlab/coreutils';

import { IMainMenu } from '@jupyterlab/mainmenu';

import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';

import { ITranslator } from '@jupyterlab/translation';
import { interfaceSwitcher } from './interfaceswitcher';

import {
jupyterIcon,
jupyterFaviconIcon,
LabIcon
} from '@jupyterlab/ui-components';

import { IRetroShell } from '@retrolab/application';

import { retroSunIcon } from '@retrolab/ui-components';

/**
* The command IDs used by the application plugin.
*/
namespace CommandIDs {
/**
* Toggle Top Bar visibility
* Launch RetroLab Tree
*/
export const launchRetroTree = 'retrolab:launch-tree';

/**
* Open RetroLab
*/
export const openRetro = 'retrolab:open-retro';

/**
* Open in Classic Notebook
*/
export const launchRetroTree = 'retrolab:launchtree';
export const openClassic = 'retrolab:open-classic';

/**
* Open in JupyterLab
*/
export const openLab = 'retrolab:open-lab';
}

interface ISwitcherChoice {
command: string;
commandLabel: string;
buttonLabel: string;
icon: LabIcon;
urlPrefix: string;
}

/**
* A plugin to add custom toolbar items to the notebook page
*/
const launchButtons: JupyterFrontEndPlugin<void> = {
id: '@retrolab/lab-extension:interface-switcher',
autoStart: true,
optional: [
INotebookTracker,
ICommandPalette,
IMainMenu,
IRetroShell,
ILabShell
],
activate: (
app: JupyterFrontEnd,
notebookTracker: INotebookTracker | null,
palette: ICommandPalette | null,
menu: IMainMenu | null,
retroShell: IRetroShell | null,
labShell: ILabShell | null
) => {
if (!notebookTracker) {
// to prevent showing the toolbar button in RetroLab
return;
}

const { commands, shell } = app;
const baseUrl = PageConfig.getBaseUrl();

const isEnabled = () => {
return (
notebookTracker.currentWidget !== null &&
notebookTracker.currentWidget === shell.currentWidget
);
};

const addInterface = (option: ISwitcherChoice) => {
const { command, icon, buttonLabel, commandLabel, urlPrefix } = option;
commands.addCommand(command, {
label: args => (args.noLabel ? '' : commandLabel),
caption: commandLabel,
icon,
execute: () => {
const current = notebookTracker.currentWidget;
if (!current) {
return;
}
window.open(`${urlPrefix}${current.context.path}`);
},
isEnabled
});

if (palette) {
palette.addItem({ command, category: 'Other' });
}

if (menu) {
menu.viewMenu.addGroup([{ command }], 1);
}

notebookTracker.widgetAdded.connect(
async (sender: INotebookTracker, panel: NotebookPanel) => {
panel.toolbar.insertBefore(
'kernelName',
buttonLabel,
new CommandToolbarButton({
commands,
id: command,
args: { noLabel: 1 }
})
);
await panel.context.ready;
commands.notifyCommandChanged();
}
);
};

// always add Classic
addInterface({
command: 'retrolab:open-classic',
commandLabel: 'Open in Classic Notebook',
buttonLabel: 'openClassic',
icon: jupyterIcon,
urlPrefix: `${baseUrl}tree/`
});

if (!retroShell) {
addInterface({
command: 'retrolab:open-retro',
commandLabel: 'Open in RetroLab',
buttonLabel: 'openRetro',
icon: retroSunIcon,
urlPrefix: `${baseUrl}retro/tree/`
});
}

if (!labShell) {
addInterface({
command: 'retrolab:open-lab',
commandLabel: 'Open in JupyterLab',
buttonLabel: 'openLab',
icon: jupyterFaviconIcon,
urlPrefix: `${baseUrl}doc/tree/`
});
}
}
};

/**
* A plugin to add a command to open the RetroLab Tree.
*/
Expand Down Expand Up @@ -60,9 +207,6 @@ const launchRetroTree: JupyterFrontEndPlugin<void> = {
/**
* Export the plugins as default.
*/
const plugins: JupyterFrontEndPlugin<any>[] = [
launchRetroTree,
interfaceSwitcher
];
const plugins: JupyterFrontEndPlugin<any>[] = [launchRetroTree, launchButtons];

export default plugins;
171 changes: 0 additions & 171 deletions packages/lab-extension/src/interfaceswitcher.tsx

This file was deleted.

3 changes: 3 additions & 0 deletions packages/lab-extension/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
},
"include": ["src/**/*"],
"references": [
{
"path": "../application"
},
{
"path": "../ui-components"
}
Expand Down

0 comments on commit 3f49eea

Please sign in to comment.