index.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. // Local CSS must be loaded prior to loading other libs.
  4. import '../style/index.css';
  5. import {
  6. PageConfig
  7. } from '@jupyterlab/coreutils';
  8. import {
  9. CommandLinker
  10. } from '@jupyterlab/apputils';
  11. import {
  12. Base64ModelFactory, DocumentRegistry
  13. } from '@jupyterlab/docregistry';
  14. import {
  15. IRenderMime
  16. } from '@jupyterlab/rendermime-interfaces';
  17. import {
  18. ServiceManager
  19. } from '@jupyterlab/services';
  20. import {
  21. Application, IPlugin
  22. } from '@phosphor/application';
  23. import {
  24. DisposableDelegate, IDisposable
  25. } from '@phosphor/disposable';
  26. import {
  27. createRendermimePlugins
  28. } from './mimerenderers';
  29. import {
  30. ApplicationShell
  31. } from './shell';
  32. export { ApplicationShell } from './shell';
  33. export { ILayoutRestorer, LayoutRestorer } from './layoutrestorer';
  34. export { IRouter, Router } from './router';
  35. /**
  36. * The type for all JupyterLab plugins.
  37. */
  38. export
  39. type JupyterLabPlugin<T> = IPlugin<JupyterLab, T>;
  40. /**
  41. * JupyterLab is the main application class. It is instantiated once and shared.
  42. */
  43. export
  44. class JupyterLab extends Application<ApplicationShell> {
  45. /**
  46. * Construct a new JupyterLab object.
  47. */
  48. constructor(options: JupyterLab.IOptions = {}) {
  49. super({ shell: new ApplicationShell() });
  50. this._info = { ...JupyterLab.defaultInfo, ...options };
  51. if (this._info.devMode) {
  52. this.shell.addClass('jp-mod-devMode');
  53. }
  54. this.serviceManager = new ServiceManager();
  55. let linker = new CommandLinker({ commands: this.commands });
  56. this.commandLinker = linker;
  57. let registry = this.docRegistry = new DocumentRegistry();
  58. registry.addModelFactory(new Base64ModelFactory());
  59. if (options.mimeExtensions) {
  60. let plugins = createRendermimePlugins(options.mimeExtensions);
  61. plugins.forEach(plugin => { this.registerPlugin(plugin); });
  62. }
  63. }
  64. /**
  65. * The document registry instance used by the application.
  66. */
  67. readonly docRegistry: DocumentRegistry;
  68. /**
  69. * The command linker used by the application.
  70. */
  71. readonly commandLinker: CommandLinker;
  72. /**
  73. * The service manager used by the application.
  74. */
  75. readonly serviceManager: ServiceManager;
  76. /**
  77. * Whether the application is dirty.
  78. */
  79. get isDirty(): boolean {
  80. return this._dirtyCount > 0;
  81. }
  82. /**
  83. * The information about the application.
  84. */
  85. get info(): JupyterLab.IInfo {
  86. return this._info;
  87. }
  88. /**
  89. * Promise that resolves when state is first restored, returning layout description.
  90. *
  91. * #### Notes
  92. * This is just a reference to `shell.restored`.
  93. */
  94. get restored(): Promise<ApplicationShell.ILayout> {
  95. return this.shell.restored;
  96. }
  97. /**
  98. * Set the application state to dirty.
  99. *
  100. * @returns A disposable used to clear the dirty state for the caller.
  101. */
  102. setDirty(): IDisposable {
  103. this._dirtyCount++;
  104. return new DisposableDelegate(() => {
  105. this._dirtyCount = Math.max(0, this._dirtyCount - 1);
  106. });
  107. }
  108. /**
  109. * Register plugins from a plugin module.
  110. *
  111. * @param mod - The plugin module to register.
  112. */
  113. registerPluginModule(mod: JupyterLab.IPluginModule): void {
  114. let data = mod.default;
  115. // Handle commonjs exports.
  116. if (!mod.hasOwnProperty('__esModule')) {
  117. data = mod as any;
  118. }
  119. if (!Array.isArray(data)) {
  120. data = [data];
  121. }
  122. data.forEach(item => { this.registerPlugin(item); });
  123. }
  124. /**
  125. * Register the plugins from multiple plugin modules.
  126. *
  127. * @param mods - The plugin modules to register.
  128. */
  129. registerPluginModules(mods: JupyterLab.IPluginModule[]): void {
  130. mods.forEach(mod => { this.registerPluginModule(mod); });
  131. }
  132. private _info: JupyterLab.IInfo;
  133. private _dirtyCount = 0;
  134. }
  135. /**
  136. * The namespace for `JupyterLab` class statics.
  137. */
  138. export
  139. namespace JupyterLab {
  140. /**
  141. * The options used to initialize a JupyterLab object.
  142. */
  143. export
  144. interface IOptions extends Partial<IInfo> {}
  145. /**
  146. * The information about a JupyterLab application.
  147. */
  148. export
  149. interface IInfo {
  150. /**
  151. * The name of the JupyterLab application.
  152. */
  153. readonly name: string;
  154. /**
  155. * The version of the JupyterLab application.
  156. */
  157. readonly version: string;
  158. /**
  159. * The namespace/prefix plugins may use to denote their origin.
  160. */
  161. readonly namespace: string;
  162. /**
  163. * Whether the application is in dev mode.
  164. */
  165. readonly devMode: boolean;
  166. /**
  167. * The collection of deferred extension patterns and matched extensions.
  168. */
  169. readonly deferred: { patterns: string[], matches: string[] };
  170. /**
  171. * The collection of disabled extension patterns and matched extensions.
  172. */
  173. readonly disabled: { patterns: string[], matches: string[] };
  174. /**
  175. * The mime renderer extensions.
  176. */
  177. readonly mimeExtensions: IRenderMime.IExtensionModule[];
  178. /**
  179. * The urls used by the application.
  180. */
  181. readonly urls: {
  182. readonly page: string,
  183. readonly public: string,
  184. readonly settings: string,
  185. readonly themes: string
  186. };
  187. /**
  188. * The local directories used by the application.
  189. */
  190. readonly directories: {
  191. readonly appSettings: string,
  192. readonly schemas: string,
  193. readonly static: string,
  194. readonly templates: string,
  195. readonly themes: string,
  196. readonly userSettings: string,
  197. readonly serverRoot: string
  198. };
  199. /**
  200. * Whether files are cached on the server.
  201. */
  202. readonly filesCached: boolean;
  203. }
  204. /**
  205. * The default application info.
  206. */
  207. export
  208. const defaultInfo: IInfo = {
  209. name: PageConfig.getOption('appName') || 'JupyterLab',
  210. namespace: PageConfig.getOption('appNamespace'),
  211. version: PageConfig.getOption('appVersion') || 'unknown',
  212. devMode: PageConfig.getOption('devMode').toLowerCase() === 'true',
  213. deferred: { patterns: [], matches: [] },
  214. disabled: { patterns: [], matches: [] },
  215. mimeExtensions: [],
  216. urls: {
  217. page: PageConfig.getOption('pageUrl'),
  218. public: PageConfig.getOption('publicUrl'),
  219. settings: PageConfig.getOption('settingsUrl'),
  220. themes: PageConfig.getOption('themesUrl')
  221. },
  222. directories: {
  223. appSettings: PageConfig.getOption('appSettingsDir'),
  224. schemas: PageConfig.getOption('schemasDir'),
  225. static: PageConfig.getOption('staticDir'),
  226. templates: PageConfig.getOption('templatesDir'),
  227. themes: PageConfig.getOption('themesDir'),
  228. userSettings: PageConfig.getOption('userSettingsDir'),
  229. serverRoot: PageConfig.getOption('serverRoot')
  230. },
  231. filesCached: PageConfig.getOption('cacheFiles').toLowerCase() === 'true'
  232. };
  233. /**
  234. * The interface for a module that exports a plugin or plugins as
  235. * the default value.
  236. */
  237. export
  238. interface IPluginModule {
  239. /**
  240. * The default export.
  241. */
  242. default: JupyterLabPlugin<any> | JupyterLabPlugin<any>[];
  243. }
  244. }