Skip to content

Commit a382098

Browse files
authored
Merge pull request #5269 from plotly/mapbox_layer_order
Fix reordering of mapbox raster/image layers on update
2 parents a51b91f + 4054735 commit a382098

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

src/plots/mapbox/layers.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ proto.needsNewSource = function(opts) {
7373
// stay safe and make new source on type changes
7474
return (
7575
this.sourceType !== opts.sourcetype ||
76-
this.source !== opts.source ||
76+
JSON.stringify(this.source) !== JSON.stringify(opts.source) ||
7777
this.layerType !== opts.type
7878
);
7979
};
@@ -85,11 +85,23 @@ proto.needsNewLayer = function(opts) {
8585
);
8686
};
8787

88+
proto.lookupBelow = function() {
89+
return this.subplot.belowLookup['layout-' + this.index];
90+
};
91+
8892
proto.updateImage = function(opts) {
8993
var map = this.subplot.map;
9094
map.getSource(this.idSource).updateImage({
9195
url: opts.source, coordinates: opts.coordinates
9296
});
97+
98+
// Since the `updateImage` control flow doesn't call updateLayer,
99+
// We need to take care of moving the image layer to match the location
100+
// where updateLayer would have placed it.
101+
var _below = this.findFollowingMapboxLayerId(this.lookupBelow());
102+
if(_below !== null) {
103+
this.subplot.map.moveLayer(this.idLayer, _below);
104+
}
93105
};
94106

95107
proto.updateSource = function(opts) {
@@ -107,29 +119,29 @@ proto.updateSource = function(opts) {
107119
map.addSource(this.idSource, sourceOpts);
108120
};
109121

110-
proto.updateLayer = function(opts) {
111-
var subplot = this.subplot;
112-
var convertedOpts = convertOpts(opts);
113-
114-
var below = this.subplot.belowLookup['layout-' + this.index];
115-
var _below;
116-
122+
proto.findFollowingMapboxLayerId = function(below) {
117123
if(below === 'traces') {
118-
var mapLayers = subplot.getMapLayers();
124+
var mapLayers = this.subplot.getMapLayers();
119125

120126
// find id of first plotly trace layer
121127
for(var i = 0; i < mapLayers.length; i++) {
122128
var layerId = mapLayers[i].id;
123129
if(typeof layerId === 'string' &&
124130
layerId.indexOf(constants.traceLayerPrefix) === 0
125131
) {
126-
_below = layerId;
132+
below = layerId;
127133
break;
128134
}
129135
}
130-
} else {
131-
_below = below;
132136
}
137+
return below;
138+
};
139+
140+
proto.updateLayer = function(opts) {
141+
var subplot = this.subplot;
142+
var convertedOpts = convertOpts(opts);
143+
var below = this.lookupBelow();
144+
var _below = this.findFollowingMapboxLayerId(below);
133145

134146
this.removeLayer();
135147

test/jasmine/tests/mapbox_test.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -948,10 +948,15 @@ describe('@noCI, mapbox plots', function() {
948948
layout: {
949949
mapbox: {
950950
layers: [{
951+
'sourcetype': 'raster',
952+
'source': ['https://a.tile.openstreetmap.org/{z}/{x}/{y}.png'],
953+
'below': 'traces',
954+
}, {
951955
'sourcetype': 'image',
952956
'coordinates': coords,
953-
'source': source
954-
}]
957+
'source': source,
958+
'below': 'traces',
959+
}],
955960
}
956961
}
957962
};
@@ -970,7 +975,7 @@ describe('@noCI, mapbox plots', function() {
970975
Plotly.react(gd, makeFigure(redImage)).then(function() {
971976
var mapbox = gd._fullLayout.mapbox._subplot;
972977
map = mapbox.map;
973-
layerSource = map.getSource(mapbox.layerList[0].idSource);
978+
layerSource = map.getSource(mapbox.layerList[1].idSource);
974979

975980
spyOn(layerSource, 'updateImage').and.callThrough();
976981
spyOn(map, 'removeSource').and.callThrough();
@@ -981,6 +986,23 @@ describe('@noCI, mapbox plots', function() {
981986
{url: greenImage, coordinates: coords}
982987
);
983988
expect(map.removeSource).not.toHaveBeenCalled();
989+
990+
// Check order of layers
991+
var mapbox = gd._fullLayout.mapbox._subplot;
992+
var mapboxLayers = mapbox.getMapLayers();
993+
var plotlyjsLayers = mapbox.layerList;
994+
995+
var indexLower = mapboxLayers.findIndex(function(layer) {
996+
return layer.id === 'plotly-layout-layer-' + plotlyjsLayers[0].uid;
997+
});
998+
999+
var indexUpper = mapboxLayers.findIndex(function(layer) {
1000+
return layer.id === 'plotly-layout-layer-' + plotlyjsLayers[1].uid;
1001+
});
1002+
1003+
expect(indexLower).toBeGreaterThan(-1);
1004+
expect(indexUpper).toBeGreaterThan(0);
1005+
expect(indexUpper).toBe(indexLower + 1);
9841006
})
9851007
.catch(failTest)
9861008
.then(done);

0 commit comments

Comments
 (0)