11/*
22 * This file manages the more options context menu.
3- * The more option button is present at the right side of the tab bar.
4- * When clicked, it will show the more options context menu.
5- * which will have various options related to the tab bar
3+ * The more options context menu is shown when a tab is right-clicked
64 */
75define ( function ( require , exports , module ) {
86 const DropdownButton = require ( "widgets/DropdownButton" ) ;
97 const Strings = require ( "strings" ) ;
10- const MainViewManager = require ( "view/MainViewManager" ) ;
118 const CommandManager = require ( "command/CommandManager" ) ;
129 const Commands = require ( "command/Commands" ) ;
10+ const FileSystem = require ( "filesystem/FileSystem" ) ;
1311
14- const Global = require ( "./global" ) ;
15- const Helper = require ( "./helper" ) ;
1612
1713 // List of items to show in the context menu
1814 // Strings defined in `src/nls/root/strings.js`
1915 const items = [
16+ Strings . CLOSE_TAB ,
17+ Strings . CLOSE_ACTIVE_TAB ,
2018 Strings . CLOSE_ALL_TABS ,
2119 Strings . CLOSE_UNMODIFIED_TABS ,
20+ "---" ,
2221 Strings . REOPEN_CLOSED_FILE
2322 ] ;
2423
2524
2625 /**
27- * This function is called when the close all tabs option is selected from the context menu
26+ * "CLOSE TAB"
27+ * this function handles the closing of the tab that was right-clicked
28+ *
29+ * @param {String } filePath - path of the file to close
30+ * @param {String } paneId - the id of the pane in which the file is present
31+ */
32+ function handleCloseTab ( filePath , paneId ) {
33+ if ( filePath ) {
34+ // Get the file object using FileSystem
35+ const fileObj = FileSystem . getFileForPath ( filePath ) ;
36+
37+ // Execute close command with file object and pane ID
38+ CommandManager . execute (
39+ Commands . FILE_CLOSE ,
40+ { file : fileObj , paneId : paneId }
41+ ) ;
42+ }
43+ }
44+
45+
46+ /**
47+ * "CLOSE ACTIVE TAB"
48+ * this closes the currently active tab
49+ * doesn't matter if the context menu is opened from this tab or some other tab
50+ */
51+ function handleCloseActiveTab ( ) {
52+ // This simply executes the FILE_CLOSE command without parameters
53+ // which will close the currently active file
54+ CommandManager . execute ( Commands . FILE_CLOSE ) ;
55+ }
56+
57+
58+ /**
59+ * "CLOSE ALL TABS"
2860 * This will close all tabs no matter whether they are in first pane or second pane
2961 */
3062 function handleCloseAllTabs ( ) {
3163 CommandManager . execute ( Commands . FILE_CLOSE_ALL ) ;
3264 }
3365
66+
3467 /**
35- * Called when the close unmodified tabs option is selected from the context menu
68+ * "CLOSE UNMODIFIED TABS"
3669 * This will close all tabs that are not modified
3770 * TODO: implement the functionality
3871 */
3972 function handleCloseUnmodifiedTabs ( ) {
40-
4173 // pass
4274 }
4375
76+
4477 /**
45- * Called when the reopen closed file option is selected from the context menu
78+ * "REOPEN CLOSED FILE"
4679 * This just calls the reopen closed file command. everthing else is handled there
80+ * TODO: disable the command if there are no closed files, look into the file menu
4781 */
4882 function reopenClosedFile ( ) {
4983 CommandManager . execute ( Commands . FILE_REOPEN_CLOSED ) ;
@@ -57,8 +91,9 @@ define(function (require, exports, module) {
5791 * @param {String } paneId - the id of the pane ["first-pane", "second-pane"]
5892 * @param {Number } x - the x coordinate for positioning the menu
5993 * @param {Number } y - the y coordinate for positioning the menu
94+ * @param {String } filePath - [optional] the path of the file that was right-clicked
6095 */
61- function showMoreOptionsContextMenu ( paneId , x , y ) {
96+ function showMoreOptionsContextMenu ( paneId , x , y , filePath ) {
6297 const dropdown = new DropdownButton . DropdownButton ( "" , items ) ;
6398
6499 // Append to document body for absolute positioning
@@ -76,13 +111,7 @@ define(function (require, exports, module) {
76111
77112 // handle the option selection
78113 dropdown . on ( "select" , function ( e , item , index ) {
79- if ( index === 0 ) {
80- handleCloseAllTabs ( ) ;
81- } else if ( index === 1 ) {
82- handleCloseUnmodifiedTabs ( ) ;
83- } else if ( index === 2 ) {
84- reopenClosedFile ( ) ;
85- }
114+ _handleSelection ( index , filePath , paneId ) ;
86115 } ) ;
87116
88117 // Remove the button after the dropdown is hidden
@@ -91,6 +120,38 @@ define(function (require, exports, module) {
91120 } ) ;
92121 }
93122
123+ /**
124+ * Handles the selection of an option in the more options context menu
125+ *
126+ * @param {Number } index - the index of the selected option
127+ * @param {String } filePath - the path of the file that was right-clicked
128+ * @param {String } paneId - the id of the pane ["first-pane", "second-pane"]
129+ */
130+ function _handleSelection ( index , filePath , paneId ) {
131+ switch ( index ) {
132+ case 0 :
133+ // Close tab (the one that was right-clicked)
134+ handleCloseTab ( filePath , paneId ) ;
135+ break ;
136+ case 1 :
137+ // Close active tab
138+ handleCloseActiveTab ( ) ;
139+ break ;
140+ case 2 :
141+ // Close all tabs
142+ handleCloseAllTabs ( ) ;
143+ break ;
144+ case 3 :
145+ // Close unmodified tabs
146+ handleCloseUnmodifiedTabs ( ) ;
147+ break ;
148+ case 5 :
149+ // Reopen closed file
150+ reopenClosedFile ( ) ;
151+ break ;
152+ }
153+ }
154+
94155 module . exports = {
95156 showMoreOptionsContextMenu
96157 } ;
0 commit comments