Skip to content

Commit 2e39d70

Browse files
committed
fix: some code climate issues
1 parent 8742a24 commit 2e39d70

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

src/handlers.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,49 +96,52 @@ export function mouseDown(chart, event) {
9696
addHandler(chart, window.document, 'keydown', keyDown);
9797
}
9898

99-
function applyAspectRatio(rect, aspectRatio, beginPoint) {
100-
let width = rect.right - rect.left;
101-
let height = rect.bottom - rect.top;
102-
const ratio = width / height;
99+
function applyAspectRatio(endPoint, beginPoint, aspectRatio) {
100+
let width = endPoint.x - beginPoint.x;
101+
let height = endPoint.y - beginPoint.y;
102+
const ratio = Math.abs(width / height);
103103

104104
if (ratio > aspectRatio) {
105-
width = height * aspectRatio;
106-
if (beginPoint.x === rect.left) {
107-
rect.right = rect.left + width;
108-
} else {
109-
rect.left = rect.right - width;
110-
}
105+
width = Math.sign(width) * Math.abs(height * aspectRatio);
111106
} else if (ratio < aspectRatio) {
112-
height = width / aspectRatio;
113-
if (beginPoint.y === rect.top) {
114-
rect.bottom = rect.top + height;
115-
} else {
116-
rect.top = rect.bottom - height;
117-
}
107+
height = Math.sign(height) * Math.abs(width / aspectRatio);
108+
}
109+
110+
endPoint.x = beginPoint.x + width;
111+
endPoint.y = beginPoint.y + height;
112+
}
113+
114+
function applyMinMaxProps(rect, beginPoint, endPoint, {min, max, prop}) {
115+
rect[min] = Math.max(0, Math.min(beginPoint[prop], endPoint[prop]));
116+
rect[max] = Math.max(beginPoint[prop], endPoint[prop]);
117+
}
118+
119+
function getReplativePoints(chart, points, maintainAspectRatio) {
120+
const beginPoint = getPointPosition(points.dragStart, chart);
121+
const endPoint = getPointPosition(points.dragEnd, chart);
122+
123+
if (maintainAspectRatio) {
124+
const aspectRatio = chart.chartArea.width / chart.chartArea.height;
125+
applyAspectRatio(endPoint, beginPoint, aspectRatio);
118126
}
127+
128+
return {beginPoint, endPoint};
119129
}
120130

121-
export function computeDragRect(chart, mode, beginPointEvent, endPointEvent, maintainAspectRatio) {
131+
export function computeDragRect(chart, mode, points, maintainAspectRatio) {
122132
const xEnabled = directionEnabled(mode, 'x', chart);
123133
const yEnabled = directionEnabled(mode, 'y', chart);
124-
let {top, left, right, bottom, width: chartWidth, height: chartHeight} = chart.chartArea;
134+
const {top, left, right, bottom, width: chartWidth, height: chartHeight} = chart.chartArea;
135+
const rect = {top, left, right, bottom};
125136

126-
const beginPoint = getPointPosition(beginPointEvent, chart);
127-
const endPoint = getPointPosition(endPointEvent, chart);
137+
const {beginPoint, endPoint} = getReplativePoints(chart, points, maintainAspectRatio && xEnabled && yEnabled);
128138

129139
if (xEnabled) {
130-
left = Math.max(0, Math.min(beginPoint.x, endPoint.x));
131-
right = Math.min(chart.width, Math.max(beginPoint.x, endPoint.x));
140+
applyMinMaxProps(rect, beginPoint, endPoint, {min: 'left', max: 'right', prop: 'x'});
132141
}
133142

134143
if (yEnabled) {
135-
top = Math.max(0, Math.min(beginPoint.y, endPoint.y));
136-
bottom = Math.min(chart.height, Math.max(beginPoint.y, endPoint.y));
137-
}
138-
const rect = {top, left, right, bottom};
139-
140-
if (xEnabled && yEnabled && maintainAspectRatio) {
141-
applyAspectRatio(rect, chartWidth / chartHeight, beginPoint);
144+
applyMinMaxProps(rect, beginPoint, endPoint, {min: 'top', max: 'bottom', prop: 'y'});
142145
}
143146

144147
const width = rect.right - rect.left;
@@ -161,7 +164,7 @@ export function mouseUp(chart, event) {
161164

162165
removeHandler(chart, 'mousemove');
163166
const {mode, onZoomComplete, drag: {threshold = 0, maintainAspectRatio}} = state.options.zoom;
164-
const rect = computeDragRect(chart, mode, state.dragStart, event, maintainAspectRatio);
167+
const rect = computeDragRect(chart, mode, {dragStart: state.dragStart, dragEnd: event}, maintainAspectRatio);
165168
const distanceX = directionEnabled(mode, 'x', chart) ? rect.width : 0;
166169
const distanceY = directionEnabled(mode, 'y', chart) ? rect.height : 0;
167170
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)