Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions src/plots/mapbox/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ proto.update = function update(opts) {
this.updateLayer(opts);
} else if(this.needsNewSource(opts)) {
// IMPORTANT: must delete layer before source to not cause errors
this.updateLayer(opts);
this.removeLayer();
this.updateSource(opts);
this.updateLayer(opts);
} else if(this.needsNewLayer(opts)) {
this.updateLayer(opts);
} else {
Expand Down Expand Up @@ -87,8 +88,7 @@ proto.updateLayer = function(opts) {
var map = this.map;
var convertedOpts = convertOpts(opts);

if(map.getLayer(this.idLayer)) map.removeLayer(this.idLayer);

this.removeLayer();
this.layerType = opts.type;

if(isVisible(opts)) {
Expand All @@ -111,6 +111,13 @@ proto.updateStyle = function(opts) {
}
};

proto.removeLayer = function() {
var map = this.map;
if(map.getLayer(this.idLayer)) {
map.removeLayer(this.idLayer);
}
};

proto.dispose = function dispose() {
var map = this.map;
map.removeLayer(this.idLayer);
Expand Down
50 changes: 50 additions & 0 deletions test/jasmine/tests/mapbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,56 @@ describe('@noCI, mapbox plots', function() {
.then(done);
}, LONG_TIMEOUT_INTERVAL);

it('should be able to react to layer changes', function(done) {
function makeFigure(color) {
return {
data: [{type: 'scattermapbox'}],
layout: {
mapbox: {
layers: [{
color: color,
sourcetype: 'geojson',
type: 'fill',
source: {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [[
[174.74475860595703, -36.86533886128865],
[174.77737426757812, -36.86533886128865],
[174.77737426757812, -36.84913134182603],
[174.74475860595703, -36.84913134182603],
[174.74475860595703, -36.86533886128865]
]]
}
}
}]
}
}
};
}

function _assert(color) {
var mapInfo = getMapInfo(gd);
var layer = mapInfo.layers[mapInfo.layoutLayers[0]];

expect(mapInfo.layoutLayers.length).toBe(1, 'one layer');
expect(mapInfo.layoutSources.length).toBe(1, 'one layer source');
expect(String(layer.paint._values['fill-color'].value.value)).toBe(color, 'layer color');
}

Plotly.react(gd, makeFigure('blue')).then(function() {
_assert('rgba(0,0,255,1)');
return Plotly.react(gd, makeFigure('red'));
})
.then(function() {
_assert('rgba(255,0,0,1)');
})
.catch(failTest)
.then(done);
}, LONG_TIMEOUT_INTERVAL);

it('should be able to update the access token', function(done) {
Plotly.relayout(gd, 'mapbox.accesstoken', 'wont-work').catch(function(err) {
expect(gd._fullLayout.mapbox.accesstoken).toEqual('wont-work');
Expand Down