UNPKG

6.73 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { PageConfig } from '@jupyterlab/coreutils';
4import { Base64ModelFactory } from '@jupyterlab/docregistry';
5import { Token } from '@lumino/coreutils';
6import { JupyterFrontEnd } from './frontend';
7import { createRendermimePlugins } from './mimerenderers';
8import { LabShell } from './shell';
9import { LabStatus } from './status';
10/**
11 * JupyterLab is the main application class. It is instantiated once and shared.
12 */
13export class JupyterLab extends JupyterFrontEnd {
14 /**
15 * Construct a new JupyterLab object.
16 */
17 constructor(options = { shell: new LabShell() }) {
18 super(Object.assign(Object.assign({}, options), { shell: options.shell || new LabShell() }));
19 /**
20 * The name of the JupyterLab application.
21 */
22 this.name = PageConfig.getOption('appName') || 'JupyterLab';
23 /**
24 * A namespace/prefix plugins may use to denote their provenance.
25 */
26 this.namespace = PageConfig.getOption('appNamespace') || this.name;
27 /**
28 * A list of all errors encountered when registering plugins.
29 */
30 this.registerPluginErrors = [];
31 /**
32 * The application busy and dirty status signals and flags.
33 */
34 this.status = new LabStatus(this);
35 /**
36 * The version of the JupyterLab application.
37 */
38 this.version = PageConfig.getOption('appVersion') || 'unknown';
39 this.restored = this.shell.restored
40 .then(() => undefined)
41 .catch(() => undefined);
42 // Create an IInfo dictionary from the options to override the defaults.
43 const info = Object.keys(JupyterLab.defaultInfo).reduce((acc, val) => {
44 if (val in options) {
45 acc[val] = JSON.parse(JSON.stringify(options[val]));
46 }
47 return acc;
48 }, {});
49 // Populate application info.
50 this._info = Object.assign(Object.assign({}, JupyterLab.defaultInfo), info);
51 // Populate application paths override the defaults if necessary.
52 const defaultURLs = JupyterLab.defaultPaths.urls;
53 const defaultDirs = JupyterLab.defaultPaths.directories;
54 const optionURLs = (options.paths && options.paths.urls) || {};
55 const optionDirs = (options.paths && options.paths.directories) || {};
56 this._paths = {
57 urls: Object.keys(defaultURLs).reduce((acc, key) => {
58 if (key in optionURLs) {
59 const value = optionURLs[key];
60 acc[key] = value;
61 }
62 else {
63 acc[key] = defaultURLs[key];
64 }
65 return acc;
66 }, {}),
67 directories: Object.keys(JupyterLab.defaultPaths.directories).reduce((acc, key) => {
68 if (key in optionDirs) {
69 const value = optionDirs[key];
70 acc[key] = value;
71 }
72 else {
73 acc[key] = defaultDirs[key];
74 }
75 return acc;
76 }, {})
77 };
78 if (this._info.devMode) {
79 this.shell.addClass('jp-mod-devMode');
80 }
81 // Add initial model factory.
82 this.docRegistry.addModelFactory(new Base64ModelFactory());
83 if (options.mimeExtensions) {
84 for (const plugin of createRendermimePlugins(options.mimeExtensions)) {
85 this.registerPlugin(plugin);
86 }
87 }
88 }
89 /**
90 * The JupyterLab application information dictionary.
91 */
92 get info() {
93 return this._info;
94 }
95 /**
96 * The JupyterLab application paths dictionary.
97 */
98 get paths() {
99 return this._paths;
100 }
101 /**
102 * Register plugins from a plugin module.
103 *
104 * @param mod - The plugin module to register.
105 */
106 registerPluginModule(mod) {
107 let data = mod.default;
108 // Handle commonjs exports.
109 if (!mod.hasOwnProperty('__esModule')) {
110 data = mod;
111 }
112 if (!Array.isArray(data)) {
113 data = [data];
114 }
115 data.forEach(item => {
116 try {
117 this.registerPlugin(item);
118 }
119 catch (error) {
120 this.registerPluginErrors.push(error);
121 }
122 });
123 }
124 /**
125 * Register the plugins from multiple plugin modules.
126 *
127 * @param mods - The plugin modules to register.
128 */
129 registerPluginModules(mods) {
130 mods.forEach(mod => {
131 this.registerPluginModule(mod);
132 });
133 }
134}
135/**
136 * The namespace for `JupyterLab` class statics.
137 */
138(function (JupyterLab) {
139 /**
140 * The layout restorer token.
141 */
142 JupyterLab.IInfo = new Token('@jupyterlab/application:IInfo');
143 /**
144 * The default JupyterLab application info.
145 */
146 JupyterLab.defaultInfo = {
147 devMode: PageConfig.getOption('devMode').toLowerCase() === 'true',
148 deferred: { patterns: [], matches: [] },
149 disabled: { patterns: [], matches: [] },
150 mimeExtensions: [],
151 filesCached: PageConfig.getOption('cacheFiles').toLowerCase() === 'true'
152 };
153 /**
154 * The default JupyterLab application paths.
155 */
156 JupyterLab.defaultPaths = {
157 urls: {
158 base: PageConfig.getOption('baseUrl'),
159 notFound: PageConfig.getOption('notFoundUrl'),
160 app: PageConfig.getOption('appUrl'),
161 doc: PageConfig.getOption('docUrl'),
162 static: PageConfig.getOption('staticUrl'),
163 settings: PageConfig.getOption('settingsUrl'),
164 themes: PageConfig.getOption('themesUrl'),
165 translations: PageConfig.getOption('translationsApiUrl'),
166 hubHost: PageConfig.getOption('hubHost') || undefined,
167 hubPrefix: PageConfig.getOption('hubPrefix') || undefined,
168 hubUser: PageConfig.getOption('hubUser') || undefined,
169 hubServerName: PageConfig.getOption('hubServerName') || undefined
170 },
171 directories: {
172 appSettings: PageConfig.getOption('appSettingsDir'),
173 schemas: PageConfig.getOption('schemasDir'),
174 static: PageConfig.getOption('staticDir'),
175 templates: PageConfig.getOption('templatesDir'),
176 themes: PageConfig.getOption('themesDir'),
177 userSettings: PageConfig.getOption('userSettingsDir'),
178 serverRoot: PageConfig.getOption('serverRoot'),
179 workspaces: PageConfig.getOption('workspacesDir')
180 }
181 };
182})(JupyterLab || (JupyterLab = {}));
183//# sourceMappingURL=lab.js.map
\No newline at end of file