Skip to content

Commit cd3d900

Browse files
committed
update typescript comments, imports
1 parent 64d4eec commit cd3d900

File tree

1 file changed

+47
-32
lines changed

1 file changed

+47
-32
lines changed

labextension/src/index.ts

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
import type { Widget, Menu } from "@lumino/widgets";
2+
13
import {
24
JupyterFrontEnd,
35
JupyterFrontEndPlugin,
46
ILayoutRestorer,
5-
ILabShell,
67
} from "@jupyterlab/application";
7-
import { IToolbarWidgetRegistry } from "@jupyterlab/apputils";
8-
9-
import { ILauncher } from "@jupyterlab/launcher";
10-
import { PageConfig } from "@jupyterlab/coreutils";
11-
import { IFrame, MainAreaWidget, WidgetTracker } from "@jupyterlab/apputils";
8+
import {
9+
IToolbarWidgetRegistry,
10+
IFrame,
11+
MainAreaWidget,
12+
WidgetTracker,
13+
} from "@jupyterlab/apputils";
14+
import { PageConfig, URLExt } from "@jupyterlab/coreutils";
1215
import { IDefaultFileBrowser } from "@jupyterlab/filebrowser";
16+
import { ILauncher } from "@jupyterlab/launcher";
17+
import { ServerConnection } from "@jupyterlab/services";
1318

1419
import {
1520
CommandIDs,
@@ -20,7 +25,11 @@ import {
2025
argSchema,
2126
} from "./tokens";
2227

23-
import type { Widget, Menu } from "@lumino/widgets";
28+
// top level constants that won't change during the application lifecycle
29+
const baseUrl = PageConfig.getBaseUrl();
30+
const notebookPage = PageConfig.getOption("notebookPage");
31+
const isNotebook7 = !!notebookPage;
32+
const isTree = isNotebook7 && notebookPage === "tree";
2433

2534
/** Create a new iframe widget. */
2635
function newServerProxyWidget(
@@ -48,6 +57,21 @@ function newServerProxyWidget(
4857
return widget;
4958
}
5059

60+
/** Generate command args for a given server. */
61+
function argsForServer(server_process: IServerProcess): IOpenArgs {
62+
const { new_browser_tab, launcher_entry, name } = server_process;
63+
64+
const suffix = new_browser_tab && !isNotebook7 ? " [↗]" : "";
65+
66+
return {
67+
// do not use URLext here due to trailing slash opinions
68+
url: `${baseUrl}${launcher_entry.path_info}`,
69+
title: `${launcher_entry.title}${suffix}`,
70+
newBrowserTab: new_browser_tab,
71+
id: `${NS}:${name}`,
72+
};
73+
}
74+
5175
/**
5276
* The activate function is registered to be called on activation of the
5377
* jupyterlab extension.
@@ -56,20 +80,19 @@ function newServerProxyWidget(
5680
*/
5781
async function activate(
5882
app: JupyterFrontEnd,
59-
labShell: ILabShell | null,
6083
launcher: ILauncher | null,
6184
restorer: ILayoutRestorer | null,
6285
toolbarRegistry: IToolbarWidgetRegistry | null,
6386
fileBrowser: IDefaultFileBrowser | null,
6487
): Promise<void> {
65-
const baseUrl = PageConfig.getBaseUrl();
66-
// determine whether we are in the Notebook 7 tree
67-
const notebookPage = PageConfig.getOption("notebookPage");
68-
const isNotebook7 = !!notebookPage;
69-
const isTree = isNotebook7 && notebookPage === "tree";
88+
// server connection settings _can't_ be a global, as it can be potentially be configured
89+
const serverSettings = ServerConnection.makeSettings();
7090

7191
// Fetch configured server processes from {base_url}/server-proxy/servers-info
72-
const response = await fetch(`${baseUrl}server-proxy/servers-info`);
92+
// TODO: consider moving this to a separate plugin
93+
// TODO: consider not blocking the application load
94+
const url = URLExt.join(baseUrl, `${NS}/servers-info`);
95+
const response = await ServerConnection.makeRequest(url, {}, serverSettings);
7396

7497
if (!response.ok) {
7598
console.warn(
@@ -79,23 +102,11 @@ async function activate(
79102
return;
80103
}
81104

82-
function argsForServer(server_process: IServerProcess): IOpenArgs {
83-
const { new_browser_tab, launcher_entry, name } = server_process;
84-
85-
const suffix = new_browser_tab && !isNotebook7 ? " [↗]" : "";
86-
87-
return {
88-
url: `${baseUrl}${launcher_entry.path_info}`,
89-
title: `${launcher_entry.title}${suffix}`,
90-
newBrowserTab: new_browser_tab,
91-
id: `${NS}:${name}`,
92-
};
93-
}
94-
105+
// TODO: consider adding JSON schema-derived types
95106
const data: IServersInfo = await response.json();
96107

108+
// handle restoring persistent JupyterLab workspace widgets on page reload
97109
const tracker = new WidgetTracker<MainAreaWidget<IFrame>>({ namespace: NS });
98-
99110
if (restorer) {
100111
void restorer.restore(tracker, {
101112
command: CommandIDs.open,
@@ -109,8 +120,8 @@ async function activate(
109120
});
110121
}
111122

123+
// register commands
112124
const { commands, shell } = app;
113-
114125
commands.addCommand(CommandIDs.open, {
115126
label: (args) => (args as IOpenArgs).title,
116127
describedBy: async () => {
@@ -138,6 +149,8 @@ async function activate(
138149
},
139150
});
140151

152+
// handle adding JupyterLab launcher cards
153+
// TODO: consider moving this to a separate plugin (keeping this ID)
141154
if (launcher) {
142155
for (let server_process of data.server_processes) {
143156
const { launcher_entry } = server_process;
@@ -155,14 +168,17 @@ async function activate(
155168
}
156169
}
157170

158-
if (isTree && !labShell && toolbarRegistry && fileBrowser) {
171+
// handle adding servers menu items to the Notebook 7 _Tree_ toolbar
172+
// TODO: consider moving this to a separate plugin
173+
if (isTree && toolbarRegistry && fileBrowser) {
159174
const { toolbar } = fileBrowser;
160175
const widgets = ((toolbar.layout || {}) as any).widgets as Widget[];
161176
if (widgets && widgets.length) {
162177
for (const widget of widgets) {
163178
if (widget && (widget as any).menus) {
179+
// simple DOM queries can't be used, as there is no guarantee it is
180+
// attached yet
164181
const menu: Menu = (widget as any).menus[0];
165-
console.warn(menu);
166182
menu.addItem({ type: "separator" });
167183
for (const server_process of data.server_processes) {
168184
// create args, overriding all to launch in new heavyweight browser tabs
@@ -188,7 +204,6 @@ const extension: JupyterFrontEndPlugin<void> = {
188204
id: "@jupyterhub/jupyter-server-proxy:add-launcher-entries",
189205
autoStart: true,
190206
optional: [
191-
ILabShell,
192207
ILauncher,
193208
ILayoutRestorer,
194209
IToolbarWidgetRegistry,

0 commit comments

Comments
 (0)