-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathindex.ts
148 lines (131 loc) · 4.15 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Menu } from '@lumino/widgets';
import { JupyterFrontEnd, JupyterFrontEndPlugin, ILayoutRestorer } from '@jupyterlab/application';
import { ILauncher } from '@jupyterlab/launcher';
import { PageConfig } from '@jupyterlab/coreutils';
import { ICommandPalette, IFrame, MainAreaWidget, WidgetTracker } from '@jupyterlab/apputils';
import { IMainMenu } from '@jupyterlab/mainmenu';
import '../style/index.css';
function newServerProxyWidget(id: string, url: string, text: string): MainAreaWidget<IFrame> {
const content = new IFrame({
sandbox: ['allow-same-origin', 'allow-scripts', 'allow-popups', 'allow-forms'],
});
content.title.label = text;
content.title.closable = true;
content.url = url;
content.addClass('jp-ServerProxy');
content.id = id;
const widget = new MainAreaWidget({ content });
widget.addClass('jp-ServerProxy');
return widget;
}
async function activate(
app: JupyterFrontEnd,
launcher?: ILauncher,
restorer?: ILayoutRestorer,
palette?: ICommandPalette,
mainMenu?: IMainMenu
) : Promise<void> {
const response = await fetch(PageConfig.getBaseUrl() + 'server-proxy/servers-info');
if (!response.ok) {
console.log('Could not fetch metadata about registered servers. Make sure jupyter-server-proxy is installed.');
console.log(response);
return;
}
const { commands, shell } = app;
const data = await response.json();
const namespace = 'server-proxy';
const tracker = new WidgetTracker<MainAreaWidget<IFrame>>({
namespace
});
const command = namespace + ':' + 'open';
if (restorer) {
void restorer.restore(tracker, {
command: command,
args: widget => ({
url: widget.content.url,
title: widget.content.title.label,
newBrowserTab: false,
id: widget.content.id
}),
name: widget => widget.content.id
});
}
commands.addCommand(command, {
label: args => args['title'] as string,
execute: args => {
const id = args['id'] as string;
const title = args['title'] as string;
const url = args['url'] as string;
const newBrowserTab = args['newBrowserTab'] as boolean;
if (newBrowserTab) {
window.open(url, '_blank');
return;
}
let widget = tracker.find((widget) => { return widget.content.id == id; });
if(!widget){
widget = newServerProxyWidget(id, url, title);
}
if (!tracker.has(widget)) {
void tracker.add(widget);
}
if (!widget.isAttached) {
shell.add(widget);
return widget;
} else {
shell.activateById(widget.id);
}
}
});
const menuItems: Menu.IItemOptions[] = [];
for (let server_process of data.server_processes) {
for (let launcher_entry of server_process.launcher_entries) {
if (!launcher_entry.enabled) {
continue;
}
const url = PageConfig.getBaseUrl() + server_process.name + launcher_entry.path;
const newBrowserTab = launcher_entry.new_browser_tab;
const title = launcher_entry.title + (newBrowserTab ? ' [↗]': '');
const id = namespace + ':' + server_process.name + ':' + launcher_entry.name;
const launcher_item : ILauncher.IItemOptions = {
command: command,
args: { url, title, newBrowserTab, id },
category: 'Notebook'
};
if (launcher_entry.icon_url) {
launcher_item.kernelIconUrl = launcher_entry.icon_url;
}
if (launcher) {
launcher.add(launcher_item);
}
if (palette) {
palette.addItem({
command,
args: {
...launcher_item.args,
title: `Launch ${title}`
},
category: 'Server Proxies'
});
}
if (mainMenu) {
menuItems.push({
command,
args: launcher_item.args
});
}
}
}
if (mainMenu && menuItems) {
mainMenu.fileMenu.newMenu.addGroup(menuItems);
}
}
/**
* Initialization data for the jupyterlab-server-proxy extension.
*/
const extension: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab-server-proxy',
autoStart: true,
optional: [ILauncher, ILayoutRestorer, ICommandPalette, IMainMenu],
activate
};
export default extension;