Skip to content

Commit 2eccfbd

Browse files
authored
Merge pull request #901 from plotly/mapbox-on-move-end
Mapbox event triggers w/o duplication
2 parents 4232ab1 + 34cf22e commit 2eccfbd

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
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

+53-3
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,10 @@ describe('mapbox plots', function() {
295295
}).then(function() {
296296
expect(countVisibleTraces(gd, modes)).toEqual(2);
297297

298-
mock.data[0].visible = false;
298+
var mockCopy = Lib.extendDeep({}, mock);
299+
mockCopy.data[0].visible = false;
299300

300-
return Plotly.newPlot(gd, mock.data, mock.layout);
301+
return Plotly.newPlot(gd, mockCopy.data, mockCopy.layout);
301302
}).then(function() {
302303
expect(countVisibleTraces(gd, modes)).toEqual(1);
303304

@@ -344,6 +345,17 @@ describe('mapbox plots', function() {
344345
});
345346

346347
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+
347359
function assertMarkerColor(expectations) {
348360
return new Promise(function(resolve) {
349361
setTimeout(function() {
@@ -366,6 +378,9 @@ describe('mapbox plots', function() {
366378
return Plotly.restyle(gd, 'marker.color', 'green');
367379
})
368380
.then(function() {
381+
expect(restyleCnt).toEqual(1);
382+
expect(relayoutCnt).toEqual(0);
383+
369384
return assertMarkerColor([
370385
[0, 0.5019, 0, 1],
371386
[0, 0.5019, 0, 1]
@@ -375,6 +390,9 @@ describe('mapbox plots', function() {
375390
return Plotly.restyle(gd, 'marker.color', 'red', [1]);
376391
})
377392
.then(function() {
393+
expect(restyleCnt).toEqual(2);
394+
expect(relayoutCnt).toEqual(0);
395+
378396
return assertMarkerColor([
379397
[0, 0.5019, 0, 1],
380398
[1, 0, 0, 1]
@@ -384,6 +402,17 @@ describe('mapbox plots', function() {
384402
});
385403

386404
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+
387416
function assertLayout(style, center, zoom, dims) {
388417
var mapInfo = getMapInfo(gd);
389418

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

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

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

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

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

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

424468
done();
@@ -652,9 +696,11 @@ describe('mapbox plots', function() {
652696
});
653697

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

657702
gd.on('plotly_relayout', function(eventData) {
703+
relayoutCnt++;
658704
updateData = eventData;
659705
});
660706

@@ -663,6 +709,9 @@ describe('mapbox plots', function() {
663709
return _mouseEvent('mousedown', p0, noop);
664710
}).then(function() {
665711
return _mouseEvent('mousemove', p1, noop);
712+
}).then(function() {
713+
// repeat mousemove to simulate long dragging motion
714+
return _mouseEvent('mousemove', p1, noop);
666715
}).then(function() {
667716
return _mouseEvent('mouseup', p1, noop);
668717
}).then(function() {
@@ -695,6 +744,7 @@ describe('mapbox plots', function() {
695744
var p1 = [pointPos[0] + 50, pointPos[1] - 20];
696745

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

700750
})

0 commit comments

Comments
 (0)