From 9cc0841efb9d4be63855f4013da898ed8d66e7e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Wed, 27 Aug 2014 09:38:08 +0200 Subject: [PATCH] Allow passing undefined to ol.layer.Vector#setStyle --- src/ol/layer/vectorlayer.js | 14 +++++------ test/spec/ol/layer/vectorlayer.test.js | 33 ++++++++++++++++---------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/ol/layer/vectorlayer.js b/src/ol/layer/vectorlayer.js index 4ac8000bc8..3061b3defc 100644 --- a/src/ol/layer/vectorlayer.js +++ b/src/ol/layer/vectorlayer.js @@ -51,8 +51,7 @@ ol.layer.Vector = function(opt_options) { */ this.styleFunction_ = undefined; - this.setStyle(goog.isDefAndNotNull(options.style) ? - options.style : ol.style.defaultStyleFunction); + this.setStyle(options.style); }; goog.inherits(ol.layer.Vector, ol.layer.Layer); @@ -102,13 +101,14 @@ ol.layer.Vector.prototype.setRenderOrder = function(renderOrder) { /** * Set the style for features. This can be a single style object, an array * of styles, or a function that takes a feature and resolution and returns - * an array of styles. - * @param {ol.style.Style|Array.|ol.style.StyleFunction} style - * Layer style. + * an array of styles. If `undefined` default styles are used. + * @param {ol.style.Style|Array.|ol.style.StyleFunction|undefined} + * style Layer style. * @api stable */ ol.layer.Vector.prototype.setStyle = function(style) { - this.style_ = style; - this.styleFunction_ = ol.style.createStyleFunction(style); + this.style_ = goog.isDef(style) ? style : ol.style.defaultStyleFunction; + this.styleFunction_ = goog.isNull(style) ? + undefined : ol.style.createStyleFunction(this.style_); this.dispatchChangeEvent(); }; diff --git a/test/spec/ol/layer/vectorlayer.test.js b/test/spec/ol/layer/vectorlayer.test.js index 92e1d790b7..79abfe2b59 100644 --- a/test/spec/ol/layer/vectorlayer.test.js +++ b/test/spec/ol/layer/vectorlayer.test.js @@ -50,22 +50,21 @@ describe('ol.layer.Vector', function() { describe('#setStyle()', function() { - var source = new ol.source.Vector(); - var style = new ol.style.Style(); + var layer, style; + + beforeEach(function() { + layer = new ol.layer.Vector({ + source: new ol.source.Vector() + }); + style = new ol.style.Style(); + }); it('allows the style to be set after construction', function() { - var layer = new ol.layer.Vector({ - source: source - }); - layer.setStyle(style); expect(layer.getStyle()).to.be(style); }); it('dispatches the change event', function(done) { - var layer = new ol.layer.Vector({ - source: source - }); layer.on('change', function() { done(); }); @@ -73,15 +72,25 @@ describe('ol.layer.Vector', function() { }); it('updates the internal style function', function() { - var layer = new ol.layer.Vector({ - source: source - }); expect(layer.getStyleFunction()).to.be(ol.style.defaultStyleFunction); layer.setStyle(style); expect(layer.getStyleFunction()).not.to.be( ol.style.defaultStyleFunction); }); + it('allows setting an null style', function() { + layer.setStyle(null); + expect(layer.getStyle()).to.be(null); + expect(layer.getStyleFunction()).to.be(undefined); + }); + + it('sets the default style when passing undefined', function() { + layer.setStyle(style); + layer.setStyle(undefined); + expect(layer.getStyle()).to.be(ol.style.defaultStyleFunction); + expect(layer.getStyleFunction()).to.be(ol.style.defaultStyleFunction); + }); + }); describe('#getStyle()', function() {