UNPKG

6.06 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
31//const Element = require( './Element.js' ) ;
32const RowMenu = require( './RowMenu.js' ) ;
33const ColumnMenu = require( './ColumnMenu.js' ) ;
34
35
36
37function DropDownMenu( options ) {
38 // Clone options if necessary
39 options = ! options ? {} : options.internal ? options : Object.create( options ) ;
40 options.internal = true ;
41
42 var i , iMax ;
43
44 RowMenu.call( this , options ) ;
45
46 this.initPage() ;
47
48 this.columnMenu = null ;
49 this.columnButtonFocusAttr = options.buttonFocusAttr || { bgColor: 'blue' , color: 'white' , bold: true } ;
50 this.columnButtonBlurAttr = options.buttonBlurAttr || { bgColor: 'brightBlack' , color: 'white' , bold: true } ;
51
52 this.onClickOut = this.onClickOut.bind( this ) ;
53 this.onButtonFocus = this.onButtonFocus.bind( this ) ;
54 this.onButtonSubmit = this.onButtonSubmit.bind( this ) ;
55 this.onColumnMenuSubmit = this.onColumnMenuSubmit.bind( this ) ;
56 //this.onColumnMenuFocus = this.onColumnMenuFocus.bind( this ) ;
57
58 this.on( 'clickOut' , this.onClickOut ) ;
59
60 for ( i = 0 , iMax = this.buttons.length ; i < iMax ; i ++ ) {
61 this.buttons[ i ].on( 'focus' , this.onButtonFocus ) ;
62 }
63
64 // Only draw if we are not a superclass of the object
65 if ( this.elementType === 'DropDownMenu' && ! options.noDraw ) { this.draw() ; }
66}
67
68module.exports = DropDownMenu ;
69
70DropDownMenu.prototype = Object.create( RowMenu.prototype ) ;
71DropDownMenu.prototype.constructor = DropDownMenu ;
72DropDownMenu.prototype.elementType = 'DropDownMenu' ;
73
74
75
76DropDownMenu.prototype.destroy = function( isSubDestroy ) {
77 this.off( 'clickOut' , this.onClickOut ) ;
78
79 RowMenu.prototype.destroy.call( this , isSubDestroy ) ;
80} ;
81
82
83
84DropDownMenu.prototype.keyBindings = {
85 LEFT: 'previous' ,
86 RIGHT: 'next' ,
87 ESCAPE: 'clearColumnMenu' ,
88 UP: 'clearColumnMenu' ,
89 DOWN: 'dropDown' ,
90 ENTER: 'submit' ,
91 KP_ENTER: 'submit' ,
92 ALT_ENTER: 'submit'
93} ;
94
95
96
97DropDownMenu.prototype.dropDown = function( index , x , y , submittedButtonValue , submittedButtonAction ) {
98 var itemsDef = this.itemsDef[ index ].items ;
99
100 //console.error( "Submit!" , button.childId ) ;
101
102 if ( this.columnMenu ) {
103 // Already dropped down? Nothing to do!
104 if ( this.columnMenu.index === index ) { return ; }
105 this.clearColumnMenu() ;
106 }
107
108 // No submenu, leave now...
109 if ( ! itemsDef || ! itemsDef.length ) {
110 if ( submittedButtonValue && this.itemsDef[ index ].topSubmit ) {
111 // Top-button without submenu that have a 'topSubmit' flag on submits themselves
112 this.emit( 'submit' , submittedButtonValue , submittedButtonAction , this ) ;
113 }
114
115 return ;
116 }
117
118 // Make the ColumnMenu a child of the button, so focus cycle will work as expected
119 this.columnMenu = new ColumnMenu( {
120 internal: true ,
121 parent: this.children[ index ] ,
122 x: x ,
123 y: y ,
124 width: this.outputWidth - x ,
125 leftPadding: ' ' ,
126 rightPadding: ' ' ,
127 items: itemsDef ,
128 buttonFocusAttr: this.columnButtonFocusAttr ,
129 buttonBlurAttr: this.columnButtonBlurAttr
130 } ) ;
131
132 this.columnMenu.on( 'submit' , this.onColumnMenuSubmit ) ;
133 //this.columnMenu.on( 'focus' , this.onColumnMenuFocus ) ;
134
135 // unused ATM
136 //this.columnMenu.menuIndex = index ;
137
138 //this.document.giveFocusTo( this.columnMenu , 'delegate' ) ;
139} ;
140
141
142
143DropDownMenu.prototype.clearColumnMenu = function() {
144 if ( ! this.columnMenu ) { return false ; }
145 this.columnMenu.destroy() ;
146 this.columnMenu = null ;
147 return true ;
148} ;
149
150
151
152DropDownMenu.prototype.onClickOut = function( buttonValue , data , button ) {
153 if ( this.columnMenu ) {
154 this.clearColumnMenu() ;
155 }
156} ;
157
158
159
160/* Does not work: focus is on column-menu children, we should have a way to make focus event bubbling up
161DropDownMenu.prototype.onColumnMenuFocus = function( focus , type ) {
162 if ( ! focus && this.columnMenu ) {
163 this.clearColumnMenu() ;
164 }
165} ;
166*/
167
168
169
170DropDownMenu.prototype.onButtonSubmit = function( buttonValue , action , button ) {
171 this.dropDown( button.childId , button.outputX , button.outputY + 1 , buttonValue , action ) ;
172} ;
173
174
175
176DropDownMenu.prototype.onButtonFocus = function( focus , type , button ) {
177 if ( focus ) { this.dropDown( button.childId , button.outputX , button.outputY + 1 ) ; }
178} ;
179
180
181
182DropDownMenu.prototype.onColumnMenuSubmit = function( buttonValue , action , button ) {
183 this.emit( 'submit' , buttonValue , action , this ) ;
184} ;
185
186
187
188DropDownMenu.prototype.onKey = function( key , trash , data ) {
189 switch( this.keyBindings[ key ] ) {
190 case 'previous' :
191 this.focusChild = this.focusPreviousChild() ;
192 //this.clearColumnMenu() ;
193 break ;
194 case 'next' :
195 this.focusChild = this.focusNextChild() ;
196 //this.clearColumnMenu() ;
197 break ;
198 case 'dropDown' :
199 if ( this.columnMenu ) { this.columnMenu.focusNextChild() ; }
200 //this.focusChild = this.focusNextChild() ;
201 //this.clearColumnMenu() ;
202 break ;
203 case 'clearColumnMenu' :
204 // Bubble up only if something was cleared
205 return this.clearColumnMenu() ;
206 default :
207 return ; // Bubble up
208 }
209
210 return true ; // Do not bubble up
211} ;
212