Skip to content

Commit a18a210

Browse files
committed
Add the Element#show() and Element#hide() methods, fixing the missing API (#151)
1 parent 188a875 commit a18a210

File tree

7 files changed

+78
-24
lines changed

7 files changed

+78
-24
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
v1.47.2
3+
-------
4+
5+
Add the Element#show() and Element#hide() methods, fixing the missing API (#151)
6+
7+
28
v1.47.1
39
-------
410

doc/Element.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* [.topZ()](#ref.Element.topZ)
2525
* [.bottomZ()](#ref.Element.bottomZ)
2626
* [.setContent()](#ref.Element.setContent)
27+
* [.show()](#ref.Element.show)
28+
* [.hide()](#ref.Element.hide)
2729
* [.draw()](#ref.Element.draw)
2830
* [.redraw()](#ref.Element.redraw)
2931
* [.drawCursor()](#ref.Element.drawCursor)
@@ -122,6 +124,24 @@ Set the content of this *element*.
122124

123125

124126

127+
<a name="ref.Element.show"></a>
128+
### .show( [dontDraw] )
129+
130+
* dontDraw `boolean` when set (default: false) the element is not redrawn (it will be made visible the next time something trigger a *redraw*)
131+
132+
Turn the element visibility **on** and redraw it immediately (unless the `dontDraw` option is on).
133+
134+
135+
136+
<a name="ref.Element.show"></a>
137+
### .hide( [dontDraw] )
138+
139+
* dontDraw `boolean` when set (default: false) the element is not redrawn (it will be hidden the next time something trigger a *redraw* on its parent)
140+
141+
Turn the element visibility **off** and redraw its parent immediately (unless the `dontDraw` option is on).
142+
143+
144+
125145
<a name="ref.Element.draw"></a>
126146
### .draw()
127147

@@ -132,7 +152,10 @@ It is called internally/automatically, userland code should not be bothered with
132152

133153

134154
<a name="ref.Element.redraw"></a>
135-
### .redraw()
155+
### .redraw( [force] )
156+
157+
* force `boolean` **INTERNAL** when set (default: false) the element is *redrawn* even if it is hidden: i.e. the parent is redrawn,
158+
it would effectively clear an hidden element from its parent
136159

137160
Redraw the *element*.
138161
While `.draw()` is used when drawing the current *element* is enough (the *element* has not moved, and has not been resized),

lib/document/Element.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,29 @@ Element.prototype.destroy = function( isSubDestroy = false , noDraw = false ) {
156156

157157

158158

159+
Element.prototype.show = function( noDraw = false ) {
160+
if ( ! this.hidden ) { return this ; }
161+
this.hidden = false ;
162+
if ( ! noDraw ) { this.redraw() ; }
163+
return this ;
164+
} ;
165+
166+
167+
168+
Element.prototype.hide = function( noDraw = false ) {
169+
if ( this.hidden ) { return this ; }
170+
this.hidden = true ;
171+
172+
if ( ! noDraw ) {
173+
// .redraw() with the 'force' option on, because .redraw() does nothing if the element is hidden, but here we want to clear it from its parent
174+
this.redraw( true ) ;
175+
}
176+
177+
return this ;
178+
} ;
179+
180+
181+
159182
// Clear the Element, destroy all children
160183
Element.prototype.clear = function() {
161184
var i , iMax ;
@@ -617,8 +640,9 @@ Element.prototype.draw = function( isInitialInlineDraw = false ) {
617640
// If it has, then it is necessary to draw the closest ancestor which is a container.
618641
// /!\ THIS METHOD IS WRONG: it should draw the parent container, but don't redraw any children of its children Container
619642
// /!\ Maybe rename this .outerDraw() or .parentDraw()
620-
Element.prototype.redraw = function() {
621-
if ( ! this.document || this.hidden ) { return this ; }
643+
// Option 'force' redraw even if the element is hidden, in fact it is used by the .hide() method to effectively hide the element on the parent container.
644+
Element.prototype.redraw = function( force = false ) {
645+
if ( ! this.document || ( this.hidden && ! force ) ) { return this ; }
622646

623647
var container = this.getParentContainer() ;
624648

lib/document/SelectList.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ function SelectList( options ) {
6262

6363
ColumnMenu.call( this , options ) ;
6464

65-
this.show = false ;
65+
this.showMenu = false ;
6666
this.zIndexRef = this.zIndex ; // Back-up for zIndex
6767

6868
if ( options.value !== undefined && this.setValue( options.value , true ) ) {
6969
// .initPage() is called by .setValue() only when a correct value was found
70-
this.toggle( this.show , options.noDraw ) ;
70+
this.toggle( this.showMenu , options.noDraw ) ;
7171
}
7272
else {
7373
this.initPage() ;
74-
this.toggle( this.show , options.noDraw ) ;
74+
this.toggle( this.showMenu , options.noDraw ) ;
7575
}
7676

7777

@@ -127,26 +127,26 @@ SelectList.prototype.destroy = function( isSubDestroy , noDraw = false ) {
127127

128128

129129

130-
SelectList.prototype.toggle = function( show = null , noDraw = false ) {
130+
SelectList.prototype.toggle = function( showMenu = null , noDraw = false ) {
131131
var i , iMax ;
132132

133-
if ( show === null ) { this.show = ! this.show ; }
134-
else { this.show = !! show ; }
133+
if ( showMenu === null ) { this.showMenu = ! this.showMenu ; }
134+
else { this.showMenu = !! showMenu ; }
135135

136136
for ( i = 1 , iMax = this.buttons.length ; i < iMax ; i ++ ) {
137-
this.buttons[ i ].hidden = ! this.show ;
137+
this.buttons[ i ].hidden = ! this.showMenu ;
138138
}
139139

140140
// Adjust outputHeight, to avoid the list to be clickable when reduced
141-
this.outputHeight = this.show ? this.pageHeight : 1 ;
141+
this.outputHeight = this.showMenu ? this.pageHeight : 1 ;
142142

143-
if ( this.show ) { this.topZ() ; }
143+
if ( this.showMenu ) { this.topZ() ; }
144144
else { this.restoreZ() ; }
145145

146146
// Return now if noDraw is set, bypassing both drawing and focus
147147
if ( noDraw ) { return ; }
148148

149-
if ( show ) {
149+
if ( showMenu ) {
150150
for ( i = 1 , iMax = this.buttons.length ; i < iMax ; i ++ ) {
151151
if ( this.buttons[ i ].value === this.value ) {
152152
this.document.giveFocusTo( this.buttons[ i ] ) ;

lib/document/SelectListMulti.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ function SelectListMulti( options ) {
6262

6363
ColumnMenuMulti.call( this , options ) ;
6464

65-
this.show = false ;
65+
this.showMenu = false ;
6666
this.zIndexRef = this.zIndex ; // Back-up for zIndex
6767

6868
this.initPage() ;
69-
this.toggle( this.show , options.noDraw ) ;
69+
this.toggle( this.showMenu , options.noDraw ) ;
7070

7171
this.onClickOut = this.onClickOut.bind( this ) ;
7272

@@ -120,20 +120,20 @@ SelectListMulti.prototype.destroy = function( isSubDestroy , noDraw = false ) {
120120

121121

122122

123-
SelectListMulti.prototype.toggle = function( show = null , noDraw = false ) {
123+
SelectListMulti.prototype.toggle = function( showMenu = null , noDraw = false ) {
124124
var i , iMax ;
125125

126-
if ( show === null ) { this.show = ! this.show ; }
127-
else { this.show = !! show ; }
126+
if ( showMenu === null ) { this.showMenu = ! this.showMenu ; }
127+
else { this.showMenu = !! showMenu ; }
128128

129129
for ( i = 1 , iMax = this.buttons.length ; i < iMax ; i ++ ) {
130-
this.buttons[ i ].hidden = ! this.show ;
130+
this.buttons[ i ].hidden = ! this.showMenu ;
131131
}
132132

133133
// Adjust outputHeight, to avoid the list to be clickable when reduced
134-
this.outputHeight = this.show ? this.pageHeight : 1 ;
134+
this.outputHeight = this.showMenu ? this.pageHeight : 1 ;
135135

136-
if ( this.show ) { this.topZ() ; }
136+
if ( this.showMenu ) { this.topZ() ; }
137137
else { this.restoreZ() ; }
138138

139139
// Return now if noDraw is set, bypassing both drawing and focus
@@ -158,7 +158,7 @@ SelectListMulti.prototype.onButtonSubmit = function( buttonValue , action , butt
158158
this.toggle() ;
159159

160160
// Submit when reducing
161-
if ( ! this.show ) {
161+
if ( ! this.showMenu ) {
162162
this.emit( 'submit' , this.value , action , this ) ;
163163
}
164164
break ;
@@ -170,7 +170,7 @@ SelectListMulti.prototype.onButtonSubmit = function( buttonValue , action , butt
170170

171171

172172
SelectListMulti.prototype.onClickOut = function() {
173-
if ( this.show ) {
173+
if ( this.showMenu ) {
174174
this.toggle( false ) ;
175175
// We also submit when the menu is closed on click out
176176
this.emit( 'submit' , this.value , undefined , this ) ;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "terminal-kit",
3-
"version": "1.47.1",
3+
"version": "1.47.2",
44
"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!",
55
"main": "lib/termkit.js",
66
"directories": {

sample/document/buttons-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ function onBlinked( value , action ) {
124124
function onToggle( value ) {
125125
term.saveCursor() ;
126126
term.moveTo.styleReset.eraseLine( 1 , 22 , 'Toggled #%i: %J\n' , counter ++ , value ) ;
127+
//if ( value ) { button1.hide() ; } else { button1.show() ; }
127128
term.restoreCursor() ;
128129
}
129130

0 commit comments

Comments
 (0)