UNPKG

4.37 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 Text = require( './Text.js' ) ;
33const Button = require( './Button.js' ) ;
34
35
36
37function ToggleButton( options ) {
38 // Clone options if necessary
39 options = ! options ? {} : options.internal ? options : Object.create( options ) ;
40 options.internal = true ;
41
42 // Almost everything is constructed by the normal 'submit-button'
43 Button.call( this , options ) ;
44
45 this.value = !! options.value ;
46 this.key = options.key || null ;
47
48 if ( this.elementType === 'ToggleButton' && ! options.noDraw ) { this.draw() ; }
49}
50
51module.exports = ToggleButton ;
52
53ToggleButton.prototype = Object.create( Button.prototype ) ;
54ToggleButton.prototype.constructor = ToggleButton ;
55ToggleButton.prototype.elementType = 'ToggleButton' ;
56
57
58
59ToggleButton.prototype.keyBindings = {
60 ENTER: 'toggle' ,
61 KP_ENTER: 'toggle' ,
62 ALT_ENTER: 'submit'
63} ;
64
65
66
67// No blink effect
68ToggleButton.prototype.blink = function() {} ;
69
70
71
72ToggleButton.prototype.setValue = function( value , noDraw ) {
73 value = !! value ;
74 if ( this.value === value ) { return ; }
75 this.value = value ;
76 this.updateStatus() ;
77 this.emit( 'toggle' , this.value , undefined , this ) ;
78 this.emit( this.value ? 'turnOn' : 'turnOff' , this.value , undefined , this ) ;
79 if ( ! noDraw ) { this.draw() ; }
80} ;
81
82
83
84ToggleButton.prototype.toggle = function( noDraw ) {
85 this.setValue( ! this.value , noDraw ) ;
86} ;
87
88
89
90ToggleButton.prototype.updateStatus = function() {
91 if ( this.disabled ) {
92 this.attr = this.disabledAttr ;
93 this.leftPadding = this.disabledLeftPadding ;
94 this.rightPadding = this.disabledRightPadding ;
95 }
96 else if ( this.hasFocus ) {
97 if ( this.value ) {
98 this.attr = this.turnedOnFocusAttr ;
99 this.leftPadding = this.turnedOnFocusLeftPadding ;
100 this.rightPadding = this.turnedOnFocusRightPadding ;
101 }
102 else {
103 this.attr = this.turnedOffFocusAttr ;
104 this.leftPadding = this.turnedOffFocusLeftPadding ;
105 this.rightPadding = this.turnedOffFocusRightPadding ;
106 }
107 }
108 else if ( this.value ) {
109 this.attr = this.turnedOnBlurAttr ;
110 this.leftPadding = this.turnedOnBlurLeftPadding ;
111 this.rightPadding = this.turnedOnBlurRightPadding ;
112 }
113 else {
114 this.attr = this.turnedOffBlurAttr ;
115 this.leftPadding = this.turnedOffBlurLeftPadding ;
116 this.rightPadding = this.turnedOffBlurRightPadding ;
117 }
118} ;
119
120
121
122ToggleButton.prototype.onKey = function( key , altKeys , data ) {
123 switch( this.keyBindings[ key ] ) {
124 case 'toggle' :
125 if ( this.disabled ) { break ; }
126 this.toggle() ;
127 break ;
128 case 'submit' :
129 if ( this.disabled ) { break ; }
130 this.emit( 'submit' , this.key ? { [ this.key ]: this.value } : this.value , this.actionKeyBindings[ key ] , this ) ;
131 break ;
132 default :
133 return ; // Bubble up
134 }
135
136 return true ; // Do not bubble up
137} ;
138
139
140
141ToggleButton.prototype.onHover = function( data ) {
142 if ( this.disabled ) { return ; }
143 this.document.giveFocusTo( this , 'hover' ) ;
144} ;
145
146
147
148ToggleButton.prototype.onClick = function( data ) {
149 if ( this.disabled ) { return ; }
150 this.document.giveFocusTo( this , 'select' ) ;
151 this.toggle() ;
152} ;
153
154
155
156ToggleButton.prototype.onShortcut = function() {
157 if ( this.disabled ) { return ; }
158 this.document.giveFocusTo( this , 'select' ) ;
159 this.toggle() ;
160} ;
161