Skip to content

Commit 9506872

Browse files
committed
DRY circular <-> linear onMove zoombox handlers
1 parent 3a94ebd commit 9506872

File tree

1 file changed

+34
-51
lines changed

1 file changed

+34
-51
lines changed

src/plots/polar/polar.js

+34-51
Original file line numberDiff line numberDiff line change
@@ -688,20 +688,10 @@ proto.updateMainDrag = function(fullLayout, polarLayout) {
688688
clearSelect(zoomlayer);
689689
}
690690

691-
function applyZoomMove(path1, cpath) {
692-
zb.attr('d', path1);
693-
corners.attr('d', cpath);
694-
dragBox.transitionZoombox(zb, corners, dimmed, lum);
695-
dimmed = true;
696-
}
697-
698-
function zoomMove(dx, dy) {
699-
var x1 = x0 + dx;
700-
var y1 = y0 + dy;
701-
var rr0 = xy2r(x0, y0);
702-
var rr1 = Math.min(xy2r(x1, y1), radius);
703-
var a0 = xy2a(x0, y0);
704-
var a1 = xy2a(x1, y1);
691+
// N.B. this sets scoped 'r0' and 'r1'
692+
// return true if 'valid' zoom distance, false otherwise
693+
function clampAndSetR0R1(rr0, rr1) {
694+
rr1 = Math.min(rr1, radius);
705695

706696
// starting or ending drag near center (outer edge),
707697
// clamps radial distance at origin (at r=radius)
@@ -710,31 +700,48 @@ proto.updateMainDrag = function(fullLayout, polarLayout) {
710700
else if(rr1 < OFFEDGE) rr1 = 0;
711701
else if((radius - rr1) < OFFEDGE) rr1 = radius;
712702

713-
var path1;
714-
var cpath;
715-
703+
// make sure r0 < r1,
704+
// to get correct fill pattern in path1 below
716705
if(Math.abs(rr1 - rr0) > MINZOOM) {
717-
// make sure r0 < r1,
718-
// to get correct fill pattern in path1 below
719706
if(rr0 < rr1) {
720707
r0 = rr0;
721708
r1 = rr1;
722709
} else {
723710
r0 = rr1;
724711
r1 = rr0;
725-
a1 = [a0, a0 = a1][0]; // swap a0 and a1
726712
}
727-
728-
path1 = path0 + _pathSectorClosed(r1) + _pathSectorClosed(r0);
729-
// keep 'starting' angle
730-
cpath = pathCorner(r0, a0) + pathCorner(r1, a0);
713+
return true;
731714
} else {
732715
r0 = null;
733716
r1 = null;
734-
path1 = path0;
735-
cpath = 'M0,0Z';
717+
return false;
736718
}
719+
}
720+
721+
function applyZoomMove(path1, cpath) {
722+
path1 = path1 || path0;
723+
cpath = cpath || 'M0,0Z';
724+
725+
zb.attr('d', path1);
726+
corners.attr('d', cpath);
727+
dragBox.transitionZoombox(zb, corners, dimmed, lum);
728+
dimmed = true;
729+
}
730+
731+
function zoomMove(dx, dy) {
732+
var x1 = x0 + dx;
733+
var y1 = y0 + dy;
734+
var rr0 = xy2r(x0, y0);
735+
var rr1 = Math.min(xy2r(x1, y1), radius);
736+
var a0 = xy2a(x0, y0);
737+
var path1;
738+
var cpath;
737739

740+
if(clampAndSetR0R1(rr0, rr1)) {
741+
path1 = path0 + _pathSectorClosed(r1) + _pathSectorClosed(r0);
742+
// keep 'starting' angle
743+
cpath = pathCorner(r0, a0) + pathCorner(r1, a0);
744+
}
738745
applyZoomMove(path1, cpath);
739746
}
740747

@@ -762,41 +769,17 @@ proto.updateMainDrag = function(fullLayout, polarLayout) {
762769
var vangles1 = findEnclosingVertexAngles(a1);
763770
var rr0 = findPolygonRadius(x0, y0, vangles0[0], vangles0[1]);
764771
var rr1 = Math.min(findPolygonRadius(x1, y1, vangles1[0], vangles1[1]), radius);
765-
766-
// starting or ending drag near center (outer edge),
767-
// clamps radial distance at origin (at r=radius)
768-
if(rr0 < OFFEDGE) rr0 = 0;
769-
else if((radius - rr0) < OFFEDGE) rr0 = radius;
770-
else if(rr1 < OFFEDGE) rr1 = 0;
771-
else if((radius - rr1) < OFFEDGE) rr1 = radius;
772-
773772
var path1;
774773
var cpath;
775774

776-
if(Math.abs(rr1 - rr0) > MINZOOM) {
777-
// make sure r0 < r1,
778-
// to get correct fill pattern in path1 below
779-
if(rr0 < rr1) {
780-
r0 = rr0;
781-
r1 = rr1;
782-
} else {
783-
r0 = rr1;
784-
r1 = rr0;
785-
}
786-
775+
if(clampAndSetR0R1(rr0, rr1)) {
787776
path1 = path0 + _pathSectorClosed(r1) + _pathSectorClosed(r0);
788777
// keep 'starting' angle here too
789778
cpath = [
790779
pathCornerForPolygons(r0, vangles0[0], vangles0[1]),
791780
pathCornerForPolygons(r1, vangles0[0], vangles0[1])
792781
].join(' ');
793-
} else {
794-
r0 = null;
795-
r1 = null;
796-
path1 = path0;
797-
cpath = 'M0,0Z';
798782
}
799-
800783
applyZoomMove(path1, cpath);
801784
}
802785

0 commit comments

Comments
 (0)