Skip to content

Commit

Permalink
Merge pull request #2158 from CartoDB/2125-add-options-leaflet-layer
Browse files Browse the repository at this point in the history
feat: add options to getLeafletLayer (#2125)
  • Loading branch information
rubenmoya authored Jul 2, 2018
2 parents bc46e95 + b419d68 commit 253618a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/api/v4/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getValidationError (code) {
*
* To create a new client you need a CARTO account, where you will be able to get
* your API key and username.
*
*
* If you want to learn more about authorization and authentication, please read the authorization fundamentals section of our Developer Center.
*
* @param {object} settings
Expand All @@ -34,7 +34,7 @@ function getValidationError (code) {
* apiKey: 'YOUR_API_KEY_HERE',
* username: 'YOUR_USERNAME_HERE'
* });
*
*
* var client = new carto.Client({
* apiKey: 'YOUR_API_KEY_HERE',
* username: 'YOUR_USERNAME_HERE',
Expand Down Expand Up @@ -324,16 +324,18 @@ Client.prototype.getDataviews = function () {
* // Add the leafletLayer to a leafletMap
* client.getLeafletLayer().addTo(map);
*
* @param {object} options - {@link https://leafletjs.com/reference-1.3.0.html#tilelayer-minzoom|L.TileLayer} options.
*
* @returns A {@link http://leafletjs.com/reference-1.3.1.html#tilelayer|L.TileLayer} layer that groups all the layers.
*
* @api
*/
Client.prototype.getLeafletLayer = function () {
Client.prototype.getLeafletLayer = function (options) {
// Check if Leaflet is loaded
utils.isLeafletLoaded();
if (!this._leafletLayer) {
var LeafletLayer = require('./native/leaflet-layer');
this._leafletLayer = new LeafletLayer(this._layers, this._engine);
this._leafletLayer = new LeafletLayer(this._layers, this._engine, options);
}
return this._leafletLayer;
};
Expand Down
4 changes: 3 additions & 1 deletion src/api/v4/native/leaflet-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ var LeafletLayer = L.TileLayer.extend({
attribution: constants.ATTRIBUTION
},

initialize: function (layers, engine) {
initialize: function (layers, engine, options) {
_.extend(this.options, options);

this._layers = layers;
this._engine = engine;
this._internalView = null;
Expand Down
16 changes: 15 additions & 1 deletion test/spec/api/v4/native/leaflet-layer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ describe('src/api/v4/native/leaflet-layer', function () {
username: 'cartojs-test'
});
map = L.map('map').setView([42.431234, -8.643616], 5);
leafletLayer = client.getLeafletLayer();
});

afterEach(function () {
document.getElementById('map').remove();
});

it('allows custom options', function () {
leafletLayer = client.getLeafletLayer({ maxZoom: 10 });

expect(leafletLayer.options.maxZoom).toBe(10);
});

describe('addTo', function () {
beforeEach(function () {
leafletLayer = client.getLeafletLayer();
});

it('should add a leaflet layer to the map', function () {
expect(countLeafletLayers(map)).toEqual(0);

Expand All @@ -35,6 +44,10 @@ describe('src/api/v4/native/leaflet-layer', function () {
});

describe('removeFrom', function () {
beforeEach(function () {
leafletLayer = client.getLeafletLayer();
});

it('should remove the leaflet layer from the map', function () {
expect(countLeafletLayers(map)).toEqual(0);

Expand All @@ -55,6 +68,7 @@ describe('src/api/v4/native/leaflet-layer', function () {
beforeEach(function () {
spy = jasmine.createSpy('spy');

leafletLayer = client.getLeafletLayer();
leafletLayer.addTo(map);

var source = new carto.source.SQL('foo');
Expand Down

0 comments on commit 253618a

Please sign in to comment.