Skip to content

Commit 48cd604

Browse files
authored
Merge pull request #301 from ericcornelissen/improve-codeclimate-score
Refactor to decrease code duplication and complexity
2 parents 40b7e34 + bf76e46 commit 48cd604

File tree

3 files changed

+78
-108
lines changed

3 files changed

+78
-108
lines changed

lib/items/tool-bar-button-view.js

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ToolBarButtonView extends ToolBarItem {
2525
priority: options.priority
2626
}, group);
2727

28+
this.subscriptions = new CompositeDisposable();
2829
this.options = options;
2930
this.enabled = true;
3031

@@ -35,38 +36,18 @@ class ToolBarButtonView extends ToolBarItem {
3536
this.putAtEnd();
3637
}
3738

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);
6044
this.addClasses();
6145
this.addOnMouseDown();
6246
this.addOnClick();
6347
}
6448

6549
destroy () {
66-
if (this.subscriptions) {
67-
this.subscriptions.dispose();
68-
this.subscriptions = null;
69-
}
50+
this.subscriptions.dispose();
7051
this.element.removeEventListener('mousedown', this._onMouseDown);
7152
this.element.removeEventListener('click', this._onClick);
7253
super.destroy(); // call super.destroy() in the end
@@ -79,6 +60,10 @@ class ToolBarButtonView extends ToolBarItem {
7960

8061
/** Add an icon for the button using built-in icons. */
8162
addIcon () {
63+
if (!this.options.icon) {
64+
return;
65+
}
66+
8267
if (this.options.iconset) {
8368
if (this.options.iconset.startsWith('fa')) {
8469
this.classNames.push(this.options.iconset, `fa-${this.options.icon}`);
@@ -92,6 +77,10 @@ class ToolBarButtonView extends ToolBarItem {
9277

9378
/** Adds a text/html to the button */
9479
addText () {
80+
if (!this.options.text) {
81+
return;
82+
}
83+
9584
if (this.options.html) {
9685
this.element.innerHTML = this.options.text;
9786
} else {
@@ -105,37 +94,38 @@ class ToolBarButtonView extends ToolBarItem {
10594
* @param {ButtonOptions.callback | null} callback
10695
* @returns {Disposable} a disposable tooltip
10796
*/
108-
addTooltip (tooltipOptions, callback = null) {
97+
addTooltip () {
98+
if (!this.options.tooltip) {
99+
return;
100+
}
101+
109102
let tooltip;
110-
if (typeof tooltipOptions === 'string') {
103+
if (typeof this.options.tooltip === 'string') {
111104
tooltip = {
112-
title: tooltipOptions
105+
title: this.options.tooltip
113106
};
114107
} else {
115-
tooltip = tooltipOptions;
108+
tooltip = this.options.tooltip;
116109
}
117110

118111
if (!tooltip.hasOwnProperty('placement')) {
119112
tooltip.placement = getTooltipPlacement();
120113
}
121114

122115
if (!tooltip.hasOwnProperty('keyBindingCommand') &&
123-
typeof callback === 'string'
116+
typeof this.options.callback === 'string'
124117
) {
125-
tooltip.keyBindingCommand = callback;
118+
tooltip.keyBindingCommand = this.options.callback;
126119
}
127120

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));
134122
}
135123

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+
}
139129
}
140130

141131
/** Add all the classes (custom and others) to the button */
@@ -153,20 +143,12 @@ class ToolBarButtonView extends ToolBarItem {
153143
}
154144

155145
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);
161147
this.enabled = enabled;
162148
}
163149

164150
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);
170152
}
171153

172154
getSelected () {
@@ -233,29 +215,28 @@ function getCallbackModifier (callback, {altKey, ctrlKey, shiftKey}) {
233215
.map(modifiers => modifiers.toLowerCase())
234216
.reverse()
235217
.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);
246221
});
247222
return callback[modifier] || callback[''];
248223
}
249224

225+
function checkKeyModifier (keyIndex, key) {
226+
return !((~keyIndex && !key) || (key && !~keyIndex));
227+
}
228+
250229
/** get the tooltip placement based on the toolbar position */
251230
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+
};
253237

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;
259240
}
260241

261242
module.exports.ToolBarButtonView = ToolBarButtonView;

