index.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ILabShell,
  5. JupyterFrontEnd,
  6. JupyterFrontEndPlugin
  7. } from '@jupyterlab/application';
  8. import { ICommandPalette, MainAreaWidget } from '@jupyterlab/apputils';
  9. import { ILauncher, LauncherModel, Launcher } from '@jupyterlab/launcher';
  10. import { toArray } from '@phosphor/algorithm';
  11. import { JSONObject } from '@phosphor/coreutils';
  12. import { Widget } from '@phosphor/widgets';
  13. import '../style/index.css';
  14. /**
  15. * The command IDs used by the launcher plugin.
  16. */
  17. namespace CommandIDs {
  18. export const create = 'launcher:create';
  19. }
  20. /**
  21. * A service providing an interface to the the launcher.
  22. */
  23. const plugin: JupyterFrontEndPlugin<ILauncher> = {
  24. activate,
  25. id: '@jupyterlab/launcher-extension:plugin',
  26. requires: [ICommandPalette, ILabShell],
  27. provides: ILauncher,
  28. autoStart: true
  29. };
  30. /**
  31. * Export the plugin as default.
  32. */
  33. export default plugin;
  34. /**
  35. * Activate the launcher.
  36. */
  37. function activate(
  38. app: JupyterFrontEnd,
  39. palette: ICommandPalette,
  40. labShell: ILabShell
  41. ): ILauncher {
  42. const { commands } = app;
  43. const model = new LauncherModel();
  44. commands.addCommand(CommandIDs.create, {
  45. label: 'New Launcher',
  46. execute: (args: JSONObject) => {
  47. const cwd = args['cwd'] ? String(args['cwd']) : '';
  48. const id = `launcher-${Private.id++}`;
  49. const callback = (item: Widget) => {
  50. labShell.add(item, 'main', { ref: id });
  51. };
  52. const launcher = new Launcher({ cwd, callback, commands });
  53. launcher.model = model;
  54. launcher.title.label = 'Launcher';
  55. launcher.title.iconClass = 'jp-LauncherIcon';
  56. let main = new MainAreaWidget({ content: launcher });
  57. // If there are any other widgets open, remove the launcher close icon.
  58. main.title.closable = !!toArray(labShell.widgets('main')).length;
  59. main.id = id;
  60. labShell.add(main, 'main', { activate: args['activate'] as boolean });
  61. labShell.layoutModified.connect(
  62. () => {
  63. // If there is only a launcher open, remove the close icon.
  64. main.title.closable = toArray(labShell.widgets('main')).length > 1;
  65. },
  66. main
  67. );
  68. return main;
  69. }
  70. });
  71. palette.addItem({ command: CommandIDs.create, category: 'Launcher' });
  72. return model;
  73. }
  74. /**
  75. * The namespace for module private data.
  76. */
  77. namespace Private {
  78. /**
  79. * The incrementing id used for launcher widgets.
  80. */
  81. export let id = 0;
  82. }