@jupyterlab/apputils 3.3.0-alpha.15 → 3.3.0-alpha.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/lib/collapse.d.ts +64 -0
  2. package/lib/collapse.js +145 -0
  3. package/lib/collapse.js.map +1 -0
  4. package/lib/commandpalette.d.ts +32 -0
  5. package/lib/commandpalette.js +7 -1
  6. package/lib/commandpalette.js.map +1 -1
  7. package/lib/dialog.d.ts +8 -10
  8. package/lib/dialog.js +20 -2
  9. package/lib/dialog.js.map +1 -1
  10. package/lib/iframe.d.ts +77 -0
  11. package/lib/iframe.js +89 -0
  12. package/lib/iframe.js.map +1 -0
  13. package/lib/index.d.ts +7 -27
  14. package/lib/index.js +7 -27
  15. package/lib/index.js.map +1 -1
  16. package/lib/inputdialog.js +1 -1
  17. package/lib/inputdialog.js.map +1 -1
  18. package/lib/mainareawidget.d.ts +2 -2
  19. package/lib/mainareawidget.js +2 -2
  20. package/lib/mainareawidget.js.map +1 -1
  21. package/lib/menufactory.js +3 -3
  22. package/lib/menufactory.js.map +1 -1
  23. package/lib/printing.js +1 -1
  24. package/lib/printing.js.map +1 -1
  25. package/lib/sanitizer.js +0 -1
  26. package/lib/sanitizer.js.map +1 -1
  27. package/lib/sessioncontext.d.ts +0 -4
  28. package/lib/sessioncontext.js +7 -37
  29. package/lib/sessioncontext.js.map +1 -1
  30. package/lib/spinner.d.ts +15 -0
  31. package/lib/spinner.js +28 -0
  32. package/lib/spinner.js.map +1 -0
  33. package/lib/splash.d.ts +19 -0
  34. package/lib/splash.js +9 -0
  35. package/lib/splash.js.map +1 -0
  36. package/lib/styling.d.ts +27 -0
  37. package/lib/styling.js +100 -0
  38. package/lib/styling.js.map +1 -0
  39. package/lib/thememanager.d.ts +2 -1
  40. package/lib/thememanager.js.map +1 -1
  41. package/lib/tokens.d.ts +0 -131
  42. package/lib/tokens.js +0 -19
  43. package/lib/tokens.js.map +1 -1
  44. package/lib/toolbar.d.ts +428 -0
  45. package/lib/toolbar.js +1016 -0
  46. package/lib/toolbar.js.map +1 -0
  47. package/lib/vdom.d.ts +189 -0
  48. package/lib/vdom.js +203 -0
  49. package/lib/vdom.js.map +1 -0
  50. package/lib/windowresolver.d.ts +14 -1
  51. package/lib/windowresolver.js +6 -1
  52. package/lib/windowresolver.js.map +1 -1
  53. package/package.json +25 -20
  54. package/style/base.css +5 -0
  55. package/style/collapse.css +30 -0
  56. package/style/dialog.css +3 -3
  57. package/style/iframe.css +33 -0
  58. package/style/mainareawidget.css +0 -16
  59. package/style/spinner.css +79 -0
  60. package/style/styling.css +101 -0
  61. package/style/toolbar.css +115 -0
  62. package/lib/kernelstatuses.d.ts +0 -10
  63. package/lib/kernelstatuses.js +0 -29
  64. package/lib/kernelstatuses.js.map +0 -1
  65. package/lib/toolbar/factory.d.ts +0 -17
  66. package/lib/toolbar/factory.js +0 -218
  67. package/lib/toolbar/factory.js.map +0 -1
  68. package/lib/toolbar/index.d.ts +0 -3
  69. package/lib/toolbar/index.js +0 -4
  70. package/lib/toolbar/index.js.map +0 -1
  71. package/lib/toolbar/registry.d.ts +0 -41
  72. package/lib/toolbar/registry.js +0 -87
  73. package/lib/toolbar/registry.js.map +0 -1
  74. package/lib/toolbar/widget.d.ts +0 -39
  75. package/lib/toolbar/widget.js +0 -158
  76. package/lib/toolbar/widget.js.map +0 -1
