Skip to content

Commit 8de275f

Browse files
devvaannshabose
authored andcommitted
fix: clicking twice on overflow button doesn't hide the dropdown
1 parent b26d243 commit 8de275f

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

src/extensionsIntegrated/TabBar/overflow.js

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ define(function (require, exports, module) {
3535
const EditorManager = require("editor/EditorManager");
3636
const FileSystem = require("filesystem/FileSystem");
3737

38+
// holds the dropdown instance
39+
let $dropdown = null;
3840

3941
/**
4042
* This function determines which tabs are hidden in the tab bar due to overflow
@@ -130,15 +132,12 @@ define(function (require, exports, module) {
130132
function showOverflowMenu(paneId, x, y) {
131133
const hiddenTabs = _getListOfHiddenTabs(paneId);
132134

133-
// first, remove any existing dropdown menus to prevent duplicates
134-
$(".dropdown-overflow-menu").remove();
135-
136135
// Create a map to track tabs that are being closed
137136
// Using paths as keys for quick lookup
138137
const closingTabPaths = {};
139138

140139
// create the dropdown
141-
const dropdown = new DropdownButton.DropdownButton("", hiddenTabs, function (item, index) {
140+
$dropdown = new DropdownButton.DropdownButton("", hiddenTabs, function (item, index) {
142141
const iconHtml = item.$icon[0].outerHTML; // the file icon
143142
const dirtyHtml = item.isDirty
144143
? '<span class="tab-dirty-icon-overflow">•</span>'
@@ -169,15 +168,14 @@ define(function (require, exports, module) {
169168
};
170169
});
171170

172-
// add the custom classes for styling the dropdown
173-
dropdown.dropdownExtraClasses = "dropdown-overflow-menu";
174-
dropdown.$button.addClass("btn-overflow-tabs");
171+
// add custom class to separate overflow dropdown from regular ones
172+
$dropdown.dropdownExtraClasses = "dropdown-overflow-menu";
175173

176174
// appending to document body. we'll position this with absolute positioning
177-
$("body").append(dropdown.$button);
175+
$("body").append($dropdown.$button);
178176

179177
// position the dropdown where the user clicked
180-
dropdown.$button.css({
178+
$dropdown.$button.css({
181179
position: "absolute",
182180
left: x + "px",
183181
top: y + "px",
@@ -203,10 +201,10 @@ define(function (require, exports, module) {
203201
e.preventDefault();
204202
});
205203

206-
dropdown.showDropdown();
204+
$dropdown.showDropdown();
207205

208206
// handle the option selection
209-
dropdown.on("select", function (e, item, index) {
207+
$dropdown.on("select", function (e, item, index) {
210208
// check if this tab was marked for closing
211209
if (closingTabPaths[item.path]) {
212210
// this tab is being closed, so handle the close operation
@@ -240,15 +238,9 @@ define(function (require, exports, module) {
240238
}
241239
});
242240

243-
// clean up when the dropdown is closed
244-
dropdown.$button.on("dropdown-closed", function () {
245-
$(document).off("mousedown", ".tab-close-icon-overflow");
246-
dropdown.$button.remove();
247-
});
248-
249241
// a button was getting displayed on the screen wherever a click was made. not sure why
250242
// but this fixes it
251-
dropdown.$button.css({
243+
$dropdown.$button.css({
252244
display: "none"
253245
});
254246
}
@@ -335,29 +327,52 @@ define(function (require, exports, module) {
335327
}
336328
}
337329

330+
/**
331+
* to close the overflow button's dropdown
332+
*/
333+
function _closeDropdown() {
334+
if ($dropdown) {
335+
if ($dropdown.$button) {
336+
$dropdown.$button.remove();
337+
}
338+
$dropdown = null;
339+
}
340+
}
341+
342+
/**
343+
* this function gets called when the overflow button gets clicked
344+
* it shows/closes the dropdown as required
345+
* @param {Event} e - the event instance
346+
* @param {String} paneId - the pane id "first-pane" or "second-pane"
347+
*/
348+
function _handleOverflowButtonClick(e, paneId) {
349+
e.stopPropagation();
350+
$dropdown ? _closeDropdown() : showOverflowMenu(paneId, e.pageX, e.pageY);
351+
}
338352

339353
/**
340-
* To setup the handlers for the overflow menu
354+
* initialize the handling of the overflow buttons
355+
* this also registers the event handlers
341356
*/
342-
function setupOverflowHandlers() {
357+
function init() {
358+
// when clicked anywhere on the page we want to close the dropdown
359+
// except the overflow-buttons
360+
$("html").on("click", function (e) {
361+
if ($(e.target).closest("#overflow-button, #overflow-button-2").length) { return; }
362+
_closeDropdown();
363+
});
364+
343365
// handle when the overflow button is clicked for the first pane
344366
$(document).on("click", "#overflow-button", function (e) {
345-
e.stopPropagation();
346-
showOverflowMenu("first-pane", e.pageX, e.pageY);
367+
_handleOverflowButtonClick(e, "first-pane");
347368
});
348369

349370
// for second pane
350371
$(document).on("click", "#overflow-button-2", function (e) {
351-
e.stopPropagation();
352-
showOverflowMenu("second-pane", e.pageX, e.pageY);
372+
_handleOverflowButtonClick(e, "second-pane");
353373
});
354374
}
355375

356-
// initialize the handling of the overflow buttons
357-
function init() {
358-
setupOverflowHandlers();
359-
}
360-
361376
module.exports = {
362377
init,
363378
toggleOverflowVisibility,

0 commit comments

Comments
 (0)