diff --git a/src/command/DefaultMenus.js b/src/command/DefaultMenus.js index 17f66609b2b..c449437b78a 100644 --- a/src/command/DefaultMenus.js +++ b/src/command/DefaultMenus.js @@ -30,7 +30,37 @@ define(function (require, exports, module) { var AppInit = require("utils/AppInit"), Commands = require("command/Commands"), Menus = require("command/Menus"), - Strings = require("strings"); + Strings = require("strings"), + MainViewManager = require("view/MainViewManager"), + CommandManager = require("command/CommandManager"); + + /** + * Disables menu items present in items if enabled is true. + * enabled is true if file is saved and present on user system. + * @param {boolean} enabled + * @param {array} items + */ + function _setContextMenuItemsVisible(enabled, items) { + items.forEach(function (item) { + CommandManager.get(item).setEnabled(enabled); + }); + } + + /** + * Checks if file saved and present on system and + * disables menu items accordingly + */ + function _setMenuItemsVisible() { + var file = MainViewManager.getCurrentlyViewedFile(MainViewManager.ACTIVE_PANE); + if (file) { + file.exists(function (err, isPresent) { + if (err) { + return err; + } + _setContextMenuItemsVisible(isPresent, [Commands.FILE_RENAME, Commands.NAVIGATE_SHOW_IN_FILE_TREE, Commands.NAVIGATE_SHOW_IN_OS]); + }); + } + } AppInit.htmlReady(function () { /* @@ -340,5 +370,9 @@ define(function (require, exports, module) { $(this).addClass("open"); } }); + // Check the visibility of context menu items before opening the context menu. + // 'Rename', 'Show in file tree' and 'Show in explorer' items will be disabled for files that have not yet been saved to disk. + Menus.getContextMenu(Menus.ContextMenuIds.WORKING_SET_CONTEXT_MENU).on("beforeContextMenuOpen", _setMenuItemsVisible); + Menus.getContextMenu(Menus.ContextMenuIds.PROJECT_MENU).on("beforeContextMenuOpen", _setMenuItemsVisible); }); });