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