Skip to content

Commit

Permalink
New method: BaseMenu#setItem() (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
cronvel committed Feb 17, 2021
1 parent a18a210 commit bd4990e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

v1.48.0
-------

New method: BaseMenu#setItem() (#150)


v1.47.2
-------

Expand Down
32 changes: 29 additions & 3 deletions lib/document/BaseMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ BaseMenu.prototype.toPage = function( page , focusType ) {



BaseMenu.prototype.focusValue = function( value , focusType ) {
BaseMenu.prototype.focusValue = function( itemValue , focusType , forceInitPage = false ) {
var focusAware , itemDef , item ;

itemDef = this.itemsDef.find( it => ! it.disabled && it.value === value ) ;
itemDef = this.itemsDef.find( it => ! it.disabled && it.value === itemValue ) ;
if ( ! itemDef ) { return ; }

if ( this.page !== itemDef.page ) {
if ( this.page !== itemDef.page || forceInitPage ) {
this.page = itemDef.page ;
this.initPage() ;
}
Expand All @@ -198,6 +198,32 @@ BaseMenu.prototype.focusValue = function( value , focusType ) {



BaseMenu.prototype.setItem = function( itemValue , itemOptions ) {
var itemDef , focusValue ;

itemDef = this.itemsDef.find( it => it.value === itemValue ) ;
if ( ! itemDef ) { return false ; }

Object.assign( itemDef , itemOptions ) ;

focusValue = this.focusChild && this.focusChild.value ;

this.initChildren( true ) ;

if ( focusValue !== undefined ) {
// Note: .focusValue() call .draw() behind the scene, and last argument force a .initPage() call
this.focusValue( focusValue , 'refocus' , true ) ;
}
else {
this.initPage() ;
this.draw() ;
}

return true ;
} ;



BaseMenu.prototype.onKey = function( key , trash , data ) {
switch( this.keyBindings[ key ] ) {
case 'previous' :
Expand Down
6 changes: 3 additions & 3 deletions lib/document/ColumnMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ ColumnMenu.prototype.toggleButtonKeyBindings = {


// Pre-compute page and eventually create Buttons automatically
ColumnMenu.prototype.initChildren = function() {
ColumnMenu.prototype.initChildren = function( noInitPage = false ) {
// Do not exit now: maybe there are masterDef and separatorDef (SelectList*)
//if ( ! this.itemsDef.length ) { return ; }

Expand Down Expand Up @@ -317,7 +317,7 @@ ColumnMenu.prototype.initChildren = function() {
}

// Only initPage if we are not a superclass of the object
if ( this.elementType === 'ColumnMenu' ) { this.initPage() ; }
if ( this.elementType === 'ColumnMenu' && ! noInitPage ) { this.initPage() ; }
} ;


Expand Down Expand Up @@ -439,7 +439,7 @@ ColumnMenu.prototype.onParentResize = function() {
this.maxHeight = Math.round( this.outputDst.height * this.autoHeight ) ;
}

this.initChildren() ;
this.initChildren( true ) ;
this.page = 0 ;
this.initPage() ;
this.draw() ;
Expand Down
4 changes: 2 additions & 2 deletions lib/document/ColumnMenuMulti.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ ColumnMenuMulti.prototype.childUseParentKeyValue = true ;



ColumnMenuMulti.prototype.initChildren = function() {
ColumnMenuMulti.prototype.initChildren = function( noInitPage = false ) {
ColumnMenu.prototype.initChildren.call( this ) ;

// Only initPage if we are not a superclass of the object
if ( this.elementType === 'ColumnMenuMulti' ) { this.initPage() ; }
if ( this.elementType === 'ColumnMenuMulti' && ! noInitPage ) { this.initPage() ; }
} ;


Expand Down
4 changes: 2 additions & 2 deletions lib/document/RowMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ RowMenu.prototype.toggleButtonKeyBindings = {


// Pre-compute page and eventually create Buttons automatically
RowMenu.prototype.initChildren = function() {
RowMenu.prototype.initChildren = function( noInitPage = false ) {
if ( ! this.itemsDef.length ) { return ; }

this.buttonPaddingWidth =
Expand Down Expand Up @@ -234,7 +234,7 @@ RowMenu.prototype.initChildren = function() {
} ) ;

// Only initPage if we are not a superclass of the object
if ( this.elementType === 'RowMenu' ) { this.initPage() ; }
if ( this.elementType === 'RowMenu' && ! noInitPage ) { this.initPage() ; }
} ;


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "terminal-kit",
"version": "1.47.2",
"version": "1.48.0",
"description": "256 colors, keys and mouse, input field, progress bars, screen buffer (including 32-bit composition and image loading), text buffer, and many more... Whether you just need colors and styles, build a simple interactive command line tool or a complexe terminal app: this is the absolute terminal lib for Node.js!",
"main": "lib/termkit.js",
"directories": {
Expand Down
3 changes: 3 additions & 0 deletions sample/document/column-menu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ var submitCount = 0 , focusCount = 0 ;

function onSubmit( buttonValue , action ) {
//console.error( 'Submitted: ' , value ) ;
if ( buttonValue === 'view' ) { columnMenu.setItem( buttonValue , { content: 'bob' } ) ; }

term.saveCursor() ;
term.moveTo.styleReset.eraseLine( 1 , 22 , 'Submitted #%i: %s %s\n' , submitCount ++ , buttonValue , action ) ;
term.restoreCursor() ;
Expand All @@ -137,6 +139,7 @@ function onItemFocus( buttonValue , focus ) {
}

columnMenu.on( 'submit' , onSubmit ) ;
//columnMenu.on( 'blinked' , onSubmit ) ;
columnMenu.on( 'itemFocus' , onItemFocus ) ;


Expand Down

0 comments on commit bd4990e

Please sign in to comment.