Skip to content

Mapbox event triggers w/o duplication #901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/plots/mapbox/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,27 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
});

// keep track of pan / zoom in user layout and emit relayout event
map.on('move', function() {
map.on('moveend', function(eventData) {
var view = self.getView();

opts._input.center = opts.center = view.center;
opts._input.zoom = opts.zoom = view.zoom;
opts._input.bearing = opts.bearing = view.bearing;
opts._input.pitch = opts.pitch = view.pitch;

var update = {};
update[self.id] = Lib.extendFlat({}, view);
gd.emit('plotly_relayout', update);
// 'moveend' gets triggered by map.setCenter, map.setZoom,
// map.setBearing and map.setPitch.
//
// Here, we make sure that 'plotly_relayout' is
// triggered here only when the 'moveend' originates from a
// mouse target (filtering out API calls) to not
// duplicate 'plotly_relayout' events.

if(eventData.originalEvent) {
var update = {};
update[self.id] = Lib.extendFlat({}, view);
gd.emit('plotly_relayout', update);
}
});

map.on('mousemove', function(evt) {
Expand Down
51 changes: 50 additions & 1 deletion test/jasmine/tests/mapbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ describe('mapbox plots', function() {
});

it('should be able to restyle', function(done) {
var restyleCnt = 0,
relayoutCnt = 0;

gd.on('plotly_restyle', function() {
restyleCnt++;
});

gd.on('plotly_relayout', function() {
relayoutCnt++;
});

function assertMarkerColor(expectations) {
return new Promise(function(resolve) {
setTimeout(function() {
Expand All @@ -360,6 +371,9 @@ describe('mapbox plots', function() {
return Plotly.restyle(gd, 'marker.color', 'green');
})
.then(function() {
expect(restyleCnt).toEqual(1);
expect(relayoutCnt).toEqual(0);

return assertMarkerColor([
[0, 0.5019, 0, 1],
[0, 0.5019, 0, 1]
Expand All @@ -369,6 +383,9 @@ describe('mapbox plots', function() {
return Plotly.restyle(gd, 'marker.color', 'red', [1]);
})
.then(function() {
expect(restyleCnt).toEqual(2);
expect(relayoutCnt).toEqual(0);

return assertMarkerColor([
[0, 0.5019, 0, 1],
[1, 0, 0, 1]
Expand All @@ -378,6 +395,17 @@ describe('mapbox plots', function() {
});

it('should be able to relayout', function(done) {
var restyleCnt = 0,
relayoutCnt = 0;

gd.on('plotly_restyle', function() {
restyleCnt++;
});

gd.on('plotly_relayout', function() {
relayoutCnt++;
});

function assertLayout(style, center, zoom, dims) {
var mapInfo = getMapInfo(gd);

Expand All @@ -397,22 +425,37 @@ describe('mapbox plots', function() {
assertLayout('Mapbox Dark', [-4.710, 19.475], 1.234, [80, 100, 908, 270]);

Plotly.relayout(gd, 'mapbox.center', { lon: 0, lat: 0 }).then(function() {
expect(restyleCnt).toEqual(0);
expect(relayoutCnt).toEqual(1);

assertLayout('Mapbox Dark', [0, 0], 1.234, [80, 100, 908, 270]);

return Plotly.relayout(gd, 'mapbox.zoom', '6');
}).then(function() {
expect(restyleCnt).toEqual(0);
expect(relayoutCnt).toEqual(2);

assertLayout('Mapbox Dark', [0, 0], 6, [80, 100, 908, 270]);

return Plotly.relayout(gd, 'mapbox.style', 'light');
}).then(function() {
expect(restyleCnt).toEqual(0);
expect(relayoutCnt).toEqual(3);

assertLayout('Mapbox Light', [0, 0], 6, [80, 100, 908, 270]);

return Plotly.relayout(gd, 'mapbox.domain.x', [0, 0.5]);
}).then(function() {
expect(restyleCnt).toEqual(0);
expect(relayoutCnt).toEqual(4);

assertLayout('Mapbox Light', [0, 0], 6, [80, 100, 454, 270]);

return Plotly.relayout(gd, 'mapbox.domain.y[0]', 0.5);
}).then(function() {
expect(restyleCnt).toEqual(0);
expect(relayoutCnt).toEqual(5);

assertLayout('Mapbox Light', [0, 0], 6, [80, 100, 454, 135]);

done();
Expand Down Expand Up @@ -646,9 +689,11 @@ describe('mapbox plots', function() {
});

it('should respond drag / scroll interactions', function(done) {
var updateData;
var relayoutCnt = 0,
updateData;

gd.on('plotly_relayout', function(eventData) {
relayoutCnt++;
updateData = eventData;
});

Expand All @@ -657,6 +702,9 @@ describe('mapbox plots', function() {
return _mouseEvent('mousedown', p0, noop);
}).then(function() {
return _mouseEvent('mousemove', p1, noop);
}).then(function() {
// repeat mousemove to simulate long dragging motion
return _mouseEvent('mousemove', p1, noop);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}).then(function() {
return _mouseEvent('mouseup', p1, noop);
}).then(function() {
Expand Down Expand Up @@ -689,6 +737,7 @@ describe('mapbox plots', function() {
var p1 = [pointPos[0] + 50, pointPos[1] - 20];

_drag(pointPos, p1, function() {
expect(relayoutCnt).toEqual(1);
assertLayout([-19.651, 13.751], 1.234, { withUpdateData: true });

})
Expand Down