-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.ts
215 lines (175 loc) · 5.63 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {
ILayoutRestorer, JupyterFrontEnd, JupyterFrontEndPlugin,
} from "@jupyterlab/application";
import {
Dialog, ICommandPalette, showDialog,
} from "@jupyterlab/apputils";
import {
PageConfig,
} from "@jupyterlab/coreutils";
import {
IDocumentManager,
} from "@jupyterlab/docmanager";
import {
Widget,
} from "@phosphor/widgets";
import {
IRequestResult, request,
} from "requests-helper";
import "../style/index.css";
// tslint:disable: variable-name
let unique = 0;
const extension: JupyterFrontEndPlugin<void> = {
activate,
autoStart: true,
id: "jupyterlab_iframe",
requires: [IDocumentManager, ICommandPalette, ILayoutRestorer],
};
class IFrameWidget extends Widget {
constructor(path: string) {
super();
this.id = path + "-" + unique;
unique += 1;
this.title.label = path;
this.title.closable = true;
if (!path.startsWith("http")) {
// use https, its 2019
path = "https://" + path;
}
const div = document.createElement("div");
div.classList.add("iframe-widget");
const iframe = document.createElement("iframe");
// TODO proxy path if necessary
request("get", path).then((res: IRequestResult) => {
if (res.ok && res.headers.indexOf("Access-Control-Allow-Origin") < 0) {
// tslint:disable-next-line: no-console
console.log("site accesible: proceeding");
iframe.src = path;
} else {
// tslint:disable-next-line: no-console
console.log("site failed with code " + res.status.toString());
// tslint:disable-next-line: no-empty
if (res.status === 404) {
} else if (res.status === 401) {
// tslint:disable-next-line: no-empty
} else {
// tslint:disable-next-line: no-console
path = "iframes/proxy?path=" + encodeURI(path);
iframe.src = PageConfig.getBaseUrl() + path;
// tslint:disable-next-line: no-console
console.log("setting proxy for " + path);
}
}
});
div.appendChild(iframe);
this.node.appendChild(div);
}
}
// tslint:disable-next-line: max-classes-per-file
class OpenIFrameWidget extends Widget {
constructor() {
const body = document.createElement("div");
const existingLabel = document.createElement("label");
existingLabel.textContent = "Site:";
const input = document.createElement("input");
input.value = "";
input.placeholder = "http://path.to.site";
body.appendChild(existingLabel);
body.appendChild(input);
super({ node: body });
}
public getValue(): string {
return this.inputNode.value;
}
get inputNode(): HTMLInputElement {
return this.node.getElementsByTagName("input")[0] as HTMLInputElement;
}
}
function registerSite(app: JupyterFrontEnd, palette: ICommandPalette, site: string) {
const command = "iframe:open-" + site;
app.commands.addCommand(command, {
execute: () => {
const widget = new IFrameWidget(site);
app.shell.add(widget);
app.shell.activateById(widget.id);
},
isEnabled: () => true,
label: "Open " + site,
});
palette.addItem({command, category: "Sites"});
}
// tslint:disable-next-line: max-line-length
function activate(app: JupyterFrontEnd, docManager: IDocumentManager, palette: ICommandPalette, restorer: ILayoutRestorer) {
// Declare a widget variable
let widget: IFrameWidget;
// Add an application command
const open_command = "iframe:open";
app.commands.addCommand(open_command, {
execute: (args) => {
let path = typeof args.path === "undefined" ? "" : args.path as string;
if (path === "") {
showDialog({
body: new OpenIFrameWidget(),
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: "GO" })],
focusNodeSelector: "input",
title: "Open site",
}).then((result) => {
if (result.button.label === "CANCEL") {
return;
}
if (!result.value) {
return null;
}
path = result.value as string;
widget = new IFrameWidget(path);
app.shell.add(widget);
app.shell.activateById(widget.id);
});
} else {
widget = new IFrameWidget(path);
app.shell.add(widget);
app.shell.activateById(widget.id);
}
},
isEnabled: () => true,
label: "Open IFrame",
});
// Add the command to the palette.
palette.addItem({command: open_command, category: "Sites"});
// grab sites from serverextension
request("get", PageConfig.getBaseUrl() + "iframes/").then((res: IRequestResult) => {
if (res.ok) {
const jsn = res.json() as {[key: string]: string};
const welcome = jsn.welcome;
let welcome_included = false;
const sites = jsn.sites;
for (const site of sites) {
// tslint:disable-next-line: no-console
console.log("adding quicklink for " + site);
if (site === welcome) {
welcome_included = true;
}
if (site) {
registerSite(app, palette, site);
}
}
if (!welcome_included) {
if (welcome !== "") {
registerSite(app, palette, welcome);
}
}
if (welcome) {
app.restored.then(() => {
if (!localStorage.getItem("jupyterlab_iframe_welcome")) {
localStorage.setItem("jupyterlab_iframe_welcome", "false");
app.commands.execute("iframe:open-" + welcome);
}
});
}
}
});
// tslint:disable-next-line: no-console
console.log("JupyterLab extension jupyterlab_iframe is activated!");
}
export default extension;
export {activate as _activate};