Skip to content

Commit 77f576c

Browse files
authored
Merge pull request plotly#4720 from plotly/fix4719-mapbox-react-style
React mapbox style
2 parents 30f3ddc + 166e2cf commit 77f576c

File tree

2 files changed

+119
-42
lines changed

2 files changed

+119
-42
lines changed

src/plots/mapbox/mapbox.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ proto.plot = function(calcData, fullLayout, promises) {
6363
self.map.remove();
6464
self.map = null;
6565
self.styleObj = null;
66-
self.traceHash = [];
67-
self.layerList = {};
66+
self.traceHash = {};
67+
self.layerList = [];
6868
}
6969

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

154-
if(self.styleObj.id !== styleObj.id) {
154+
if(JSON.stringify(self.styleObj) !== JSON.stringify(styleObj)) {
155155
self.styleObj = styleObj;
156156
map.setStyle(styleObj.style);
157157

test/jasmine/tests/mapbox_test.js

+116-39
Original file line numberDiff line numberDiff line change
@@ -1551,45 +1551,6 @@ describe('@noCI, mapbox plots', function() {
15511551
});
15521552
});
15531553

1554-
function getMapInfo(gd) {
1555-
var subplot = gd._fullLayout.mapbox._subplot;
1556-
var map = subplot.map;
1557-
1558-
var sources = map.style.sourceCaches;
1559-
var layers = map.style._layers;
1560-
var uid = subplot.uid;
1561-
1562-
var traceSources = Object.keys(sources).filter(function(k) {
1563-
return k.indexOf('source-') === 0;
1564-
});
1565-
1566-
var traceLayers = Object.keys(layers).filter(function(k) {
1567-
return k.indexOf('plotly-trace-layer-') === 0;
1568-
});
1569-
1570-
var layoutSources = Object.keys(sources).filter(function(k) {
1571-
return k.indexOf(uid) !== -1;
1572-
});
1573-
1574-
var layoutLayers = Object.keys(layers).filter(function(k) {
1575-
return k.indexOf(uid) !== -1;
1576-
});
1577-
1578-
return {
1579-
map: map,
1580-
div: subplot.div,
1581-
sources: sources,
1582-
layers: layers,
1583-
traceSources: traceSources,
1584-
traceLayers: traceLayers,
1585-
layoutSources: layoutSources,
1586-
layoutLayers: layoutLayers,
1587-
center: map.getCenter(),
1588-
zoom: map.getZoom(),
1589-
style: map.getStyle()
1590-
};
1591-
}
1592-
15931554
function countVisibleTraces(gd, modes) {
15941555
var mapInfo = getMapInfo(gd);
15951556
var cnts = [];
@@ -1683,6 +1644,83 @@ describe('@noCI, mapbox plots', function() {
16831644
}
16841645
});
16851646

1647+
describe('@noCI, mapbox react', function() {
1648+
var gd;
1649+
1650+
beforeEach(function() {
1651+
gd = createGraphDiv();
1652+
});
1653+
1654+
afterEach(function() {
1655+
Plotly.purge(gd);
1656+
destroyGraphDiv();
1657+
});
1658+
1659+
it('@gl should be able to react to new tiles', function(done) {
1660+
function assertTile(link) {
1661+
var mapInfo = getMapInfo(gd);
1662+
expect(mapInfo.style.sources.REF.tiles[0]).toEqual(link);
1663+
}
1664+
1665+
var firstLink = 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png';
1666+
var secondLink = 'https://a.tile.stamen.com/watercolor/{z}/{x}/{y}.jpg';
1667+
1668+
var fig = {
1669+
data: [
1670+
{
1671+
type: 'scattermapbox',
1672+
lon: [10, 20],
1673+
lat: [20, 10]
1674+
}
1675+
],
1676+
layout: {
1677+
mapbox: {
1678+
style: {
1679+
version: 8,
1680+
sources: {
1681+
REF: {
1682+
type: 'raster',
1683+
tileSize: 256,
1684+
tiles: [firstLink]
1685+
}
1686+
},
1687+
layers: [{
1688+
id: 'REF',
1689+
source: 'REF',
1690+
type: 'raster'
1691+
}],
1692+
}
1693+
}
1694+
}
1695+
};
1696+
1697+
Plotly.newPlot(gd, fig)
1698+
.then(function() {
1699+
assertTile(firstLink);
1700+
1701+
// copy figure
1702+
var newFig = JSON.parse(JSON.stringify(fig));
1703+
1704+
// new figure
1705+
newFig.layout.mapbox.style.sources = {
1706+
REF: {
1707+
type: 'raster',
1708+
tileSize: 256,
1709+
tiles: [secondLink]
1710+
}
1711+
};
1712+
1713+
// update
1714+
Plotly.react(gd, newFig);
1715+
})
1716+
.then(function() {
1717+
assertTile(secondLink);
1718+
})
1719+
.catch(failTest)
1720+
.then(done);
1721+
}, LONG_TIMEOUT_INTERVAL);
1722+
});
1723+
16861724
describe('@noCI test mapbox trace/layout *below* interactions', function() {
16871725
var gd;
16881726

@@ -2088,3 +2126,42 @@ describe('@noCI, mapbox toImage', function() {
20882126
.then(done);
20892127
}, LONG_TIMEOUT_INTERVAL);
20902128
});
2129+
2130+
function getMapInfo(gd) {
2131+
var subplot = gd._fullLayout.mapbox._subplot;
2132+
var map = subplot.map;
2133+
2134+
var sources = map.style.sourceCaches;
2135+
var layers = map.style._layers;
2136+
var uid = subplot.uid;
2137+
2138+
var traceSources = Object.keys(sources).filter(function(k) {
2139+
return k.indexOf('source-') === 0;
2140+
});
2141+
2142+
var traceLayers = Object.keys(layers).filter(function(k) {
2143+
return k.indexOf('plotly-trace-layer-') === 0;
2144+
});
2145+
2146+
var layoutSources = Object.keys(sources).filter(function(k) {
2147+
return k.indexOf(uid) !== -1;
2148+
});
2149+
2150+
var layoutLayers = Object.keys(layers).filter(function(k) {
2151+
return k.indexOf(uid) !== -1;
2152+
});
2153+
2154+
return {
2155+
map: map,
2156+
div: subplot.div,
2157+
sources: sources,
2158+
layers: layers,
2159+
traceSources: traceSources,
2160+
traceLayers: traceLayers,
2161+
layoutSources: layoutSources,
2162+
layoutLayers: layoutLayers,
2163+
center: map.getCenter(),
2164+
zoom: map.getZoom(),
2165+
style: map.getStyle()
2166+
};
2167+
}

0 commit comments

Comments
 (0)