@@ -25,6 +25,7 @@ class ToolBarButtonView extends ToolBarItem {
25
25
priority : options . priority
26
26
} , group ) ;
27
27
28
+ this . subscriptions = new CompositeDisposable ( ) ;
28
29
this . options = options ;
29
30
this . enabled = true ;
30
31
@@ -35,38 +36,18 @@ class ToolBarButtonView extends ToolBarItem {
35
36
this . putAtEnd ( ) ;
36
37
}
37
38
38
- if ( options . icon ) {
39
- this . addIcon ( ) ;
40
- }
41
-
42
- if ( options . text ) {
43
- this . addText ( ) ;
44
- }
45
-
46
- if ( options . tooltip ) {
47
- this . subscriptions = new CompositeDisposable ( ) ;
48
- this . subscriptions . add (
49
- this . addTooltip ( this . options . tooltip , this . options . callback )
50
- ) ;
51
- }
52
-
53
- if ( options . color ) {
54
- this . addColor ( ) ;
55
- }
56
- if ( options . background ) {
57
- this . addBackgroundColor ( ) ;
58
- }
59
-
39
+ this . addIcon ( ) ;
40
+ this . addText ( ) ;
41
+ this . addTooltip ( ) ;
42
+ this . setStyle ( 'color' , options . color ) ;
43
+ this . setStyle ( 'background' , options . background ) ;
60
44
this . addClasses ( ) ;
61
45
this . addOnMouseDown ( ) ;
62
46
this . addOnClick ( ) ;
63
47
}
64
48
65
49
destroy ( ) {
66
- if ( this . subscriptions ) {
67
- this . subscriptions . dispose ( ) ;
68
- this . subscriptions = null ;
69
- }
50
+ this . subscriptions . dispose ( ) ;
70
51
this . element . removeEventListener ( 'mousedown' , this . _onMouseDown ) ;
71
52
this . element . removeEventListener ( 'click' , this . _onClick ) ;
72
53
super . destroy ( ) ; // call super.destroy() in the end
@@ -79,6 +60,10 @@ class ToolBarButtonView extends ToolBarItem {
79
60
80
61
/** Add an icon for the button using built-in icons. */
81
62
addIcon ( ) {
63
+ if ( ! this . options . icon ) {
64
+ return ;
65
+ }
66
+
82
67
if ( this . options . iconset ) {
83
68
if ( this . options . iconset . startsWith ( 'fa' ) ) {
84
69
this . classNames . push ( this . options . iconset , `fa-${ this . options . icon } ` ) ;
@@ -92,6 +77,10 @@ class ToolBarButtonView extends ToolBarItem {
92
77
93
78
/** Adds a text/html to the button */
94
79
addText ( ) {
80
+ if ( ! this . options . text ) {
81
+ return ;
82
+ }
83
+
95
84
if ( this . options . html ) {
96
85
this . element . innerHTML = this . options . text ;
97
86
} else {
@@ -105,37 +94,38 @@ class ToolBarButtonView extends ToolBarItem {
105
94
* @param {ButtonOptions.callback | null } callback
106
95
* @returns {Disposable } a disposable tooltip
107
96
*/
108
- addTooltip ( tooltipOptions , callback = null ) {
97
+ addTooltip ( ) {
98
+ if ( ! this . options . tooltip ) {
99
+ return ;
100
+ }
101
+
109
102
let tooltip ;
110
- if ( typeof tooltipOptions === 'string' ) {
103
+ if ( typeof this . options . tooltip === 'string' ) {
111
104
tooltip = {
112
- title : tooltipOptions
105
+ title : this . options . tooltip
113
106
} ;
114
107
} else {
115
- tooltip = tooltipOptions ;
108
+ tooltip = this . options . tooltip ;
116
109
}
117
110
118
111
if ( ! tooltip . hasOwnProperty ( 'placement' ) ) {
119
112
tooltip . placement = getTooltipPlacement ( ) ;
120
113
}
121
114
122
115
if ( ! tooltip . hasOwnProperty ( 'keyBindingCommand' ) &&
123
- typeof callback === 'string'
116
+ typeof this . options . callback === 'string'
124
117
) {
125
- tooltip . keyBindingCommand = callback ;
118
+ tooltip . keyBindingCommand = this . options . callback ;
126
119
}
127
120
128
- return atom . tooltips . add ( this . element , tooltip ) ;
129
- }
130
-
131
- /** Add color to the button */
132
- addColor ( ) {
133
- this . element . style . color = this . options . color ;
121
+ this . subscriptions . add ( atom . tooltips . add ( this . element , tooltip ) ) ;
134
122
}
135
123
136
- /** Add background color to the button */
137
- addBackgroundColor ( ) {
138
- this . element . style . background = this . options . background ;
124
+ /** Set a style on the button */
125
+ setStyle ( style , value ) {
126
+ if ( value ) {
127
+ this . element . style [ style ] = value ;
128
+ }
139
129
}
140
130
141
131
/** Add all the classes (custom and others) to the button */
@@ -153,20 +143,12 @@ class ToolBarButtonView extends ToolBarItem {
153
143
}
154
144
155
145
setEnabled ( enabled ) {
156
- if ( enabled ) {
157
- this . element . classList . remove ( 'disabled' ) ;
158
- } else {
159
- this . element . classList . add ( 'disabled' ) ;
160
- }
146
+ this . element . classList . toggle ( 'disabled' , ! enabled ) ;
161
147
this . enabled = enabled ;
162
148
}
163
149
164
150
setSelected ( selected ) {
165
- if ( selected ) {
166
- this . element . classList . add ( 'selected' ) ;
167
- } else {
168
- this . element . classList . remove ( 'selected' ) ;
169
- }
151
+ this . element . classList . toggle ( 'selected' , selected ) ;
170
152
}
171
153
172
154
getSelected ( ) {
@@ -233,29 +215,28 @@ function getCallbackModifier (callback, {altKey, ctrlKey, shiftKey}) {
233
215
. map ( modifiers => modifiers . toLowerCase ( ) )
234
216
. reverse ( )
235
217
. find ( item => {
236
- if ( ( ~ item . indexOf ( 'alt' ) && ! altKey ) || ( altKey && ! ~ item . indexOf ( 'alt' ) ) ) {
237
- return false ;
238
- }
239
- if ( ( ~ item . indexOf ( 'ctrl' ) && ! ctrlKey ) || ( ctrlKey && ! ~ item . indexOf ( 'ctrl' ) ) ) {
240
- return false ;
241
- }
242
- if ( ( ~ item . indexOf ( 'shift' ) && ! shiftKey ) || ( shiftKey && ! ~ item . indexOf ( 'shift' ) ) ) {
243
- return false ;
244
- }
245
- return true ;
218
+ return checkKeyModifier ( item . indexOf ( 'alt' ) , altKey )
219
+ && checkKeyModifier ( item . indexOf ( 'ctrl' ) , ctrlKey )
220
+ && checkKeyModifier ( item . indexOf ( 'shift' ) , shiftKey ) ;
246
221
} ) ;
247
222
return callback [ modifier ] || callback [ '' ] ;
248
223
}
249
224
225
+ function checkKeyModifier ( keyIndex , key ) {
226
+ return ! ( ( ~ keyIndex && ! key ) || ( key && ! ~ keyIndex ) ) ;
227
+ }
228
+
250
229
/** get the tooltip placement based on the toolbar position */
251
230
function getTooltipPlacement ( ) {
252
- const toolbarPosition = atom . config . get ( 'tool-bar.position' ) ;
231
+ const tooltipPlacement = {
232
+ Top : 'bottom' ,
233
+ Right : 'left' ,
234
+ Bottom : 'top' ,
235
+ Left : 'right'
236
+ } ;
253
237
254
- return toolbarPosition === 'Top' ? 'bottom'
255
- : toolbarPosition === 'Right' ? 'left'
256
- : toolbarPosition === 'Bottom' ? 'top'
257
- : toolbarPosition === 'Left' ? 'right'
258
- : null ;
238
+ const toolbarPosition = atom . config . get ( 'tool-bar.position' ) ;
239
+ return tooltipPlacement [ toolbarPosition ] || null ;
259
240
}
260
241
261
242
module . exports . ToolBarButtonView = ToolBarButtonView ;
0 commit comments