-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathPanelView.js
More file actions
1138 lines (1021 loc) · 39 KB
/
PanelView.js
File metadata and controls
1138 lines (1021 loc) · 39 KB
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// @INCLUDE_IN_API_DOCS
/*global fs, Phoenix, process*/
/*eslint no-console: 0*/
/*eslint strict: ["error", "global"]*/
/* jshint ignore:start */
define(function (require, exports, module) {
const EventDispatcher = require("utils/EventDispatcher"),
PreferencesManager = require("preferences/PreferencesManager"),
Resizer = require("utils/Resizer"),
DropdownButton = require("widgets/DropdownButton"),
Strings = require("strings");
/**
* Event when panel is hidden
* @type {string}
* @constant
*/
const EVENT_PANEL_HIDDEN = 'panelHidden';
/**
* Event when panel is shown
* @type {string}
* @constant
*/
const EVENT_PANEL_SHOWN = 'panelShown';
/**
* type for bottom panel
* @type {string}
* @constant
*/
const PANEL_TYPE_BOTTOM_PANEL = 'bottomPanel';
// --- Module-level tab state ---
/** @type {Object.<string, Panel>} Maps panel ID to Panel instance */
let _panelMap = {};
/** @type {jQueryObject} The single container wrapping all bottom panels */
let _$container;
/** @type {jQueryObject} The tab bar inside the container */
let _$tabBar;
/** @type {jQueryObject} Scrollable area holding the tab elements */
let _$tabsOverflow;
/** @type {string[]} Ordered list of currently open (tabbed) panel IDs */
let _openIds = [];
/** @type {string|null} The panel ID of the currently visible (active) tab */
let _activeId = null;
/** @type {boolean} Whether the bottom panel is currently maximized */
let _isMaximized = false;
/**
* Pixel threshold for detecting near-maximize state during resize.
* If the editor holder height is within this many pixels of zero, the
* panel is treated as maximized. Keeps the maximize icon responsive
* during drag without being overly sensitive.
* @const
* @type {number}
*/
const MAXIMIZE_THRESHOLD = 2;
/**
* Minimum panel height (matches Resizer minSize) used as a floor
* when computing a sensible restore height.
* @const
* @type {number}
*/
const MIN_PANEL_HEIGHT = 200;
/** Preference key for persisting the maximize state across reloads. */
const PREF_BOTTOM_PANEL_MAXIMIZED = "bottomPanelMaximized";
/** @type {number|null} The panel height before maximize, for restore */
let _preMaximizeHeight = null;
/** @type {jQueryObject} The editor holder element, passed from WorkspaceManager */
let _$editorHolder = null;
/** @type {function} recomputeLayout callback from WorkspaceManager */
let _recomputeLayout = null;
/** @type {string|null} The default/quick-access panel ID */
let _defaultPanelId = null;
/** @type {jQueryObject} The "+" button inside the tab overflow area */
let _$addBtn = null;
// --- Tab helper functions ---
/**
* Resolve the display title for a bottom panel tab.
* Uses the explicit title if provided, then checks for a .toolbar .title
* DOM element in the panel, and finally derives a name from the panel id.
* @param {string} id The panel registration ID
* @param {jQueryObject} $panel The panel's jQuery element
* @param {string=} title Explicit title passed to createBottomPanel
* @return {string}
* @private
*/
function _getPanelTitle(id, $panel, title) {
if (title) {
return title;
}
let $titleEl = $panel.find(".toolbar .title");
if ($titleEl.length && $.trim($titleEl.text())) {
return $.trim($titleEl.text());
}
let label = id.replace(new RegExp("[-_.]", "g"), " ").split(" ")[0];
return label.charAt(0).toUpperCase() + label.slice(1);
}
/**
* Full rebuild of the tab bar DOM from _openIds.
* Call this when tabs are added, removed, or renamed.
* @private
*/
/**
* Build a tab element for a panel.
* @param {Panel} panel
* @param {boolean} isActive
* @return {jQueryObject}
* @private
*/
function _buildTab(panel, isActive) {
let title = panel._tabTitle || _getPanelTitle(panel.panelID, panel.$panel);
let $tab = $('<div class="bottom-panel-tab" draggable="true"></div>')
.toggleClass('active', isActive)
.attr('data-panel-id', panel.panelID);
const opts = panel._options;
if (opts.iconClass) {
$tab.append($('<i class="bottom-panel-tab-icon panel-titlebar-icon"></i>')
.addClass(opts.iconClass));
} else if (opts.iconSvg) {
$tab.append($('<img class="bottom-panel-tab-icon panel-titlebar-icon">')
.attr("src", opts.iconSvg));
} else {
// Fallback generic icon for panels without a custom icon
$tab.append($('<i class="bottom-panel-tab-icon panel-titlebar-icon fa-solid fa-window-maximize"></i>'));
}
$tab.append($('<span class="bottom-panel-tab-title"></span>').text(title));
$tab.append($('<span class="bottom-panel-tab-close-btn">×</span>').attr('title', Strings.CLOSE));
return $tab;
}
function _updateBottomPanelTabBar() {
if (!_$tabsOverflow) {
return;
}
// Detach the add button before emptying to preserve its event handlers
if (_$addBtn) {
_$addBtn.detach();
}
_$tabsOverflow.empty();
_openIds.forEach(function (panelId) {
let panel = _panelMap[panelId];
if (!panel) {
return;
}
_$tabsOverflow.append(_buildTab(panel, panelId === _activeId));
});
// Re-append the Tools button at the end
if (_$addBtn) {
_$tabsOverflow.append(_$addBtn);
}
_updateAddButtonVisibility();
_checkTabOverflow();
}
/**
* Swap the .active class on the tab bar without rebuilding the DOM.
* @private
*/
function _updateActiveTabHighlight() {
if (!_$tabBar) {
return;
}
_$tabBar.find(".bottom-panel-tab").each(function () {
let $tab = $(this);
if ($tab.data("panel-id") === _activeId) {
$tab.addClass("active");
} else {
$tab.removeClass("active");
}
});
}
/**
* Append a single tab to the tab bar for the given panel.
* Use instead of _updateBottomPanelTabBar() when adding one tab.
* @param {string} panelId
* @private
*/
function _addTabToBar(panelId) {
if (!_$tabsOverflow) {
return;
}
let panel = _panelMap[panelId];
if (!panel) {
return;
}
let $tab = _buildTab(panel, panelId === _activeId);
// Insert before the Tools button so it stays at the end
if (_$addBtn && _$addBtn.parent().length) {
_$addBtn.before($tab);
} else {
_$tabsOverflow.append($tab);
}
_updateAddButtonVisibility();
_checkTabOverflow();
}
/**
* Remove a single tab from the tab bar by panel ID.
* Use instead of _updateBottomPanelTabBar() when removing one tab.
* @param {string} panelId
* @private
*/
function _removeTabFromBar(panelId) {
if (!_$tabsOverflow) {
return;
}
_$tabsOverflow.find('.bottom-panel-tab[data-panel-id="' + panelId + '"]').remove();
_updateAddButtonVisibility();
_checkTabOverflow();
}
/**
* Set up drag-and-drop tab reordering on the bottom panel tab bar.
* Uses a vertical line indicator matching the file tab bar UX.
* @private
*/
function _initDragAndDrop() {
let draggedTab = null;
let $indicator = $('<div class="tab-drag-indicator"></div>');
$("body").append($indicator);
function getDropPosition(targetTab, mouseX) {
const rect = targetTab.getBoundingClientRect();
return mouseX < rect.left + rect.width / 2;
}
function updateIndicator(targetTab, insertBefore) {
if (!targetTab) {
$indicator.hide();
return;
}
const rect = targetTab.getBoundingClientRect();
$indicator.css({
position: "fixed",
left: (insertBefore ? rect.left : rect.right) + "px",
top: rect.top + "px",
height: rect.height + "px",
width: "2px",
zIndex: 10001
}).show();
}
function cleanup() {
if (draggedTab) {
$(draggedTab).removeClass("bottom-panel-tab-dragging");
}
draggedTab = null;
$indicator.hide();
_$tabBar.find(".bottom-panel-tab").removeClass("drag-target");
}
_$tabBar.on("dragstart", ".bottom-panel-tab", function (e) {
draggedTab = this;
e.originalEvent.dataTransfer.effectAllowed = "move";
e.originalEvent.dataTransfer.setData("application/x-phoenix-panel-tab", "1");
$(this).addClass("bottom-panel-tab-dragging");
});
_$tabBar.on("dragend", ".bottom-panel-tab", function () {
setTimeout(cleanup, 50);
});
_$tabBar.on("dragover", ".bottom-panel-tab", function (e) {
if (!draggedTab || this === draggedTab) {
return;
}
e.preventDefault();
e.originalEvent.dataTransfer.dropEffect = "move";
_$tabBar.find(".bottom-panel-tab").removeClass("drag-target");
$(this).addClass("drag-target");
updateIndicator(this, getDropPosition(this, e.originalEvent.clientX));
});
_$tabBar.on("dragleave", ".bottom-panel-tab", function (e) {
const related = e.originalEvent.relatedTarget;
if (!$(this).is(related) && !$(this).has(related).length) {
$(this).removeClass("drag-target");
}
});
_$tabBar.on("drop", ".bottom-panel-tab", function (e) {
e.preventDefault();
e.stopPropagation();
if (!draggedTab || this === draggedTab) {
cleanup();
return;
}
let draggedId = $(draggedTab).data("panel-id");
let targetId = $(this).data("panel-id");
let fromIdx = _openIds.indexOf(draggedId);
let toIdx = _openIds.indexOf(targetId);
if (fromIdx === -1 || toIdx === -1) {
cleanup();
return;
}
const insertBefore = getDropPosition(this, e.originalEvent.clientX);
_openIds.splice(fromIdx, 1);
let newIdx = _openIds.indexOf(targetId);
if (!insertBefore) {
newIdx++;
}
_openIds.splice(newIdx, 0, draggedId);
cleanup();
_updateBottomPanelTabBar();
_updateActiveTabHighlight();
});
}
/**
* Check if the tab bar is overflowing and collapse tabs to icons if so.
* Only collapses tabs that have an icon available.
* @private
*/
/** @type {jQueryObject} Overflow dropdown button */
let _$overflowBtn = null;
function _checkTabOverflow() {
if (!_$tabBar) {
return;
}
// Remove collapsed state first to measure true width
_$tabBar.removeClass("bottom-panel-tabs-collapsed");
const isOverflowing = _$tabsOverflow[0].scrollWidth > _$tabsOverflow[0].clientWidth;
_$tabBar.toggleClass("bottom-panel-tabs-collapsed", isOverflowing);
// Check if still overflowing after collapse
const stillOverflowing = isOverflowing &&
_$tabsOverflow[0].scrollWidth > _$tabsOverflow[0].clientWidth;
// Show/hide overflow button
if (_$overflowBtn) {
_$overflowBtn.toggle(stillOverflowing);
}
// Show tooltip on hover only in collapsed mode (title text is hidden)
_$tabBar.find(".bottom-panel-tab").each(function () {
const $tab = $(this);
if (isOverflowing) {
$tab.attr("title", $tab.find(".bottom-panel-tab-title").text());
} else {
$tab.removeAttr("title");
}
});
}
/**
* Get the list of hidden (not fully visible) panel tabs.
* @return {Array<{panelId: string, title: string}>}
* @private
*/
function _getHiddenTabs() {
const hidden = [];
const barRect = _$tabsOverflow[0].getBoundingClientRect();
_$tabsOverflow.find(".bottom-panel-tab").each(function () {
const tabRect = this.getBoundingClientRect();
const isVisible = tabRect.left >= barRect.left &&
tabRect.right <= (barRect.right + 2);
if (!isVisible) {
const $tab = $(this);
hidden.push({
panelId: $tab.data("panel-id"),
title: $tab.find(".bottom-panel-tab-title").text()
});
}
});
return hidden;
}
/** @type {DropdownButton.DropdownButton} */
let _overflowDropdown = null;
/**
* Show a dropdown menu listing hidden panel tabs.
* Uses the same DropdownButton widget as the file tab bar overflow.
* @private
*/
function _showOverflowMenu() {
// If dropdown is already open, close it (toggle behavior)
if (_overflowDropdown) {
_overflowDropdown.closeDropdown();
_overflowDropdown = null;
return;
}
const hidden = _getHiddenTabs();
if (!hidden.length) {
return;
}
_overflowDropdown = new DropdownButton.DropdownButton("", hidden, function (item) {
const panel = _panelMap[item.panelId];
let iconHtml = "";
if (panel && panel._options) {
if (panel._options.iconClass) {
iconHtml = '<i class="panel-titlebar-icon ' + panel._options.iconClass
+ '" style="margin-right:6px"></i>';
} else if (panel._options.iconSvg) {
iconHtml = '<img class="panel-titlebar-icon" src="' + panel._options.iconSvg
+ '" style="width:14px;height:14px;margin-right:6px;vertical-align:middle">';
}
}
const activeClass = item.panelId === _activeId ? ' style="font-weight:600"' : '';
return {
html: '<div class="dropdown-tab-item"' + activeClass + '>'
+ iconHtml + '<span>' + item.title + '</span></div>',
enabled: true
};
});
_overflowDropdown.dropdownExtraClasses = "dropdown-overflow-menu";
// Position at the overflow button
const btnRect = _$overflowBtn[0].getBoundingClientRect();
$("body").append(_overflowDropdown.$button);
_overflowDropdown.$button.css({
position: "absolute",
left: btnRect.left + "px",
top: (btnRect.top - 2) + "px",
zIndex: 1000
});
_overflowDropdown.showDropdown();
_overflowDropdown.on("select", function (e, item) {
const panel = _panelMap[item.panelId];
if (panel) {
panel.show();
// Scroll the newly active tab into view
const $tab = _$tabsOverflow.find('.bottom-panel-tab[data-panel-id="' + item.panelId + '"]');
if ($tab.length) {
$tab[0].scrollIntoView({inline: "nearest"});
}
}
});
// Clean up reference when dropdown closes
_overflowDropdown.on(DropdownButton.EVENT_DROPDOWN_CLOSED, function () {
if (_overflowDropdown) {
_overflowDropdown.$button.remove();
_overflowDropdown = null;
}
});
}
/**
* Show or hide the "+" button based on whether the default panel is active.
* The button is hidden when the default panel is the active tab (since
* clicking "+" would be a no-op) and shown otherwise.
* @private
*/
function _updateAddButtonVisibility() {
if (!_$addBtn) {
return;
}
if (_defaultPanelId && _activeId === _defaultPanelId) {
_$addBtn.hide();
} else {
_$addBtn.show();
}
}
/**
* Switch the active tab to the given panel. Does not show/hide the container.
* @param {string} panelId
* @private
*/
function _switchToTab(panelId) {
if (_activeId === panelId) {
return;
}
// Remove active class from current
if (_activeId) {
let prevPanel = _panelMap[_activeId];
if (prevPanel) {
prevPanel.$panel.removeClass("active-bottom-panel");
}
}
// Set new active
_activeId = panelId;
let newPanel = _panelMap[panelId];
if (newPanel) {
newPanel.$panel.addClass("active-bottom-panel");
}
_updateActiveTabHighlight();
_updateAddButtonVisibility();
}
/**
* Represents a panel below the editor area (a child of ".content").
* @constructor
* @param {!jQueryObject} $panel The entire panel, including any chrome, already in the DOM.
* @param {string} id Unique panel identifier.
* @param {string=} title Optional display title for the tab bar.
*/
/**
* @param {jQueryObject} $panel
* @param {string} id
* @param {string=} title
* @param {Object=} options
* @param {string=} options.iconClass FontAwesome class string (e.g. "fa-solid fa-terminal").
* @param {string=} options.iconSvg Path to an SVG icon (e.g. "styles/images/icon.svg").
*/
function Panel($panel, id, title, options) {
this.$panel = $panel;
this.panelID = id;
this._tabTitle = _getPanelTitle(id, $panel, title);
this._options = options || {};
_panelMap[id] = this;
}
/**
* Dom node holding the rendered panel
* @type {jQueryObject}
*/
Panel.prototype.$panel = null;
/**
* Determines if the panel is visible
* @return {boolean} true if visible, false if not
*/
Panel.prototype.isVisible = function () {
return (_activeId === this.panelID) && _$container && _$container.is(":visible");
};
/**
* Registers a call back function that will be called just before panel is shown. The handler should return true
* if the panel can be shown, else return false and the panel will not be shown.
* @param {function|null} canShowHandlerFn function that should return true of false if the panel can be shown/not.
* or null to clear the handler.
* @return {boolean} true if visible, false if not
*/
Panel.prototype.registerCanBeShownHandler = function (canShowHandlerFn) {
if(this.canBeShownHandler && canShowHandlerFn){
console.warn(`canBeShownHandler already registered for panel: ${this.panelID}. will be overwritten`);
}
this.canBeShownHandler = canShowHandlerFn;
};
/**
* Returns true if th panel can be shown, else false.
* @return {boolean}
*/
Panel.prototype.canBeShown = function () {
let self = this;
if(self.canBeShownHandler){
return self.canBeShownHandler();
}
return true;
};
/**
* Registers an async handler that is called before the panel is closed via user interaction (e.g. clicking the
* tab close button). The handler should return `true` to allow the close, or `false` to prevent it.
* @param {function|null} handler An async function returning a boolean, or null to clear the handler.
*/
Panel.prototype.registerOnCloseRequestedHandler = function (handler) {
if (this._onCloseRequestedHandler && handler) {
console.warn(`onCloseRequestedHandler already registered for panel: ${this.panelID}. will be overwritten`);
}
this._onCloseRequestedHandler = handler;
};
/**
* Requests the panel to hide, invoking the registered onCloseRequested handler first (if any).
* If the handler returns false, the panel stays open. If it returns true or no handler is
* registered, `hide()` is called.
* @return {Promise<boolean>} Resolves to true if the panel was hidden, false if prevented.
*/
Panel.prototype.requestClose = async function () {
if (this._onCloseRequestedHandler) {
const allowed = await this._onCloseRequestedHandler();
if (!allowed) {
return false;
}
}
this.hide();
return true;
};
/**
* Shows the panel
*/
Panel.prototype.show = function () {
if (!this.canBeShown() || !_$container) {
return;
}
let panelId = this.panelID;
let isOpen = _openIds.indexOf(panelId) !== -1;
let isActive = (_activeId === panelId);
if (isOpen && isActive) {
// Already open and active — just ensure container is visible
if (!_$container.is(":visible")) {
Resizer.show(_$container[0]);
exports.trigger(EVENT_PANEL_SHOWN, panelId);
}
return;
}
if (isOpen && !isActive) {
// Open but not active - switch tab and ensure container is visible
_switchToTab(panelId);
if (!_$container.is(":visible")) {
Resizer.show(_$container[0]);
}
exports.trigger(EVENT_PANEL_SHOWN, panelId);
return;
}
// Not open: add to open set
_openIds.push(panelId);
// Show container if it was hidden
if (!_$container.is(":visible")) {
Resizer.show(_$container[0]);
}
_switchToTab(panelId);
_addTabToBar(panelId);
exports.trigger(EVENT_PANEL_SHOWN, panelId);
};
/**
* Hides the panel
*/
Panel.prototype.hide = function () {
let panelId = this.panelID;
let idx = _openIds.indexOf(panelId);
if (idx === -1) {
// Not open - no-op
return;
}
// Remove from open set
_openIds.splice(idx, 1);
this.$panel.removeClass("active-bottom-panel");
let wasActive = (_activeId === panelId);
let activatedId = null;
if (wasActive && _openIds.length > 0) {
let nextIdx = Math.min(idx, _openIds.length - 1);
activatedId = _openIds[nextIdx];
_activeId = null; // clear so _switchToTab runs
_switchToTab(activatedId);
} else if (wasActive) {
// No more tabs - hide the container
_activeId = null;
if (_$container) {
restoreIfMaximized();
Resizer.hide(_$container[0]);
}
}
_removeTabFromBar(panelId);
// Always fire HIDDEN for the closed panel first
exports.trigger(EVENT_PANEL_HIDDEN, panelId);
// Then fire SHOWN for the newly activated tab, if any
if (activatedId) {
exports.trigger(EVENT_PANEL_SHOWN, activatedId);
}
};
/**
* Attempts to focus the panel. Override this in panels that support focus
* (e.g. terminal). The default implementation returns false.
* @return {boolean} true if the panel accepted focus, false otherwise
*/
Panel.prototype.focus = function () {
return false;
};
/**
* Sets the panel's visibility state
* @param {boolean} visible true to show, false to hide
*/
Panel.prototype.setVisible = function (visible) {
if (visible) {
this.show();
} else {
this.hide();
}
};
/**
* Updates the display title shown in the tab bar for this panel.
* @param {string} newTitle The new title to display.
*/
Panel.prototype.setTitle = function (newTitle) {
this._tabTitle = newTitle;
if (_$tabsOverflow) {
_$tabsOverflow.find('.bottom-panel-tab[data-panel-id="' + this.panelID + '"] .bottom-panel-tab-title')
.text(newTitle);
}
};
/**
* Destroys the panel, removing it from the tab bar, internal maps, and the DOM.
* After calling this, the Panel instance should not be reused.
*/
Panel.prototype.destroy = function () {
if (_openIds.indexOf(this.panelID) !== -1) {
this.hide();
}
delete _panelMap[this.panelID];
this.$panel.remove();
};
/**
* gets the Panel's type
* @return {string}
*/
Panel.prototype.getPanelType = function () {
return PANEL_TYPE_BOTTOM_PANEL;
};
/**
* Initializes the PanelView module with references to the bottom panel container DOM elements.
* Called by WorkspaceManager during htmlReady.
* @param {jQueryObject} $container The bottom panel container element.
* @param {jQueryObject} $tabBar The tab bar element inside the container.
* @param {jQueryObject} $tabsOverflow The scrollable area holding tab elements.
* @param {jQueryObject} $editorHolder The editor holder element (for maximize height calculation).
* @param {function} recomputeLayoutFn Callback to trigger workspace layout recomputation.
* @param {string} defaultPanelId The ID of the default/quick-access panel.
*/
function init($container, $tabBar, $tabsOverflow, $editorHolder, recomputeLayoutFn, defaultPanelId) {
_$container = $container;
_$tabBar = $tabBar;
_$tabsOverflow = $tabsOverflow;
_$editorHolder = $editorHolder;
_recomputeLayout = recomputeLayoutFn;
_defaultPanelId = defaultPanelId;
// Create the "Tools" button inside the scrollable tabs area.
_$addBtn = $('<span class="bottom-panel-add-btn" title="' + Strings.BOTTOM_PANEL_DEFAULT_TITLE + '">'
+ '<img class="app-drawer-tab-icon" src="styles/images/app-drawer.svg"'
+ ' style="width:12px;height:12px;vertical-align:middle;margin-right:4px">'
+ Strings.BOTTOM_PANEL_DEFAULT_TITLE + '</span>');
_$tabsOverflow.append(_$addBtn);
// Tab bar click handlers
_$tabBar.on("click", ".bottom-panel-tab-close-btn", function (e) {
e.stopPropagation();
let panelId = $(this).closest(".bottom-panel-tab").data("panel-id");
if (panelId) {
let panel = _panelMap[panelId];
if (panel) {
panel.requestClose();
}
}
});
_$tabBar.on("click", ".bottom-panel-tab", function (e) {
let panelId = $(this).data("panel-id");
if (panelId && panelId !== _activeId) {
let panel = _panelMap[panelId];
if (panel) {
panel.show();
}
}
// Scroll clicked tab into view if partially hidden
this.scrollIntoView({inline: "nearest"});
});
_initDragAndDrop();
// Overflow button for hidden tabs (inserted between tabs and action buttons)
_$overflowBtn = $('<span class="bottom-panel-overflow-btn" title="' + Strings.TABBAR_SHOW_HIDDEN_TABS + '">'
+ '<i class="fa-solid fa-chevron-down"></i></span>');
_$overflowBtn.hide();
_$tabBar.find(".bottom-panel-tab-bar-actions").before(_$overflowBtn);
_$overflowBtn.on("click", function (e) {
e.stopPropagation();
_showOverflowMenu();
});
// "+" button opens the default/quick-access panel
_$addBtn.on("click", function (e) {
e.stopPropagation();
if (_defaultPanelId && _panelMap[_defaultPanelId]) {
_panelMap[_defaultPanelId].show();
}
});
// Hide-panel button collapses the container but keeps tabs intact.
// Maximize state is preserved so the panel re-opens maximized.
_$tabBar.on("click", ".bottom-panel-hide-btn", function (e) {
e.stopPropagation();
if (_$container.is(":visible")) {
Resizer.hide(_$container[0]);
}
});
// Maximize/restore toggle button
_$tabBar.on("click", ".bottom-panel-maximize-btn", function (e) {
e.stopPropagation();
_toggleMaximize();
});
// Double-click on empty tab bar area toggles maximize.
// Exclude tabs themselves, action buttons, and the add button.
_$tabBar.on("dblclick", function (e) {
if ($(e.target).closest(".bottom-panel-tab, .bottom-panel-tab-close-btn, .bottom-panel-hide-btn, .bottom-panel-maximize-btn, .bottom-panel-add-btn").length) {
return;
}
_toggleMaximize();
});
// Re-check tab overflow when the tab bar resizes (e.g. window resize)
const tabBarResizeObserver = new ResizeObserver(_checkTabOverflow);
tabBarResizeObserver.observe(_$tabsOverflow[0]);
// Restore maximize state from preferences (survives reload).
_isMaximized = PreferencesManager.getViewState(PREF_BOTTOM_PANEL_MAXIMIZED) === true;
// When the container expands, re-apply maximize if the preference
// says we were maximized (covers both minimize→show and reload).
_$container.on("panelExpanded", function () {
if (_isMaximized) {
// Defer to let all synchronous panelExpanded handlers
// (including WorkspaceManager's recomputeLayout) finish first.
setTimeout(function () {
let maxHeight = (_$editorHolder ? _$editorHolder.height() : 0) +
_$container.height();
_$container.height(maxHeight);
_updateMaximizeButton();
if (_recomputeLayout) {
_recomputeLayout();
}
}, 0);
}
});
}
/**
* Toggle maximize/restore of the bottom panel.
* @private
*/
function _toggleMaximize() {
if (!_$container || !_$container.is(":visible")) {
return;
}
if (_isMaximized) {
_restorePanel();
} else {
_maximizePanel();
}
}
/**
* Maximize the bottom panel to fill all available vertical space.
* @private
*/
function _maximizePanel() {
_preMaximizeHeight = _$container.height();
let maxHeight = _$editorHolder.height() + _$container.height();
_$container.height(maxHeight);
_isMaximized = true;
PreferencesManager.setViewState(PREF_BOTTOM_PANEL_MAXIMIZED, true);
_updateMaximizeButton();
if (_recomputeLayout) {
_recomputeLayout();
}
}
/**
* Compute a sensible panel height for restore when the saved height is
* missing or indistinguishable from the maximized height.
* Returns roughly one-third of the total available space, floored at
* MIN_PANEL_HEIGHT so the panel never restores too small.
* @return {number}
* @private
*/
function _getDefaultRestoreHeight() {
let totalHeight = (_$editorHolder ? _$editorHolder.height() : 0) +
(_$container ? _$container.height() : 0);
return Math.max(MIN_PANEL_HEIGHT, Math.round(totalHeight / 3));
}
/**
* Return true if the given height is effectively the same as the
* maximized height (within MAXIMIZE_THRESHOLD).
* @param {number} height
* @return {boolean}
* @private
*/
function _isNearMaxHeight(height) {
let totalHeight = (_$editorHolder ? _$editorHolder.height() : 0) +
(_$container ? _$container.height() : 0);
return (totalHeight - height) <= MAXIMIZE_THRESHOLD;
}
/**
* Restore the bottom panel to its pre-maximize height.
* If the saved height is missing (e.g. maximize was triggered by
* drag-to-max) or was essentially the same as the maximized height,
* a sensible default (≈ 1/3 of available space) is used instead so
* that the restore feels like a visible change.
* @private
*/
function _restorePanel() {
let restoreHeight;
if (_preMaximizeHeight !== null && !_isNearMaxHeight(_preMaximizeHeight)) {
restoreHeight = _preMaximizeHeight;
} else {
restoreHeight = _getDefaultRestoreHeight();
}
_$container.height(restoreHeight);
_isMaximized = false;
_preMaximizeHeight = null;
PreferencesManager.setViewState(PREF_BOTTOM_PANEL_MAXIMIZED, false);
_updateMaximizeButton();
if (_recomputeLayout) {
_recomputeLayout();
}
}
/**
* Update the maximize button icon and tooltip based on current state.
* @private
*/
function _updateMaximizeButton() {
if (!_$tabBar) {
return;
}
let $btn = _$tabBar.find(".bottom-panel-maximize-btn");
let $icon = $btn.find("i");
if (_isMaximized) {
$icon.removeClass("fa-regular fa-square")
.addClass("fa-regular fa-window-restore");
$btn.attr("title", Strings.BOTTOM_PANEL_RESTORE);
} else {
$icon.removeClass("fa-regular fa-window-restore")
.addClass("fa-regular fa-square");
$btn.attr("title", Strings.BOTTOM_PANEL_MAXIMIZE);
}
}
/**
* Exit maximize state without resizing (for external callers like drag-resize).
* Clears internal maximize state and resets the button icon.
*/
function exitMaximizeOnResize() {
if (!_isMaximized) {
return;
}
_isMaximized = false;
_preMaximizeHeight = null;
PreferencesManager.setViewState(PREF_BOTTOM_PANEL_MAXIMIZED, false);
_updateMaximizeButton();
}
/**
* Enter maximize state during a drag-resize that reaches the maximum
* height. No pre-maximize height is stored because the user arrived