plugin.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. JupyterLab, JupyterLabPlugin
  5. } from '../application';
  6. import {
  7. ICommandLinker
  8. } from '../commandlinker';
  9. import {
  10. ICommandPalette
  11. } from '../commandpalette';
  12. import {
  13. InstanceTracker
  14. } from '../common/instancetracker';
  15. import {
  16. ILayoutRestorer
  17. } from '../layoutrestorer';
  18. import {
  19. FaqModel, FaqWidget
  20. } from './widget';
  21. /**
  22. * The FAQ page extension.
  23. */
  24. const plugin: JupyterLabPlugin<void> = {
  25. id: 'jupyter.extensions.faq',
  26. requires: [ICommandPalette, ICommandLinker, ILayoutRestorer],
  27. activate: activateFAQ,
  28. autoStart: true
  29. };
  30. /**
  31. * Export the plugin as default.
  32. */
  33. export default plugin;
  34. /**
  35. * Activate the FAQ plugin.
  36. */
  37. function activateFAQ(app: JupyterLab, palette: ICommandPalette, linker: ICommandLinker, layout: ILayoutRestorer): void {
  38. const category = 'Help';
  39. const command = 'faq-jupyterlab:show';
  40. const model = new FaqModel();
  41. const tracker = new InstanceTracker<FaqWidget>({ namespace: 'faq' });
  42. // Handle state restoration.
  43. layout.restore(tracker, {
  44. command,
  45. args: () => null,
  46. name: () => 'faq'
  47. });
  48. let widget: FaqWidget;
  49. function newWidget(): FaqWidget {
  50. let widget = new FaqWidget({ linker });
  51. widget.model = model;
  52. widget.id = 'faq';
  53. widget.title.label = 'FAQ';
  54. widget.title.closable = true;
  55. tracker.add(widget);
  56. return widget;
  57. }
  58. app.commands.addCommand(command, {
  59. label: 'Frequently Asked Questions',
  60. execute: () => {
  61. if (!widget || widget.isDisposed) {
  62. widget = newWidget();
  63. app.shell.addToMainArea(widget);
  64. }
  65. app.shell.activateMain(widget.id);
  66. }
  67. });
  68. palette.addItem({ command, category });
  69. }