mimemodel.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* -----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
  6. import { ReadonlyPartialJSONObject } from '@lumino/coreutils';
  7. /**
  8. * The default mime model implementation.
  9. */
  10. export class MimeModel implements IRenderMime.IMimeModel {
  11. /**
  12. * Construct a new mime model.
  13. */
  14. constructor(options: MimeModel.IOptions = {}) {
  15. this.trusted = !!options.trusted;
  16. this._data = options.data || {};
  17. this._metadata = options.metadata || {};
  18. this._callback = options.callback || Private.noOp;
  19. }
  20. /**
  21. * Whether the model is trusted.
  22. */
  23. readonly trusted: boolean;
  24. /**
  25. * The data associated with the model.
  26. */
  27. get data(): ReadonlyPartialJSONObject {
  28. return this._data;
  29. }
  30. /**
  31. * The metadata associated with the model.
  32. */
  33. get metadata(): ReadonlyPartialJSONObject {
  34. return this._metadata;
  35. }
  36. /**
  37. * Set the data associated with the model.
  38. *
  39. * #### Notes
  40. * Depending on the implementation of the mime model,
  41. * this call may or may not have deferred effects,
  42. */
  43. setData(options: IRenderMime.IMimeModel.ISetDataOptions): void {
  44. this._data = options.data || this._data;
  45. this._metadata = options.metadata || this._metadata;
  46. this._callback(options);
  47. }
  48. private _callback: (options: IRenderMime.IMimeModel.ISetDataOptions) => void;
  49. private _data: ReadonlyPartialJSONObject;
  50. private _metadata: ReadonlyPartialJSONObject;
  51. }
  52. /**
  53. * The namespace for MimeModel class statics.
  54. */
  55. export namespace MimeModel {
  56. /**
  57. * The options used to create a mime model.
  58. */
  59. export interface IOptions {
  60. /**
  61. * Whether the model is trusted. Defaults to `false`.
  62. */
  63. trusted?: boolean;
  64. /**
  65. * A callback function for when the data changes.
  66. */
  67. callback?: (options: IRenderMime.IMimeModel.ISetDataOptions) => void;
  68. /**
  69. * The initial mime data.
  70. */
  71. data?: ReadonlyPartialJSONObject;
  72. /**
  73. * The initial mime metadata.
  74. */
  75. metadata?: ReadonlyPartialJSONObject;
  76. }
  77. }
  78. /**
  79. * The namespace for module private data.
  80. */
  81. namespace Private {
  82. /**
  83. * A no-op callback function.
  84. */
  85. export function noOp() {
  86. /* no-op */
  87. }
  88. }