Skip to content

Commit 01f1e3f

Browse files
committed
Submenu: WIP (#151)
1 parent 60d457a commit 01f1e3f

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

lib/document/BaseMenu.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ function BaseMenu( options = {} ) {
6262

6363
// Submenu
6464
this.hasSubmenu = !! options.submenu ;
65+
this.isSubmenu = !! options.isSubmenu ;
6566
this.submenu = null ; // A child (column) menu
67+
this.submenuParentButton = null ; // The button that opened the submenu
6668
this.submenuOptions = null ;
6769

6870
if ( this.hasSubmenu ) {
@@ -324,8 +326,18 @@ BaseMenu.prototype.onButtonSubmit = function( buttonValue , action , button ) {
324326
this.nextPage() ;
325327
break ;
326328
default :
327-
if ( this.hasSubmenu && this.submenuOptions.openOn === 'submit' ) { this.openSubmenu( button.value , button ) ; }
328-
this.emit( 'submit' , buttonValue , action , this ) ;
329+
if ( this.hasSubmenu && button.def.items ) {
330+
if ( this.submenuOptions.openOn === 'parentSubmit' ) {
331+
this.openSubmenu( button.value , button ) ;
332+
}
333+
334+
if ( this.submenu ) {
335+
this.document.giveFocusTo( this.submenu ) ;
336+
}
337+
}
338+
else {
339+
this.emit( 'submit' , buttonValue , action , this ) ;
340+
}
329341
}
330342
} ;
331343

@@ -337,8 +349,14 @@ BaseMenu.prototype.onButtonBlinked = function( buttonValue , action , button ) {
337349
case 'nextPage' :
338350
break ;
339351
default :
340-
if ( this.hasSubmenu && this.submenuOptions.openOn === 'blinked' ) { this.openSubmenu( button.value , button ) ; }
341-
this.emit( 'blinked' , buttonValue , action , this ) ;
352+
if ( this.hasSubmenu && button.def.items ) {
353+
if ( this.submenuOptions.openOn === 'parentBlinked' ) {
354+
this.openSubmenu( button.value , button ) ;
355+
}
356+
}
357+
else {
358+
this.emit( 'blinked' , buttonValue , action , this ) ;
359+
}
342360
}
343361
} ;
344362

@@ -350,7 +368,10 @@ BaseMenu.prototype.onButtonFocus = function( focus , type , button ) {
350368
case 'nextPage' :
351369
break ;
352370
default :
353-
if ( this.hasSubmenu && this.submenuOptions.openOn === 'focus' ) { this.openSubmenu( button.value , button ) ; }
371+
if ( focus && this.hasSubmenu && button.def.items && this.submenuOptions.openOn === 'parentFocus' ) {
372+
this.openSubmenu( button.value , button ) ;
373+
}
374+
354375
this.emit( 'itemFocus' , button.value , focus , button ) ;
355376
}
356377
} ;

lib/document/ColumnMenu.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,18 @@ ColumnMenu.prototype.initPage = function( page = this.page ) {
434434
// Internal: .submenu( itemValue , button )
435435
ColumnMenu.prototype.openSubmenu = function( itemValue , button = null ) {
436436
var x , y , width , height ,
437-
itemDef = button ? this.pageItemsDef[ this.page ][ button.childId ] :
437+
itemDef = button ? this.itemsDef.find( it => it === button.def ) :
438438
this.itemsDef.find( it => it.value === itemValue ) ;
439439

440440
if ( ! itemDef || ! itemDef.items || ! itemDef.items.length ) { return ; }
441441

442442
if ( this.submenu ) {
443-
if ( this.submenu.meta.fromItemDef === itemDef ) { return ; }
444-
else { this.submenu.destroy() ; }
443+
if ( this.submenu.def === itemDef ) { return ; }
444+
else { this.closeSubmenu() ; }
445445
}
446446

447+
this.submenuParentButton = button ;
448+
447449
switch ( this.submenuOptions.disposition ) {
448450
case 'overwrite' :
449451
x = this.outputX ;
@@ -466,7 +468,8 @@ ColumnMenu.prototype.openSubmenu = function( itemValue , button = null ) {
466468
this.submenu = new ColumnMenu( Object.assign( {} , this.submenuOptions , {
467469
internal: true ,
468470
parent: this ,
469-
meta: { fromItemDef: itemDef } ,
471+
isSubmenu: true ,
472+
def: itemDef ,
470473
outputX: x ,
471474
outputY: y ,
472475
outputWidth: width ,
@@ -476,14 +479,17 @@ ColumnMenu.prototype.openSubmenu = function( itemValue , button = null ) {
476479
} ) ) ;
477480

478481
this.redraw() ;
479-
this.document.giveFocusTo( this.submenu ) ;
482+
483+
if ( this.submenuOptions.focusOnOpen ) {
484+
this.document.giveFocusTo( this.submenu ) ;
485+
}
480486

481487
this.submenu.on( 'submit' , this.onSubmenuSubmit ) ;
482488
} ;
483489

484490

485491

486-
ColumnMenu.prototype.clearSubmenu = function() {
492+
ColumnMenu.prototype.closeSubmenu = function() {
487493
if ( ! this.submenu ) { return false ; }
488494
this.submenu.destroy() ;
489495
this.submenu = null ;
@@ -495,9 +501,11 @@ ColumnMenu.prototype.clearSubmenu = function() {
495501
ColumnMenu.prototype.onSubmenuSubmit = function( buttonValue , action , button ) {
496502
button.once( 'blinked' , ( buttonValue_ , reserved , button_ ) => {
497503
if ( this.submenuOptions.hideParent ) { this.children.forEach( e => e.hidden = false ) ; }
498-
this.submenu.destroy() ;
499-
this.document.giveFocusTo( this ) ;
500-
504+
if ( this.submenuOptions.closeOn === 'childSubmit' ) {
505+
this.closeSubmenu() ;
506+
this.document.giveFocusTo( this.submenuParentButton || this ) ;
507+
//this.document.giveFocusTo( this ) ;
508+
}
501509
this.emit( 'blinked' , buttonValue_ , reserved , this ) ;
502510
} ) ;
503511

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"test": "test"
88
},
99
"engines": {
10-
"node": ">=10.0.0"
10+
"node": ">=14.0.0"
1111
},
1212
"dependencies": {
1313
"@cronvel/get-pixels": "^3.4.0",

sample/document/column-menu-submenu-test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@ var columnMenu = new termkit.ColumnMenu( {
5656
/*
5757
disposition: 'overwrite' ,
5858
hideParent: true ,
59-
openOn: 'blinked' ,
59+
openOn: 'parentBlinked' ,
60+
focusOnOpen: true ,
6061
//*/
6162
//*
6263
disposition: 'right' ,
63-
openOn: 'focus' ,
64+
hideParent: false ,
65+
openOn: 'parentFocus' ,
66+
closeOn: 'childSubmit' ,
67+
focusOnOpen: false ,
6468
//*/
6569
} ,
6670
buttonEvenBlurAttr: { bgColor: '@dark-gray' , color: 'white' , bold: true } ,

0 commit comments

Comments
 (0)