Common code for creating a style function
This commit is contained in:
@@ -238,3 +238,38 @@ ol.feature.defaultStyleFunction = function(feature, resolution) {
|
|||||||
}
|
}
|
||||||
return featureStyleFunction.call(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;
|
||||||
|
};
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ goog.require('ol.CollectionEventType');
|
|||||||
goog.require('ol.Feature');
|
goog.require('ol.Feature');
|
||||||
goog.require('ol.feature');
|
goog.require('ol.feature');
|
||||||
goog.require('ol.render.EventType');
|
goog.require('ol.render.EventType');
|
||||||
goog.require('ol.style.Style');
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -53,35 +52,12 @@ ol.FeatureOverlay = function(opt_options) {
|
|||||||
*/
|
*/
|
||||||
this.postComposeListenerKey_ = null;
|
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
|
* @private
|
||||||
* @type {ol.feature.StyleFunction|undefined}
|
* @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.isDef(options.features)) {
|
||||||
if (goog.isArray(options.features)) {
|
if (goog.isArray(options.features)) {
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
goog.provide('ol.layer.Vector');
|
goog.provide('ol.layer.Vector');
|
||||||
|
|
||||||
goog.require('goog.asserts');
|
|
||||||
goog.require('ol.feature');
|
goog.require('ol.feature');
|
||||||
goog.require('ol.layer.Layer');
|
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));
|
goog.base(this, /** @type {olx.layer.LayerOptions} */ (options));
|
||||||
|
|
||||||
if (goog.isDef(options.style)) {
|
if (goog.isDef(options.style)) {
|
||||||
|
this.setStyleFunction(ol.feature.createStyleFunction(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ goog.require('ol.render.canvas.ReplayGroup');
|
|||||||
goog.require('ol.renderer.vector');
|
goog.require('ol.renderer.vector');
|
||||||
goog.require('ol.source.ImageCanvas');
|
goog.require('ol.source.ImageCanvas');
|
||||||
goog.require('ol.source.Vector');
|
goog.require('ol.source.Vector');
|
||||||
goog.require('ol.style.Style');
|
|
||||||
goog.require('ol.vec.Mat4');
|
goog.require('ol.vec.Mat4');
|
||||||
|
|
||||||
|
|
||||||
@@ -41,38 +40,13 @@ ol.source.ImageVector = function(options) {
|
|||||||
*/
|
*/
|
||||||
this.source_ = options.source;
|
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
|
* @private
|
||||||
* @type {!ol.feature.StyleFunction}
|
* @type {!ol.feature.StyleFunction}
|
||||||
*/
|
*/
|
||||||
this.styleFunction_ = goog.isDef(styleFunction) ?
|
this.styleFunction_ = goog.isDef(options.style) ?
|
||||||
styleFunction : ol.feature.defaultStyleFunction;
|
ol.feature.createStyleFunction(options.style) :
|
||||||
|
ol.feature.defaultStyleFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
|
|||||||
@@ -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.events');
|
||||||
goog.require('goog.object');
|
goog.require('goog.object');
|
||||||
goog.require('ol.Feature');
|
goog.require('ol.Feature');
|
||||||
|
goog.require('ol.feature');
|
||||||
goog.require('ol.geom.Point');
|
goog.require('ol.geom.Point');
|
||||||
|
goog.require('ol.style.Style');
|
||||||
|
|||||||
Reference in New Issue
Block a user