|
8 | 8 |
|
9 | 9 | 'use strict';
|
10 | 10 |
|
| 11 | +/* global PlotlyGeoAssets:false */ |
| 12 | + |
11 | 13 | var mapboxgl = require('mapbox-gl');
|
| 14 | +var d3 = require('d3'); |
12 | 15 |
|
13 | 16 | var Fx = require('../../components/fx');
|
14 | 17 | var Lib = require('../../lib');
|
@@ -111,7 +114,15 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
|
111 | 114 |
|
112 | 115 | self.rejectOnError(reject);
|
113 | 116 |
|
114 |
| - map.once('load', function() { |
| 117 | + var promises = []; |
| 118 | + |
| 119 | + promises.push(new Promise(function(resolve) { |
| 120 | + map.once('load', resolve); |
| 121 | + })); |
| 122 | + |
| 123 | + promises = promises.concat(self.fetchMapData(calcData, fullLayout)); |
| 124 | + |
| 125 | + Promise.all(promises).then(function() { |
115 | 126 | self.updateData(calcData);
|
116 | 127 | self.updateLayout(fullLayout);
|
117 | 128 | self.resolveOnRender(resolve);
|
@@ -232,32 +243,68 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
|
232 | 243 | };
|
233 | 244 | };
|
234 | 245 |
|
| 246 | +proto.fetchMapData = function(calcData) { |
| 247 | + var promises = []; |
| 248 | + |
| 249 | + function fetch(url) { |
| 250 | + return new Promise(function(resolve, reject) { |
| 251 | + d3.json(url, function(err, d) { |
| 252 | + if(err) { |
| 253 | + var msg = err.status === 404 ? |
| 254 | + ('GeoJSON at URL ' + url + ' does not exist.') : |
| 255 | + ('Unexpected error while fetching from ' + url); |
| 256 | + return reject(new Error(msg)); |
| 257 | + } |
| 258 | + |
| 259 | + PlotlyGeoAssets[url] = d; |
| 260 | + resolve(d); |
| 261 | + }); |
| 262 | + }); |
| 263 | + } |
| 264 | + |
| 265 | + for(var i = 0; i < calcData.length; i++) { |
| 266 | + var trace = calcData[i][0].trace; |
| 267 | + var url = trace.geojson; |
| 268 | + |
| 269 | + if(typeof url === 'string' && !PlotlyGeoAssets[url]) { |
| 270 | + PlotlyGeoAssets[url] = 'pending'; |
| 271 | + promises.push(fetch(url)); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + return promises; |
| 276 | +}; |
| 277 | + |
235 | 278 | proto.updateMap = function(calcData, fullLayout, resolve, reject) {
|
236 | 279 | var self = this;
|
237 | 280 | var map = self.map;
|
238 | 281 | var opts = fullLayout[this.id];
|
239 | 282 |
|
240 | 283 | self.rejectOnError(reject);
|
241 | 284 |
|
| 285 | + var promises = []; |
242 | 286 | var styleObj = getStyleObj(opts.style);
|
243 | 287 |
|
244 | 288 | if(self.styleObj.id !== styleObj.id) {
|
245 | 289 | self.styleObj = styleObj;
|
246 | 290 | map.setStyle(styleObj.style);
|
247 | 291 |
|
248 |
| - map.once('styledata', function() { |
249 |
| - // need to rebuild trace layers on reload |
250 |
| - // to avoid 'lost event' errors |
251 |
| - self.traceHash = {}; |
252 |
| - self.updateData(calcData); |
253 |
| - self.updateLayout(fullLayout); |
254 |
| - self.resolveOnRender(resolve); |
255 |
| - }); |
256 |
| - } else { |
| 292 | + // need to rebuild trace layers on reload |
| 293 | + // to avoid 'lost event' errors |
| 294 | + self.traceHash = {}; |
| 295 | + |
| 296 | + promises.push(new Promise(function(resolve) { |
| 297 | + map.once('styledata', resolve); |
| 298 | + })); |
| 299 | + } |
| 300 | + |
| 301 | + promises = promises.concat(self.fetchMapData(calcData, fullLayout)); |
| 302 | + |
| 303 | + Promise.all(promises).then(function() { |
257 | 304 | self.updateData(calcData);
|
258 | 305 | self.updateLayout(fullLayout);
|
259 | 306 | self.resolveOnRender(resolve);
|
260 |
| - } |
| 307 | + }); |
261 | 308 | };
|
262 | 309 |
|
263 | 310 | proto.updateData = function(calcData) {
|
|
0 commit comments