inspector.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { Printing } from '@jupyterlab/apputils';
  4. import {
  5. nullTranslator,
  6. ITranslator,
  7. TranslationBundle
  8. } from '@jupyterlab/translation';
  9. import { Panel, PanelLayout, Widget } from '@lumino/widgets';
  10. import { IInspector } from './tokens';
  11. /**
  12. * The class name added to inspector panels.
  13. */
  14. const PANEL_CLASS = 'jp-Inspector';
  15. /**
  16. * The class name added to inspector content.
  17. */
  18. const CONTENT_CLASS = 'jp-Inspector-content';
  19. /**
  20. * The class name added to default inspector content.
  21. */
  22. const DEFAULT_CONTENT_CLASS = 'jp-Inspector-default-content';
  23. /**
  24. * A panel which contains a set of inspectors.
  25. */
  26. export class InspectorPanel
  27. extends Panel
  28. implements IInspector, Printing.IPrintable {
  29. /**
  30. * Construct an inspector.
  31. */
  32. constructor(options: InspectorPanel.IOptions = {}) {
  33. super();
  34. this.translator = options.translator || nullTranslator;
  35. this._trans = this.translator.load('jupyterlab');
  36. if (options.initialContent instanceof Widget) {
  37. this._content = options.initialContent;
  38. } else if (typeof options.initialContent === 'string') {
  39. this._content = InspectorPanel._generateContentWidget(
  40. `<p>${options.initialContent}</p>`
  41. );
  42. } else {
  43. this._content = InspectorPanel._generateContentWidget(
  44. '<p>' +
  45. this._trans.__('Click on a function to see documentation.') +
  46. '</p>'
  47. );
  48. }
  49. this.addClass(PANEL_CLASS);
  50. (this.layout as PanelLayout).addWidget(this._content);
  51. }
  52. /**
  53. * Print in iframe
  54. */
  55. [Printing.symbol]() {
  56. return () => Printing.printWidget(this);
  57. }
  58. /**
  59. * The source of events the inspector panel listens for.
  60. */
  61. get source(): IInspector.IInspectable | null {
  62. return this._source;
  63. }
  64. set source(source: IInspector.IInspectable | null) {
  65. if (this._source === source) {
  66. return;
  67. }
  68. // Disconnect old signal handler.
  69. if (this._source) {
  70. this._source.standby = true;
  71. this._source.inspected.disconnect(this.onInspectorUpdate, this);
  72. this._source.disposed.disconnect(this.onSourceDisposed, this);
  73. }
  74. // Reject a source that is already disposed.
  75. if (source && source.isDisposed) {
  76. source = null;
  77. }
  78. // Update source.
  79. this._source = source;
  80. // Connect new signal handler.
  81. if (this._source) {
  82. this._source.standby = false;
  83. this._source.inspected.connect(this.onInspectorUpdate, this);
  84. this._source.disposed.connect(this.onSourceDisposed, this);
  85. }
  86. }
  87. /**
  88. * Dispose of the resources held by the widget.
  89. */
  90. dispose(): void {
  91. if (this.isDisposed) {
  92. return;
  93. }
  94. this.source = null;
  95. super.dispose();
  96. }
  97. /**
  98. * Handle inspector update signals.
  99. */
  100. protected onInspectorUpdate(
  101. sender: any,
  102. args: IInspector.IInspectorUpdate
  103. ): void {
  104. const { content } = args;
  105. // Update the content of the inspector widget.
  106. if (!content || content === this._content) {
  107. return;
  108. }
  109. this._content.dispose();
  110. this._content = content;
  111. content.addClass(CONTENT_CLASS);
  112. (this.layout as PanelLayout).addWidget(content);
  113. }
  114. /**
  115. * Handle source disposed signals.
  116. */
  117. protected onSourceDisposed(sender: any, args: void): void {
  118. this.source = null;
  119. }
  120. /**
  121. * Generate content widget from string
  122. */
  123. private static _generateContentWidget(message: string): Widget {
  124. const widget = new Widget();
  125. widget.node.innerHTML = message;
  126. widget.addClass(CONTENT_CLASS);
  127. widget.addClass(DEFAULT_CONTENT_CLASS);
  128. return widget;
  129. }
  130. protected translator: ITranslator;
  131. private _trans: TranslationBundle;
  132. private _content: Widget;
  133. private _source: IInspector.IInspectable | null = null;
  134. }
  135. export namespace InspectorPanel {
  136. export interface IOptions {
  137. initialContent?: Widget | string | undefined;
  138. /**
  139. * The aplication language translator.
  140. */
  141. translator?: ITranslator;
  142. }
  143. }