Add setStyle and getStyle methods to ol.layer.Vector

The setStyle method accepts a single style, an array of styles, or a style function.  The getStyle method returns what was set.  Internally, we use the getStyleFunction method which always returns a function.  When calling setStyle, a change event is dispatched (fixes #1671).
This commit is contained in:
Tim Schaub
2014-02-12 16:00:25 -07:00
parent a185fc963d
commit a1f714f7dc
2 changed files with 97 additions and 17 deletions

View File

@@ -46,6 +46,69 @@ describe('ol.layer.Vector', function() {
});
describe('#setStyle()', function() {
var source = new ol.source.Vector();
var 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();
});
layer.setStyle(style);
});
it('updates the internal style function', function() {
var layer = new ol.layer.Vector({
source: source
});
expect(layer.getStyleFunction()).to.be(undefined);
layer.setStyle(style);
expect(layer.getStyleFunction()).to.be.a('function');
});
});
describe('#getStyle()', function() {
var source = new ol.source.Vector();
var style = new ol.style.Style();
it('returns what is provided to setStyle', function() {
var layer = new ol.layer.Vector({
source: source
});
expect(layer.getStyle()).to.be(null);
layer.setStyle(style);
expect(layer.getStyle()).to.be(style);
layer.setStyle([style]);
expect(layer.getStyle()).to.eql([style]);
var styleFunction = function(feature, resolution) {
return [style];
};
layer.setStyle(styleFunction);
expect(layer.getStyle()).to.be(styleFunction);
});
});
});
goog.require('ol.layer.Layer');