UNPKG

5.17 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 ColumnMenuMulti = require( './ColumnMenuMulti.js' ) ;
34const Button = require( './Button.js' ) ;
35
36
37
38// Inherit from ColumnMenuMulti for common methods
39
40function SelectListMulti( 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 ColumnMenuMulti.call( this , options ) ;
64
65 this.show = false ;
66 this.zIndexRef = this.zIndex ; // Back-up for zIndex
67
68 this.initPage() ;
69 this.toggle( this.show , options.noDraw ) ;
70
71 this.onClickOut = this.onClickOut.bind( this ) ;
72
73 this.on( 'clickOut' , this.onClickOut ) ;
74
75 // Only draw if we are not a superclass of the object
76 if ( this.elementType === 'SelectListMulti' && ! options.noDraw ) { this.draw() ; }
77}
78
79module.exports = SelectListMulti ;
80
81SelectListMulti.prototype = Object.create( ColumnMenuMulti.prototype ) ;
82SelectListMulti.prototype.constructor = SelectListMulti ;
83SelectListMulti.prototype.elementType = 'SelectListMulti' ;
84
85
86
87SelectListMulti.prototype.defaultOptions = {
88 buttonBlurAttr: { bgColor: 'gray' , color: 'white' , bold: true } ,
89 buttonFocusAttr: { bgColor: 'white' , color: 'black' , bold: true } ,
90 buttonDisabledAttr: {
91 bgColor: 'gray' , color: 'white' , bold: true , dim: true
92 } ,
93 buttonSubmittedAttr: { bgColor: 'gray' , color: 'brightWhite' , bold: true } ,
94 turnedOnBlurAttr: { bgColor: 'cyan' } ,
95 turnedOnFocusAttr: { bgColor: 'brightCyan' , bold: true } ,
96 turnedOffBlurAttr: { bgColor: 'gray' , dim: true } ,
97 turnedOffFocusAttr: { bgColor: 'white' , color: 'black' , bold: true } ,
98
99 master: {
100 content: 'select-list-multi' ,
101 symbol: '▼' ,
102 internalRole: 'toggle'
103 } ,
104 separator: {
105 content: '-' ,
106 contentRepeat: true ,
107 internalRole: 'separator'
108 }
109} ;
110
111
112
113SelectListMulti.prototype.destroy = function( isSubDestroy ) {
114 this.off( 'clickOut' , this.onClickOut ) ;
115
116 ColumnMenuMulti.prototype.destroy.call( this , isSubDestroy ) ;
117} ;
118
119
120
121SelectListMulti.prototype.toggle = function( show = null , noDraw = false ) {
122 var i , iMax ;
123
124 if ( show === null ) { this.show = ! this.show ; }
125 else { this.show = !! show ; }
126
127 for ( i = 1 , iMax = this.buttons.length ; i < iMax ; i ++ ) {
128 this.buttons[ i ].hidden = ! this.show ;
129 }
130
131 // Adjust outputHeight, to avoid the list to be clickable when reduced
132 this.outputHeight = this.show ? this.pageHeight : 1 ;
133
134 if ( this.show ) { this.topZ() ; }
135 else { this.restoreZ() ; }
136
137 // Return now if noDraw is set, bypassing both drawing and focus
138 if ( noDraw ) { return ; }
139
140 this.document.giveFocusTo( this.buttons[ 0 ] ) ;
141
142 this.redraw() ;
143} ;
144
145
146
147SelectListMulti.prototype.onButtonSubmit = function( buttonValue , action , button ) {
148 switch ( button.internalRole ) {
149 case 'previousPage' :
150 this.previousPage() ;
151 break ;
152 case 'nextPage' :
153 this.nextPage() ;
154 break ;
155 case 'toggle' :
156 this.toggle() ;
157
158 // Submit when reducing
159 if ( ! this.show ) {
160 this.emit( 'submit' , this.value , action , this ) ;
161 }
162 break ;
163 default :
164 this.emit( 'submit' , this.value , action , this ) ;
165 }
166} ;
167
168
169
170SelectListMulti.prototype.onClickOut = function() {
171 if ( this.show ) {
172 this.toggle( false ) ;
173 // We also submit when the menu is closed on click out
174 this.emit( 'submit' , this.value , undefined , this ) ;
175 }
176} ;
177
178SelectListMulti.prototype.getValue = function() { return this.value ; } ;
179