1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | "use strict" ;
|
28 |
|
29 |
|
30 |
|
31 | const Element = require( './Element.js' ) ;
|
32 | const BaseMenu = require( './BaseMenu.js' ) ;
|
33 | const ColumnMenu = require( './ColumnMenu.js' ) ;
|
34 | const Button = require( './Button.js' ) ;
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function SelectList( options ) {
|
41 |
|
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 ;
|
67 |
|
68 | if ( options.value !== undefined && this.setValue( options.value , true ) ) {
|
69 |
|
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 |
|
83 | if ( this.elementType === 'SelectList' && ! options.noDraw ) { this.draw() ; }
|
84 | }
|
85 |
|
86 | module.exports = SelectList ;
|
87 |
|
88 | SelectList.prototype = Object.create( ColumnMenu.prototype ) ;
|
89 | SelectList.prototype.constructor = SelectList ;
|
90 | SelectList.prototype.elementType = 'SelectList' ;
|
91 |
|
92 |
|
93 |
|
94 | SelectList.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 |
|
121 | SelectList.prototype.destroy = function( isSubDestroy ) {
|
122 | this.off( 'clickOut' , this.onClickOut ) ;
|
123 |
|
124 | ColumnMenu.prototype.destroy.call( this , isSubDestroy ) ;
|
125 | } ;
|
126 |
|
127 |
|
128 |
|
129 | SelectList.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 |
|
140 | this.outputHeight = this.show ? this.pageHeight : 1 ;
|
141 |
|
142 | if ( this.show ) { this.topZ() ; }
|
143 | else { this.restoreZ() ; }
|
144 |
|
145 |
|
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 |
|
166 | SelectList.prototype.select = function( button , selectOnly ) {
|
167 |
|
168 |
|
169 |
|
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 |
|
184 | if ( button.page !== undefined ) { this.page = button.page ; }
|
185 |
|
186 | this.initPage() ;
|
187 |
|
188 | if ( selectOnly ) { return ; }
|
189 |
|
190 | this.toggle( false ) ;
|
191 | } ;
|
192 |
|
193 |
|
194 |
|
195 | SelectList.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 |
|
215 | SelectList.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 |
|
224 | SelectList.prototype.onClickOut = function() { this.toggle( false ) ; } ;
|
225 | SelectList.prototype.getValue = function() { return this.value ; } ;
|
226 |
|