@@ -30,7 +30,37 @@ define(function (require, exports, module) {
3030 var AppInit = require ( "utils/AppInit" ) ,
3131 Commands = require ( "command/Commands" ) ,
3232 Menus = require ( "command/Menus" ) ,
33- Strings = require ( "strings" ) ;
33+ Strings = require ( "strings" ) ,
34+ MainViewManager = require ( "view/MainViewManager" ) ,
35+ CommandManager = require ( "command/CommandManager" ) ;
36+
37+ /**
38+ * Disables menu items present in items if enabled is true.
39+ * enabled is true if file is saved and present on user system.
40+ * @param {boolean } enabled
41+ * @param {array } items
42+ */
43+ function _setContextMenuItemsVisible ( enabled , items ) {
44+ items . forEach ( function ( item ) {
45+ CommandManager . get ( item ) . setEnabled ( enabled ) ;
46+ } ) ;
47+ }
48+
49+ /**
50+ * Checks if file saved and present on system and
51+ * disables menu items accordingly
52+ */
53+ function _setMenuItemsVisible ( ) {
54+ var file = MainViewManager . getCurrentlyViewedFile ( MainViewManager . ACTIVE_PANE ) ;
55+ if ( file ) {
56+ file . exists ( function ( err , isPresent ) {
57+ if ( err ) {
58+ return err ;
59+ }
60+ _setContextMenuItemsVisible ( isPresent , [ Commands . FILE_RENAME , Commands . NAVIGATE_SHOW_IN_FILE_TREE , Commands . NAVIGATE_SHOW_IN_OS ] ) ;
61+ } ) ;
62+ }
63+ }
3464
3565 AppInit . htmlReady ( function ( ) {
3666 /*
@@ -340,5 +370,9 @@ define(function (require, exports, module) {
340370 $ ( this ) . addClass ( "open" ) ;
341371 }
342372 } ) ;
373+ // Check the visibility of context menu items before opening the context menu.
374+ // 'Rename', 'Show in file tree' and 'Show in explorer' items will be disabled for files that have not yet been saved to disk.
375+ Menus . getContextMenu ( Menus . ContextMenuIds . WORKING_SET_CONTEXT_MENU ) . on ( "beforeContextMenuOpen" , _setMenuItemsVisible ) ;
376+ Menus . getContextMenu ( Menus . ContextMenuIds . PROJECT_MENU ) . on ( "beforeContextMenuOpen" , _setMenuItemsVisible ) ;
343377 } ) ;
344378} ) ;
0 commit comments