Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a72ce55

Browse files
committed
adapt polar main drag to dragElement updates (+ tests)
1 parent b365374 commit a72ce55

File tree

2 files changed

+176
-29
lines changed

2 files changed

+176
-29
lines changed

src/plots/polar/polar.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,10 @@ proto.updateMainDrag = function(fullLayout, polarLayout) {
679679
dimmed = true;
680680
}
681681

682-
function zoomDone(dragged, numClicks) {
682+
function zoomDone() {
683683
dragBox.removeZoombox(gd);
684684

685-
if(r0 === null || r1 === null) {
686-
if(numClicks === 2) doubleClick();
687-
return;
688-
}
685+
if(r0 === null || r1 === null) return;
689686

690687
dragBox.showDoubleClickNotifier(gd);
691688

@@ -756,32 +753,16 @@ proto.updateMainDrag = function(fullLayout, polarLayout) {
756753
}
757754
}
758755

759-
function panDone(dragged, numClicks) {
760-
if(dragged) {
761-
var updateObj = {};
762-
updateObj[_this.id + '.angularaxis.rotation'] = rot1;
763-
Plotly.relayout(gd, updateObj);
764-
} else if(numClicks === 2) {
765-
doubleClick();
766-
}
756+
function panDone() {
757+
var updateObj = {};
758+
updateObj[_this.id + '.angularaxis.rotation'] = rot1;
759+
Plotly.relayout(gd, updateObj);
767760
}
768761

769762
function zoomWheel() {
770763
// TODO
771764
}
772765

773-
function doubleClick() {
774-
gd.emit('plotly_doubleclick', null);
775-
// TODO double once vs twice logic (autorange vs fixed range)
776-
777-
var updateObj = {};
778-
for(var k in _this.viewInitial) {
779-
updateObj[_this.id + '.' + k] = _this.viewInitial[k];
780-
}
781-
782-
Plotly.relayout(gd, updateObj);
783-
}
784-
785766
dragOpts.prepFn = function(evt, startX, startY) {
786767
var dragModeNow = gd._fullLayout.dragmode;
787768

@@ -807,6 +788,23 @@ proto.updateMainDrag = function(fullLayout, polarLayout) {
807788
}
808789
};
809790

791+
dragOpts.clickFn = function(numClicks, evt) {
792+
dragBox.removeZoombox(gd);
793+
794+
// TODO double once vs twice logic (autorange vs fixed range)
795+
if(numClicks === 2) {
796+
var updateObj = {};
797+
for(var k in _this.viewInitial) {
798+
updateObj[_this.id + '.' + k] = _this.viewInitial[k];
799+
}
800+
801+
gd.emit('plotly_doubleclick', null);
802+
Plotly.relayout(gd, updateObj);
803+
}
804+
805+
Fx.click(gd, evt, _this.id);
806+
};
807+
810808
mainDrag.onmousemove = function(evt) {
811809
Fx.hover(gd, evt, _this.id);
812810
gd._fullLayout._lasthover = mainDrag;
@@ -818,10 +816,6 @@ proto.updateMainDrag = function(fullLayout, polarLayout) {
818816
dragElement.unhover(gd, evt);
819817
};
820818

821-
mainDrag.onclick = function(evt) {
822-
Fx.click(gd, evt, _this.id);
823-
};
824-
825819
if(gd._context.scaleZoom) {
826820
dragBox.attachWheelEventHandler(mainDrag, zoomWheel);
827821
}

test/jasmine/tests/polar_test.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ var d3 = require('d3');
77
var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
99
var fail = require('../assets/fail_test');
10+
var mouseEvent = require('../assets/mouse_event');
11+
var click = require('../assets/click');
12+
var doubleClick = require('../assets/double_click');
1013

1114
describe('Test legacy polar plots logs:', function() {
1215
var gd;
@@ -496,3 +499,153 @@ describe('Test relayout on polar subplots:', function() {
496499
.then(done);
497500
});
498501
});
502+
503+
describe('Test polar interactions:', function() {
504+
var gd;
505+
var eventData;
506+
var eventCnts;
507+
508+
var eventNames = [
509+
'plotly_hover', 'plotly_unhover',
510+
'plotly_click', 'plotly_doubleclick',
511+
'plotly_relayout'
512+
];
513+
514+
beforeEach(function() {
515+
eventData = '';
516+
eventCnts = {};
517+
gd = createGraphDiv();
518+
});
519+
520+
afterEach(destroyGraphDiv);
521+
522+
function _plot(fig) {
523+
return Plotly.plot(gd, fig).then(function() {
524+
eventNames.forEach(function(k) {
525+
eventCnts[k] = 0;
526+
gd.on(k, function(d) {
527+
eventData = d;
528+
eventCnts[k]++;
529+
Lib.clearThrottle();
530+
});
531+
});
532+
});
533+
}
534+
535+
function assertEventPointData(expected, msg) {
536+
var actual = eventData.points || [];
537+
538+
expect(actual.length)
539+
.toBe(expected.length, msg + ' same number of pts');
540+
541+
expected.forEach(function(e, i) {
542+
var a = actual[i];
543+
var m = msg + ' (pt ' + i + ')';
544+
545+
for(var k in e) {
546+
expect(a[k]).toBeCloseTo(e[k], 1, m + ' ' + k);
547+
}
548+
});
549+
}
550+
551+
function assertEventCnt(expected, msg) {
552+
eventNames.forEach(function(k) {
553+
var m = msg + ' event cnt for ' + k;
554+
555+
if(k in expected) {
556+
expect(eventCnts[k]).toBe(expected[k], m);
557+
} else {
558+
expect(eventCnts[k]).toBe(0, m);
559+
}
560+
});
561+
}
562+
563+
function _hover(pos) {
564+
eventData = '';
565+
mouseEvent('mousemove', pos[0], pos[1]);
566+
}
567+
568+
function _unhover(pos) {
569+
eventData = '';
570+
mouseEvent('mouseout', pos[0], pos[1]);
571+
}
572+
573+
function _click(pos) {
574+
eventData = '';
575+
gd._mouseDownTime = 0;
576+
click(pos[0], pos[1]);
577+
}
578+
579+
function _doubleClick(pos) {
580+
gd._mouseDownTime = 0;
581+
eventData = '';
582+
return doubleClick(pos[0], pos[1]);
583+
}
584+
585+
it('should trigger hover/unhover/click/doubleclick events', function(done) {
586+
var fig = Lib.extendDeep({}, require('@mocks/polar_scatter.json'));
587+
var ptPos = [250, 200];
588+
var blankPos = [200, 120];
589+
var marginPos = [20, 20];
590+
591+
function _assert(ptExpectation, cntExpecation, msg) {
592+
if(Array.isArray(ptExpectation)) {
593+
assertEventPointData(ptExpectation, msg);
594+
} else {
595+
expect(eventData).toBe(ptExpectation, msg);
596+
}
597+
assertEventCnt(cntExpecation, msg);
598+
}
599+
600+
_plot(fig)
601+
.then(function() { _hover(ptPos); })
602+
.then(function() {
603+
_assert([{
604+
r: 3.26,
605+
theta: 68.08
606+
}], {
607+
plotly_hover: 1
608+
}, 'after hover on pt');
609+
})
610+
.then(function() { _unhover(blankPos);})
611+
.then(function() {
612+
_assert([{
613+
r: 3.26,
614+
theta: 68.08
615+
}], {
616+
plotly_hover: 1,
617+
plotly_unhover: 1
618+
}, 'after unhover off pt');
619+
})
620+
.then(function() { _hover(marginPos);})
621+
.then(function() {
622+
_assert('', {
623+
plotly_hover: 1,
624+
plotly_unhover: 1,
625+
}, 'after hovering in margin');
626+
})
627+
.then(function() { _click(ptPos); })
628+
.then(function() {
629+
_assert([{
630+
r: 3.26,
631+
theta: 68.08
632+
}], {
633+
plotly_hover: 2,
634+
plotly_unhover: 1,
635+
plotly_click: 1
636+
}, 'after click');
637+
})
638+
.then(function() { return _doubleClick(ptPos); })
639+
.then(function() {
640+
assertEventCnt({
641+
plotly_hover: 2,
642+
plotly_unhover: 1,
643+
plotly_click: 3,
644+
plotly_doubleclick: 1,
645+
plotly_relayout: 1
646+
}, 'after doubleclick');
647+
})
648+
.catch(fail)
649+
.then(done);
650+
});
651+
});

0 commit comments

Comments
 (0)