@@ -89,6 +89,9 @@ define(function (require, exports, module) {
89
89
// live preview mode pref
90
90
const PREFERENCE_LIVE_PREVIEW_MODE = "livePreviewMode" ;
91
91
92
+ // holds the dropdown instance
93
+ let $dropdown = null ;
94
+
92
95
/**
93
96
* Get the appropriate default mode based on whether edit features are active
94
97
* @returns {string } "highlight" if edit features inactive, "edit" if active
@@ -309,7 +312,7 @@ define(function (require, exports, module) {
309
312
// this is to take care of invalid values in the pref file
310
313
const currentMode = [ "preview" , "highlight" , "edit" ] . includes ( rawMode ) ? rawMode : _getDefaultMode ( ) ;
311
314
312
- const dropdown = new DropdownButton . DropdownButton ( "" , items , function ( item , index ) {
315
+ $ dropdown = new DropdownButton . DropdownButton ( "" , items , function ( item , index ) {
313
316
if ( item === Strings . LIVE_PREVIEW_MODE_PREVIEW ) {
314
317
// using empty spaces to keep content aligned
315
318
return currentMode === "preview" ? `✓ ${ item } ` : `${ '\u00A0' . repeat ( 4 ) } ${ item } ` ;
@@ -333,25 +336,25 @@ define(function (require, exports, module) {
333
336
} ) ;
334
337
335
338
// Append to document body for absolute positioning
336
- $ ( "body" ) . append ( dropdown . $button ) ;
339
+ $ ( "body" ) . append ( $ dropdown. $button ) ;
337
340
338
341
// Position the dropdown at the mouse coordinates
339
- dropdown . $button . css ( {
342
+ $ dropdown. $button . css ( {
340
343
position : "absolute" ,
341
344
left : event . pageX + "px" ,
342
345
top : event . pageY + "px" ,
343
346
zIndex : 1000
344
347
} ) ;
345
348
346
349
// Add a custom class to override the max-height
347
- dropdown . dropdownExtraClasses = "mode-context-menu" ;
350
+ $ dropdown. dropdownExtraClasses = "mode-context-menu" ;
348
351
349
- dropdown . showDropdown ( ) ;
352
+ $ dropdown. showDropdown ( ) ;
350
353
351
354
$ ( ".mode-context-menu" ) . css ( "max-height" , "300px" ) ;
352
355
353
356
// handle the option selection
354
- dropdown . on ( "select" , function ( e , item , index ) {
357
+ $ dropdown. on ( "select" , function ( e , item , index ) {
355
358
// here we just set the preference
356
359
// as the preferences listener will automatically handle the required changes
357
360
if ( index === 0 ) {
@@ -384,11 +387,28 @@ define(function (require, exports, module) {
384
387
} ) ;
385
388
386
389
// Remove the button after the dropdown is hidden
387
- dropdown . $button . css ( {
390
+ $ dropdown. $button . css ( {
388
391
display : "none"
389
392
} ) ;
390
393
}
391
394
395
+ /**
396
+ * to close the overflow button's dropdown
397
+ */
398
+ function _closeDropdown ( ) {
399
+ if ( $dropdown ) {
400
+ if ( $dropdown . $button ) {
401
+ $dropdown . $button . remove ( ) ;
402
+ }
403
+ $dropdown = null ;
404
+ }
405
+ }
406
+
407
+ function _handleLPModeBtnClick ( e ) {
408
+ e . stopPropagation ( ) ;
409
+ $dropdown ? _closeDropdown ( ) : _showModeSelectionDropdown ( e ) ;
410
+ }
411
+
392
412
function _getTrustProjectPage ( ) {
393
413
const trustProjectMessage = StringUtils . format ( Strings . TRUST_PROJECT ,
394
414
path . basename ( ProjectManager . getProjectRoot ( ) . fullPath ) ) ;
@@ -692,7 +712,7 @@ define(function (require, exports, module) {
692
712
_popoutLivePreview ( "firefox" ) ;
693
713
} ) ;
694
714
695
- $modeBtn . on ( "click" , _showModeSelectionDropdown ) ;
715
+ $modeBtn . on ( "click" , _handleLPModeBtnClick ) ;
696
716
$previewBtn . on ( "click" , _handlePreviewBtnClick ) ;
697
717
698
718
_showOpenBrowserIcons ( ) ;
@@ -1046,6 +1066,18 @@ define(function (require, exports, module) {
1046
1066
} ) ;
1047
1067
}
1048
1068
1069
+ function _registerHandlers ( ) {
1070
+ // when clicked anywhere on the page we want to close the dropdown
1071
+ $ ( "html" ) . on ( "click" , function ( e ) {
1072
+ if ( $ ( e . target ) . closest ( "#livePreviewModeBtn" ) . length ) { return ; }
1073
+ _closeDropdown ( ) ;
1074
+ } ) ;
1075
+
1076
+ $ ( document ) . on ( "click" , "#livePreviewModeBtn" , function ( e ) {
1077
+ _handleLPModeBtnClick ( e ) ;
1078
+ } ) ;
1079
+ }
1080
+
1049
1081
AppInit . appReady ( function ( ) {
1050
1082
if ( Phoenix . isSpecRunnerWindow ) {
1051
1083
return ;
@@ -1088,6 +1120,7 @@ define(function (require, exports, module) {
1088
1120
Menus . AFTER , Commands . FILE_LIVE_FILE_PREVIEW ) ;
1089
1121
fileMenu . addMenuDivider ( Menus . BEFORE , Commands . FILE_LIVE_FILE_PREVIEW ) ;
1090
1122
1123
+ _registerHandlers ( ) ;
1091
1124
// init live preview mode from saved preferences
1092
1125
_initializeMode ( ) ;
1093
1126
// listen for pref changes
0 commit comments