Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 6, 2022. It is now read-only.

Commit 641b231

Browse files
authored
Merge pull request plotly#795 from plotly/mapbox-style-json
Add support for mapbox style JSON
2 parents 66e8a22 + 0e23045 commit 641b231

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed

src/lib/coerce.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ exports.valObjects = {
222222
any: {
223223
description: 'Any type.',
224224
requiredOpts: [],
225-
otherOpts: ['dflt'],
225+
otherOpts: ['dflt', 'values'],
226226
coerceFunction: function(v, propOut, dflt) {
227227
if(v === undefined) propOut.set(dflt);
228228
else propOut.set(v);

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) {

test/jasmine/tests/mapbox_test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ describe('mapbox defaults', function() {
5555
expect(layoutOut.mapbox._input).toBe(mapbox);
5656
});
5757

58+
it('should accept both string and object style', function() {
59+
var mapboxStyleJSON = {
60+
id: 'cdsa213wqdsa',
61+
owner: 'johnny'
62+
};
63+
64+
layoutIn = {
65+
mapbox: { style: 'light' },
66+
mapbox2: { style: mapboxStyleJSON }
67+
};
68+
69+
fullData.push({ type: 'scattermapbox', subplot: 'mapbox2' });
70+
71+
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
72+
expect(layoutOut.mapbox.style).toEqual('light');
73+
expect(layoutOut.mapbox2.style).toBe(mapboxStyleJSON);
74+
});
75+
5876
it('should fill layer containers', function() {
5977
layoutIn = {
6078
mapbox: {

0 commit comments

Comments
 (0)