package/lib/toolbar.js ADDED
@@ -0,0 +1,1016 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+ import { nullTranslator } from '@jupyterlab/translation';
4
+ import { Button, circleEmptyIcon, circleIcon, classes, ellipsesIcon, LabIcon, offlineBoltIcon, refreshIcon, stopIcon } from '@jupyterlab/ui-components';
5
+ import { find, map, some } from '@lumino/algorithm';
6
+ import { CommandRegistry } from '@lumino/commands';
7
+ import { MessageLoop } from '@lumino/messaging';
8
+ import { AttachedProperty } from '@lumino/properties';
9
+ import { PanelLayout, Widget } from '@lumino/widgets';
10
+ import * as React from 'react';
11
+ import { sessionContextDialogs } from './sessioncontext';
12
+ import { ReactWidget, UseSignal } from './vdom';
13
+ import { Throttler } from '@lumino/polling';
14
+ /**
15
+ * The class name added to toolbars.
16
+ */
17
+ const TOOLBAR_CLASS = 'jp-Toolbar';
18
+ /**
19
+ * Toolbar pop-up opener button name
20
+ */
21
+ const TOOLBAR_OPENER_NAME = 'toolbar-popup-opener';
22
+ /**
23
+ * The class name added to toolbar items.
24
+ */
25
+ const TOOLBAR_ITEM_CLASS = 'jp-Toolbar-item';
26
+ /**
27
+ * The class name added to toolbar kernel name text.
28
+ */
29
+ const TOOLBAR_KERNEL_NAME_CLASS = 'jp-Toolbar-kernelName';
30
+ /**
31
+ * The class name added to toolbar spacer.
32
+ */
33
+ const TOOLBAR_SPACER_CLASS = 'jp-Toolbar-spacer';
34
+ /**
35
+ * The class name added to toolbar kernel status icon.
36
+ */
37
+ const TOOLBAR_KERNEL_STATUS_CLASS = 'jp-Toolbar-kernelStatus';
38
+ /**
39
+ * A layout for toolbars.
40
+ *
41
+ * #### Notes
42
+ * This layout automatically collapses its height if there are no visible
43
+ * toolbar widgets, and expands to the standard toolbar height if there are
44
+ * visible toolbar widgets.
45
+ */
46
+ class ToolbarLayout extends PanelLayout {
47
+ constructor() {
48
+ super(...arguments);
49
+ this._dirty = false;
50
+ }
51
+ /**
52
+ * A message handler invoked on a `'fit-request'` message.
53
+ *
54
+ * If any child widget is visible, expand the toolbar height to the normal
55
+ * toolbar height.
56
+ */
57
+ onFitRequest(msg) {
58
+ super.onFitRequest(msg);
59
+ if (this.parent.isAttached) {
60
+ // If there are any widgets not explicitly hidden, expand the toolbar to
61
+ // accommodate them.
62
+ if (some(this.widgets, w => !w.isHidden)) {
63
+ this.parent.node.style.minHeight = 'var(--jp-private-toolbar-height)';
64
+ this.parent.removeClass('jp-Toolbar-micro');
65
+ }
66
+ else {
67
+ this.parent.node.style.minHeight = '';
68
+ this.parent.addClass('jp-Toolbar-micro');
69
+ }
70
+ }
71
+ // Set the dirty flag to ensure only a single update occurs.
72
+ this._dirty = true;
73
+ // Notify the ancestor that it should fit immediately. This may
74
+ // cause a resize of the parent, fulfilling the required update.
75
+ if (this.parent.parent) {
76
+ MessageLoop.sendMessage(this.parent.parent, Widget.Msg.FitRequest);
77
+ }
78
+ // If the dirty flag is still set, the parent was not resized.
79
+ // Trigger the required update on the parent widget immediately.
80
+ if (this._dirty) {
81
+ MessageLoop.sendMessage(this.parent, Widget.Msg.UpdateRequest);
82
+ }
83
+ }
84
+ /**
85
+ * A message handler invoked on an `'update-request'` message.
86
+ */
87
+ onUpdateRequest(msg) {
88
+ super.onUpdateRequest(msg);
89
+ if (this.parent.isVisible) {
90
+ this._dirty = false;
91
+ }
92
+ }
93
+ /**
94
+ * A message handler invoked on a `'child-shown'` message.
95
+ */
96
+ onChildShown(msg) {
97
+ super.onChildShown(msg);
98
+ // Post a fit request for the parent widget.
99
+ this.parent.fit();
100
+ }
101
+ /**
102
+ * A message handler invoked on a `'child-hidden'` message.
103
+ */
104
+ onChildHidden(msg) {
105
+ super.onChildHidden(msg);
106
+ // Post a fit request for the parent widget.
107
+ this.parent.fit();
108
+ }
109
+ /**
110
+ * A message handler invoked on a `'before-attach'` message.
111
+ */
112
+ onBeforeAttach(msg) {
113
+ super.onBeforeAttach(msg);
114
+ // Post a fit request for the parent widget.
115
+ this.parent.fit();
116
+ }
117
+ /**
118
+ * Attach a widget to the parent's DOM node.
119
+ *
120
+ * @param index - The current index of the widget in the layout.
121
+ *
122
+ * @param widget - The widget to attach to the parent.
123
+ *
124
+ * #### Notes
125
+ * This is a reimplementation of the superclass method.
126
+ */
127
+ attachWidget(index, widget) {
128
+ super.attachWidget(index, widget);
129
+ // Post a fit request for the parent widget.
130
+ this.parent.fit();
131
+ }
132
+ /**
133
+ * Detach a widget from the parent's DOM node.
134
+ *
135
+ * @param index - The previous index of the widget in the layout.
136
+ *
137
+ * @param widget - The widget to detach from the parent.
138
+ *
139
+ * #### Notes
140
+ * This is a reimplementation of the superclass method.
141
+ */
142
+ detachWidget(index, widget) {
143
+ super.detachWidget(index, widget);
144
+ // Post a fit request for the parent widget.
145
+ this.parent.fit();
146
+ }
147
+ }
148
+ /**
149
+ * A class which provides a toolbar widget.
150
+ */
151
+ export class Toolbar extends Widget {
152
+ /**
153
+ * Construct a new toolbar widget.
154
+ */
155
+ constructor() {
156
+ super();
157
+ this.addClass(TOOLBAR_CLASS);
158
+ this.layout = new ToolbarLayout();
159
+ }
160
+ /**
161
+ * Get an iterator over the ordered toolbar item names.
162
+ *
163
+ * @returns An iterator over the toolbar item names.
164
+ */
165
+ names() {
166
+ const layout = this.layout;
167
+ return map(layout.widgets, widget => {
168
+ return Private.nameProperty.get(widget);
169
+ });
170
+ }
171
+ /**
172
+ * Add an item to the end of the toolbar.
173
+ *
174
+ * @param name - The name of the widget to add to the toolbar.
175
+ *
176
+ * @param widget - The widget to add to the toolbar.
177
+ *
178
+ * @param index - The optional name of the item to insert after.
179
+ *
180
+ * @returns Whether the item was added to toolbar. Returns false if
181
+ * an item of the same name is already in the toolbar.
182
+ *
183
+ * #### Notes
184
+ * The item can be removed from the toolbar by setting its parent to `null`.
185
+ */
186
+ addItem(name, widget) {
187
+ const layout = this.layout;
188
+ return this.insertItem(layout.widgets.length, name, widget);
189
+ }
190
+ /**
191
+ * Insert an item into the toolbar at the specified index.
192
+ *
193
+ * @param index - The index at which to insert the item.
194
+ *
195
+ * @param name - The name of the item.
196
+ *
197
+ * @param widget - The widget to add.
198
+ *
199
+ * @returns Whether the item was added to the toolbar. Returns false if
200
+ * an item of the same name is already in the toolbar.
201
+ *
202
+ * #### Notes
203
+ * The index will be clamped to the bounds of the items.
204
+ * The item can be removed from the toolbar by setting its parent to `null`.
205
+ */
206
+ insertItem(index, name, widget) {
207
+ const existing = find(this.names(), value => value === name);
208
+ if (existing) {
209
+ return false;
210
+ }
211
+ widget.addClass(TOOLBAR_ITEM_CLASS);
212
+ const layout = this.layout;
213
+ const j = Math.max(0, Math.min(index, layout.widgets.length));
214
+ layout.insertWidget(j, widget);
215
+ Private.nameProperty.set(widget, name);
216
+ return true;
217
+ }
218
+ /**
219
+ * Insert an item into the toolbar at the after a target item.
220
+ *
221
+ * @param at - The target item to insert after.
222
+ *
223
+ * @param name - The name of the item.
224
+ *
225
+ * @param widget - The widget to add.
226
+ *
227
+ * @returns Whether the item was added to the toolbar. Returns false if
228
+ * an item of the same name is already in the toolbar.
229
+ *
230
+ * #### Notes
231
+ * The index will be clamped to the bounds of the items.
232
+ * The item can be removed from the toolbar by setting its parent to `null`.
233
+ */
234
+ insertAfter(at, name, widget) {
235
+ return this._insertRelative(at, 1, name, widget);
236
+ }
237
+ /**
238
+ * Insert an item into the toolbar at the before a target item.
239
+ *
240
+ * @param at - The target item to insert before.
241
+ *
242
+ * @param name - The name of the item.
243
+ *
244
+ * @param widget - The widget to add.
245
+ *
246
+ * @returns Whether the item was added to the toolbar. Returns false if
247
+ * an item of the same name is already in the toolbar.
248
+ *
249
+ * #### Notes
250
+ * The index will be clamped to the bounds of the items.
251
+ * The item can be removed from the toolbar by setting its parent to `null`.
252
+ */
253
+ insertBefore(at, name, widget) {
254
+ return this._insertRelative(at, 0, name, widget);
255
+ }
256
+ _insertRelative(at, offset, name, widget) {
257
+ const nameWithIndex = map(this.names(), (name, i) => {
258
+ return { name: name, index: i };
259
+ });
260
+ const target = find(nameWithIndex, x => x.name === at);
261
+ if (target) {
262
+ return this.insertItem(target.index + offset, name, widget);
263
+ }
264
+ return false;
265
+ }
266
+ /**
267
+ * Handle the DOM events for the widget.
268
+ *
269
+ * @param event - The DOM event sent to the widget.
270
+ *
271
+ * #### Notes
272
+ * This method implements the DOM `EventListener` interface and is
273
+ * called in response to events on the dock panel's node. It should
274
+ * not be called directly by user code.
275
+ */
276
+ handleEvent(event) {
277
+ switch (event.type) {
278
+ case 'click':
279
+ this.handleClick(event);
280
+ break;
281
+ default:
282
+ break;
283
+ }
284
+ }
285
+ /**
286
+ * Handle a DOM click event.
287
+ */
288
+ handleClick(event) {
289
+ // Clicking a label focuses the corresponding control
290
+ // that is linked with `for` attribute, so let it be.
291
+ if (event.target instanceof HTMLLabelElement) {
292
+ const forId = event.target.getAttribute('for');
293
+ if (forId && this.node.querySelector(`#${forId}`)) {
294
+ return;
295
+ }
296
+ }
297
+ // If this click already focused a control, let it be.
298
+ if (this.node.contains(document.activeElement)) {
299
+ return;
300
+ }
301
+ // Otherwise, activate the parent widget, which may take focus if desired.
302
+ if (this.parent) {
303
+ this.parent.activate();
304
+ }
305
+ }
306
+ /**
307
+ * Handle `after-attach` messages for the widget.
308
+ */
309
+ onAfterAttach(msg) {
310
+ this.node.addEventListener('click', this);
311
+ }
312
+ /**
313
+ * Handle `before-detach` messages for the widget.
314
+ */
315
+ onBeforeDetach(msg) {
316
+ this.node.removeEventListener('click', this);
317
+ }
318
+ }
319
+ /**
320
+ * A class which provides a toolbar widget.
321
+ */
322
+ export class ReactiveToolbar extends Toolbar {
323
+ /**
324
+ * Construct a new toolbar widget.
325
+ */
326
+ constructor() {
327
+ super();
328
+ this.popupOpener = new ToolbarPopupOpener();
329
+ this._widgetWidths = {};
330
+ this.insertItem(0, TOOLBAR_OPENER_NAME, this.popupOpener);
331
+ this.popupOpener.hide();
332
+ this._resizer = new Throttler(this._onResize.bind(this), 500);
333
+ }
334
+ /**
335
+ * Dispose of the widget and its descendant widgets.
336
+ */
337
+ dispose() {
338
+ if (this.isDisposed) {
339
+ return;
340
+ }
341
+ if (this._resizer) {
342
+ this._resizer.dispose();
343
+ }
344
+ super.dispose();
345
+ }
346
+ /**
347
+ * Insert an item into the toolbar at the after a target item.
348
+ *
349
+ * @param at - The target item to insert after.
350
+ *
351
+ * @param name - The name of the item.
352
+ *
353
+ * @param widget - The widget to add.
354
+ *
355
+ * @returns Whether the item was added to the toolbar. Returns false if
356
+ * an item of the same name is already in the toolbar or if the target
357
+ * is the toolbar pop-up opener.
358
+ *
359
+ * #### Notes
360
+ * The index will be clamped to the bounds of the items.
361
+ * The item can be removed from the toolbar by setting its parent to `null`.
362
+ */
363
+ insertAfter(at, name, widget) {
364
+ if (at === TOOLBAR_OPENER_NAME) {
365
+ return false;
366
+ }
367
+ return super.insertAfter(at, name, widget);
368
+ }
369
+ /**
370
+ * Insert an item into the toolbar at the specified index.
371
+ *
372
+ * @param index - The index at which to insert the item.
373
+ *
374
+ * @param name - The name of the item.
375
+ *
376
+ * @param widget - The widget to add.
377
+ *
378
+ * @returns Whether the item was added to the toolbar. Returns false if
379
+ * an item of the same name is already in the toolbar.
380
+ *
381
+ * #### Notes
382
+ * The index will be clamped to the bounds of the items.
383
+ * The item can be removed from the toolbar by setting its parent to `null`.
384
+ */
385
+ insertItem(index, name, widget) {
386
+ if (widget instanceof ToolbarPopupOpener) {
387
+ return super.insertItem(index, name, widget);
388
+ }
389
+ else {
390
+ const j = Math.max(0, Math.min(index, this.layout.widgets.length - 1));
391
+ return super.insertItem(j, name, widget);
392
+ }
393
+ }
394
+ /**
395
+ * A message handler invoked on a `'before-hide'` message.
396
+ *
397
+ * It will hide the pop-up panel
398
+ */
399
+ onBeforeHide(msg) {
400
+ this.popupOpener.hidePopup();
401
+ super.onBeforeHide(msg);
402
+ }
403
+ onResize(msg) {
404
+ super.onResize(msg);
405
+ if (msg.width > 0 && this._resizer) {
406
+ void this._resizer.invoke();
407
+ }
408
+ }
409
+ _onResize() {
410
+ if (this.parent && this.parent.isAttached) {
411
+ const toolbarWidth = this.node.clientWidth;
412
+ const opener = this.popupOpener;
413
+ const openerWidth = 30;
414
+ const toolbarPadding = 2;
415
+ const layout = this.layout;
416
+ let width = opener.isHidden
417
+ ? toolbarPadding
418
+ : toolbarPadding + openerWidth;
419
+ let index = 0;
420
+ const widgetsToRemove = [];
421
+ const toIndex = layout.widgets.length - 1;
422
+ while (index < toIndex) {
423
+ const widget = layout.widgets[index];
424
+ this._saveWidgetWidth(widget);
425
+ width += this._getWidgetWidth(widget);
426
+ if (widgetsToRemove.length === 0 &&
427
+ opener.isHidden &&
428
+ width + openerWidth > toolbarWidth) {
429
+ width += openerWidth;
430
+ }
431
+ if (width > toolbarWidth) {
432
+ widgetsToRemove.push(widget);
433
+ }
434
+ index++;
435
+ }
436
+ while (widgetsToRemove.length > 0) {
437
+ const widget = widgetsToRemove.pop();
438
+ width -= this._getWidgetWidth(widget);
439
+ opener.addWidget(widget);
440
+ }
441
+ if (opener.widgetCount() > 0) {
442
+ const widgetsToAdd = [];
443
+ let index = 0;
444
+ let widget = opener.widgetAt(index);
445
+ const widgetCount = opener.widgetCount();
446
+ width += this._getWidgetWidth(widget);
447
+ if (widgetCount === 1 && width - openerWidth <= toolbarWidth) {
448
+ width -= openerWidth;
449
+ }
450
+ while (width < toolbarWidth && index < widgetCount) {
451
+ widgetsToAdd.push(widget);
452
+ index++;
453
+ widget = opener.widgetAt(index);
454
+ if (widget) {
455
+ width += this._getWidgetWidth(widget);
456
+ }
457
+ else {
458
+ break;
459
+ }
460
+ }
461
+ while (widgetsToAdd.length > 0) {
462
+ const widget = widgetsToAdd.shift();
463
+ this.addItem(Private.nameProperty.get(widget), widget);
464
+ }
465
+ }
466
+ if (opener.widgetCount() > 0) {
467
+ opener.updatePopup();
468
+ opener.show();
469
+ }
470
+ else {
471
+ opener.hide();
472
+ }
473
+ }
474
+ }
475
+ _saveWidgetWidth(widget) {
476
+ const widgetName = Private.nameProperty.get(widget);
477
+ this._widgetWidths[widgetName] = widget.hasClass(TOOLBAR_SPACER_CLASS)
478
+ ? 2
479
+ : widget.node.clientWidth;
480
+ }
481
+ _getWidgetWidth(widget) {
482
+ const widgetName = Private.nameProperty.get(widget);
483
+ return this._widgetWidths[widgetName];
484
+ }
485
+ }
486
+ /**
487
+ * The namespace for Toolbar class statics.
488
+ */
489
+ (function (Toolbar) {
490
+ /**
491
+ * Create an interrupt toolbar item.
492
+ */
493
+ function createInterruptButton(sessionContext, translator) {
494
+ translator = translator || nullTranslator;
495
+ const trans = translator.load('jupyterlab');
496
+ return new ToolbarButton({
497
+ icon: stopIcon,
498
+ onClick: () => {
499
+ var _a, _b;
500
+ void ((_b = (_a = sessionContext.session) === null || _a === void 0 ? void 0 : _a.kernel) === null || _b === void 0 ? void 0 : _b.interrupt());
501
+ },
502
+ tooltip: trans.__('Interrupt the kernel')
503
+ });
504
+ }
505
+ Toolbar.createInterruptButton = createInterruptButton;
506
+ /**
507
+ * Create a restart toolbar item.
508
+ */
509
+ function createRestartButton(sessionContext, dialogs, translator) {
510
+ translator = translator || nullTranslator;
511
+ const trans = translator.load('jupyterlab');
512
+ return new ToolbarButton({
513
+ icon: refreshIcon,
514
+ onClick: () => {
515
+ void (dialogs !== null && dialogs !== void 0 ? dialogs : sessionContextDialogs).restart(sessionContext, translator);
516
+ },
517
+ tooltip: trans.__('Restart the kernel')
518
+ });
519
+ }
520
+ Toolbar.createRestartButton = createRestartButton;
521
+ /**
522
+ * Create a toolbar spacer item.
523
+ *
524
+ * #### Notes
525
+ * It is a flex spacer that separates the left toolbar items
526
+ * from the right toolbar items.
527
+ */
528
+ function createSpacerItem() {
529
+ return new Private.Spacer();
530
+ }
531
+ Toolbar.createSpacerItem = createSpacerItem;
532
+ /**
533
+ * Create a kernel name indicator item.
534
+ *
535
+ * #### Notes
536
+ * It will display the `'display_name`' of the session context. It can
537
+ * handle a change in context or kernel.
538
+ */
539
+ function createKernelNameItem(sessionContext, dialogs, translator) {
540
+ const el = ReactWidget.create(React.createElement(Private.KernelNameComponent, { sessionContext: sessionContext, dialogs: dialogs !== null && dialogs !== void 0 ? dialogs : sessionContextDialogs, translator: translator }));
541
+ el.addClass('jp-KernelName');
542
+ return el;
543
+ }
544
+ Toolbar.createKernelNameItem = createKernelNameItem;
545
+ /**
546
+ * Create a kernel status indicator item.
547
+ *
548
+ * #### Notes
549
+ * It will show a busy status if the kernel status is busy.
550
+ * It will show the current status in the node title.
551
+ * It can handle a change to the context or the kernel.
552
+ */
553
+ function createKernelStatusItem(sessionContext, translator) {
554
+ return new Private.KernelStatus(sessionContext, translator);
555
+ }
556
+ Toolbar.createKernelStatusItem = createKernelStatusItem;
557
+ })(Toolbar || (Toolbar = {}));
558
+ /**
559
+ * React component for a toolbar button.
560
+ *
561
+ * @param props - The props for ToolbarButtonComponent.
562
+ */
563
+ export function ToolbarButtonComponent(props) {
564
+ var _a, _b;
565
+ // In some browsers, a button click event moves the focus from the main
566
+ // content to the button (see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus).
567
+ // We avoid a click event by calling preventDefault in mousedown, and
568
+ // we bind the button action to `mousedown`.
569
+ const handleMouseDown = (event) => {
570
+ var _a;
571
+ // Fire action only when left button is pressed.
572
+ if (event.button === 0) {
573
+ event.preventDefault();
574
+ (_a = props.onClick) === null || _a === void 0 ? void 0 : _a.call(props);
575
+ }
576
+ };
577
+ const handleKeyDown = (event) => {
578
+ var _a;
579
+ const { key } = event;
580
+ if (key === 'Enter' || key === ' ') {
581
+ (_a = props.onClick) === null || _a === void 0 ? void 0 : _a.call(props);
582
+ }
583
+ };
584
+ const handleClick = (event) => {
585
+ var _a;
586
+ if (event.button === 0) {
587
+ (_a = props.onClick) === null || _a === void 0 ? void 0 : _a.call(props);
588
+ }
589
+ };
590
+ const getTooltip = () => {
591
+ if (props.enabled === false && props.disabledTooltip) {
592
+ return props.disabledTooltip;
593
+ }
594
+ else if (props.pressed && props.pressedTooltip) {
595
+ return props.pressedTooltip;
596
+ }
597
+ else {
598
+ return props.tooltip || props.iconLabel;
599
+ }
600
+ };
601
+ return (React.createElement(Button, { className: props.className
602
+ ? props.className + ' jp-ToolbarButtonComponent'
603
+ : 'jp-ToolbarButtonComponent', "aria-pressed": props.pressed, "aria-disabled": props.enabled === false, disabled: props.enabled === false, onClick: ((_a = props.actualOnClick) !== null && _a !== void 0 ? _a : false) ? handleClick : undefined, onMouseDown: !((_b = props.actualOnClick) !== null && _b !== void 0 ? _b : false) ? handleMouseDown : undefined, onKeyDown: handleKeyDown, title: getTooltip(), minimal: true },
604
+ (props.icon || props.iconClass) && (React.createElement(LabIcon.resolveReact, { icon: props.pressed ? props.pressedIcon : props.icon, iconClass:
605
+ // add some extra classes for proper support of icons-as-css-background
606
+ classes(props.iconClass, 'jp-Icon'), className: "jp-ToolbarButtonComponent-icon", tag: "span", stylesheet: "toolbarButton" })),
607
+ props.label && (React.createElement("span", { className: "jp-ToolbarButtonComponent-label" }, props.label))));
608
+ }
609
+ /**
610
+ * Adds the toolbar button class to the toolbar widget.
611
+ * @param w Toolbar button widget.
612
+ */
613
+ export function addToolbarButtonClass(w) {
614
+ w.addClass('jp-ToolbarButton');
615
+ return w;
616
+ }
617
+ /**
618
+ * Phosphor Widget version of static ToolbarButtonComponent.
619
+ */
620
+ export class ToolbarButton extends ReactWidget {
621
+ /**
622
+ * Creates a toolbar button
623
+ * @param props props for underlying `ToolbarButton` component
624
+ */
625
+ constructor(props = {}) {
626
+ var _a, _b;
627
+ super();
628
+ this.props = props;
629
+ addToolbarButtonClass(this);
630
+ this._enabled = (_a = props.enabled) !== null && _a !== void 0 ? _a : true;
631
+ this._pressed = this._enabled && ((_b = props.pressed) !== null && _b !== void 0 ? _b : false);
632
+ this._onClick = props.onClick;
633
+ }
634
+ /**
635
+ * Sets the pressed state for the button
636
+ * @param value true if button is pressed, false otherwise
637
+ */
638
+ set pressed(value) {
639
+ if (this.enabled && value !== this._pressed) {
640
+ this._pressed = value;
641
+ this.update();
642
+ }
643
+ }
644
+ /**
645
+ * Returns true if button is pressed, false otherwise
646
+ */
647
+ get pressed() {
648
+ return this._pressed;
649
+ }
650
+ /**
651
+ * Sets the enabled state for the button
652
+ * @param value true to enable the button, false otherwise
653
+ */
654
+ set enabled(value) {
655
+ if (value != this._enabled) {
656
+ this._enabled = value;
657
+ if (!this._enabled) {
658
+ this._pressed = false;
659
+ }
660
+ this.update();
661
+ }
662
+ }
663
+ /**
664
+ * Returns true if button is enabled, false otherwise
665
+ */
666
+ get enabled() {
667
+ return this._enabled;
668
+ }
669
+ /**
670
+ * Sets the click handler for the button
671
+ * @param value click handler
672
+ */
673
+ set onClick(value) {
674
+ if (value !== this._onClick) {
675
+ this._onClick = value;
676
+ this.update();
677
+ }
678
+ }
679
+ /**
680
+ * Returns the click handler for the button
681
+ */
682
+ get onClick() {
683
+ return this._onClick;
684
+ }
685
+ render() {
686
+ return (React.createElement(ToolbarButtonComponent, Object.assign({}, this.props, { pressed: this.pressed, enabled: this.enabled, onClick: this.onClick })));
687
+ }
688
+ }
689
+ /**
690
+ * React component for a toolbar button that wraps a command.
691
+ *
692
+ * This wraps the ToolbarButtonComponent and watches the command registry
693
+ * for changes to the command.
694
+ */
695
+ export function CommandToolbarButtonComponent(props) {
696
+ return (React.createElement(UseSignal, { signal: props.commands.commandChanged, shouldUpdate: (sender, args) => (args.id === props.id && args.type === 'changed') ||
697
+ args.type === 'many-changed' }, () => React.createElement(ToolbarButtonComponent, Object.assign({}, Private.propsFromCommand(props)))));
698
+ }
699
+ /*
700
+ * Adds the command toolbar button class to the command toolbar widget.
701
+ * @param w Command toolbar button widget.
702
+ */
703
+ export function addCommandToolbarButtonClass(w) {
704
+ w.addClass('jp-CommandToolbarButton');
705
+ return w;
706
+ }
707
+ /**
708
+ * Phosphor Widget version of CommandToolbarButtonComponent.
709
+ */
710
+ export class CommandToolbarButton extends ReactWidget {
711
+ /**
712
+ * Creates a command toolbar button
713
+ * @param props props for underlying `CommandToolbarButtonComponent` component
714
+ */
715
+ constructor(props) {
716
+ super();
717
+ this.props = props;
718
+ addCommandToolbarButtonClass(this);
719
+ }
720
+ render() {
721
+ return React.createElement(CommandToolbarButtonComponent, Object.assign({}, this.props));
722
+ }
723
+ }
724
+ /**
725
+ * A class which provides a toolbar popup
726
+ * used to store widgets that don't fit
727
+ * in the toolbar when it is resized
728
+ */
729
+ class ToolbarPopup extends Widget {
730
+ /**
731
+ * Construct a new ToolbarPopup
732
+ */
733
+ constructor() {
734
+ super();
735
+ this.width = 0;
736
+ this.addClass('jp-Toolbar-responsive-popup');
737
+ this.layout = new PanelLayout();
738
+ Widget.attach(this, document.body);
739
+ this.hide();
740
+ }
741
+ /**
742
+ * Updates the width of the popup, this
743
+ * should match with the toolbar width
744
+ *
745
+ * @param width - The width to resize to
746
+ * @protected
747
+ */
748
+ updateWidth(width) {
749
+ if (width > 0) {
750
+ this.width = width;
751
+ this.node.style.width = `${width}px`;
752
+ }
753
+ }
754
+ /**
755
+ * Aligns the popup to left bottom of widget
756
+ *
757
+ * @param widget the widget to align to
758
+ * @private
759
+ */
760
+ alignTo(widget) {
761
+ const { height: widgetHeight, width: widgetWidth, x: widgetX, y: widgetY } = widget.node.getBoundingClientRect();
762
+ const width = this.width;
763
+ this.node.style.left = `${widgetX + widgetWidth - width + 1}px`;
764
+ this.node.style.top = `${widgetY + widgetHeight + 1}px`;
765
+ }
766
+ /**
767
+ * Inserts the widget at specified index
768
+ * @param index the index
769
+ * @param widget widget to add
770
+ */
771
+ insertWidget(index, widget) {
772
+ this.layout.insertWidget(0, widget);
773
+ }
774
+ /**
775
+ * Total number of widgets in the popup
776
+ */
777
+ widgetCount() {
778
+ return this.layout.widgets.length;
779
+ }
780
+ /**
781
+ * Returns the widget at index
782
+ * @param index the index
783
+ */
784
+ widgetAt(index) {
785
+ return this.layout.widgets[index];
786
+ }
787
+ }
788
+ /**
789
+ * A class that provides a ToolbarPopupOpener,
790
+ * which is a button added to toolbar when
791
+ * the toolbar items overflow toolbar width
792
+ */
793
+ class ToolbarPopupOpener extends ToolbarButton {
794
+ /**
795
+ * Create a new popup opener
796
+ */
797
+ constructor() {
798
+ super({
799
+ icon: ellipsesIcon,
800
+ onClick: () => {
801
+ this.handleClick();
802
+ }
803
+ });
804
+ this.addClass('jp-Toolbar-responsive-opener');
805
+ this.popup = new ToolbarPopup();
806
+ }
807
+ /**
808
+ * Add widget to the popup, prepends widgets
809
+ * @param widget the widget to add
810
+ */
811
+ addWidget(widget) {
812
+ this.popup.insertWidget(0, widget);
813
+ }
814
+ /**
815
+ * Dispose of the widget and its descendant widgets.
816
+ *
817
+ * #### Notes
818
+ * It is unsafe to use the widget after it has been disposed.
819
+ *
820
+ * All calls made to this method after the first are a no-op.
821
+ */
822
+ dispose() {
823
+ if (this.isDisposed) {
824
+ return;
825
+ }
826
+ this.popup.dispose();
827
+ super.dispose();
828
+ }
829
+ /**
830
+ * Hides the opener and the popup
831
+ */
832
+ hide() {
833
+ super.hide();
834
+ this.hidePopup();
835
+ }
836
+ /**
837
+ * Hides the popup
838
+ */
839
+ hidePopup() {
840
+ this.popup.hide();
841
+ }
842
+ /**
843
+ * Updates width and position of the popup
844
+ * to align with the toolbar
845
+ */
846
+ updatePopup() {
847
+ this.popup.updateWidth(this.parent.node.clientWidth);
848
+ this.popup.alignTo(this.parent);
849
+ }
850
+ /**
851
+ * Returns widget at index in the popup
852
+ * @param index
853
+ */
854
+ widgetAt(index) {
855
+ return this.popup.widgetAt(index);
856
+ }
857
+ /**
858
+ * Returns total number of widgets in the popup
859
+ *
860
+ * @returns Number of widgets
861
+ */
862
+ widgetCount() {
863
+ return this.popup.widgetCount();
864
+ }
865
+ handleClick() {
866
+ this.updatePopup();
867
+ this.popup.setHidden(!this.popup.isHidden);
868
+ }
869
+ }
870
+ /**
871
+ * A namespace for private data.
872
+ */
873
+ var Private;
874
+ (function (Private) {
875
+ function propsFromCommand(options) {
876
+ const { commands, id, args } = options;
877
+ const iconClass = commands.iconClass(id, args);
878
+ const iconLabel = commands.iconLabel(id, args);
879
+ // DEPRECATED: remove _icon when lumino 2.0 is adopted
880
+ // if icon is aliasing iconClass, don't use it
881
+ const _icon = commands.icon(id, args);
882
+ const icon = _icon === iconClass ? undefined : _icon;
883
+ const label = commands.label(id, args);
884
+ let className = commands.className(id, args);
885
+ // Add the boolean state classes.
886
+ if (commands.isToggled(id, args)) {
887
+ className += ' lm-mod-toggled';
888
+ }
889
+ if (!commands.isVisible(id, args)) {
890
+ className += ' lm-mod-hidden';
891
+ }
892
+ let tooltip = commands.caption(id, args) || label || iconLabel;
893
+ // Shows hot keys in tooltips
894
+ const binding = commands.keyBindings.find(b => b.command === id);
895
+ if (binding) {
896
+ const ks = CommandRegistry.formatKeystroke(binding.keys.join(' '));
897
+ tooltip = `${tooltip} (${ks})`;
898
+ }
899
+ const onClick = () => {
900
+ void commands.execute(id, args);
901
+ };
902
+ const enabled = commands.isEnabled(id, args);
903
+ return { className, icon, iconClass, tooltip, onClick, enabled, label };
904
+ }
905
+ Private.propsFromCommand = propsFromCommand;
906
+ /**
907
+ * An attached property for the name of a toolbar item.
908
+ */
909
+ Private.nameProperty = new AttachedProperty({
910
+ name: 'name',
911
+ create: () => ''
912
+ });
913
+ /**
914
+ * A no-op function.
915
+ */
916
+ function noOp() {
917
+ /* no-op */
918
+ }
919
+ Private.noOp = noOp;
920
+ /**
921
+ * A spacer widget.
922
+ */
923
+ class Spacer extends Widget {
924
+ /**
925
+ * Construct a new spacer widget.
926
+ */
927
+ constructor() {
928
+ super();
929
+ this.addClass(TOOLBAR_SPACER_CLASS);
930
+ }
931
+ }
932
+ Private.Spacer = Spacer;
933
+ /**
934
+ * React component for a kernel name button.
935
+ *
936
+ * This wraps the ToolbarButtonComponent and watches the kernel
937
+ * session for changes.
938
+ */
939
+ function KernelNameComponent(props) {
940
+ const translator = props.translator || nullTranslator;
941
+ const trans = translator.load('jupyterlab');
942
+ const callback = () => {
943
+ void props.dialogs.selectKernel(props.sessionContext, translator);
944
+ };
945
+ return (React.createElement(UseSignal, { signal: props.sessionContext.kernelChanged, initialSender: props.sessionContext }, sessionContext => (React.createElement(ToolbarButtonComponent, { className: TOOLBAR_KERNEL_NAME_CLASS, onClick: callback, tooltip: trans.__('Switch kernel'), label: sessionContext === null || sessionContext === void 0 ? void 0 : sessionContext.kernelDisplayName }))));
946
+ }
947
+ Private.KernelNameComponent = KernelNameComponent;
948
+ /**
949
+ * A toolbar item that displays kernel status.
950
+ */
951
+ class KernelStatus extends Widget {
952
+ /**
953
+ * Construct a new kernel status widget.
954
+ */
955
+ constructor(sessionContext, translator) {
956
+ super();
957
+ this.translator = translator || nullTranslator;
958
+ this._trans = this.translator.load('jupyterlab');
959
+ this.addClass(TOOLBAR_KERNEL_STATUS_CLASS);
960
+ // TODO-FIXME: this mapping is duplicated in statusbar/kernelStatus.tsx
961
+ this._statusNames = {
962
+ unknown: this._trans.__('Unknown'),
963
+ starting: this._trans.__('Starting'),
964
+ idle: this._trans.__('Idle'),
965
+ busy: this._trans.__('Busy'),
966
+ terminating: this._trans.__('Terminating'),
967
+ restarting: this._trans.__('Restarting'),
968
+ autorestarting: this._trans.__('Autorestarting'),
969
+ dead: this._trans.__('Dead'),
970
+ connected: this._trans.__('Connected'),
971
+ connecting: this._trans.__('Connecting'),
972
+ disconnected: this._trans.__('Disconnected'),
973
+ initializing: this._trans.__('Initializing'),
974
+ '': ''
975
+ };
976
+ this._onStatusChanged(sessionContext);
977
+ sessionContext.statusChanged.connect(this._onStatusChanged, this);
978
+ sessionContext.connectionStatusChanged.connect(this._onStatusChanged, this);
979
+ }
980
+ /**
981
+ * Handle a status on a kernel.
982
+ */
983
+ _onStatusChanged(sessionContext) {
984
+ if (this.isDisposed) {
985
+ return;
986
+ }
987
+ const status = sessionContext.kernelDisplayStatus;
988
+ const circleIconProps = {
989
+ container: this.node,
990
+ title: this._trans.__('Kernel %1', this._statusNames[status] || status),
991
+ stylesheet: 'toolbarButton',
992
+ alignSelf: 'normal',
993
+ height: '24px'
994
+ };
995
+ // set the icon
996
+ LabIcon.remove(this.node);
997
+ if (status === 'busy' ||
998
+ status === 'starting' ||
999
+ status === 'terminating' ||
1000
+ status === 'restarting' ||
1001
+ status === 'initializing') {
1002
+ circleIcon.element(circleIconProps);
1003
+ }
1004
+ else if (status === 'connecting' ||
1005
+ status === 'disconnected' ||
1006
+ status === 'unknown') {
1007
+ offlineBoltIcon.element(circleIconProps);
1008
+ }
1009
+ else {
1010
+ circleEmptyIcon.element(circleIconProps);
1011
+ }
1012
+ }
1013
+ }
1014
+ Private.KernelStatus = KernelStatus;
1015
+ })(Private || (Private = {}));
1016
+ //# sourceMappingURL=toolbar.js.map