Skip to content
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

A few mapbox fixes #4197

Merged
merged 4 commits into from
Sep 18, 2019
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
1 change: 1 addition & 0 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2657,6 +2657,7 @@ function react(gd, data, layout, config) {
function addFrames() { return exports.addFrames(gd, frames); }

gd = Lib.getGraphDiv(gd);
helpers.clearPromiseQueue(gd);

var oldFullData = gd._fullData;
var oldFullLayout = gd._fullLayout;
Expand Down
21 changes: 15 additions & 6 deletions src/plots/mapbox/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,26 @@ proto.removeLayer = function() {

proto.dispose = function() {
var map = this.subplot.map;
map.removeLayer(this.idLayer);
map.removeSource(this.idSource);
if(map.getLayer(this.idLayer)) map.removeLayer(this.idLayer);
if(map.getSource(this.idSource)) map.removeSource(this.idSource);
};

function isVisible(opts) {
if(!opts.visible) return false;

var source = opts.source;

return opts.visible && (
Lib.isPlainObject(source) ||
((typeof source === 'string' || Array.isArray(source)) && source.length > 0)
);
if(Array.isArray(source) && source.length > 0) {
for(var i = 0; i < source.length; i++) {
if(typeof source[i] !== 'string' || source[i].length === 0) {
return false;
}
}
return true;
}

return Lib.isPlainObject(source) ||
(typeof source === 'string' && source.length > 0);
}

function convertOpts(opts) {
Expand Down
5 changes: 3 additions & 2 deletions src/traces/choropleth/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function makeHoverInfo(pointData, trace, pt) {
if(trace.hovertemplate) return;

var hoverinfo = pt.hi || trace.hoverinfo;
var loc = String(pt.loc);

var parts = (hoverinfo === 'all') ?
attributes.hoverinfo.flags :
Expand All @@ -72,10 +73,10 @@ function makeHoverInfo(pointData, trace, pt) {
var text = [];

if(hasIdAsNameLabel) {
pointData.nameOverride = pt.loc;
pointData.nameOverride = loc;
} else {
if(hasName) pointData.nameOverride = trace.name;
if(hasLocation) text.push(pt.loc);
if(hasLocation) text.push(loc);
}

if(hasZ) {
Expand Down
38 changes: 38 additions & 0 deletions test/jasmine/tests/choroplethmapbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,44 @@ describe('@noCI Test choroplethmapbox hover:', function() {
nums: '10.000',
name: 'PROP::New York',
evtPts: [{location: 'NY', z: 10, pointNumber: 0, curveNumber: 0, properties: {name: 'New York'}}]
}, {
desc: 'with "typeof number" locations[i] and feature id (in *name* label case)',
patch: function() {
var fig = Lib.extendDeep({}, require('@mocks/mapbox_choropleth-raw-geojson.json'));
fig.data.shift();
fig.data[0].locations = [100];
fig.data[0].geojson.id = 100;
return fig;
},
nums: '10',
name: '100',
evtPts: [{location: 100, z: 10, pointNumber: 0, curveNumber: 0}]
}, {
desc: 'with "typeof number" locations[i] and feature id (in *nums* label case)',
patch: function() {
var fig = Lib.extendDeep({}, require('@mocks/mapbox_choropleth-raw-geojson.json'));
fig.data.shift();
fig.data[0].locations = [100];
fig.data[0].geojson.id = 100;
fig.data[0].hoverinfo = 'location+name';
return fig;
},
nums: '100',
name: 'trace 0',
evtPts: [{location: 100, z: 10, pointNumber: 0, curveNumber: 0}]
}, {
desc: 'with "typeof number" locations[i] and feature id (hovertemplate case)',
patch: function() {
var fig = Lib.extendDeep({}, require('@mocks/mapbox_choropleth-raw-geojson.json'));
fig.data.shift();
fig.data[0].locations = [100];
fig.data[0].geojson.id = 100;
fig.data[0].hovertemplate = '### %{location}<extra>%{location} ###</extra>';
return fig;
},
nums: '### 100',
name: '100 ###',
evtPts: [{location: 100, z: 10, pointNumber: 0, curveNumber: 0}]
}];

specs.forEach(function(s) {
Expand Down
74 changes: 74 additions & 0 deletions test/jasmine/tests/mapbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,80 @@ describe('@noCI, mapbox plots', function() {
.then(done);
}, LONG_TIMEOUT_INTERVAL);

it('@gl should not wedge graph after reacting to invalid layer', function(done) {
Plotly.react(gd, [{type: 'scattermapbox'}], {
mapbox: {
layers: [{ source: 'invalid' }]
}
})
.then(function() {
fail('The above Plotly.react promise should be rejected');
})
.catch(function() {
expect(gd._promises.length).toBe(1, 'has 1 rejected promise in queue');
})
.then(function() {
return Plotly.react(gd, [{type: 'scattermapbox'}], {
mapbox: {
layers: [{
sourcetype: 'vector',
sourcelayer: 'contour',
source: 'mapbox://mapbox.mapbox-terrain-v2'
}]
}
});
})
.then(function() {
expect(gd._promises.length).toBe(0, 'rejected promise has been cleared');

var mapInfo = getMapInfo(gd);
expect(mapInfo.layoutLayers.length).toBe(1, 'one layer');
expect(mapInfo.layoutSources.length).toBe(1, 'one layer source');
})
.catch(failTest)
.then(done);
}, LONG_TIMEOUT_INTERVAL);

it('@gl should not attempt to remove non-existing layer sources', function(done) {
function _assert(msg, exp) {
return function() {
var layerList = gd._fullLayout.mapbox._subplot.layerList;
expect(layerList.length).toBe(exp, msg);
};
}

Plotly.react(gd, [{type: 'scattermapbox'}], {
mapbox: { layers: [{}] }
})
.then(_assert('1 visible:false layer', 1))
.then(function() {
return Plotly.react(gd, [{type: 'scattermapbox'}], {
mapbox: { layers: [] }
});
})
.then(_assert('no layers', 0))
.catch(failTest)
.then(done);
}, LONG_TIMEOUT_INTERVAL);

it('@gl should validate layout layer input', function(done) {
Plotly.newPlot(gd, [{type: 'scattermapbox'}], {
mapbox: {
layers: [{
sourcetype: 'raster',
source: ['']
}]
}
})
.then(function() {
var mapInfo = getMapInfo(gd);
expect(mapInfo.layoutLayers.length).toBe(0, 'no on-map layer');
expect(mapInfo.layoutSources.length).toBe(0, 'no map source');
})
.catch(failTest)
.then(done);
}, LONG_TIMEOUT_INTERVAL);

it('@gl 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