modelfactory.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Contents
  5. } from '@jupyterlab/services';
  6. import {
  7. DocumentRegistry
  8. } from '../docregistry';
  9. import {
  10. CodeCellModel
  11. } from '../cells';
  12. import {
  13. INotebookModel, NotebookModel
  14. } from './model';
  15. /**
  16. * A model factory for notebooks.
  17. */
  18. export
  19. class NotebookModelFactory implements DocumentRegistry.IModelFactory<INotebookModel> {
  20. /**
  21. * Construct a new notebook model factory.
  22. */
  23. constructor(options: NotebookModelFactory.IOptions) {
  24. let codeCellContentFactory = options.codeCellContentFactory;
  25. this.contentFactory = (options.contentFactory ||
  26. new NotebookModel.ContentFactory({ codeCellContentFactory })
  27. );
  28. }
  29. /**
  30. * The content model factory used by the NotebookModelFactory.
  31. */
  32. readonly contentFactory: NotebookModel.IContentFactory;
  33. /**
  34. * The name of the model.
  35. */
  36. get name(): string {
  37. return 'notebook';
  38. }
  39. /**
  40. * The content type of the file.
  41. */
  42. get contentType(): Contents.ContentType {
  43. return 'notebook';
  44. }
  45. /**
  46. * The format of the file.
  47. */
  48. get fileFormat(): Contents.FileFormat {
  49. return 'json';
  50. }
  51. /**
  52. * Get whether the model factory has been disposed.
  53. */
  54. get isDisposed(): boolean {
  55. return this._disposed;
  56. }
  57. /**
  58. * Dispose of the model factory.
  59. */
  60. dispose(): void {
  61. this._disposed = true;
  62. }
  63. /**
  64. * Create a new model for a given path.
  65. *
  66. * @param languagePreference - An optional kernel language preference.
  67. *
  68. * @returns A new document model.
  69. */
  70. createNew(languagePreference?: string): INotebookModel {
  71. let contentFactory = this.contentFactory;
  72. return new NotebookModel({ languagePreference, contentFactory });
  73. }
  74. /**
  75. * Get the preferred kernel language given a path.
  76. */
  77. preferredLanguage(path: string): string {
  78. return '';
  79. }
  80. private _disposed = false;
  81. }
  82. /**
  83. * The namespace for notebook model factory statics.
  84. */
  85. export
  86. namespace NotebookModelFactory {
  87. /**
  88. * The options used to initialize a NotebookModelFactory.
  89. */
  90. export
  91. interface IOptions {
  92. /**
  93. * The factory for code cell content.
  94. */
  95. codeCellContentFactory?: CodeCellModel.IContentFactory;
  96. /**
  97. * The content factory used by the NotebookModelFactory. If
  98. * given, it will supercede the `codeCellContentFactory`.
  99. */
  100. contentFactory?: NotebookModel.IContentFactory;
  101. }
  102. }