widgetfactory.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. IEditorMimeTypeService
  5. } from '@jupyterlab/codeeditor';
  6. import {
  7. ABCWidgetFactory, DocumentRegistry
  8. } from '@jupyterlab/docregistry';
  9. import {
  10. IRenderMime
  11. } from '@jupyterlab/rendermime';
  12. import {
  13. ToolbarItems
  14. } from './default-toolbar';
  15. import {
  16. INotebookModel
  17. } from './model';
  18. import {
  19. NotebookPanel
  20. } from './panel';
  21. /**
  22. * A widget factory for notebook panels.
  23. */
  24. export
  25. class NotebookWidgetFactory extends ABCWidgetFactory<NotebookPanel, INotebookModel> {
  26. /**
  27. * Construct a new notebook widget factory.
  28. *
  29. * @param options - The options used to construct the factory.
  30. */
  31. constructor(options: NotebookWidgetFactory.IOptions) {
  32. super(options);
  33. this.rendermime = options.rendermime;
  34. this.contentFactory = options.contentFactory;
  35. this.mimeTypeService = options.mimeTypeService;
  36. }
  37. /*
  38. * The rendermime instance.
  39. */
  40. readonly rendermime: IRenderMime;
  41. /**
  42. * The content factory used by the widget factory.
  43. */
  44. readonly contentFactory: NotebookPanel.IContentFactory;
  45. /**
  46. * The service used to look up mime types.
  47. */
  48. readonly mimeTypeService: IEditorMimeTypeService;
  49. /**
  50. * Create a new widget.
  51. *
  52. * #### Notes
  53. * The factory will start the appropriate kernel and populate
  54. * the default toolbar items using `ToolbarItems.populateDefaults`.
  55. */
  56. protected createNewWidget(context: DocumentRegistry.IContext<INotebookModel>): NotebookPanel {
  57. let rendermime = this.rendermime.clone();
  58. let panel = new NotebookPanel({
  59. rendermime,
  60. contentFactory: this.contentFactory,
  61. mimeTypeService: this.mimeTypeService
  62. });
  63. panel.context = context;
  64. ToolbarItems.populateDefaults(panel);
  65. return panel;
  66. }
  67. }
  68. /**
  69. * The namespace for `NotebookWidgetFactory` statics.
  70. */
  71. export
  72. namespace NotebookWidgetFactory {
  73. /**
  74. * The options used to construct a `NotebookWidgetFactory`.
  75. */
  76. export
  77. interface IOptions extends DocumentRegistry.IWidgetFactoryOptions {
  78. /*
  79. * A rendermime instance.
  80. */
  81. rendermime: IRenderMime;
  82. /**
  83. * A notebook panel content factory.
  84. */
  85. contentFactory: NotebookPanel.IContentFactory;
  86. /**
  87. * The service used to look up mime types.
  88. */
  89. mimeTypeService: IEditorMimeTypeService;
  90. }
  91. }