Allow passing undefined to ol.layer.Vector#setStyle

This commit is contained in:
Éric Lemoine
2014-08-27 09:38:08 +02:00
parent 68b3691cd6
commit 9cc0841efb
2 changed files with 28 additions and 19 deletions

View File

@@ -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.Style>|ol.style.StyleFunction} style
* Layer style.
* an array of styles. If `undefined` default styles are used.
* @param {ol.style.Style|Array.<ol.style.Style>|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();
};

View File

@@ -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() {