panel.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ClientSession, IClientSession
  5. } from '@jupyterlab/apputils';
  6. import {
  7. IEditorMimeTypeService
  8. } from '@jupyterlab/codeeditor';
  9. import {
  10. PathExt, Time, uuid
  11. } from '@jupyterlab/coreutils';
  12. import {
  13. RenderMimeRegistry
  14. } from '@jupyterlab/rendermime';
  15. import {
  16. ServiceManager
  17. } from '@jupyterlab/services';
  18. import {
  19. Token
  20. } from '@phosphor/coreutils';
  21. import {
  22. Message
  23. } from '@phosphor/messaging';
  24. import {
  25. Panel
  26. } from '@phosphor/widgets';
  27. import {
  28. CodeConsole
  29. } from './widget';
  30. /**
  31. * The class name added to console panels.
  32. */
  33. const PANEL_CLASS = 'jp-ConsolePanel';
  34. const CONSOLE_ICON_CLASS = 'jp-CodeConsoleIcon';
  35. /**
  36. * A panel which contains a console and the ability to add other children.
  37. */
  38. export
  39. class ConsolePanel extends Panel {
  40. /**
  41. * Construct a console panel.
  42. */
  43. constructor(options: ConsolePanel.IOptions) {
  44. super();
  45. this.addClass(PANEL_CLASS);
  46. let {
  47. rendermime, mimeTypeService, path, basePath, name, manager, modelFactory
  48. } = options;
  49. let contentFactory = this.contentFactory = (
  50. options.contentFactory || ConsolePanel.defaultContentFactory
  51. );
  52. let count = Private.count++;
  53. if (!path) {
  54. path = `${basePath || ''}/console-${count}-${uuid()}`;
  55. }
  56. let session = this._session = new ClientSession({
  57. manager: manager.sessions,
  58. path,
  59. name: name || `Console ${count}`,
  60. type: 'console',
  61. kernelPreference: options.kernelPreference
  62. });
  63. let resolver = new RenderMimeRegistry.UrlResolver({
  64. session,
  65. contents: manager.contents
  66. });
  67. rendermime = rendermime.clone({ resolver });
  68. this.console = contentFactory.createConsole({
  69. rendermime, session, mimeTypeService, contentFactory, modelFactory
  70. });
  71. this.addWidget(this.console);
  72. session.initialize().then(() => {
  73. this._connected = new Date();
  74. this._updateTitle();
  75. });
  76. this.console.executed.connect(this._onExecuted, this);
  77. this._updateTitle();
  78. session.kernelChanged.connect(this._updateTitle, this);
  79. session.propertyChanged.connect(this._updateTitle, this);
  80. this.title.icon = CONSOLE_ICON_CLASS;
  81. this.title.closable = true;
  82. this.id = `console-${count}`;
  83. }
  84. /**
  85. * The content factory used by the console panel.
  86. */
  87. readonly contentFactory: ConsolePanel.IContentFactory;
  88. /**
  89. * The console widget used by the panel.
  90. */
  91. readonly console: CodeConsole;
  92. /**
  93. * The session used by the panel.
  94. */
  95. get session(): IClientSession {
  96. return this._session;
  97. }
  98. /**
  99. * Dispose of the resources held by the widget.
  100. */
  101. dispose(): void {
  102. this.session.dispose();
  103. this.console.dispose();
  104. super.dispose();
  105. }
  106. /**
  107. * Handle `'activate-request'` messages.
  108. */
  109. protected onActivateRequest(msg: Message): void {
  110. let prompt = this.console.promptCell;
  111. if (prompt) {
  112. prompt.editor.focus();
  113. }
  114. }
  115. /**
  116. * Handle `'close-request'` messages.
  117. */
  118. protected onCloseRequest(msg: Message): void {
  119. super.onCloseRequest(msg);
  120. this.dispose();
  121. }
  122. /**
  123. * Handle a console execution.
  124. */
  125. private _onExecuted(sender: CodeConsole, args: Date) {
  126. this._executed = args;
  127. this._updateTitle();
  128. }
  129. /**
  130. * Update the console panel title.
  131. */
  132. private _updateTitle(): void {
  133. Private.updateTitle(this, this._connected, this._executed);
  134. }
  135. private _executed: Date | null = null;
  136. private _connected: Date | null = null;
  137. private _session: ClientSession;
  138. }
  139. /**
  140. * A namespace for ConsolePanel statics.
  141. */
  142. export
  143. namespace ConsolePanel {
  144. /**
  145. * The initialization options for a console panel.
  146. */
  147. export
  148. interface IOptions {
  149. /**
  150. * The rendermime instance used by the panel.
  151. */
  152. rendermime: RenderMimeRegistry;
  153. /**
  154. * The content factory for the panel.
  155. */
  156. contentFactory: IContentFactory;
  157. /**
  158. * The service manager used by the panel.
  159. */
  160. manager: ServiceManager.IManager;
  161. /**
  162. * The path of an existing console.
  163. */
  164. path?: string;
  165. /**
  166. * The base path for a new console.
  167. */
  168. basePath?: string;
  169. /**
  170. * The name of the console.
  171. */
  172. name?: string;
  173. /**
  174. * A kernel preference.
  175. */
  176. kernelPreference?: IClientSession.IKernelPreference;
  177. /**
  178. * The model factory for the console widget.
  179. */
  180. modelFactory?: CodeConsole.IModelFactory;
  181. /**
  182. * The service used to look up mime types.
  183. */
  184. mimeTypeService: IEditorMimeTypeService;
  185. }
  186. /**
  187. * The console panel renderer.
  188. */
  189. export
  190. interface IContentFactory extends CodeConsole.IContentFactory {
  191. /**
  192. * Create a new console panel.
  193. */
  194. createConsole(options: CodeConsole.IOptions): CodeConsole;
  195. }
  196. /**
  197. * Default implementation of `IContentFactory`.
  198. */
  199. export
  200. class ContentFactory extends CodeConsole.ContentFactory implements IContentFactory {
  201. /**
  202. * Create a new console panel.
  203. */
  204. createConsole(options: CodeConsole.IOptions): CodeConsole {
  205. return new CodeConsole(options);
  206. }
  207. }
  208. /**
  209. * A namespace for the console panel content factory.
  210. */
  211. export
  212. namespace ContentFactory {
  213. /**
  214. * Options for the code console content factory.
  215. */
  216. export
  217. interface IOptions extends CodeConsole.ContentFactory.IOptions { }
  218. }
  219. /**
  220. * A default code console content factory.
  221. */
  222. export
  223. const defaultContentFactory: IContentFactory = new ContentFactory();
  224. /* tslint:disable */
  225. /**
  226. * The console renderer token.
  227. */
  228. export
  229. const IContentFactory = new Token<IContentFactory>('@jupyterlab/console:IContentFactory');
  230. /* tslint:enable */
  231. }
  232. /**
  233. * A namespace for private data.
  234. */
  235. namespace Private {
  236. /**
  237. * The counter for new consoles.
  238. */
  239. export
  240. let count = 1;
  241. /**
  242. * Update the title of a console panel.
  243. */
  244. export
  245. function updateTitle(panel: ConsolePanel, connected: Date | null, executed: Date | null) {
  246. let session = panel.console.session;
  247. let caption = (
  248. `Name: ${session.name}\n` +
  249. `Directory: ${PathExt.dirname(session.path)}\n` +
  250. `Kernel: ${session.kernelDisplayName}`
  251. );
  252. if (connected) {
  253. caption += `\nConnected: ${Time.format(connected.toISOString())}`;
  254. }
  255. if (executed) {
  256. caption += `\nLast Execution: ${Time.format(executed.toISOString())}`;
  257. }
  258. panel.title.label = session.name;
  259. panel.title.caption = caption;
  260. }
  261. }