forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.ts
962 lines (836 loc) · 25.5 KB
/
shell.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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ArrayExt, each, find, IIterator, iter, toArray
} from '@phosphor/algorithm';
import {
PromiseDelegate
} from '@phosphor/coreutils';
import {
Message, MessageLoop
} from '@phosphor/messaging';
import {
ISignal, Signal
} from '@phosphor/signaling';
import {
BoxLayout, BoxPanel, DockLayout, DockPanel, FocusTracker,
Panel, SplitPanel, StackedPanel, TabBar, Title, Widget
} from '@phosphor/widgets';
/**
* The class name added to AppShell instances.
*/
const APPLICATION_SHELL_CLASS = 'jp-ApplicationShell';
/**
* The class name added to side bar instances.
*/
const SIDEBAR_CLASS = 'jp-SideBar';
/**
* The class name added to the current widget's title.
*/
const CURRENT_CLASS = 'jp-mod-current';
/**
* The class name added to the active widget's title.
*/
const ACTIVE_CLASS = 'jp-mod-active';
/**
* The default rank of items added to a sidebar.
*/
const DEFAULT_RANK = 500;
/**
* The data attribute added to the document body indicating shell's mode.
*/
const MODE_ATTRIBUTE = 'data-shell-mode';
/**
* The application shell for JupyterLab.
*/
export
class ApplicationShell extends Widget {
/**
* Construct a new application shell.
*/
constructor() {
super();
this.addClass(APPLICATION_SHELL_CLASS);
this.id = 'main';
let topPanel = this._topPanel = new Panel();
let hboxPanel = this._hboxPanel = new BoxPanel();
let dockPanel = this._dockPanel = new DockPanel();
let hsplitPanel = this._hsplitPanel = new SplitPanel();
let leftHandler = this._leftHandler = new Private.SideBarHandler('left');
let rightHandler = this._rightHandler = new Private.SideBarHandler('right');
let rootLayout = new BoxLayout();
topPanel.id = 'jp-top-panel';
hboxPanel.id = 'jp-main-content-panel';
dockPanel.id = 'jp-main-dock-panel';
hsplitPanel.id = 'jp-main-split-panel';
leftHandler.sideBar.addClass(SIDEBAR_CLASS);
leftHandler.sideBar.addClass('jp-mod-left');
leftHandler.stackedPanel.id = 'jp-left-stack';
rightHandler.sideBar.addClass(SIDEBAR_CLASS);
rightHandler.sideBar.addClass('jp-mod-right');
rightHandler.stackedPanel.id = 'jp-right-stack';
hboxPanel.spacing = 0;
dockPanel.spacing = 5;
hsplitPanel.spacing = 1;
hboxPanel.direction = 'left-to-right';
hsplitPanel.orientation = 'horizontal';
SplitPanel.setStretch(leftHandler.stackedPanel, 0);
SplitPanel.setStretch(dockPanel, 1);
SplitPanel.setStretch(rightHandler.stackedPanel, 0);
BoxPanel.setStretch(leftHandler.sideBar, 0);
BoxPanel.setStretch(hsplitPanel, 1);
BoxPanel.setStretch(rightHandler.sideBar, 0);
hsplitPanel.addWidget(leftHandler.stackedPanel);
hsplitPanel.addWidget(dockPanel);
hsplitPanel.addWidget(rightHandler.stackedPanel);
hboxPanel.addWidget(leftHandler.sideBar);
hboxPanel.addWidget(hsplitPanel);
hboxPanel.addWidget(rightHandler.sideBar);
rootLayout.direction = 'top-to-bottom';
rootLayout.spacing = 0; // TODO make this configurable?
BoxLayout.setStretch(topPanel, 0);
BoxLayout.setStretch(hboxPanel, 1);
rootLayout.addWidget(topPanel);
rootLayout.addWidget(hboxPanel);
this.layout = rootLayout;
// Connect change listeners.
this._tracker.currentChanged.connect(this._onCurrentChanged, this);
this._tracker.activeChanged.connect(this._onActiveChanged, this);
// Connect main layout change listener.
this._dockPanel.layoutModified.connect(() => {
this._layoutModified.emit(void 0);
this._save();
}, this);
}
/**
* A signal emitted when main area's active focus changes.
*/
get activeChanged(): ISignal<this, ApplicationShell.IChangedArgs> {
return this._activeChanged;
}
/**
* The active widget in the shell's main area.
*/
get activeWidget(): Widget | null {
return this._tracker.activeWidget;
}
/**
* A signal emitted when main area's current focus changes.
*/
get currentChanged(): ISignal<this, ApplicationShell.IChangedArgs> {
return this._currentChanged;
}
/**
* The current widget in the shell's main area.
*/
get currentWidget(): Widget | null {
return this._tracker.currentWidget;
}
/**
* A signal emitted when the main area's layout is modified.
*/
get layoutModified(): ISignal<this, void> {
return this._layoutModified;
}
/**
* The main dock area's user interface mode.
*/
get mode(): DockPanel.Mode {
return this._dockPanel.mode;
}
set mode(mode: DockPanel.Mode) {
const dock = this._dockPanel;
if (mode === dock.mode) {
return;
}
if (mode === 'single-document') {
this._cachedLayout = dock.saveLayout();
dock.mode = mode;
// In case the active widget in the dock panel is *not* the active widget
// of the application, defer to the application.
dock.activateWidget(this.currentWidget);
// Set the mode data attribute on the document body.
document.body.setAttribute(MODE_ATTRIBUTE, mode);
return;
}
// Cache a reference to every widget currently in the dock panel.
const widgets = toArray(dock.widgets());
// Toggle back to multiple document mode.
dock.mode = mode;
// Restore the original layout.
if (this._cachedLayout) {
// Remove any disposed widgets in the cached layout and restore.
Private.normalizeAreaConfig(dock, this._cachedLayout.main);
dock.restoreLayout(this._cachedLayout);
this._cachedLayout = null;
}
// Add any widgets created during single document mode, which have
// subsequently been removed from the dock panel after the multiple document
// layout has been restored.
widgets.forEach(widget => {
if (!widget.parent) {
this.addToMainArea(widget);
}
});
// In case the active widget in the dock panel is *not* the active widget
// of the application, defer to the application.
dock.activateWidget(this.currentWidget);
// Set the mode data attribute on the document body.
document.body.setAttribute(MODE_ATTRIBUTE, mode);
}
/**
* Promise that resolves when state is restored, returning layout description.
*/
get restored(): Promise<ApplicationShell.ILayout> {
return this._restored.promise;
}
/**
* Activate a widget in its area.
*/
activateById(id: string): void {
if (this._leftHandler.has(id)) {
this._leftHandler.activate(id);
} else if (this._rightHandler.has(id)) {
this._rightHandler.activate(id);
} else {
const dock = this._dockPanel;
const widget = find(dock.widgets(), value => value.id === id);
if (widget) {
dock.activateWidget(widget);
}
}
}
/*
* Activate the next Tab in the active TabBar.
*/
activateNextTab(): void {
let current = this._currentTabBar();
if (!current) {
return;
}
let ci = current.currentIndex;
if (ci === -1) {
return;
}
if (ci < current.titles.length - 1) {
current.currentIndex += 1;
current.currentTitle.owner.activate();
return;
}
if (ci === current.titles.length - 1) {
let nextBar = this._adjacentBar('next');
if (nextBar) {
nextBar.currentIndex = 0;
nextBar.currentTitle.owner.activate();
}
}
}
/*
* Activate the previous Tab in the active TabBar.
*/
activatePreviousTab(): void {
let current = this._currentTabBar();
if (!current) {
return;
}
let ci = current.currentIndex;
if (ci === -1) {
return;
}
if (ci > 0) {
current.currentIndex -= 1;
current.currentTitle.owner.activate();
return;
}
if (ci === 0) {
let prevBar = this._adjacentBar('previous');
if (prevBar) {
let len = prevBar.titles.length;
prevBar.currentIndex = len - 1;
prevBar.currentTitle.owner.activate();
}
}
}
/**
* Add a widget to the left content area.
*
* #### Notes
* Widgets must have a unique `id` property, which will be used as the DOM id.
*/
addToLeftArea(widget: Widget, options: ApplicationShell.ISideAreaOptions = {}): void {
if (!widget.id) {
console.error('Widgets added to app shell must have unique id property.');
return;
}
let rank = 'rank' in options ? options.rank : DEFAULT_RANK;
this._leftHandler.addWidget(widget, rank);
this._save();
}
/**
* Add a widget to the main content area.
*
* #### Notes
* Widgets must have a unique `id` property, which will be used as the DOM id.
* All widgets added to the main area should be disposed after removal (or
* simply disposed in order to remove).
*/
addToMainArea(widget: Widget): void {
if (!widget.id) {
console.error('Widgets added to app shell must have unique id property.');
return;
}
this._dockPanel.addWidget(widget, { mode: 'tab-after' });
this._tracker.add(widget);
}
/**
* Add a widget to the right content area.
*
* #### Notes
* Widgets must have a unique `id` property, which will be used as the DOM id.
*/
addToRightArea(widget: Widget, options: ApplicationShell.ISideAreaOptions = {}): void {
if (!widget.id) {
console.error('Widgets added to app shell must have unique id property.');
return;
}
let rank = 'rank' in options ? options.rank : DEFAULT_RANK;
this._rightHandler.addWidget(widget, rank);
this._save();
}
/**
* Add a widget to the top content area.
*
* #### Notes
* Widgets must have a unique `id` property, which will be used as the DOM id.
*/
addToTopArea(widget: Widget, options: ApplicationShell.ISideAreaOptions = {}): void {
if (!widget.id) {
console.error('Widgets added to app shell must have unique id property.');
return;
}
// Temporary: widgets are added to the panel in order of insertion.
this._topPanel.addWidget(widget);
this._save();
}
/**
* Collapse the left area.
*/
collapseLeft(): void {
this._leftHandler.collapse();
this._save();
}
/**
* Collapse the right area.
*/
collapseRight(): void {
this._rightHandler.collapse();
this._save();
}
/**
* Close all widgets in the main area.
*/
closeAll(): void {
// Make a copy of all the widget in the dock panel (using `toArray()`)
// before removing them because removing them while iterating through them
// modifies the underlying data of the iterator.
each(toArray(this._dockPanel.widgets()), widget => { widget.close(); });
}
/**
* True if the given area is empty.
*/
isEmpty(area: ApplicationShell.Area): boolean {
switch (area) {
case 'left':
return this._leftHandler.stackedPanel.widgets.length === 0;
case 'main':
return this._dockPanel.isEmpty;
case 'top':
return this._topPanel.widgets.length === 0;
case 'right':
return this._rightHandler.stackedPanel.widgets.length === 0;
default:
return true;
}
}
/**
* Set the layout data store for the application shell.
*/
setLayoutDB(database: ApplicationShell.ILayoutDB): void {
if (this._database) {
throw new Error('cannot reset layout database');
}
this._database = database;
this._database.fetch().then(saved => {
if (this.isDisposed || !saved) {
return;
}
const { mainArea, leftArea, rightArea } = saved;
// Rehydrate the main area.
if (mainArea) {
const { currentWidget, dock, mode } = mainArea;
if (dock) {
this._dockPanel.restoreLayout(dock);
}
if (mode) {
this.mode = mode;
}
if (currentWidget) {
this.activateById(currentWidget.id);
}
}
// Rehydrate the left area.
if (leftArea) {
this._leftHandler.rehydrate(leftArea);
}
// Rehydrate the right area.
if (rightArea) {
this._rightHandler.rehydrate(rightArea);
}
// Set restored flag, save state, and resolve the restoration promise.
this._isRestored = true;
return this._save().then(() => {
// Make sure all messages in the queue are finished before notifying
// any extensions that are waiting for the promise that guarantees the
// application state has been restored.
MessageLoop.flush();
this._restored.resolve(saved);
});
});
// Catch current changed events on the side handlers.
this._tracker.currentChanged.connect(this._save, this);
this._leftHandler.sideBar.currentChanged.connect(this._save, this);
this._rightHandler.sideBar.currentChanged.connect(this._save, this);
}
/**
* Returns the widgets for an application area.
*/
widgets(area: ApplicationShell.Area): IIterator<Widget> {
switch (area) {
case 'main':
return this._dockPanel.widgets();
case 'left':
return iter(this._leftHandler.sideBar.titles.map(t => t.owner));
case 'right':
return iter(this._rightHandler.sideBar.titles.map(t => t.owner));
case 'top':
return this._topPanel.children();
default:
break;
}
}
/**
* Handle `after-attach` messages for the application shell.
*/
protected onAfterAttach(msg: Message): void {
document.body.setAttribute(MODE_ATTRIBUTE, this.mode);
}
/*
* Return the tab bar adjacent to the current TabBar or `null`.
*/
private _adjacentBar(direction: 'next' | 'previous'): TabBar<Widget> | null {
const current = this._currentTabBar();
if (!current) {
return null;
}
const bars = toArray(this._dockPanel.tabBars());
const len = bars.length;
const index = bars.indexOf(current);
if (direction === 'previous') {
return index > 0 ? bars[index - 1]
: index === 0 ? bars[len - 1]
: null;
}
// Otherwise, direction is 'next'.
return index < len - 1 ? bars[index + 1]
: index === len - 1 ? bars[0]
: null;
}
/*
* Return the TabBar that has the currently active Widget or null.
*/
private _currentTabBar(): TabBar<Widget> | null {
const current = this._tracker.currentWidget;
if (!current) {
return null;
}
const title = current.title;
const bars = this._dockPanel.tabBars();
return find(bars, bar => bar.titles.indexOf(title) > -1) || null;
}
/**
* Handle a change to the dock area active widget.
*/
private _onActiveChanged(sender: any, args: FocusTracker.IChangedArgs<Widget>): void {
if (args.newValue) {
args.newValue.title.className += ` ${ACTIVE_CLASS}`;
}
if (args.oldValue) {
args.oldValue.title.className = (
args.oldValue.title.className.replace(ACTIVE_CLASS, '')
);
}
this._activeChanged.emit(args);
}
/**
* Handle a change to the dock area current widget.
*/
private _onCurrentChanged(sender: any, args: FocusTracker.IChangedArgs<Widget>): void {
if (args.newValue) {
args.newValue.title.className += ` ${CURRENT_CLASS}`;
}
if (args.oldValue) {
args.oldValue.title.className = (
args.oldValue.title.className.replace(CURRENT_CLASS, '')
);
}
this._currentChanged.emit(args);
}
/**
* Save the dehydrated state of the application shell.
*/
private _save(): Promise<void> {
if (!this._database || !this._isRestored) {
return;
}
// If the application is in single document mode, use the cached layout if
// available. Otherwise, default to querying the dock panel for layout.
const data: ApplicationShell.ILayout = {
mainArea: {
currentWidget: this._tracker.currentWidget,
dock: this.mode === 'single-document' ?
this._cachedLayout || this._dockPanel.saveLayout()
: this._dockPanel.saveLayout(),
mode: this._dockPanel.mode
},
leftArea: this._leftHandler.dehydrate(),
rightArea: this._rightHandler.dehydrate()
};
return this._database.save(data);
}
private _activeChanged = new Signal<this, ApplicationShell.IChangedArgs>(this);
private _cachedLayout: DockLayout.ILayoutConfig | null = null;
private _currentChanged = new Signal<this, ApplicationShell.IChangedArgs>(this);
private _database: ApplicationShell.ILayoutDB = null;
private _dockPanel: DockPanel;
private _hboxPanel: BoxPanel;
private _hsplitPanel: SplitPanel;
private _isRestored = false;
private _layoutModified = new Signal<this, void>(this);
private _leftHandler: Private.SideBarHandler;
private _restored = new PromiseDelegate<ApplicationShell.ILayout>();
private _rightHandler: Private.SideBarHandler;
private _tracker = new FocusTracker<Widget>();
private _topPanel: Panel;
}
/**
* The namespace for `ApplicationShell` class statics.
*/
export
namespace ApplicationShell {
/**
* The areas of the application shell where widgets can reside.
*/
export
type Area = 'main' | 'top' | 'left' | 'right';
/**
* The restorable description of an area within the main dock panel.
*/
export
type AreaConfig = DockLayout.AreaConfig;
/**
* An arguments object for the changed signals.
*/
export
type IChangedArgs = FocusTracker.IChangedArgs<Widget>;
/**
* A description of the application's user interface layout.
*/
export
interface ILayout {
/**
* Indicates whether fetched session restore data was actually retrieved
* from the state database or whether it is a fresh blank slate.
*
* #### Notes
* This attribute is only relevant when the layout data is retrieved via a
* `fetch` call. If it is set when being passed into `save`, it will be
* ignored.
*/
readonly fresh?: boolean;
/**
* The main area of the user interface.
*/
readonly mainArea: IMainArea | null;
/**
* The left area of the user interface.
*/
readonly leftArea: ISideArea | null;
/**
* The right area of the user interface.
*/
readonly rightArea: ISideArea | null;
}
/**
* An application layout data store.
*/
export
interface ILayoutDB {
/**
* Fetch the layout state for the application.
*
* #### Notes
* Fetching the layout relies on all widget restoration to be complete, so
* calls to `fetch` are guaranteed to return after restoration is complete.
*/
fetch(): Promise<ApplicationShell.ILayout>;
/**
* Save the layout state for the application.
*/
save(data: ApplicationShell.ILayout): Promise<void>;
}
/**
* The restorable description of the main application area.
*/
export
interface IMainArea {
/**
* The current widget that has application focus.
*/
readonly currentWidget: Widget | null;
/**
* The contents of the main application dock panel.
*/
readonly dock: DockLayout.ILayoutConfig | null;
/**
* The document mode (i.e., multiple/single) of the main dock panel.
*/
readonly mode: DockPanel.Mode | null;
};
/**
* The restorable description of a sidebar in the user interface.
*/
export
interface ISideArea {
/**
* A flag denoting whether the sidebar has been collapsed.
*/
readonly collapsed: boolean;
/**
* The current widget that has side area focus.
*/
readonly currentWidget: Widget | null;
/**
* The collection of widgets held by the sidebar.
*/
readonly widgets: Array<Widget> | null;
}
/**
* The options for adding a widget to a side area of the shell.
*/
export
interface ISideAreaOptions {
/**
* The rank order of the widget among its siblings.
*/
rank?: number;
}
}
namespace Private {
/**
* An object which holds a widget and its sort rank.
*/
export
interface IRankItem {
/**
* The widget for the item.
*/
widget: Widget;
/**
* The sort rank of the widget.
*/
rank: number;
}
/**
* A less-than comparison function for side bar rank items.
*/
export
function itemCmp(first: IRankItem, second: IRankItem): number {
return first.rank - second.rank;
}
/**
* Removes widgets that have been disposed from an area config, mutates area.
*/
export
function normalizeAreaConfig(parent: DockPanel, area: DockLayout.AreaConfig): void {
if (area.type === 'tab-area') {
area.widgets = area.widgets
.filter(widget => !widget.isDisposed && widget.parent === parent);
return;
}
area.children.forEach(child => { normalizeAreaConfig(parent, child); });
}
/**
* A class which manages a side bar and related stacked panel.
*/
export
class SideBarHandler {
/**
* Construct a new side bar handler.
*/
constructor(side: string) {
this._side = side;
this._sideBar = new TabBar<Widget>({
insertBehavior: 'none',
removeBehavior: 'none',
allowDeselect: true
});
this._stackedPanel = new StackedPanel();
this._sideBar.hide();
this._stackedPanel.hide();
this._sideBar.currentChanged.connect(this._onCurrentChanged, this);
this._sideBar.tabActivateRequested.connect(this._onTabActivateRequested, this);
this._stackedPanel.widgetRemoved.connect(this._onWidgetRemoved, this);
}
/**
* Get the tab bar managed by the handler.
*/
get sideBar(): TabBar<Widget> {
return this._sideBar;
}
/**
* Get the stacked panel managed by the handler
*/
get stackedPanel(): StackedPanel {
return this._stackedPanel;
}
/**
* Activate a widget residing in the side bar by ID.
*
* @param id - The widget's unique ID.
*/
activate(id: string): void {
let widget = this._findWidgetByID(id);
if (widget) {
this._sideBar.currentTitle = widget.title;
widget.activate();
}
}
/**
* Test whether the sidebar has the given widget by id.
*/
has(id: string): boolean {
return this._findWidgetByID(id) !== null;
}
/**
* Collapse the sidebar so no items are expanded.
*/
collapse(): void {
this._sideBar.currentTitle = null;
}
/**
* Add a widget and its title to the stacked panel and side bar.
*
* If the widget is already added, it will be moved.
*/
addWidget(widget: Widget, rank: number): void {
widget.parent = null;
widget.hide();
let item = { widget, rank };
let index = this._findInsertIndex(item);
ArrayExt.insert(this._items, index, item);
this._stackedPanel.insertWidget(index, widget);
this._sideBar.insertTab(index, widget.title);
this._refreshVisibility();
}
/**
* Dehydrate the side bar data.
*/
dehydrate(): ApplicationShell.ISideArea {
let collapsed = this._sideBar.currentTitle === null;
let widgets = toArray(this._stackedPanel.widgets);
let currentWidget = widgets[this._sideBar.currentIndex];
return { collapsed, currentWidget, widgets };
}
/**
* Rehydrate the side bar.
*/
rehydrate(data: ApplicationShell.ISideArea): void {
if (data.currentWidget) {
this.activate(data.currentWidget.id);
} else if (data.collapsed) {
this.collapse();
}
}
/**
* Find the insertion index for a rank item.
*/
private _findInsertIndex(item: Private.IRankItem): number {
return ArrayExt.upperBound(this._items, item, Private.itemCmp);
}
/**
* Find the index of the item with the given widget, or `-1`.
*/
private _findWidgetIndex(widget: Widget): number {
return ArrayExt.findFirstIndex(this._items, i => i.widget === widget);
}
/**
* Find the widget which owns the given title, or `null`.
*/
private _findWidgetByTitle(title: Title<Widget>): Widget {
let item = find(this._items, value => value.widget.title === title);
return item ? item.widget : null;
}
/**
* Find the widget with the given id, or `null`.
*/
private _findWidgetByID(id: string): Widget {
let item = find(this._items, value => value.widget.id === id);
return item ? item.widget : null;
}
/**
* Refresh the visibility of the side bar and stacked panel.
*/
private _refreshVisibility(): void {
this._sideBar.setHidden(this._sideBar.titles.length === 0);
this._stackedPanel.setHidden(this._sideBar.currentTitle === null);
}
/**
* Handle the `currentChanged` signal from the sidebar.
*/
private _onCurrentChanged(sender: TabBar<Widget>, args: TabBar.ICurrentChangedArgs<Widget>): void {
const oldWidget = this._findWidgetByTitle(args.previousTitle);
const newWidget = this._findWidgetByTitle(args.currentTitle);
if (oldWidget) {
oldWidget.hide();
}
if (newWidget) {
newWidget.show();
}
if (newWidget) {
const id = newWidget.id;
document.body.setAttribute(`data-${this._side}-sidebar-widget`, id);
} else {
document.body.removeAttribute(`data-${this._side}-sidebar-widget`);
}
this._refreshVisibility();
}
/**
* Handle a `tabActivateRequest` signal from the sidebar.
*/
private _onTabActivateRequested(sender: TabBar<Widget>, args: TabBar.ITabActivateRequestedArgs<Widget>): void {
args.title.owner.activate();
}
/*
* Handle the `widgetRemoved` signal from the stacked panel.
*/
private _onWidgetRemoved(sender: StackedPanel, widget: Widget): void {
ArrayExt.removeAt(this._items, this._findWidgetIndex(widget));
this._sideBar.removeTab(widget.title);
this._refreshVisibility();
}
private _items = new Array<Private.IRankItem>();
private _side: string;
private _sideBar: TabBar<Widget>;
private _stackedPanel: StackedPanel;
}
}