Common code for creating a style function

This commit is contained in:
Tim Schaub
2014-02-07 13:25:21 -07:00
parent 6abb691224
commit c64c24d3dc
5 changed files with 72 additions and 82 deletions

View File

@@ -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>|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.<ol.style.Style>}
*/
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;
};

View File

@@ -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.<ol.style.Style>}
*/
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)) {

View File

@@ -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.<ol.style.Style>}
*/
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));
}
};

View File

@@ -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.<ol.style.Style>}
*/
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

View File

@@ -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');