Skip to content

React mapbox style #4720

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

Merged
merged 3 commits into from
Apr 6, 2020
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
6 changes: 3 additions & 3 deletions src/plots/mapbox/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ proto.plot = function(calcData, fullLayout, promises) {
self.map.remove();
self.map = null;
self.styleObj = null;
self.traceHash = [];
self.layerList = {};
self.traceHash = {};
self.layerList = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂

}

var promise;
Expand Down Expand Up @@ -151,7 +151,7 @@ proto.updateMap = function(calcData, fullLayout, resolve, reject) {
var promises = [];
var styleObj = getStyleObj(opts.style);

if(self.styleObj.id !== styleObj.id) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I was concerned about order of keys:

JSON.stringify({a: 1, b: 2}) !== JSON.stringify({b: 2, a: 1})

But I see we're making styleObj in a predictable order in the getStyleObj function, so this should be robust 🎉

if(JSON.stringify(self.styleObj) !== JSON.stringify(styleObj)) {
self.styleObj = styleObj;
map.setStyle(styleObj.style);

Expand Down
155 changes: 116 additions & 39 deletions test/jasmine/tests/mapbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1551,45 +1551,6 @@ describe('@noCI, mapbox plots', function() {
});
});

function getMapInfo(gd) {
var subplot = gd._fullLayout.mapbox._subplot;
var map = subplot.map;

var sources = map.style.sourceCaches;
var layers = map.style._layers;
var uid = subplot.uid;

var traceSources = Object.keys(sources).filter(function(k) {
return k.indexOf('source-') === 0;
});

var traceLayers = Object.keys(layers).filter(function(k) {
return k.indexOf('plotly-trace-layer-') === 0;
});

var layoutSources = Object.keys(sources).filter(function(k) {
return k.indexOf(uid) !== -1;
});

var layoutLayers = Object.keys(layers).filter(function(k) {
return k.indexOf(uid) !== -1;
});

return {
map: map,
div: subplot.div,
sources: sources,
layers: layers,
traceSources: traceSources,
traceLayers: traceLayers,
layoutSources: layoutSources,
layoutLayers: layoutLayers,
center: map.getCenter(),
zoom: map.getZoom(),
style: map.getStyle()
};
}

function countVisibleTraces(gd, modes) {
var mapInfo = getMapInfo(gd);
var cnts = [];
Expand Down Expand Up @@ -1683,6 +1644,83 @@ describe('@noCI, mapbox plots', function() {
}
});

describe('@noCI, mapbox react', function() {
var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});

it('@gl should be able to react to new tiles', function(done) {
function assertTile(link) {
var mapInfo = getMapInfo(gd);
expect(mapInfo.style.sources.REF.tiles[0]).toEqual(link);
}

var firstLink = 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png';
var secondLink = 'https://a.tile.stamen.com/watercolor/{z}/{x}/{y}.jpg';

var fig = {
data: [
{
type: 'scattermapbox',
lon: [10, 20],
lat: [20, 10]
}
],
layout: {
mapbox: {
style: {
version: 8,
sources: {
REF: {
type: 'raster',
tileSize: 256,
tiles: [firstLink]
}
},
layers: [{
id: 'REF',
source: 'REF',
type: 'raster'
}],
}
}
}
};

Plotly.newPlot(gd, fig)
.then(function() {
assertTile(firstLink);

// copy figure
var newFig = JSON.parse(JSON.stringify(fig));

// new figure
newFig.layout.mapbox.style.sources = {
REF: {
type: 'raster',
tileSize: 256,
tiles: [secondLink]
}
};

// update
Plotly.react(gd, newFig);
})
.then(function() {
assertTile(secondLink);
})
.catch(failTest)
.then(done);
}, LONG_TIMEOUT_INTERVAL);
});

describe('@noCI test mapbox trace/layout *below* interactions', function() {
var gd;

Expand Down Expand Up @@ -2088,3 +2126,42 @@ describe('@noCI, mapbox toImage', function() {
.then(done);
}, LONG_TIMEOUT_INTERVAL);
});

function getMapInfo(gd) {
var subplot = gd._fullLayout.mapbox._subplot;
var map = subplot.map;

var sources = map.style.sourceCaches;
var layers = map.style._layers;
var uid = subplot.uid;

var traceSources = Object.keys(sources).filter(function(k) {
return k.indexOf('source-') === 0;
});

var traceLayers = Object.keys(layers).filter(function(k) {
return k.indexOf('plotly-trace-layer-') === 0;
});

var layoutSources = Object.keys(sources).filter(function(k) {
return k.indexOf(uid) !== -1;
});

var layoutLayers = Object.keys(layers).filter(function(k) {
return k.indexOf(uid) !== -1;
});

return {
map: map,
div: subplot.div,
sources: sources,
layers: layers,
traceSources: traceSources,
traceLayers: traceLayers,
layoutSources: layoutSources,
layoutLayers: layoutLayers,
center: map.getCenter(),
zoom: map.getZoom(),
style: map.getStyle()
};
}