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

Commit

Permalink
Disabled context menu items for unsaved files (#12806)
Browse files Browse the repository at this point in the history
* disabled context menu items for unsaved files

* Added JSDocs for the new functions added
  • Loading branch information
mansimarkaur authored and petetnt committed Mar 1, 2017
1 parent 885b97a commit f4f40d8
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/command/DefaultMenus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
/*
Expand Down Expand Up @@ -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);
});
});

0 comments on commit f4f40d8

Please sign in to comment.