lib/raf-debounce.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module.exports = function rafDebounce (fn) {
2-
let args;
3-
let context;
4-
let requestID;
2+
let args, context, requestID;
53

64
function later () {
75
fn.apply(context, args);
@@ -14,19 +12,13 @@ module.exports = function rafDebounce (fn) {
1412
}
1513
args = arguments;
1614
context = this;
17-
if (requestID) {
18-
window.cancelAnimationFrame(requestID);
19-
}
15+
window.cancelAnimationFrame(requestID);
2016
requestID = window.requestAnimationFrame(later);
2117
}
2218

2319
debounced.dispose = () => {
24-
if (fn != null) {
25-
if (requestID) {
26-
window.cancelAnimationFrame(requestID);
27-
}
28-
args = context = requestID = fn = null;
29-
}
20+
window.cancelAnimationFrame(requestID);
21+
fn = args = context = requestID = null;
3022
};
3123

3224
return debounced;

lib/tool-bar-view.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@ module.exports = class ToolBarView {
1515
}),
1616
atom.commands.add('atom-workspace', 'tool-bar:position-top', () => {
1717
this.updatePosition('Top');
18-
atom.config.set('tool-bar.position', 'Top');
1918
}),
2019
atom.commands.add('atom-workspace', 'tool-bar:position-right', () => {
2120
this.updatePosition('Right');
22-
atom.config.set('tool-bar.position', 'Right');
2321
}),
2422
atom.commands.add('atom-workspace', 'tool-bar:position-bottom', () => {
2523
this.updatePosition('Bottom');
26-
atom.config.set('tool-bar.position', 'Bottom');
2724
}),
2825
atom.commands.add('atom-workspace', 'tool-bar:position-left', () => {
2926
this.updatePosition('Left');
30-
atom.config.set('tool-bar.position', 'Left');
3127
}),
3228
atom.config.observe('tool-bar.iconSize', newValue => {
3329
this.updateSize(newValue);
3430
}),
3531
atom.config.onDidChange('tool-bar.position', () => {
36-
if (atom.config.get('tool-bar.visible')) {
37-
this.show();
38-
}
32+
this.refresh();
3933
}),
4034
atom.config.onDidChange('tool-bar.visible', ({newValue}) => {
4135
if (newValue) {
@@ -45,9 +39,7 @@ module.exports = class ToolBarView {
4539
}
4640
}),
4741
atom.config.onDidChange('tool-bar.fullWidth', () => {
48-
if (atom.config.get('tool-bar.visible')) {
49-
this.show();
50-
}
42+
this.refresh();
5143
})
5244
);
5345

@@ -147,36 +139,35 @@ module.exports = class ToolBarView {
147139
'tool-bar-vertical'
148140
);
149141

150-
const fullWidth = atom.config.get('tool-bar.fullWidth');
142+
this.panel = this.createPanel(position);
143+
144+
this.element.classList.add(`tool-bar-${position.toLowerCase()}`);
145+
if (position === 'Top' || position === 'Bottom') {
146+
this.element.classList.add('tool-bar-horizontal');
147+
} else {
148+
this.element.classList.add('tool-bar-vertical');
149+
}
150+
151+
this.drawGutter();
152+
atom.config.set('tool-bar.position', position);
153+
}
151154

155+
createPanel (position) {
156+
const fullWidth = atom.config.get('tool-bar.fullWidth');
152157
switch (position) {
153158
case 'Top':
154-
this.panel = fullWidth
159+
return fullWidth
155160
? atom.workspace.addHeaderPanel({item: this.element})
156161
: atom.workspace.addTopPanel({item: this.element});
157-
break;
158162
case 'Right':
159-
this.panel = atom.workspace.addRightPanel({item: this.element});
160-
break;
163+
return atom.workspace.addRightPanel({item: this.element});
161164
case 'Bottom':
162-
this.panel = fullWidth
165+
return fullWidth
163166
? atom.workspace.addFooterPanel({item: this.element})
164167
: atom.workspace.addBottomPanel({item: this.element});
165-
break;
166168
case 'Left':
167-
this.panel = atom.workspace.addLeftPanel({item: this.element, priority: 50});
168-
break;
169-
}
170-
171-
const classNames = [`tool-bar-${position.toLowerCase()}`];
172-
if (position === 'Top' || position === 'Bottom') {
173-
classNames.push('tool-bar-horizontal');
174-
} else {
175-
classNames.push('tool-bar-vertical');
169+
return atom.workspace.addLeftPanel({item: this.element, priority: 50});
176170
}
177-
this.element.classList.add(...classNames);
178-
179-
this.drawGutter();
180171
}
181172

182173
drawGutter () {
@@ -212,6 +203,12 @@ module.exports = class ToolBarView {
212203
this.updateSize(atom.config.get('tool-bar.iconSize'));
213204
}
214205

206+
refresh () {
207+
if (atom.config.get('tool-bar.visible')) {
208+
this.show();
209+
}
210+
}
211+
215212
toggle () {
216213
atom.config.set('tool-bar.visible', !atom.config.get('tool-bar.visible'));
217214
}

0 commit comments

Comments
 (0)