panel.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.ready.then(() => {
  73. this._connected = new Date();
  74. this._updateTitle();
  75. });
  76. this.console.executed.connect(this._onExecuted, this);
  77. session.kernelChanged.connect(this._updateTitle, this);
  78. session.propertyChanged.connect(this._updateTitle, this);
  79. this.title.icon = CONSOLE_ICON_CLASS;
  80. this.title.closable = true;
  81. this.id = `console-${count}`;
  82. }
  83. /**
  84. * The content factory used by the console panel.
  85. */
  86. readonly contentFactory: ConsolePanel.IContentFactory;
  87. /**
  88. * The console widget used by the panel.
  89. */
  90. readonly console: CodeConsole;
  91. /**
  92. * The session used by the panel.
  93. */
  94. get session(): IClientSession {
  95. return this._session;
  96. }
  97. /**
  98. * Dispose of the resources held by the widget.
  99. */
  100. dispose(): void {
  101. this.console.dispose();
  102. super.dispose();
  103. }
  104. /**
  105. * Handle `'after-attach'` messages.
  106. */
  107. protected onAfterAttach(msg: Message): void {
  108. this._session.initialize();
  109. let prompt = this.console.promptCell;
  110. if (prompt) {
  111. prompt.editor.focus();
  112. }
  113. }
  114. /**
  115. * Handle `'activate-request'` messages.
  116. */
  117. protected onActivateRequest(msg: Message): void {
  118. let prompt = this.console.promptCell;
  119. if (prompt) {
  120. prompt.editor.focus();
  121. }
  122. }
  123. /**
  124. * Handle `'close-request'` messages.
  125. */
  126. protected onCloseRequest(msg: Message): void {
  127. super.onCloseRequest(msg);
  128. this.dispose();
  129. }
  130. /**
  131. * Handle a console execution.
  132. */
  133. private _onExecuted(sender: CodeConsole, args: Date) {
  134. this._executed = args;
  135. this._updateTitle();
  136. }
  137. /**
  138. * Update the console panel title.
  139. */
  140. private _updateTitle(): void {
  141. Private.updateTitle(this, this._connected, this._executed);
  142. }
  143. private _executed: Date | null = null;
  144. private _connected: Date | null = null;
  145. private _session: ClientSession;
  146. }
  147. /**
  148. * A namespace for ConsolePanel statics.
  149. */
  150. export
  151. namespace ConsolePanel {
  152. /**
  153. * The initialization options for a console panel.
  154. */
  155. export
  156. interface IOptions {
  157. /**
  158. * The rendermime instance used by the panel.
  159. */
  160. rendermime: RenderMimeRegistry;
  161. /**
  162. * The content factory for the panel.
  163. */
  164. contentFactory: IContentFactory;
  165. /**
  166. * The service manager used by the panel.
  167. */
  168. manager: ServiceManager.IManager;
  169. /**
  170. * The path of an existing console.
  171. */
  172. path?: string;
  173. /**
  174. * The base path for a new console.
  175. */
  176. basePath?: string;
  177. /**
  178. * The name of the console.
  179. */
  180. name?: string;
  181. /**
  182. * A kernel preference.
  183. */
  184. kernelPreference?: IClientSession.IKernelPreference;
  185. /**
  186. * The model factory for the console widget.
  187. */
  188. modelFactory?: CodeConsole.IModelFactory;
  189. /**
  190. * The service used to look up mime types.
  191. */
  192. mimeTypeService: IEditorMimeTypeService;
  193. }
  194. /**
  195. * The console panel renderer.
  196. */
  197. export
  198. interface IContentFactory extends CodeConsole.IContentFactory {
  199. /**
  200. * Create a new console panel.
  201. */
  202. createConsole(options: CodeConsole.IOptions): CodeConsole;
  203. }
  204. /**
  205. * Default implementation of `IContentFactory`.
  206. */
  207. export
  208. class ContentFactory extends CodeConsole.ContentFactory implements IContentFactory {
  209. /**
  210. * Create a new console panel.
  211. */
  212. createConsole(options: CodeConsole.IOptions): CodeConsole {
  213. return new CodeConsole(options);
  214. }
  215. }
  216. /**
  217. * A namespace for the console panel content factory.
  218. */
  219. export
  220. namespace ContentFactory {
  221. /**
  222. * Options for the code console content factory.
  223. */
  224. export
  225. interface IOptions extends CodeConsole.ContentFactory.IOptions { }
  226. }
  227. /**
  228. * A default code console content factory.
  229. */
  230. export
  231. const defaultContentFactory: IContentFactory = new ContentFactory();
  232. /* tslint:disable */
  233. /**
  234. * The console renderer token.
  235. */
  236. export
  237. const IContentFactory = new Token<IContentFactory>('@jupyterlab/console:IContentFactory');
  238. /* tslint:enable */
  239. }
  240. /**
  241. * A namespace for private data.
  242. */
  243. namespace Private {
  244. /**
  245. * The counter for new consoles.
  246. */
  247. export
  248. let count = 1;
  249. /**
  250. * Update the title of a console panel.
  251. */
  252. export
  253. function updateTitle(panel: ConsolePanel, connected: Date | null, executed: Date | null) {
  254. let session = panel.console.session;
  255. let caption = (
  256. `Name: ${session.name}\n` +
  257. `Directory: ${PathExt.dirname(session.path)}\n` +
  258. `Kernel: ${session.kernelDisplayName}`
  259. );
  260. if (connected) {
  261. caption += `\nConnected: ${Time.format(connected.toISOString())}`;
  262. }
  263. if (executed) {
  264. caption += `\nLast Execution: ${Time.format(executed.toISOString())}`;
  265. }
  266. panel.title.label = session.name;
  267. panel.title.caption = caption;
  268. }
  269. }