Skip to content

Commit a6bd1c6

Browse files
committed
fix: some code climate issues
1 parent 9aba79a commit a6bd1c6

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/handlers.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,46 +86,59 @@ export function mouseDown(chart, event) {
8686
addHandler(chart, window.document, 'keydown', keyDown);
8787
}
8888

89+
function applyDimensions(rect, width, height, beginPoint) {
90+
if (beginPoint.x === rect.left) {
91+
rect.right = rect.left + width;
92+
} else {
93+
rect.left = rect.right - width;
94+
}
95+
if (beginPoint.y === rect.top) {
96+
rect.bottom = rect.top + height;
97+
} else {
98+
rect.top = rect.bottom - height;
99+
}
100+
}
101+
89102
function applyAspectRatio(rect, aspectRatio, beginPoint) {
90103
let width = rect.right - rect.left;
91104
let height = rect.bottom - rect.top;
92105
const ratio = width / height;
93106

94107
if (ratio > aspectRatio) {
95108
width = height * aspectRatio;
96-
if (beginPoint.x === rect.left) {
97-
rect.right = rect.left + width;
98-
} else {
99-
rect.left = rect.right - width;
100-
}
101109
} else if (ratio < aspectRatio) {
102110
height = width / aspectRatio;
103-
if (beginPoint.y === rect.top) {
104-
rect.bottom = rect.top + height;
105-
} else {
106-
rect.top = rect.bottom - height;
107-
}
108111
}
112+
113+
applyDimensions(rect, width, height, beginPoint);
114+
}
115+
116+
function applyX(rect, beginPoint, endPoint) {
117+
rect.left = Math.min(beginPoint.x, endPoint.x);
118+
rect.right = Math.max(beginPoint.x, endPoint.x);
109119
}
110120

111-
export function computeDragRect(chart, mode, beginPointEvent, endPointEvent, maintainAspectRatio) {
121+
function applyY(rect, beginPoint, endPoint) {
122+
rect.top = Math.min(beginPoint.y, endPoint.y);
123+
rect.bottom = Math.max(beginPoint.y, endPoint.y);
124+
}
125+
126+
export function computeDragRect(chart, mode, points, maintainAspectRatio) {
112127
const xEnabled = directionEnabled(mode, 'x', chart);
113128
const yEnabled = directionEnabled(mode, 'y', chart);
114-
let {top, left, right, bottom, width: chartWidth, height: chartHeight} = chart.chartArea;
129+
const {top, left, right, bottom, width: chartWidth, height: chartHeight} = chart.chartArea;
130+
const rect = {top, left, right, bottom};
115131

116-
const beginPoint = getRelativePosition(beginPointEvent, chart);
117-
const endPoint = getRelativePosition(endPointEvent, chart);
132+
const beginPoint = getRelativePosition(points.dragStart, chart);
133+
const endPoint = getRelativePosition(points.dragEnd, chart);
118134

119135
if (xEnabled) {
120-
left = Math.min(beginPoint.x, endPoint.x);
121-
right = Math.max(beginPoint.x, endPoint.x);
136+
applyX(rect, beginPoint, endPoint);
122137
}
123138

124139
if (yEnabled) {
125-
top = Math.min(beginPoint.y, endPoint.y);
126-
bottom = Math.max(beginPoint.y, endPoint.y);
140+
applyY(rect, beginPoint, endPoint);
127141
}
128-
const rect = {top, left, right, bottom};
129142

130143
if (xEnabled && yEnabled && maintainAspectRatio) {
131144
applyAspectRatio(rect, chartWidth / chartHeight, beginPoint);
@@ -151,7 +164,7 @@ export function mouseUp(chart, event) {
151164

152165
removeHandler(chart, 'mousemove');
153166
const {mode, onZoomComplete, drag: {threshold = 0, maintainAspectRatio}} = state.options.zoom;
154-
const rect = computeDragRect(chart, mode, state.dragStart, event, maintainAspectRatio);
167+
const rect = computeDragRect(chart, mode, {dragStart: state.dragStart, dragEnd: event}, maintainAspectRatio);
155168
const distanceX = directionEnabled(mode, 'x', chart) ? rect.width : 0;
156169
const distanceY = directionEnabled(mode, 'y', chart) ? rect.height : 0;
157170
const distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);

src/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function draw(chart, caller, options) {
1313
if (dragOptions.drawTime !== caller || !dragEnd) {
1414
return;
1515
}
16-
const {left, top, width, height} = computeDragRect(chart, options.zoom.mode, dragStart, dragEnd, dragOptions.maintainAspectRatio);
16+
const {left, top, width, height} = computeDragRect(chart, options.zoom.mode, {dragStart, dragEnd}, dragOptions.maintainAspectRatio);
1717
const ctx = chart.ctx;
1818

1919
ctx.save();

0 commit comments

Comments
 (0)