Skip to content

Commit a6f8343

Browse files
committed
Merge pull request #901 from plotly/mapbox-on-move-end
Mapbox event triggers w/o duplication
1 parent 9164679 commit a6f8343

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/plots/mapbox/mapbox.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,27 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
125125
});
126126

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

131131
opts._input.center = opts.center = view.center;
132132
opts._input.zoom = opts.zoom = view.zoom;
133133
opts._input.bearing = opts.bearing = view.bearing;
134134
opts._input.pitch = opts.pitch = view.pitch;
135135

136-
var update = {};
137-
update[self.id] = Lib.extendFlat({}, view);
138-
gd.emit('plotly_relayout', update);
136+
// 'moveend' gets triggered by map.setCenter, map.setZoom,
137+
// map.setBearing and map.setPitch.
138+
//
139+
// Here, we make sure that 'plotly_relayout' is
140+
// triggered here only when the 'moveend' originates from a
141+
// mouse target (filtering out API calls) to not
142+
// duplicate 'plotly_relayout' events.
143+
144+
if(eventData.originalEvent) {
145+
var update = {};
146+
update[self.id] = Lib.extendFlat({}, view);
147+
gd.emit('plotly_relayout', update);
148+
}
139149
});
140150

141151
map.on('mousemove', function(evt) {

test/jasmine/tests/mapbox_test.js

+57-1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ describe('mapbox plots', function() {
295295
}).then(function() {
296296
expect(countVisibleTraces(gd, modes)).toEqual(2);
297297

298+
var mockCopy = Lib.extendDeep({}, mock);
299+
mockCopy.data[0].visible = false;
300+
301+
return Plotly.newPlot(gd, mockCopy.data, mockCopy.layout);
302+
}).then(function() {
303+
expect(countVisibleTraces(gd, modes)).toEqual(1);
304+
298305
done();
299306
});
300307
});
@@ -338,6 +345,17 @@ describe('mapbox plots', function() {
338345
});
339346

340347
it('should be able to restyle', function(done) {
348+
var restyleCnt = 0,
349+
relayoutCnt = 0;
350+
351+
gd.on('plotly_restyle', function() {
352+
restyleCnt++;
353+
});
354+
355+
gd.on('plotly_relayout', function() {
356+
relayoutCnt++;
357+
});
358+
341359
function assertMarkerColor(expectations) {
342360
return new Promise(function(resolve) {
343361
setTimeout(function() {
@@ -360,6 +378,9 @@ describe('mapbox plots', function() {
360378
return Plotly.restyle(gd, 'marker.color', 'green');
361379
})
362380
.then(function() {
381+
expect(restyleCnt).toEqual(1);
382+
expect(relayoutCnt).toEqual(0);
383+
363384
return assertMarkerColor([
364385
[0, 0.5019, 0, 1],
365386
[0, 0.5019, 0, 1]
@@ -369,6 +390,9 @@ describe('mapbox plots', function() {
369390
return Plotly.restyle(gd, 'marker.color', 'red', [1]);
370391
})
371392
.then(function() {
393+
expect(restyleCnt).toEqual(2);
394+
expect(relayoutCnt).toEqual(0);
395+
372396
return assertMarkerColor([
373397
[0, 0.5019, 0, 1],
374398
[1, 0, 0, 1]
@@ -378,6 +402,17 @@ describe('mapbox plots', function() {
378402
});
379403

380404
it('should be able to relayout', function(done) {
405+
var restyleCnt = 0,
406+
relayoutCnt = 0;
407+
408+
gd.on('plotly_restyle', function() {
409+
restyleCnt++;
410+
});
411+
412+
gd.on('plotly_relayout', function() {
413+
relayoutCnt++;
414+
});
415+
381416
function assertLayout(style, center, zoom, dims) {
382417
var mapInfo = getMapInfo(gd);
383418

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

399434
Plotly.relayout(gd, 'mapbox.center', { lon: 0, lat: 0 }).then(function() {
435+
expect(restyleCnt).toEqual(0);
436+
expect(relayoutCnt).toEqual(1);
437+
400438
assertLayout('Mapbox Dark', [0, 0], 1.234, [80, 100, 908, 270]);
401439

402440
return Plotly.relayout(gd, 'mapbox.zoom', '6');
403441
}).then(function() {
442+
expect(restyleCnt).toEqual(0);
443+
expect(relayoutCnt).toEqual(2);
444+
404445
assertLayout('Mapbox Dark', [0, 0], 6, [80, 100, 908, 270]);
405446

406447
return Plotly.relayout(gd, 'mapbox.style', 'light');
407448
}).then(function() {
449+
expect(restyleCnt).toEqual(0);
450+
expect(relayoutCnt).toEqual(3);
451+
408452
assertLayout('Mapbox Light', [0, 0], 6, [80, 100, 908, 270]);
409453

410454
return Plotly.relayout(gd, 'mapbox.domain.x', [0, 0.5]);
411455
}).then(function() {
456+
expect(restyleCnt).toEqual(0);
457+
expect(relayoutCnt).toEqual(4);
458+
412459
assertLayout('Mapbox Light', [0, 0], 6, [80, 100, 454, 270]);
413460

414461
return Plotly.relayout(gd, 'mapbox.domain.y[0]', 0.5);
415462
}).then(function() {
463+
expect(restyleCnt).toEqual(0);
464+
expect(relayoutCnt).toEqual(5);
465+
416466
assertLayout('Mapbox Light', [0, 0], 6, [80, 100, 454, 135]);
417467

418468
done();
@@ -646,9 +696,11 @@ describe('mapbox plots', function() {
646696
});
647697

648698
it('should respond drag / scroll interactions', function(done) {
649-
var updateData;
699+
var relayoutCnt = 0,
700+
updateData;
650701

651702
gd.on('plotly_relayout', function(eventData) {
703+
relayoutCnt++;
652704
updateData = eventData;
653705
});
654706

@@ -657,6 +709,9 @@ describe('mapbox plots', function() {
657709
return _mouseEvent('mousedown', p0, noop);
658710
}).then(function() {
659711
return _mouseEvent('mousemove', p1, noop);
712+
}).then(function() {
713+
// repeat mousemove to simulate long dragging motion
714+
return _mouseEvent('mousemove', p1, noop);
660715
}).then(function() {
661716
return _mouseEvent('mouseup', p1, noop);
662717
}).then(function() {
@@ -689,6 +744,7 @@ describe('mapbox plots', function() {
689744
var p1 = [pointPos[0] + 50, pointPos[1] - 20];
690745

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

694750
})

0 commit comments

Comments
 (0)