UNPKG

6.62 kBJavaScriptView Raw
1/*
2 Terminal Kit
3
4 Copyright (c) 2009 - 2020 Cédric Ronvel
5
6 The MIT License (MIT)
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25*/
26
27"use strict" ;
28
29
30
31const Element = require( './Element.js' ) ;
32const BaseMenu = require( './BaseMenu.js' ) ;
33const ColumnMenu = require( './ColumnMenu.js' ) ;
34const Button = require( './Button.js' ) ;
35
36
37
38// Inherit from ColumnMenu for common methods
39
40function SelectList( options ) {
41 // Clone options if necessary
42 options = ! options ? {} : options.internal ? options : Object.create( options ) ;
43 options.internal = true ;
44
45 if ( ! options.master || typeof options.master !== 'object' ) {
46 options.master = Object.assign( {} , this.defaultOptions.master ) ;
47 }
48 else {
49 options.master = Object.assign( {} , this.defaultOptions.master , options.master ) ;
50 }
51
52 if ( options.content ) {
53 options.master.content = options.content ;
54 }
55
56 if ( ! options.separator || typeof options.separator !== 'object' ) {
57 options.separator = Object.assign( {} , this.defaultOptions.separator ) ;
58 }
59 else {
60 options.separator = Object.assign( {} , this.defaultOptions.separator , options.separator ) ;
61 }
62
63 ColumnMenu.call( this , options ) ;
64
65 this.show = false ;
66 this.zIndexRef = this.zIndex ; // Back-up for zIndex
67
68 if ( options.value !== undefined && this.setValue( options.value , true ) ) {
69 // .initPage() is called by .setValue() only when a correct value was found
70 this.toggle( this.show , options.noDraw ) ;
71 }
72 else {
73 this.initPage() ;
74 this.toggle( this.show , options.noDraw ) ;
75 }
76
77
78 this.onClickOut = this.onClickOut.bind( this ) ;
79
80 this.on( 'clickOut' , this.onClickOut ) ;
81
82 // Only draw if we are not a superclass of the object
83 if ( this.elementType === 'SelectList' && ! options.noDraw ) { this.draw() ; }
84}
85
86module.exports = SelectList ;
87
88SelectList.prototype = Object.create( ColumnMenu.prototype ) ;
89SelectList.prototype.constructor = SelectList ;
90SelectList.prototype.elementType = 'SelectList' ;
91
92
93
94SelectList.prototype.defaultOptions = {
95 buttonBlurAttr: { bgColor: 'gray' , color: 'white' , bold: true } ,
96 buttonFocusAttr: { bgColor: 'white' , color: 'black' , bold: true } ,
97 buttonDisabledAttr: {
98 bgColor: 'gray' , color: 'white' , bold: true , dim: true
99 } ,
100 buttonSubmittedAttr: { bgColor: 'gray' , color: 'brightWhite' , bold: true } ,
101 turnedOnBlurAttr: { bgColor: 'cyan' } ,
102 turnedOnFocusAttr: { bgColor: 'brightCyan' , bold: true } ,
103 turnedOffBlurAttr: { bgColor: 'gray' , dim: true } ,
104 turnedOffFocusAttr: { bgColor: 'white' , color: 'black' , bold: true } ,
105
106 master: {
107 content: 'select-list' ,
108 symbol: '▼' ,
109 internalRole: 'toggle'
110 } ,
111 separator: {
112 content: '-' ,
113 contentRepeat: true ,
114 internalRole: 'separator'
115 }
116} ;
117
118
119
120
121SelectList.prototype.destroy = function( isSubDestroy ) {
122 this.off( 'clickOut' , this.onClickOut ) ;
123
124 ColumnMenu.prototype.destroy.call( this , isSubDestroy ) ;
125} ;
126
127
128
129SelectList.prototype.toggle = function( show = null , noDraw = false ) {
130 var i , iMax ;
131
132 if ( show === null ) { this.show = ! this.show ; }
133 else { this.show = !! show ; }
134
135 for ( i = 1 , iMax = this.buttons.length ; i < iMax ; i ++ ) {
136 this.buttons[ i ].hidden = ! this.show ;
137 }
138
139 // Adjust outputHeight, to avoid the list to be clickable when reduced
140 this.outputHeight = this.show ? this.pageHeight : 1 ;
141
142 if ( this.show ) { this.topZ() ; }
143 else { this.restoreZ() ; }
144
145 // Return now if noDraw is set, bypassing both drawing and focus
146 if ( noDraw ) { return ; }
147
148 if ( show ) {
149 for ( i = 1 , iMax = this.buttons.length ; i < iMax ; i ++ ) {
150 if ( this.buttons[ i ].value === this.value ) {
151 this.document.giveFocusTo( this.buttons[ i ] ) ;
152 break ;
153 }
154 }
155 }
156 else {
157 this.document.giveFocusTo( this.buttons[ 0 ] ) ;
158 }
159
160 this.redraw() ;
161} ;
162
163
164
165// selectOnly: don't draw, don't toggle
166SelectList.prototype.select = function( button , selectOnly ) {
167 // /!\ Warning! button can be a button (called from .onButtonSubmit()) or a buttonDef (called from .setValue()) /!\
168 // This is nasty an should be fixed!
169 // Ideally, a button should retain a 'def' pointer
170 var width = Element.computeContentWidth( button.content , button.contentHasMarkup ) + this.buttonPaddingWidth + this.buttonSymbolWidth ;
171
172 if ( width < this.buttonsMaxWidth ) {
173 this.masterDef.buttonContent = button.content + ' ' + ' '.repeat( this.buttonsMaxWidth - width ) + this.masterDef.symbol ;
174 }
175 else {
176 this.masterDef.buttonContent = button.content + ' ' + this.masterDef.symbol ;
177 }
178
179 this.masterDef.contentHasMarkup = button.contentHasMarkup ;
180 this.masterDef.width = this.buttonsMaxWidth ;
181 this.value = this.masterDef.value = button.value ;
182
183 // Only when it's a buttonDef ATM:
184 if ( button.page !== undefined ) { this.page = button.page ; }
185
186 this.initPage() ;
187
188 if ( selectOnly ) { return ; }
189
190 this.toggle( false ) ; // , noDraw ) ;
191} ;
192
193
194
195SelectList.prototype.onButtonSubmit = function( buttonValue , action , button ) {
196 switch ( button.internalRole ) {
197 case 'previousPage' :
198 this.previousPage() ;
199 break ;
200 case 'nextPage' :
201 this.nextPage() ;
202 break ;
203 case 'toggle' :
204 this.toggle() ;
205 break ;
206 default :
207 this.select( button ) ;
208 this.emit( 'submit' , buttonValue , action , this ) ;
209 }
210} ;
211
212
213
214// .setValue() will also select the first matching item
215SelectList.prototype.setValue = function( value , noDraw ) {
216 var buttonDef = this.itemsDef.find( b => b.value === value ) ;
217 if ( ! buttonDef ) { return false ; }
218 this.select( buttonDef , noDraw ) ;
219 return true ;
220} ;
221
222
223
224SelectList.prototype.onClickOut = function() { this.toggle( false ) ; } ;
225SelectList.prototype.getValue = function() { return this.value ; } ;
226