@@ -41,15 +41,19 @@ define(function (require, exports, module) {
4141 closeBelow = "file.close_below" ;
4242
4343 // Global vars and preferences
44- var commandsRegistered = false ,
45- menuEntriesShown = { } ,
46- prefs = PreferencesManager . getExtensionPrefs ( "closeOthers" ) ;
47- prefs . definePreference ( "below" , "boolean" , true ) ;
44+ var prefs = PreferencesManager . getExtensionPrefs ( "closeOthers" ) ,
45+ menuEntriesShown = { } ;
46+
47+ prefs . definePreference ( "below" , "boolean" , true ) ;
4848 prefs . definePreference ( "others" , "boolean" , true ) ;
49- prefs . definePreference ( "above" , "boolean" , true ) ;
50-
49+ prefs . definePreference ( "above" , "boolean" , true ) ;
50+
51+
52+ /**
53+ * Handle the different Close Other commands
54+ * @param {string } mode
55+ */
5156 function handleClose ( mode ) {
52-
5357 var targetIndex = DocumentManager . findInWorkingSet ( DocumentManager . getCurrentDocument ( ) . file . fullPath ) ,
5458 workingSet = DocumentManager . getWorkingSet ( ) . slice ( 0 ) ,
5559 start = ( mode === closeBelow ) ? ( targetIndex + 1 ) : 0 ,
@@ -68,8 +72,11 @@ define(function (require, exports, module) {
6872
6973 CommandManager . execute ( Commands . FILE_CLOSE_LIST , { fileList : files } ) ;
7074 }
71-
72- function _contextMenuOpenHandler ( ) {
75+
76+ /**
77+ * Enable/Disable the menu items depending on which document is selected in the working set
78+ */
79+ function contextMenuOpenHandler ( ) {
7380 var doc = DocumentManager . getCurrentDocument ( ) ;
7481
7582 if ( doc ) {
@@ -95,56 +102,89 @@ define(function (require, exports, module) {
95102 }
96103 }
97104 }
98-
99- function prefChangeHandler ( ) {
100- // it's senseless to look prefs up for the current file, instead look them up for
105+
106+
107+ /**
108+ * Returns the preferences used to add/remove the menu items
109+ * @return {{closeBelow: boolean, closeOthers: boolean, closeAbove: boolean} }
110+ */
111+ function getPreferences ( ) {
112+ // It's senseless to look prefs up for the current file, instead look them up for
101113 // the current project (or globally)
102- var prefCloseBelow = prefs . get ( "below" , PreferencesManager . CURRENT_PROJECT ) ,
103- prefCloseOthers = prefs . get ( "others" , PreferencesManager . CURRENT_PROJECT ) ,
104- prefCloseAbove = prefs . get ( "above" , PreferencesManager . CURRENT_PROJECT ) ;
105-
106- if ( ! commandsRegistered && ( prefCloseBelow || prefCloseOthers || prefCloseAbove ) ) {
107- CommandManager . register ( Strings . CMD_FILE_CLOSE_BELOW , closeBelow , function ( ) {
108- handleClose ( closeBelow ) ;
109- } ) ;
110- CommandManager . register ( Strings . CMD_FILE_CLOSE_OTHERS , closeOthers , function ( ) {
111- handleClose ( closeOthers ) ;
112- } ) ;
113- CommandManager . register ( Strings . CMD_FILE_CLOSE_ABOVE , closeAbove , function ( ) {
114- handleClose ( closeAbove ) ;
115- } ) ;
116- commandsRegistered = true ;
117- }
114+ return {
115+ closeBelow : prefs . get ( "below" , PreferencesManager . CURRENT_PROJECT ) ,
116+ closeOthers : prefs . get ( "others" , PreferencesManager . CURRENT_PROJECT ) ,
117+ closeAbove : prefs . get ( "above" , PreferencesManager . CURRENT_PROJECT )
118+ } ;
119+ }
120+
121+ /**
122+ * When the preferences changed, add/remove the required menu items
123+ */
124+ function prefChangeHandler ( ) {
125+ var prefs = getPreferences ( ) ;
118126
119- if ( prefCloseBelow !== menuEntriesShown . closeBelow ) {
120- if ( prefCloseBelow ) {
127+ if ( prefs . closeBelow !== menuEntriesShown . closeBelow ) {
128+ if ( prefs . closeBelow ) {
121129 workingSetCmenu . addMenuItem ( closeBelow , "" , Menus . AFTER , Commands . FILE_CLOSE ) ;
122130 } else {
123131 workingSetCmenu . removeMenuItem ( closeBelow ) ;
124132 }
125133 }
126- if ( prefCloseOthers !== menuEntriesShown . closeOthers ) {
127- if ( prefCloseOthers ) {
134+
135+ if ( prefs . closeOthers !== menuEntriesShown . closeOthers ) {
136+ if ( prefs . closeOthers ) {
128137 workingSetCmenu . addMenuItem ( closeOthers , "" , Menus . AFTER , Commands . FILE_CLOSE ) ;
129138 } else {
130139 workingSetCmenu . removeMenuItem ( closeOthers ) ;
131140 }
132141 }
133- if ( prefCloseAbove !== menuEntriesShown . closeAbove ) {
134- if ( prefCloseAbove ) {
142+
143+ if ( prefs . closeAbove !== menuEntriesShown . closeAbove ) {
144+ if ( prefs . closeAbove ) {
135145 workingSetCmenu . addMenuItem ( closeAbove , "" , Menus . AFTER , Commands . FILE_CLOSE ) ;
136146 } else {
137147 workingSetCmenu . removeMenuItem ( closeAbove ) ;
138148 }
139149 }
140- menuEntriesShown = { "closeBelow" : prefCloseBelow , "closeOthers" : prefCloseOthers , "closeAbove" : prefCloseAbove } ;
150+
151+ menuEntriesShown = prefs ;
152+ }
153+
154+ /**
155+ * Register the Commands and add the Menu Items, if required
156+ */
157+ function initializeCommands ( ) {
158+ var prefs = getPreferences ( ) ;
159+
160+ CommandManager . register ( Strings . CMD_FILE_CLOSE_BELOW , closeBelow , function ( ) {
161+ handleClose ( closeBelow ) ;
162+ } ) ;
163+ CommandManager . register ( Strings . CMD_FILE_CLOSE_OTHERS , closeOthers , function ( ) {
164+ handleClose ( closeOthers ) ;
165+ } ) ;
166+ CommandManager . register ( Strings . CMD_FILE_CLOSE_ABOVE , closeAbove , function ( ) {
167+ handleClose ( closeAbove ) ;
168+ } ) ;
169+
170+ if ( prefs . closeBelow ) {
171+ workingSetCmenu . addMenuItem ( closeBelow , "" , Menus . AFTER , Commands . FILE_CLOSE ) ;
172+ }
173+ if ( prefs . closeOthers ) {
174+ workingSetCmenu . addMenuItem ( closeOthers , "" , Menus . AFTER , Commands . FILE_CLOSE ) ;
175+ }
176+ if ( prefs . closeAbove ) {
177+ workingSetCmenu . addMenuItem ( closeAbove , "" , Menus . AFTER , Commands . FILE_CLOSE ) ;
178+ }
179+ menuEntriesShown = prefs ;
141180 }
142181
182+
143183 // Initialize using the prefs
144- prefChangeHandler ( ) ;
184+ initializeCommands ( ) ;
145185
146186 // Add a context menu open handler
147- $ ( workingSetCmenu ) . on ( "beforeContextMenuOpen" , _contextMenuOpenHandler ) ;
187+ $ ( workingSetCmenu ) . on ( "beforeContextMenuOpen" , contextMenuOpenHandler ) ;
148188
149189 prefs . on ( "change" , prefChangeHandler ) ;
150190} ) ;
0 commit comments