From deaf7e5b4c513b13fdd60ab80d1c8cfcd6fd2f52 Mon Sep 17 00:00:00 2001 From: Bill Noon Date: Sat, 28 Feb 2015 12:11:46 -0500 Subject: [PATCH 1/3] Move done() outside the loop to test all layers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit even though there is only one layer… --- test/spec/feature_layer.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/spec/feature_layer.js b/test/spec/feature_layer.js index b65a9af17..878c331b7 100644 --- a/test/spec/feature_layer.js +++ b/test/spec/feature_layer.js @@ -105,8 +105,8 @@ describe('L.mapbox.featureLayer', function() { expect(layer.setGeoJSON(helpers.geoJsonPoly)).to.eql(layer); layer.eachLayer(function(l) { expect(l.options.color).to.eql('#f00'); - done(); }); + done(); }); it('supports custom access token', function() { @@ -154,8 +154,8 @@ describe('L.mapbox.featureLayer', function() { }); layer.eachLayer(function(l) { expect(l.options.fillColor).to.eql('blue'); - done(); }); + done(); }); it('styles polygons as an object', function(done) { @@ -164,8 +164,8 @@ describe('L.mapbox.featureLayer', function() { }); layer.eachLayer(function(l) { expect(l.options.fillColor).to.eql('blue'); - done(); }); + done(); }); it('also works with pointToLayer as a function', function(done) { @@ -179,8 +179,8 @@ describe('L.mapbox.featureLayer', function() { }); layer.eachLayer(function(l) { expect(l.options.fillColor).to.eql('blue'); - done(); }); + done(); }); it('also works with pointToLayer as an object', function(done) { @@ -192,8 +192,8 @@ describe('L.mapbox.featureLayer', function() { }); layer.eachLayer(function(l) { expect(l.options.fillColor).to.eql('blue'); - done(); }); + done(); }); }); From c660102823510d13a63ef3cba55b07a1ef4f5b32 Mon Sep 17 00:00:00 2001 From: Bill Noon Date: Sat, 28 Feb 2015 12:38:30 -0500 Subject: [PATCH 2/3] Don't actually need the done() calls Nothing async. --- test/spec/feature_layer.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/test/spec/feature_layer.js b/test/spec/feature_layer.js index 878c331b7..f22a1dc10 100644 --- a/test/spec/feature_layer.js +++ b/test/spec/feature_layer.js @@ -100,13 +100,12 @@ describe('L.mapbox.featureLayer', function() { expect(layer.getGeoJSON()).to.eql(helpers.geoJson); }); - it("styles GeoJSON features", function(done) { + it("styles GeoJSON features", function() { var layer = L.mapbox.featureLayer(); expect(layer.setGeoJSON(helpers.geoJsonPoly)).to.eql(layer); layer.eachLayer(function(l) { expect(l.options.color).to.eql('#f00'); }); - done(); }); it('supports custom access token', function() { @@ -146,7 +145,7 @@ describe('L.mapbox.featureLayer', function() { }); describe("supports a style option", function() { - it('styles polygons as a function', function(done) { + it('styles polygons as a function', function() { var layer = L.mapbox.featureLayer(helpers.geoJsonPoly, { style: function (feature) { return {fillColor: 'blue'}; @@ -155,20 +154,18 @@ describe('L.mapbox.featureLayer', function() { layer.eachLayer(function(l) { expect(l.options.fillColor).to.eql('blue'); }); - done(); }); - it('styles polygons as an object', function(done) { + it('styles polygons as an object', function() { var layer = L.mapbox.featureLayer(helpers.geoJsonPoly, { style: {fillColor: 'blue'} }); layer.eachLayer(function(l) { expect(l.options.fillColor).to.eql('blue'); }); - done(); }); - it('also works with pointToLayer as a function', function(done) { + it('also works with pointToLayer as a function', function() { var layer = L.mapbox.featureLayer(helpers.geoJson, { pointToLayer: function (feature, lonlat) { return L.circleMarker(lonlat); @@ -180,10 +177,9 @@ describe('L.mapbox.featureLayer', function() { layer.eachLayer(function(l) { expect(l.options.fillColor).to.eql('blue'); }); - done(); }); - it('also works with pointToLayer as an object', function(done) { + it('also works with pointToLayer as an object', function() { var layer = L.mapbox.featureLayer(helpers.geoJson, { pointToLayer: function (feature, lonlat) { return L.circleMarker(lonlat); @@ -193,7 +189,6 @@ describe('L.mapbox.featureLayer', function() { layer.eachLayer(function(l) { expect(l.options.fillColor).to.eql('blue'); }); - done(); }); }); From c4f4d44d6c81e6b085d7c26243afd836c098eab1 Mon Sep 17 00:00:00 2001 From: Bill Noon Date: Sat, 28 Feb 2015 12:52:54 -0500 Subject: [PATCH 3/3] Add setStyle and getStyle methods Need to save the style in the object _options --- src/feature_layer.js | 13 ++++++++++++ test/spec/feature_layer.js | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/feature_layer.js b/src/feature_layer.js index 1c2d6efd6..bab6c72fb 100644 --- a/src/feature_layer.js +++ b/src/feature_layer.js @@ -61,6 +61,19 @@ var FeatureLayer = L.FeatureGroup.extend({ return this.loadURL(urlhelper('/' + id + '/features.json', this.options.accessToken)); }, + setStyle: function(_) { + this.options.style = _; + if (this._geojson) { + this.clearLayers(); + this._initialize(this._geojson); + } + return this; + }, + + getStyle: function() { + return this.options.style; + }, + setFilter: function(_) { this.options.filter = _; if (this._geojson) { diff --git a/test/spec/feature_layer.js b/test/spec/feature_layer.js index f22a1dc10..6771ee5e1 100644 --- a/test/spec/feature_layer.js +++ b/test/spec/feature_layer.js @@ -144,6 +144,48 @@ describe('L.mapbox.featureLayer', function() { }); }); + describe("#getStyle", function() { + it("returns the style option", function() { + var style = {fillColor: 'blue'}, + layer = L.mapbox.featureLayer(null, {style: style}); + expect(layer.getStyle()).to.equal(style); + }); + }); + + describe("#setStyle", function() { + it('resets the style of the polygon feature layer', function() { + var layer = L.mapbox.featureLayer(helpers.geoJsonPoly, { + style: {fillColor: 'blue'} + }); + layer.eachLayer(function(l) { + expect(l.options.fillColor).to.eql('blue'); + }); + layer.setFilter(function (f) {return false;}); + layer.setStyle({fillColor:'yellow'}); + layer.setFilter(function (f) {return true;}); + layer.eachLayer(function(l) { + expect(l.options.fillColor).to.eql('yellow'); + }); + }); + it('resets the style of Points using pointToLayer', function() { + var layer = L.mapbox.featureLayer(helpers.geoJson, { + pointToLayer: function (feature, lonlat) { + return L.circleMarker(lonlat); + }, + style: {fillColor: 'blue'} + }); + layer.eachLayer(function(l) { + expect(l.options.fillColor).to.eql('blue'); + }); + layer.setFilter(function (f) {return false;}); + layer.setStyle({fillColor:'yellow'}); + layer.setFilter(function (f) {return true;}); + layer.eachLayer(function(l) { + expect(l.options.fillColor).to.eql('yellow'); + }); + }); + }); + describe("supports a style option", function() { it('styles polygons as a function', function() { var layer = L.mapbox.featureLayer(helpers.geoJsonPoly, {