@@ -47,22 +47,21 @@ exports.layoutAttributes = require('./layout_attributes');
4747exports . supplyLayoutDefaults = require ( './layout_defaults' ) ;
4848
4949exports . plot = function plotMapbox ( gd ) {
50-
51- if ( ! gd . _context . mapboxAccessToken ) {
52- throw new Error ( constants . noAccessTokenErrorMsg ) ;
53- }
54- else {
55- mapboxgl . accessToken = gd . _context . mapboxAccessToken ;
56- }
57-
5850 var fullLayout = gd . _fullLayout ,
5951 calcData = gd . calcdata ,
6052 mapboxIds = Plots . getSubplotIds ( fullLayout , 'mapbox' ) ;
6153
54+ var accessToken = findAccessToken ( gd , mapboxIds ) ;
55+ mapboxgl . accessToken = accessToken ;
56+
6257 for ( var i = 0 ; i < mapboxIds . length ; i ++ ) {
6358 var id = mapboxIds [ i ] ,
6459 subplotCalcData = getSubplotCalcData ( calcData , id ) ,
65- mapbox = fullLayout [ id ] . _subplot ;
60+ opts = fullLayout [ id ] ,
61+ mapbox = opts . _subplot ;
62+
63+ // copy access token to fullLayout (to handle the context case)
64+ opts . accesstoken = accessToken ;
6665
6766 if ( ! mapbox ) {
6867 mapbox = createMapbox ( {
@@ -131,3 +130,27 @@ function getSubplotCalcData(calcData, id) {
131130
132131 return subplotCalcData ;
133132}
133+
134+ function findAccessToken ( gd , mapboxIds ) {
135+ var fullLayout = gd . _fullLayout ,
136+ context = gd . _context ;
137+
138+ // first look for access token in context
139+ var accessToken = context . mapboxAccessToken ;
140+
141+ // allow mapbox layout options to override it
142+ for ( var i = 0 ; i < mapboxIds . length ; i ++ ) {
143+ var opts = fullLayout [ mapboxIds [ i ] ] ;
144+
145+ if ( opts . accesstoken ) {
146+ accessToken = opts . accesstoken ;
147+ break ;
148+ }
149+ }
150+
151+ if ( ! accessToken ) {
152+ throw new Error ( constants . noAccessTokenErrorMsg ) ;
153+ }
154+
155+ return accessToken ;
156+ }
0 commit comments