forked from jupyterlab/jupyterlab
/
index.tsx
500 lines (454 loc) · 12.6 KB
/
index.tsx
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
showErrorMessage,
VDomModel,
VDomRenderer
} from '@jupyterlab/apputils';
import {
combineClasses,
DefaultIconReact,
defaultIconRegistry
} from '@jupyterlab/ui-components';
import {
ArrayExt,
ArrayIterator,
IIterator,
map,
each,
toArray
} from '@phosphor/algorithm';
import { CommandRegistry } from '@phosphor/commands';
import { Token, ReadonlyJSONObject } from '@phosphor/coreutils';
import { DisposableDelegate, IDisposable } from '@phosphor/disposable';
import { AttachedProperty } from '@phosphor/properties';
import { Widget } from '@phosphor/widgets';
import * as React from 'react';
/**
* The class name added to Launcher instances.
*/
const LAUNCHER_CLASS = 'jp-Launcher';
/**
* The known categories of launcher items and their default ordering.
*/
const KNOWN_CATEGORIES = ['Notebook', 'Console', 'Other'];
/**
* These launcher item categories are known to have kernels, so the kernel icons
* are used.
*/
const KERNEL_CATEGORIES = ['Notebook', 'Console'];
/* tslint:disable */
/**
* The launcher token.
*/
export const ILauncher = new Token<ILauncher>('@jupyterlab/launcher:ILauncher');
/* tslint:enable */
/**
* The launcher interface.
*/
export interface ILauncher {
/**
* Add a command item to the launcher, and trigger re-render event for parent
* widget.
*
* @param options - The specification options for a launcher item.
*
* @returns A disposable that will remove the item from Launcher, and trigger
* re-render event for parent widget.
*
*/
add(options: ILauncher.IItemOptions): IDisposable;
}
/**
* LauncherModel keeps track of the path to working directory and has a list of
* LauncherItems, which the Launcher will render.
*/
export class LauncherModel extends VDomModel implements ILauncher {
/**
* Add a command item to the launcher, and trigger re-render event for parent
* widget.
*
* @param options - The specification options for a launcher item.
*
* @returns A disposable that will remove the item from Launcher, and trigger
* re-render event for parent widget.
*
*/
add(options: ILauncher.IItemOptions): IDisposable {
// Create a copy of the options to circumvent mutations to the original.
let item = Private.createItem(options);
this._items.push(item);
this.stateChanged.emit(void 0);
return new DisposableDelegate(() => {
ArrayExt.removeFirstOf(this._items, item);
this.stateChanged.emit(void 0);
});
}
/**
* Return an iterator of launcher items.
*/
items(): IIterator<ILauncher.IItemOptions> {
return new ArrayIterator(this._items);
}
private _items: ILauncher.IItemOptions[] = [];
}
/**
* A virtual-DOM-based widget for the Launcher.
*/
export class Launcher extends VDomRenderer<LauncherModel> {
/**
* Construct a new launcher widget.
*/
constructor(options: ILauncher.IOptions) {
super();
this._cwd = options.cwd;
this._callback = options.callback;
this._commands = options.commands;
this.addClass(LAUNCHER_CLASS);
}
/**
* The cwd of the launcher.
*/
get cwd(): string {
return this._cwd;
}
set cwd(value: string) {
this._cwd = value;
this.update();
}
/**
* Whether there is a pending item being launched.
*/
get pending(): boolean {
return this._pending;
}
set pending(value: boolean) {
this._pending = value;
}
/**
* Render the launcher to virtual DOM nodes.
*/
protected render(): React.ReactElement<any> {
// Bail if there is no model.
if (!this.model) {
return null;
}
// First group-by categories
let categories = Object.create(null);
each(this.model.items(), (item, index) => {
let cat = item.category || 'Other';
if (!(cat in categories)) {
categories[cat] = [];
}
categories[cat].push(item);
});
// Within each category sort by rank
for (let cat in categories) {
categories[cat] = categories[cat].sort(
(a: ILauncher.IItemOptions, b: ILauncher.IItemOptions) => {
return Private.sortCmp(a, b, this._cwd, this._commands);
}
);
}
// Variable to help create sections
let sections: React.ReactElement<any>[] = [];
let section: React.ReactElement<any>;
// Assemble the final ordered list of categories, beginning with
// KNOWN_CATEGORIES.
let orderedCategories: string[] = [];
each(KNOWN_CATEGORIES, (cat, index) => {
orderedCategories.push(cat);
});
for (let cat in categories) {
if (KNOWN_CATEGORIES.indexOf(cat) === -1) {
orderedCategories.push(cat);
}
}
// Now create the sections for each category
orderedCategories.forEach(cat => {
const item = categories[cat][0] as ILauncher.IItemOptions;
let iconClass = this._commands.iconClass(item.command, {
...item.args,
cwd: this.cwd
});
let kernel = KERNEL_CATEGORIES.indexOf(cat) > -1;
if (cat in categories) {
section = (
<div className="jp-Launcher-section" key={cat}>
<div className="jp-Launcher-sectionHeader">
{kernel && defaultIconRegistry.contains(iconClass) ? (
<DefaultIconReact
name={iconClass}
className={''}
center={true}
kind={'launcherSection'}
/>
) : (
<div
className={combineClasses(
iconClass,
'jp-Launcher-sectionIcon',
'jp-Launcher-icon'
)}
/>
)}
<h2 className="jp-Launcher-sectionTitle">{cat}</h2>
</div>
<div className="jp-Launcher-cardContainer">
{toArray(
map(categories[cat], (item: ILauncher.IItemOptions) => {
return Card(
kernel,
item,
this,
this._commands,
this._callback
);
})
)}
</div>
</div>
);
sections.push(section);
}
});
// Wrap the sections in body and content divs.
return (
<div className="jp-Launcher-body">
<div className="jp-Launcher-content">
<div className="jp-Launcher-cwd">
<h3>{this.cwd}</h3>
</div>
{sections}
</div>
</div>
);
}
private _commands: CommandRegistry;
private _callback: (widget: Widget) => void;
private _pending = false;
private _cwd = '';
}
/**
* The namespace for `ILauncher` class statics.
*/
export namespace ILauncher {
/**
* The options used to create a Launcher.
*/
export interface IOptions {
/**
* The cwd of the launcher.
*/
cwd: string;
/**
* The command registry used by the launcher.
*/
commands: CommandRegistry;
/**
* The callback used when an item is launched.
*/
callback: (widget: Widget) => void;
}
/**
* The options used to create a launcher item.
*/
export interface IItemOptions {
/**
* The command ID for the launcher item.
*
* #### Notes
* If the command's `execute` method returns a `Widget` or
* a promise that resolves with a `Widget`, then that widget will
* replace the launcher in the same location of the application
* shell. If the `execute` method does something else
* (i.e., create a modal dialog), then the launcher will not be
* disposed.
*/
command: string;
/**
* The arguments given to the command for
* creating the launcher item.
*
* ### Notes
* The launcher will also add the current working
* directory of the filebrowser in the `cwd` field
* of the args, which a command may use to create
* the activity with respect to the right directory.
*/
args?: ReadonlyJSONObject;
/**
* The category for the launcher item.
*
* The default value is the an empty string.
*/
category?: string;
/**
* The rank for the launcher item.
*
* The rank is used when ordering launcher items for display. After grouping
* into categories, items are sorted in the following order:
* 1. Rank (lower is better)
* 3. Display Name (locale order)
*
* The default rank is `Infinity`.
*/
rank?: number;
/**
* For items that have a kernel associated with them, the URL of the kernel
* icon.
*
* This is not a CSS class, but the URL that points to the icon in the kernel
* spec.
*/
kernelIconUrl?: string;
}
}
/**
* A pure tsx component for a launcher card.
*
* @param kernel - whether the item takes uses a kernel.
*
* @param item - the launcher item to render.
*
* @param launcher - the Launcher instance to which this is added.
*
* @param launcherCallback - a callback to call after an item has been launched.
*
* @returns a vdom `VirtualElement` for the launcher card.
*/
function Card(
kernel: boolean,
item: ILauncher.IItemOptions,
launcher: Launcher,
commands: CommandRegistry,
launcherCallback: (widget: Widget) => void
): React.ReactElement<any> {
// Get some properties of the command
const command = item.command;
const args = { ...item.args, cwd: launcher.cwd };
const caption = commands.caption(command, args);
const label = commands.label(command, args);
const title = kernel ? label : caption || label;
// Build the onclick handler.
let onclick = () => {
// If an item has already been launched,
// don't try to launch another.
if (launcher.pending === true) {
return;
}
launcher.pending = true;
void commands
.execute(command, {
...item.args,
cwd: launcher.cwd
})
.then(value => {
launcher.pending = false;
if (value instanceof Widget) {
launcherCallback(value);
launcher.dispose();
}
})
.catch(err => {
launcher.pending = false;
void showErrorMessage('Launcher Error', err);
});
};
// With tabindex working, you can now pick a kernel by tabbing around and
// pressing Enter.
let onkeypress = (event: React.KeyboardEvent) => {
if (event.key === 'Enter') {
onclick();
}
};
// Return the VDOM element.
const iconClass = kernel ? '' : commands.iconClass(command, args);
return (
<div
className="jp-LauncherCard"
title={title}
onClick={onclick}
onKeyPress={onkeypress}
tabIndex={100}
data-category={item.category || 'Other'}
key={Private.keyProperty.get(item)}
>
{kernel ? (
<div className="jp-LauncherCard-icon">
{item.kernelIconUrl ? (
<img src={item.kernelIconUrl} className="jp-Launcher-kernelIcon" />
) : (
<div className="jp-LauncherCard-noKernelIcon">
{label[0].toUpperCase()}
</div>
)}
</div>
) : (
<div className="jp-LauncherCard-icon">
<DefaultIconReact
name={`${iconClass} jp-Launcher-icon`}
className={''}
fallback={true}
center={true}
kind={'launcherCard'}
/>
</div>
)}
<div className="jp-LauncherCard-label" title={title}>
<p>{label}</p>
</div>
</div>
);
}
/**
* The namespace for module private data.
*/
namespace Private {
/**
* An incrementing counter for keys.
*/
let id = 0;
/**
* An attached property for an item's key.
*/
export const keyProperty = new AttachedProperty<
ILauncher.IItemOptions,
number
>({
name: 'key',
create: () => id++
});
/**
* Create a fully specified item given item options.
*/
export function createItem(
options: ILauncher.IItemOptions
): ILauncher.IItemOptions {
return {
...options,
category: options.category || '',
rank: options.rank !== undefined ? options.rank : Infinity
};
}
/**
* A sort comparison function for a launcher item.
*/
export function sortCmp(
a: ILauncher.IItemOptions,
b: ILauncher.IItemOptions,
cwd: string,
commands: CommandRegistry
): number {
// First, compare by rank.
let r1 = a.rank;
let r2 = b.rank;
if (r1 !== r2 && r1 !== undefined && r2 !== undefined) {
return r1 < r2 ? -1 : 1; // Infinity safe
}
// Finally, compare by display name.
const aLabel = commands.label(a.command, { ...a.args, cwd });
const bLabel = commands.label(b.command, { ...b.args, cwd });
return aLabel.localeCompare(bLabel);
}
}