Skip to content

Commit 4aa511a

Browse files
authored
Merge pull request #2361 from plotly/bump-mapbox-gl
Bump mapbox gl to 0.44.0
2 parents cd79f0d + b79abb5 commit 4aa511a

29 files changed

+1159
-883
lines changed

package-lock.json

+513-296
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"has-hover": "^1.0.1",
8888
"has-passive-events": "^1.0.0",
8989
"kdgrass": "^1.0.1",
90-
"mapbox-gl": "^0.22.0",
90+
"mapbox-gl": "^0.44.0",
9191
"matrix-camera-controller": "^2.1.3",
9292
"mouse-change": "^1.4.0",
9393
"mouse-event-offset": "^3.0.2",

src/components/drawing/index.js

+46-28
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ drawing.pointStyle = function(s, trace, gd) {
439439
});
440440
};
441441

442-
drawing.selectedPointStyle = function(s, trace) {
443-
if(!s.size() || !trace.selectedpoints) return;
442+
drawing.makeSelectedPointStyleFns = function(trace) {
443+
var out = {};
444444

445445
var selectedAttrs = trace.selected || {};
446446
var unselectedAttrs = trace.unselected || {};
@@ -455,59 +455,77 @@ drawing.selectedPointStyle = function(s, trace) {
455455
var smoIsDefined = smo !== undefined;
456456
var usmoIsDefined = usmo !== undefined;
457457

458-
s.each(function(d) {
459-
var pt = d3.select(this);
458+
out.opacityFn = function(d) {
460459
var dmo = d.mo;
461460
var dmoIsDefined = dmo !== undefined;
462-
var mo2;
463461

464462
if(dmoIsDefined || smoIsDefined || usmoIsDefined) {
465463
if(d.selected) {
466-
if(smoIsDefined) mo2 = smo;
464+
if(smoIsDefined) return smo;
467465
} else {
468-
if(usmoIsDefined) mo2 = usmo;
469-
else mo2 = DESELECTDIM * (dmoIsDefined ? dmo : mo);
466+
if(usmoIsDefined) return usmo;
467+
return DESELECTDIM * (dmoIsDefined ? dmo : mo);
470468
}
471469
}
472-
473-
if(mo2 !== undefined) pt.style('opacity', mo2);
474-
});
470+
};
475471

476472
var smc = selectedMarker.color;
477473
var usmc = unselectedMarker.color;
478474

479475
if(smc || usmc) {
480-
s.each(function(d) {
481-
var pt = d3.select(this);
482-
var mc2;
483-
476+
out.colorFn = function(d) {
484477
if(d.selected) {
485-
if(smc) mc2 = smc;
478+
if(smc) return smc;
486479
} else {
487-
if(usmc) mc2 = usmc;
480+
if(usmc) return usmc;
488481
}
489-
490-
if(mc2) Color.fill(pt, mc2);
491-
});
482+
};
492483
}
493484

494485
var sms = selectedMarker.size;
495486
var usms = unselectedMarker.size;
496487
var smsIsDefined = sms !== undefined;
497488
var usmsIsDefined = usms !== undefined;
498489

499-
if(Registry.traceIs(trace, 'symbols') && (smsIsDefined || usmsIsDefined)) {
500-
s.each(function(d) {
501-
var pt = d3.select(this);
490+
if(smsIsDefined || usmsIsDefined) {
491+
out.sizeFn = function(d) {
502492
var mrc = d.mrc;
503-
var mx = d.mx || marker.symbol || 0;
504-
var mrc2;
505-
506493
if(d.selected) {
507-
mrc2 = (smsIsDefined) ? sms / 2 : mrc;
494+
return smsIsDefined ? sms / 2 : mrc;
508495
} else {
509-
mrc2 = (usmsIsDefined) ? usms / 2 : mrc;
496+
return usmsIsDefined ? usms / 2 : mrc;
510497
}
498+
};
499+
}
500+
501+
return out;
502+
};
503+
504+
drawing.selectedPointStyle = function(s, trace) {
505+
if(!s.size() || !trace.selectedpoints) return;
506+
507+
var fns = drawing.makeSelectedPointStyleFns(trace);
508+
var marker = trace.marker || {};
509+
510+
s.each(function(d) {
511+
var pt = d3.select(this);
512+
var mo2 = fns.opacityFn(d);
513+
if(mo2 !== undefined) pt.style('opacity', mo2);
514+
});
515+
516+
if(fns.colorFn) {
517+
s.each(function(d) {
518+
var pt = d3.select(this);
519+
var mc2 = fns.colorFn(d);
520+
if(mc2) Color.fill(pt, mc2);
521+
});
522+
}
523+
524+
if(Registry.traceIs(trace, 'symbols') && fns.sizeFn) {
525+
s.each(function(d) {
526+
var pt = d3.select(this);
527+
var mx = d.mx || marker.symbol || 0;
528+
var mrc2 = fns.sizeFn(d);
511529

512530
pt.attr('d', makePointPath(drawing.symbolNumber(mx), mrc2));
513531

src/plots/cartesian/dragbox.js

+4
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
358358
return;
359359
}
360360

361+
if(redrawTimer === null) {
362+
clearSelect(zoomlayer);
363+
}
364+
361365
// If a transition is in progress, then disable any behavior:
362366
if(gd._transitioningWithDuration) {
363367
e.preventDefault();

src/plots/mapbox/constants.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@
99

1010
'use strict';
1111

12+
var requiredVersion = '0.44.0';
1213

1314
module.exports = {
15+
requiredVersion: requiredVersion,
16+
1417
styleUrlPrefix: 'mapbox://styles/mapbox/',
1518
styleUrlSuffix: 'v9',
1619

1720
controlContainerClassName: 'mapboxgl-control-container',
1821

22+
wrongVersionErrorMsg: [
23+
'Your custom plotly.js bundle is not using the correct mapbox-gl version',
24+
'Please install mapbox-gl@' + requiredVersion + '.'
25+
].join('\n'),
26+
1927
noAccessTokenErrorMsg: [
2028
'Missing Mapbox access token.',
2129
'Mapbox trace type require a Mapbox access token to be registered.',
@@ -24,5 +32,11 @@ module.exports = {
2432
'More info here: https://www.mapbox.com/help/define-access-token/'
2533
].join('\n'),
2634

27-
mapOnErrorMsg: 'Mapbox error.'
35+
mapOnErrorMsg: 'Mapbox error.',
36+
37+
// a subset of node_modules/mapbox-gl/dist/mapbox-gl.css
38+
styleRules: {
39+
map: 'overflow:hidden;position:relative;',
40+
'missing-css': 'display:none',
41+
}
2842
};

src/plots/mapbox/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var mapboxgl = require('mapbox-gl');
@@ -20,6 +19,9 @@ var constants = require('./constants');
2019

2120
var MAPBOX = 'mapbox';
2221

22+
for(var k in constants.styleRules) {
23+
Lib.addStyleRule('.mapboxgl-' + k, constants.styleRules[k]);
24+
}
2325

2426
exports.name = MAPBOX;
2527

@@ -53,6 +55,10 @@ exports.plot = function plotMapbox(gd) {
5355
var calcData = gd.calcdata;
5456
var mapboxIds = fullLayout._subplots[MAPBOX];
5557

58+
if(mapboxgl.version !== constants.requiredVersion) {
59+
throw new Error(constants.wrongVersionErrorMsg);
60+
}
61+
5662
var accessToken = findAccessToken(gd, mapboxIds);
5763
mapboxgl.accessToken = accessToken;
5864

src/plots/mapbox/layers.js

+25-37
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var Lib = require('../../lib');
1312
var convertTextOpts = require('./convert_text_opts');
1413

15-
1614
function MapboxLayer(mapbox, index) {
1715
this.mapbox = mapbox;
1816
this.map = mapbox.map;
@@ -36,32 +34,26 @@ var proto = MapboxLayer.prototype;
3634

3735
proto.update = function update(opts) {
3836
if(!this.visible) {
39-
4037
// IMPORTANT: must create source before layer to not cause errors
4138
this.updateSource(opts);
4239
this.updateLayer(opts);
43-
}
44-
else if(this.needsNewSource(opts)) {
45-
40+
} else if(this.needsNewSource(opts)) {
4641
// IMPORTANT: must delete layer before source to not cause errors
4742
this.updateLayer(opts);
4843
this.updateSource(opts);
49-
}
50-
else if(this.needsNewLayer(opts)) {
44+
} else if(this.needsNewLayer(opts)) {
5145
this.updateLayer(opts);
46+
} else {
47+
this.updateStyle(opts);
5248
}
5349

54-
this.updateStyle(opts);
55-
5650
this.visible = isVisible(opts);
5751
};
5852

5953
proto.needsNewSource = function(opts) {
60-
6154
// for some reason changing layer to 'fill' or 'symbol'
6255
// w/o changing the source throws an exception in mapbox-gl 0.18 ;
6356
// stay safe and make new source on type changes
64-
6557
return (
6658
this.sourceType !== opts.sourcetype ||
6759
this.source !== opts.source ||
@@ -93,37 +85,34 @@ proto.updateSource = function(opts) {
9385

9486
proto.updateLayer = function(opts) {
9587
var map = this.map;
88+
var convertedOpts = convertOpts(opts);
9689

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

9992
this.layerType = opts.type;
10093

101-
if(!isVisible(opts)) return;
102-
103-
map.addLayer({
104-
id: this.idLayer,
105-
source: this.idSource,
106-
'source-layer': opts.sourcelayer || '',
107-
type: opts.type
108-
}, opts.below);
109-
110-
// the only way to make a layer invisible is to remove it
111-
var layoutOpts = { visibility: 'visible' };
112-
this.mapbox.setOptions(this.idLayer, 'setLayoutProperty', layoutOpts);
94+
if(isVisible(opts)) {
95+
map.addLayer({
96+
id: this.idLayer,
97+
source: this.idSource,
98+
'source-layer': opts.sourcelayer || '',
99+
type: opts.type,
100+
layout: convertedOpts.layout,
101+
paint: convertedOpts.paint
102+
}, opts.below);
103+
}
113104
};
114105

115106
proto.updateStyle = function(opts) {
116-
var convertedOpts = convertOpts(opts);
117-
118107
if(isVisible(opts)) {
108+
var convertedOpts = convertOpts(opts);
119109
this.mapbox.setOptions(this.idLayer, 'setLayoutProperty', convertedOpts.layout);
120110
this.mapbox.setOptions(this.idLayer, 'setPaintProperty', convertedOpts.paint);
121111
}
122112
};
123113

124114
proto.dispose = function dispose() {
125115
var map = this.map;
126-
127116
map.removeLayer(this.idLayer);
128117
map.removeSource(this.idSource);
129118
};
@@ -198,19 +187,18 @@ function convertOpts(opts) {
198187
}
199188

200189
function convertSourceOpts(opts) {
201-
var sourceType = opts.sourcetype,
202-
source = opts.source,
203-
sourceOpts = { type: sourceType },
204-
isSourceAString = (typeof source === 'string'),
205-
field;
206-
207-
if(sourceType === 'geojson') field = 'data';
208-
else if(sourceType === 'vector') {
209-
field = isSourceAString ? 'url' : 'tiles';
190+
var sourceType = opts.sourcetype;
191+
var source = opts.source;
192+
var sourceOpts = {type: sourceType};
193+
var field;
194+
195+
if(sourceType === 'geojson') {
196+
field = 'data';
197+
} else if(sourceType === 'vector') {
198+
field = typeof source === 'string' ? 'url' : 'tiles';
210199
}
211200

212201
sourceOpts[field] = source;
213-
214202
return sourceOpts;
215203
}
216204

0 commit comments

Comments
 (0)