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 Text = require( './Text.js' ) ;
|
33 | const Button = require( './Button.js' ) ;
|
34 |
|
35 |
|
36 |
|
37 | function ToggleButton( options ) {
|
38 |
|
39 | options = ! options ? {} : options.internal ? options : Object.create( options ) ;
|
40 | options.internal = true ;
|
41 |
|
42 |
|
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 |
|
51 | module.exports = ToggleButton ;
|
52 |
|
53 | ToggleButton.prototype = Object.create( Button.prototype ) ;
|
54 | ToggleButton.prototype.constructor = ToggleButton ;
|
55 | ToggleButton.prototype.elementType = 'ToggleButton' ;
|
56 |
|
57 |
|
58 |
|
59 | ToggleButton.prototype.keyBindings = {
|
60 | ENTER: 'toggle' ,
|
61 | KP_ENTER: 'toggle' ,
|
62 | ALT_ENTER: 'submit'
|
63 | } ;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | ToggleButton.prototype.blink = function() {} ;
|
69 |
|
70 |
|
71 |
|
72 | ToggleButton.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 |
|
84 | ToggleButton.prototype.toggle = function( noDraw ) {
|
85 | this.setValue( ! this.value , noDraw ) ;
|
86 | } ;
|
87 |
|
88 |
|
89 |
|
90 | ToggleButton.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 |
|
122 | ToggleButton.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 ;
|
134 | }
|
135 |
|
136 | return true ;
|
137 | } ;
|
138 |
|
139 |
|
140 |
|
141 | ToggleButton.prototype.onHover = function( data ) {
|
142 | if ( this.disabled ) { return ; }
|
143 | this.document.giveFocusTo( this , 'hover' ) ;
|
144 | } ;
|
145 |
|
146 |
|
147 |
|
148 | ToggleButton.prototype.onClick = function( data ) {
|
149 | if ( this.disabled ) { return ; }
|
150 | this.document.giveFocusTo( this , 'select' ) ;
|
151 | this.toggle() ;
|
152 | } ;
|
153 |
|
154 |
|
155 |
|
156 | ToggleButton.prototype.onShortcut = function() {
|
157 | if ( this.disabled ) { return ; }
|
158 | this.document.giveFocusTo( this , 'select' ) ;
|
159 | this.toggle() ;
|
160 | } ;
|
161 |
|