Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 78814b4

Browse files
committed
Merge pull request #5217 from lkcampbell/fix-issue-5210
Fix issue #5210: Adding Menus.removeMenu() and Menus.getAllMenus()
2 parents a58cb63 + 316fc60 commit 78814b4

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

src/command/Menus.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ define(function (require, exports, module) {
154154
function getMenu(id) {
155155
return menuMap[id];
156156
}
157+
158+
/**
159+
* Retrieves the map of all Menu objects.
160+
* @return {Object.<string, Menu>}
161+
*/
162+
function getAllMenus() {
163+
return menuMap;
164+
}
157165

158166
/**
159167
* Retrieves the ContextMenu object for the corresponding id.
@@ -861,6 +869,36 @@ define(function (require, exports, module) {
861869
return menu;
862870
}
863871

872+
/**
873+
* Removes a top-level menu from the application menu bar which may be native or HTML-based.
874+
*
875+
* @param {!string} id - unique identifier for a menu.
876+
* Core Menus in Brackets use a simple title as an id, for example "file-menu".
877+
* Extensions should use the following format: "author.myextension.mymenuname".
878+
*/
879+
function removeMenu(id) {
880+
if (!id) {
881+
console.error("removeMenu(): missing required parameter: id");
882+
return;
883+
}
884+
885+
if (!menuMap[id]) {
886+
console.error("removeMenu(): menu id not found: %s", id);
887+
return;
888+
}
889+
890+
if (_isHTMLMenu(id)) {
891+
$(_getHTMLMenu(id)).remove();
892+
} else {
893+
brackets.app.removeMenu(id, function (err) {
894+
if (err) {
895+
console.error("removeMenu() -- id not found: " + id + " (error: " + err + ")");
896+
}
897+
});
898+
}
899+
900+
delete menuMap[id];
901+
}
864902

865903
/**
866904
* @constructor
@@ -1007,13 +1045,6 @@ define(function (require, exports, module) {
10071045
return cmenu;
10081046
}
10091047

1010-
/** NOT IMPLEMENTED
1011-
* Removes Menu
1012-
*/
1013-
// function removeMenu(id) {
1014-
// NOT IMPLEMENTED
1015-
// }
1016-
10171048
// Define public API
10181049
exports.AppMenuBar = AppMenuBar;
10191050
exports.ContextMenuIds = ContextMenuIds;
@@ -1026,9 +1057,11 @@ define(function (require, exports, module) {
10261057
exports.LAST_IN_SECTION = LAST_IN_SECTION;
10271058
exports.DIVIDER = DIVIDER;
10281059
exports.getMenu = getMenu;
1060+
exports.getAllMenus = getAllMenus;
10291061
exports.getMenuItem = getMenuItem;
10301062
exports.getContextMenu = getContextMenu;
10311063
exports.addMenu = addMenu;
1064+
exports.removeMenu = removeMenu;
10321065
exports.registerContextMenu = registerContextMenu;
10331066
exports.closeAll = closeAll;
10341067
exports.Menu = Menu;

test/spec/Menu-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,45 @@ define(function (require, exports, module) {
619619
});
620620

621621

622+
describe("Remove Menu", function () {
623+
624+
function menuDOM(menuId) {
625+
return testWindow.$("#" + menuId);
626+
}
627+
628+
it("should add then remove new menu to menu bar with a menu id", function () {
629+
runs(function () {
630+
var menuId = "Menu-test";
631+
Menus.addMenu("Custom", menuId);
632+
var $menu = menuDOM(menuId);
633+
expect($menu.length).toBe(1);
634+
635+
Menus.removeMenu(menuId);
636+
$menu = menuDOM(menuId);
637+
expect($menu.length).toBe(0);
638+
});
639+
});
640+
641+
it("should gracefully handle someone trying to remove a menu that doesn't exist", function () {
642+
runs(function () {
643+
var menuId = "Menu-test";
644+
645+
Menus.removeMenu(menuId);
646+
expect(Menus).toBeTruthy(); // Verify that we got this far...
647+
});
648+
});
649+
650+
it("should gracefully handle someone trying to remove a menu without supply the id", function () {
651+
runs(function () {
652+
var menuId = "Menu-test";
653+
654+
Menus.removeMenu();
655+
expect(Menus).toBeTruthy(); // Verify that we got this far...
656+
});
657+
});
658+
});
659+
660+
622661
describe("Menu Item synchronizing", function () {
623662

624663
it("should have same state as command", function () {

0 commit comments

Comments
 (0)