Skip to content

Commit 89d16de

Browse files
committed
mapbox: add support for style JSON
1 parent 3ee4cef commit 89d16de

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/plots/mapbox/layout_attributes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ module.exports = {
5757
].join(' ')
5858
},
5959
style: {
60-
valType: 'string',
60+
valType: 'any',
6161
values: ['basic', 'streets', 'outdoors', 'light', 'dark', 'satellite', 'satellite-streets'],
6262
dflt: 'basic',
6363
role: 'style',
6464
description: [
6565
'Sets the Mapbox map style.',
66-
'Either input the defaults Mapbox names or the URL to a custom style.'
66+
'Either input one of the default Mapbox style names or the URL to a custom style',
67+
'or a valid Mapbox style JSON.'
6768
].join(' ')
6869
},
6970

src/plots/mapbox/mapbox.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function Mapbox(opts) {
4141
// state variables used to infer how and what to update
4242
this.map = null;
4343
this.accessToken = null;
44-
this.styleUrl = null;
44+
this.styleObj = null;
4545
this.traceHash = {};
4646
this.layerList = [];
4747
}
@@ -64,7 +64,7 @@ proto.plot = function(calcData, fullLayout, promises) {
6464
if(self.map && (opts.accesstoken !== self.accessToken)) {
6565
self.map.remove();
6666
self.map = null;
67-
self.styleUrl = null;
67+
self.styleObj = null;
6868
self.traceHash = [];
6969
self.layerList = {};
7070
}
@@ -90,16 +90,17 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
9090
gd = self.gd,
9191
opts = self.opts;
9292

93-
// mapbox doesn't have a way to get the current style URL; do it ourselves
94-
var styleUrl = self.styleUrl = convertStyleUrl(opts.style);
93+
// store style id and URL or object
94+
var styleObj = self.styleObj = getStyleObj(opts.style);
9595

9696
// store access token associated with this map
9797
self.accessToken = opts.accesstoken;
9898

99+
// create the map!
99100
var map = self.map = new mapboxgl.Map({
100101
container: self.div,
101102

102-
style: styleUrl,
103+
style: styleObj.style,
103104
center: convertCenter(opts.center),
104105
zoom: opts.zoom,
105106
bearing: opts.bearing,
@@ -172,11 +173,11 @@ proto.updateMap = function(calcData, fullLayout, resolve, reject) {
172173

173174
self.rejectOnError(reject);
174175

175-
var styleUrl = convertStyleUrl(self.opts.style);
176+
var styleObj = getStyleObj(self.opts.style);
176177

177-
if(self.styleUrl !== styleUrl) {
178-
self.styleUrl = styleUrl;
179-
map.setStyle(styleUrl);
178+
if(self.styleObj.id !== styleObj.id) {
179+
self.styleObj = styleObj;
180+
map.setStyle(styleObj.style);
180181

181182
map.style.once('load', function() {
182183

@@ -402,16 +403,32 @@ proto.getView = function() {
402403
};
403404
};
404405

405-
function convertStyleUrl(style) {
406-
var styleValues = layoutAttributes.style.values;
406+
function getStyleObj(val) {
407+
var styleValues = layoutAttributes.style.values,
408+
styleDflt = layoutAttributes.style.dflt,
409+
styleObj = {};
407410

408-
// if style is part of the 'official' mapbox values,
409-
// add URL prefix and suffix
410-
if(styleValues.indexOf(style) !== -1) {
411-
return constants.styleUrlPrefix + style + '-' + constants.styleUrlSuffix;
411+
if(Lib.isPlainObject(val)) {
412+
styleObj.id = val.id;
413+
styleObj.style = val;
412414
}
415+
else if(typeof val === 'string') {
416+
styleObj.id = val;
417+
styleObj.style = (styleValues.indexOf(val) !== -1) ?
418+
convertStyleVal(val) :
419+
val;
420+
}
421+
else {
422+
styleObj.id = styleDflt;
423+
styleObj.style = convertStyleVal(styleDflt);
424+
}
425+
426+
return styleObj;
427+
}
413428

414-
return style;
429+
// if style is part of the 'official' mapbox values, add URL prefix and suffix
430+
function convertStyleVal(val) {
431+
return constants.styleUrlPrefix + val + '-' + constants.styleUrlSuffix;
415432
}
416433

417434
function convertCenter(center) {

0 commit comments

Comments
 (0)