-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
frontend.ts
266 lines (233 loc) · 7.12 KB
/
frontend.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { CommandLinker } from '@jupyterlab/apputils';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { ServiceManager } from '@jupyterlab/services';
import { IIterator } from '@phosphor/algorithm';
import { Application, IPlugin } from '@phosphor/application';
import { Token } from '@phosphor/coreutils';
import { Widget } from '@phosphor/widgets';
/**
* The type for all JupyterFrontEnd application plugins.
*
* #### Notes
* The generic `T` argument indicates the type that the plugin `provides` upon
* being activated.
*/
export type JupyterFrontEndPlugin<T> = IPlugin<JupyterFrontEnd, T>;
/**
* The base Jupyter front-end application class.
*
* #### Notes
* This type is useful as a generic application against which front-end plugins
* can be authored. It inherits from the phosphor `Application`.
*
* The generic type argument semantics are as follows.
*
* `T extends JupyterFrontEnd.Shell = JupyterFrontEnd.Shell` - the type of the
* `shell` attribute of a `JupyterFrontEnd`.
*/
export abstract class JupyterFrontEnd<
T extends JupyterFrontEnd.IShell = JupyterFrontEnd.IShell
> extends Application<T> {
/**
* Construct a new JupyterFrontEnd object.
*/
constructor(options: JupyterFrontEnd.IOptions<T>) {
super(options);
// The default restored promise if one does not exist in the options.
const restored = new Promise(resolve => {
requestAnimationFrame(() => {
resolve();
});
});
this.commandLinker =
options.commandLinker || new CommandLinker({ commands: this.commands });
this.docRegistry = options.docRegistry || new DocumentRegistry();
this.restored =
options.restored ||
this.started.then(() => restored).catch(() => restored);
this.serviceManager = options.serviceManager || new ServiceManager();
}
/**
* The name of this Jupyter front-end application.
*/
abstract readonly name: string;
/**
* A namespace/prefix plugins may use to denote their provenance.
*/
abstract readonly namespace: string;
/**
* The version of this Jupyter front-end application.
*/
abstract readonly version: string;
/**
* The command linker used by the application.
*/
readonly commandLinker: CommandLinker;
/**
* The document registry instance used by the application.
*/
readonly docRegistry: DocumentRegistry;
/**
* Promise that resolves when state is first restored.
*/
readonly restored: Promise<void>;
/**
* The service manager used by the application.
*/
readonly serviceManager: ServiceManager;
/**
* Walks up the DOM hierarchy of the target of the active `contextmenu`
* event, testing the nodes for a user-supplied funcion. This can
* be used to find a node on which to operate, given a context menu click.
*
* @param test - a function that takes an `HTMLElement` and returns a
* boolean for whether it is the element the requester is seeking.
*
* @returns an HTMLElement or undefined, if none is found.
*/
contextMenuHitTest(
test: (node: HTMLElement) => boolean
): HTMLElement | undefined {
if (
!this._contextMenuEvent ||
!(this._contextMenuEvent.target instanceof HTMLElement)
) {
return undefined;
}
let node = this._contextMenuEvent.target as HTMLElement;
do {
if (test(node)) {
return node;
}
node = node.parentNode as HTMLElement;
} while (node.parentNode && node !== node.parentNode);
return undefined;
// TODO: we should be able to use .composedPath() to simplify this function
// down to something like the below, but it seems like composedPath is
// sometimes returning an empty list.
/*
if (this._contextMenuEvent) {
this._contextMenuEvent
.composedPath()
.filter(x => x instanceof HTMLElement)
.find(test);
}
return undefined;
*/
}
/**
* A method invoked on a document `'contextmenu'` event.
*/
protected evtContextMenu(event: MouseEvent): void {
this._contextMenuEvent = event;
super.evtContextMenu(event);
}
private _contextMenuEvent: MouseEvent;
}
/**
* The namespace for `JupyterFrontEnd` class statics.
*/
export namespace JupyterFrontEnd {
/**
* The options used to initialize a JupyterFrontEnd.
*/
export interface IOptions<T extends IShell = IShell, U = any>
extends Application.IOptions<T> {
/**
* The document registry instance used by the application.
*/
docRegistry?: DocumentRegistry;
/**
* The command linker used by the application.
*/
commandLinker?: CommandLinker;
/**
* The service manager used by the application.
*/
serviceManager?: ServiceManager;
/**
* Promise that resolves when state is first restored, returning layout
* description.
*/
restored?: Promise<U>;
}
/**
* A minimal shell type for Jupyter front-end applications.
*/
export interface IShell extends Widget {
/**
* Activates a widget inside the application shell.
*
* @param id - The ID of the widget being activated.
*/
activateById(id: string): void;
/**
* Add a widget to the application shell.
*
* @param widget - The widget being added.
*
* @param area - Optional region in the shell into which the widget should
* be added.
*
* @param options - Optional flags the shell might use when opening the
* widget, as defined in the `DocumentRegistry`.
*/
add(
widget: Widget,
area?: string,
options?: DocumentRegistry.IOpenOptions
): void;
/**
* The focused widget in the application shell.
*
* #### Notes
* Different shell implementations have latitude to decide what "current"
* or "focused" mean, depending on their user interface characteristics.
*/
readonly currentWidget: Widget;
/**
* Returns an iterator for the widgets inside the application shell.
*
* @param area - Optional regions in the shell whose widgets are iterated.
*/
widgets(area?: string): IIterator<Widget>;
}
/**
* The application paths dictionary token.
*/
export const IPaths = new Token<IPaths>('@jupyterlab/application:IPaths');
/**
* An interface for URL and directory paths used by a Jupyter front-end.
*/
export interface IPaths {
/**
* The urls used by the application.
*/
readonly urls: {
readonly base: string;
readonly defaultWorkspace: string;
readonly notFound?: string;
readonly page: string;
readonly public: string;
readonly settings: string;
readonly themes: string;
readonly tree: string;
readonly workspaces: string;
};
/**
* The local directories used by the application.
*/
readonly directories: {
readonly appSettings: string;
readonly schemas: string;
readonly static: string;
readonly templates: string;
readonly themes: string;
readonly userSettings: string;
readonly serverRoot: string;
readonly workspaces: string;
};
}
}