UNPKG

4.94 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 ColumnMenu = require( './ColumnMenu.js' ) ;
32const ToggleButton = require( './ToggleButton.js' ) ;
33
34
35
36// Inherit from ColumnMenu for common methods
37
38function ColumnMenuMulti( options ) {
39 // Clone options if necessary
40 options = ! options ? {} : options.internal ? options : Object.create( options ) ;
41 options.internal = true ;
42
43 // ColumnMenuMulti value is always a set of values
44 if ( options.value && typeof options.value === 'object' ) {
45 this.setValue( options.value , true ) ;
46 }
47 else {
48 this.value = {} ;
49 }
50
51 ColumnMenu.call( this , options ) ;
52
53 // Only draw if we are not a superclass of the object
54 if ( this.elementType === 'ColumnMenuMulti' && ! options.noDraw ) { this.draw() ; }
55}
56
57module.exports = ColumnMenuMulti ;
58
59ColumnMenuMulti.prototype = Object.create( ColumnMenu.prototype ) ;
60ColumnMenuMulti.prototype.constructor = ColumnMenuMulti ;
61ColumnMenuMulti.prototype.elementType = 'ColumnMenuMulti' ;
62
63
64
65ColumnMenuMulti.prototype.ButtonClass = ToggleButton ;
66ColumnMenuMulti.prototype.childUseParentKeyValue = true ;
67
68
69
70ColumnMenuMulti.prototype.initChildren = function() {
71 ColumnMenu.prototype.initChildren.call( this ) ;
72
73 // Only initPage if we are not a superclass of the object
74 if ( this.elementType === 'ColumnMenuMulti' ) { this.initPage() ; }
75} ;
76
77
78
79// Set all existing children ToggleButton to the correct value
80ColumnMenuMulti.prototype.setValue = function( value , noDraw ) {
81 if ( ! value || typeof value !== 'object' ) { return ; }
82
83 if ( Array.isArray( value ) || value instanceof Set ) {
84 this.value = {} ;
85 for ( let key of value ) {
86 if ( key && typeof key === 'string' ) {
87 this.value[ key ] = true ;
88 }
89 }
90 }
91 else {
92 this.value = value ;
93 }
94
95 // Can be called during init, before .initPage() is called
96 if ( ! this.buttons ) { return ; }
97
98 this.buttons.forEach( button => {
99 if ( button.internalRole || ! button.key || ! ( button instanceof ToggleButton ) ) { return ; }
100 button.setValue( this.value[ button.key ] ) ;
101 } ) ;
102
103 if ( ! noDraw ) { this.draw() ; }
104} ;
105
106
107
108ColumnMenuMulti.prototype.onKey = function( key , altKeys , data ) {
109 switch( this.keyBindings[ key ] ) {
110 case 'previous' :
111 this.focusChild = this.focusPreviousChild( ! this.maxPage ) ;
112 if ( this.focusChild === this.children[ 0 ] && this.maxPage && this.page > 0 ) {
113 this.previousPage( 'cycle' ) ;
114 }
115 break ;
116 case 'next' :
117 this.focusChild = this.focusNextChild( ! this.maxPage ) ;
118 if ( this.focusChild === this.children[ this.children.length - 1 ] && this.maxPage && this.page < this.maxPage ) {
119 this.nextPage( 'cycle' ) ;
120 }
121 break ;
122 case 'previousPage' :
123 if ( this.maxPage && this.page > 0 ) {
124 this.previousPage( 'cycle' ) ;
125 }
126 break ;
127 case 'nextPage' :
128 if ( this.maxPage && this.page < this.maxPage ) {
129 this.nextPage( 'cycle' ) ;
130 }
131 break ;
132 case 'firstPage' :
133 if ( this.maxPage && this.page !== 0 ) {
134 this.toPage( 0 , 'cycle' ) ;
135 }
136 break ;
137 case 'lastPage' :
138 if ( this.maxPage && this.page !== this.maxPage ) {
139 this.toPage( this.maxPage , 'cycle' ) ;
140 }
141 break ;
142 case 'submit' :
143 this.emit( 'submit' , this.value , undefined , this ) ;
144 break ;
145 default :
146 return ; // Bubble up
147 }
148
149 return true ; // Do not bubble up
150} ;
151
152
153
154ColumnMenuMulti.prototype.onButtonSubmit = function( buttonValue , action , button ) {
155 switch ( button.internalRole ) {
156 case 'previousPage' :
157 this.previousPage() ;
158 break ;
159 case 'nextPage' :
160 this.nextPage() ;
161 break ;
162 default :
163 this.emit( 'submit' , this.value , action , this ) ;
164 }
165} ;
166
167
168
169ColumnMenuMulti.prototype.onButtonToggle = function( buttonValue , action , button ) {
170 if ( ! button.key ) { return ; }
171 if ( buttonValue ) { this.value[ button.key ] = true ; }
172 else { delete this.value[ button.key ] ; }
173} ;
174