From 640ed9659e00a1cee53e289e7432559e641ae8d9 Mon Sep 17 00:00:00 2001 From: john gravois Date: Thu, 14 Jul 2016 14:45:16 -0400 Subject: [PATCH] allow users to pass tokens through to basemaps (#800) * allow users to pass tokens through to basemaps * sometimes people make maps with no attribution control --- spec/Layers/BasemapLayerSpec.js | 6 ++++++ src/Layers/BasemapLayer.js | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/spec/Layers/BasemapLayerSpec.js b/spec/Layers/BasemapLayerSpec.js index 86beef8ec..b735087c5 100644 --- a/spec/Layers/BasemapLayerSpec.js +++ b/spec/Layers/BasemapLayerSpec.js @@ -86,6 +86,12 @@ describe('L.esri.BasemapLayer', function () { expect(map.hasLayer(baseLayer)).to.equal(true); }); + it('will append tokens when fetching tiles if necessary', function () { + var baseLayer = L.esri.basemapLayer('Streets', {token: 'bogus'} ); + map.addLayer(baseLayer); + expect(baseLayer._url).to.contain('token=bogus'); + }); + it('will throw an error given invalid basemap name', function () { expect(function () { L.esri.basemapLayer('junk'); diff --git a/src/Layers/BasemapLayer.js b/src/Layers/BasemapLayer.js index 64e6f7425..e0a72bdb7 100644 --- a/src/Layers/BasemapLayer.js +++ b/src/Layers/BasemapLayer.js @@ -184,12 +184,18 @@ export var BasemapLayer = L.TileLayer.extend({ L.Util.setOptions(this, tileOptions); + if (this.options.token) { + config.urlTemplate += ('?token=' + this.options.token); + } + // call the initialize method on L.TileLayer to set everything up L.TileLayer.prototype.initialize.call(this, config.urlTemplate, tileOptions); }, onAdd: function (map) { - map.attributionControl.addAttribution('© Esri'); + if (map.attributionControl) { + map.attributionControl.addAttribution('© Esri'); + } if (this.options.pane === 'esri-labels') { this._initPane(); @@ -204,8 +210,10 @@ export var BasemapLayer = L.TileLayer.extend({ }, onRemove: function (map) { - map.attributionControl.removeAttribution('© Esri'); - map.off('moveend', Util._updateMapAttribution, this); + if (map.attributionControl) { + map.attributionControl.removeAttribution('© Esri'); + } + map.off('moveend', this._updateMapAttribution, this); L.TileLayer.prototype.onRemove.call(this, map); },