diff --git a/src/ol/feature.js b/src/ol/feature.js
index 54439b1b8c..77d35aa333 100644
--- a/src/ol/feature.js
+++ b/src/ol/feature.js
@@ -238,3 +238,38 @@ ol.feature.defaultStyleFunction = function(feature, resolution) {
}
return featureStyleFunction.call(feature, resolution);
};
+
+
+/**
+ * Convert the provided object into a style function. Functions passed through
+ * unchanged. Arrays of ol.style.Style or single style objects wrapped in a
+ * new style function.
+ * @param {ol.feature.StyleFunction|Array.
|ol.style.Style} obj
+ * A style function, a single style, or an array of styles.
+ * @return {ol.feature.StyleFunction} A style function.
+ */
+ol.feature.createStyleFunction = function(obj) {
+ /**
+ * @type {ol.feature.StyleFunction}
+ */
+ var styleFunction;
+
+ if (goog.isFunction(obj)) {
+ styleFunction = /** @type {ol.feature.StyleFunction} */ (obj);
+ } else {
+ /**
+ * @type {Array.}
+ */
+ var styles;
+ if (goog.isArray(obj)) {
+ styles = obj;
+ } else {
+ goog.asserts.assertInstanceof(obj, ol.style.Style);
+ styles = [obj];
+ }
+ styleFunction = function(feature, resolution) {
+ return styles;
+ };
+ }
+ return styleFunction;
+};
diff --git a/src/ol/featureoverlay.js b/src/ol/featureoverlay.js
index e5d2784af0..50987b3a4b 100644
--- a/src/ol/featureoverlay.js
+++ b/src/ol/featureoverlay.js
@@ -10,7 +10,6 @@ goog.require('ol.CollectionEventType');
goog.require('ol.Feature');
goog.require('ol.feature');
goog.require('ol.render.EventType');
-goog.require('ol.style.Style');
@@ -53,35 +52,12 @@ ol.FeatureOverlay = function(opt_options) {
*/
this.postComposeListenerKey_ = null;
- /**
- * @type {ol.feature.StyleFunction}
- */
- var styleFunction;
- if (goog.isDef(options.style)) {
- if (goog.isFunction(options.style)) {
- styleFunction = /** @type {ol.feature.StyleFunction} */ (options.style);
- } else {
- /**
- * @type {Array.}
- */
- var styles;
- if (goog.isArray(options.style)) {
- styles = options.style;
- } else {
- goog.asserts.assertInstanceof(options.style, ol.style.Style);
- styles = [options.style];
- }
- styleFunction = function(feature, resolution) {
- return styles;
- };
- }
- }
-
/**
* @private
* @type {ol.feature.StyleFunction|undefined}
*/
- this.styleFunction_ = styleFunction;
+ this.styleFunction_ = goog.isDef(options.style) ?
+ ol.feature.createStyleFunction(options.style) : undefined;
if (goog.isDef(options.features)) {
if (goog.isArray(options.features)) {
diff --git a/src/ol/layer/vectorlayer.js b/src/ol/layer/vectorlayer.js
index 39f5b0eb1e..f27775b3a7 100644
--- a/src/ol/layer/vectorlayer.js
+++ b/src/ol/layer/vectorlayer.js
@@ -1,9 +1,7 @@
goog.provide('ol.layer.Vector');
-goog.require('goog.asserts');
goog.require('ol.feature');
goog.require('ol.layer.Layer');
-goog.require('ol.style.Style');
/**
@@ -30,31 +28,7 @@ ol.layer.Vector = function(opt_options) {
goog.base(this, /** @type {olx.layer.LayerOptions} */ (options));
if (goog.isDef(options.style)) {
-
- /**
- * @type {ol.feature.StyleFunction}
- */
- var styleFunction;
-
- if (goog.isFunction(options.style)) {
- styleFunction = /** @type {ol.feature.StyleFunction} */ (options.style);
- } else {
- /**
- * @type {Array.}
- */
- var styles;
- if (goog.isArray(options.style)) {
- styles = options.style;
- } else {
- goog.asserts.assertInstanceof(options.style, ol.style.Style);
- styles = [options.style];
- }
- styleFunction = function(feature, resolution) {
- return styles;
- };
- }
-
- this.setStyleFunction(styleFunction);
+ this.setStyleFunction(ol.feature.createStyleFunction(options.style));
}
};
diff --git a/src/ol/source/imagevectorsource.js b/src/ol/source/imagevectorsource.js
index d362fec8f4..d6050ac8d4 100644
--- a/src/ol/source/imagevectorsource.js
+++ b/src/ol/source/imagevectorsource.js
@@ -12,7 +12,6 @@ goog.require('ol.render.canvas.ReplayGroup');
goog.require('ol.renderer.vector');
goog.require('ol.source.ImageCanvas');
goog.require('ol.source.Vector');
-goog.require('ol.style.Style');
goog.require('ol.vec.Mat4');
@@ -41,38 +40,13 @@ ol.source.ImageVector = function(options) {
*/
this.source_ = options.source;
-
- /**
- * @type {ol.feature.StyleFunction}
- */
- var styleFunction;
-
- if (goog.isDef(options.style)) {
- if (goog.isFunction(options.style)) {
- styleFunction = /** @type {ol.feature.StyleFunction} */ (options.style);
- } else {
- /**
- * @type {Array.}
- */
- var styles;
- if (goog.isArray(options.style)) {
- styles = options.style;
- } else {
- goog.asserts.assertInstanceof(options.style, ol.style.Style);
- styles = [options.style];
- }
- styleFunction = function(feature, resolution) {
- return styles;
- };
- }
- }
-
/**
* @private
* @type {!ol.feature.StyleFunction}
*/
- this.styleFunction_ = goog.isDef(styleFunction) ?
- styleFunction : ol.feature.defaultStyleFunction;
+ this.styleFunction_ = goog.isDef(options.style) ?
+ ol.feature.createStyleFunction(options.style) :
+ ol.feature.defaultStyleFunction;
/**
* @private
diff --git a/test/spec/ol/feature.test.js b/test/spec/ol/feature.test.js
index afbf896ed9..ece506aad4 100644
--- a/test/spec/ol/feature.test.js
+++ b/test/spec/ol/feature.test.js
@@ -209,8 +209,39 @@ describe('ol.Feature', function() {
});
+describe('ol.feature.createStyleFunction()', function() {
+ var style = new ol.style.Style();
+
+ it('creates a style function from a single style', function() {
+ var styleFunction = ol.feature.createStyleFunction(style);
+ expect(styleFunction()).to.eql([style]);
+ });
+
+ it('creates a style function from an array of styles', function() {
+ var styleFunction = ol.feature.createStyleFunction([style]);
+ expect(styleFunction()).to.eql([style]);
+ });
+
+ it('passes through a function', function() {
+ var original = function() {
+ return [style];
+ };
+ var styleFunction = ol.feature.createStyleFunction(original);
+ expect(styleFunction).to.be(original);
+ });
+
+ it('throws on (some) unexpected input', function() {
+ expect(function() {
+ ol.feature.createStyleFunction({bogus: 'input'});
+ }).to.throwException();
+ });
+
+});
+
goog.require('goog.events');
goog.require('goog.object');
goog.require('ol.Feature');
+goog.require('ol.feature');
goog.require('ol.geom.Point');
+goog.require('ol.style.Style');