Static ol.style.Style functions

This commit is contained in:
Tim Schaub
2016-08-09 00:36:28 -06:00
parent d4a5b2f48e
commit 2da724cad4
8 changed files with 28 additions and 27 deletions

View File

@@ -312,7 +312,7 @@ ol.inherits(ol.interaction.Draw, ol.interaction.Pointer);
* @return {ol.StyleFunction} Styles.
*/
ol.interaction.Draw.getDefaultStyleFunction = function() {
var styles = ol.style.createDefaultEditingStyles();
var styles = ol.style.Style.createDefaultEditing();
return function(feature, resolution) {
return styles[feature.getGeometry().getType()];
};

View File

@@ -1034,7 +1034,7 @@ ol.interaction.Modify.prototype.updateSegmentIndices_ = function(
* @return {ol.StyleFunction} Styles.
*/
ol.interaction.Modify.getDefaultStyleFunction = function() {
var style = ol.style.createDefaultEditingStyles();
var style = ol.style.Style.createDefaultEditing();
return function(feature, resolution) {
return style[ol.geom.GeometryType.POINT];
};

View File

@@ -342,7 +342,7 @@ ol.interaction.Select.prototype.setMap = function(map) {
* @return {ol.StyleFunction} Styles.
*/
ol.interaction.Select.getDefaultStyleFunction = function() {
var styles = ol.style.createDefaultEditingStyles();
var styles = ol.style.Style.createDefaultEditing();
ol.array.extend(styles[ol.geom.GeometryType.POLYGON],
styles[ol.geom.GeometryType.LINE_STRING]);
ol.array.extend(styles[ol.geom.GeometryType.GEOMETRY_COLLECTION],

View File

@@ -177,8 +177,8 @@ ol.layer.Vector.prototype.setRenderOrder = function(renderOrder) {
* @api stable
*/
ol.layer.Vector.prototype.setStyle = function(style) {
this.style_ = style !== undefined ? style : ol.style.defaultStyleFunction;
this.style_ = style !== undefined ? style : ol.style.Style.defaultFunction;
this.styleFunction_ = style === null ?
undefined : ol.style.createStyleFunction(this.style_);
undefined : ol.style.Style.createFunction(this.style_);
this.changed();
};

View File

@@ -293,8 +293,8 @@ ol.source.ImageVector.prototype.renderFeature_ = function(feature, resolution, p
* @api stable
*/
ol.source.ImageVector.prototype.setStyle = function(style) {
this.style_ = style !== undefined ? style : ol.style.defaultStyleFunction;
this.style_ = style !== undefined ? style : ol.style.Style.defaultFunction;
this.styleFunction_ = !style ?
undefined : ol.style.createStyleFunction(this.style_);
undefined : ol.style.Style.createFunction(this.style_);
this.changed();
};

View File

@@ -1,5 +1,6 @@
goog.provide('ol.style.Style');
goog.require('ol.asserts');
goog.require('ol.geom.GeometryType');
goog.require('ol.style.Circle');
goog.require('ol.style.Fill');
@@ -31,7 +32,7 @@ ol.style.Style = function(opt_options) {
* @private
* @type {!ol.StyleGeometryFunction}
*/
this.geometryFunction_ = ol.style.defaultGeometryFunction;
this.geometryFunction_ = ol.style.Style.defaultGeometryFunction;
if (options.geometry !== undefined) {
this.setGeometry(options.geometry);
@@ -159,7 +160,7 @@ ol.style.Style.prototype.setGeometry = function(geometry) {
return /** @type {ol.geom.Geometry} */ (feature.get(geometry));
};
} else if (!geometry) {
this.geometryFunction_ = ol.style.defaultGeometryFunction;
this.geometryFunction_ = ol.style.Style.defaultGeometryFunction;
} else if (geometry !== undefined) {
this.geometryFunction_ = function() {
return /** @type {ol.geom.Geometry} */ (geometry);
@@ -188,7 +189,7 @@ ol.style.Style.prototype.setZIndex = function(zIndex) {
* A style function, a single style, or an array of styles.
* @return {ol.StyleFunction} A style function.
*/
ol.style.createStyleFunction = function(obj) {
ol.style.Style.createFunction = function(obj) {
var styleFunction;
if (typeof obj === 'function') {
@@ -217,7 +218,7 @@ ol.style.createStyleFunction = function(obj) {
* @type {Array.<ol.style.Style>}
* @private
*/
ol.style.defaultStyle_ = null;
ol.style.Style.default_ = null;
/**
@@ -225,13 +226,13 @@ ol.style.defaultStyle_ = null;
* @param {number} resolution Resolution.
* @return {Array.<ol.style.Style>} Style.
*/
ol.style.defaultStyleFunction = function(feature, resolution) {
ol.style.Style.defaultFunction = function(feature, resolution) {
// We don't use an immediately-invoked function
// and a closure so we don't get an error at script evaluation time in
// browsers that do not support Canvas. (ol.style.Circle does
// canvas.getContext('2d') at construction time, which will cause an.error
// in such browsers.)
if (!ol.style.defaultStyle_) {
if (!ol.style.Style.default_) {
var fill = new ol.style.Fill({
color: 'rgba(255,255,255,0.4)'
});
@@ -239,7 +240,7 @@ ol.style.defaultStyleFunction = function(feature, resolution) {
color: '#3399CC',
width: 1.25
});
ol.style.defaultStyle_ = [
ol.style.Style.default_ = [
new ol.style.Style({
image: new ol.style.Circle({
fill: fill,
@@ -251,7 +252,7 @@ ol.style.defaultStyleFunction = function(feature, resolution) {
})
];
}
return ol.style.defaultStyle_;
return ol.style.Style.default_;
};
@@ -259,7 +260,7 @@ ol.style.defaultStyleFunction = function(feature, resolution) {
* Default styles for editing features.
* @return {Object.<ol.geom.GeometryType, Array.<ol.style.Style>>} Styles
*/
ol.style.createDefaultEditingStyles = function() {
ol.style.Style.createDefaultEditing = function() {
/** @type {Object.<ol.geom.GeometryType, Array.<ol.style.Style>>} */
var styles = {};
var white = [255, 255, 255, 1];
@@ -332,6 +333,6 @@ ol.style.createDefaultEditingStyles = function() {
* for.
* @return {ol.geom.Geometry|ol.render.Feature|undefined} Geometry to render.
*/
ol.style.defaultGeometryFunction = function(feature) {
ol.style.Style.defaultGeometryFunction = function(feature) {
return feature.getGeometry();
};

View File

@@ -76,10 +76,10 @@ describe('ol.layer.Vector', function() {
});
it('updates the internal style function', function() {
expect(layer.getStyleFunction()).to.be(ol.style.defaultStyleFunction);
expect(layer.getStyleFunction()).to.be(ol.style.Style.defaultFunction);
layer.setStyle(style);
expect(layer.getStyleFunction()).not.to.be(
ol.style.defaultStyleFunction);
ol.style.Style.defaultFunction);
});
it('allows setting an null style', function() {
@@ -91,8 +91,8 @@ describe('ol.layer.Vector', function() {
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);
expect(layer.getStyle()).to.be(ol.style.Style.defaultFunction);
expect(layer.getStyleFunction()).to.be(ol.style.Style.defaultFunction);
});
});
@@ -107,7 +107,7 @@ describe('ol.layer.Vector', function() {
source: source
});
expect(layer.getStyle()).to.be(ol.style.defaultStyleFunction);
expect(layer.getStyle()).to.be(ol.style.Style.defaultFunction);
layer.setStyle(style);
expect(layer.getStyle()).to.be(style);

View File

@@ -67,16 +67,16 @@ describe('ol.style.Style', function() {
});
describe('ol.style.createStyleFunction()', function() {
describe('ol.style.Style.createFunction()', function() {
var style = new ol.style.Style();
it('creates a style function from a single style', function() {
var styleFunction = ol.style.createStyleFunction(style);
var styleFunction = ol.style.Style.createFunction(style);
expect(styleFunction()).to.eql([style]);
});
it('creates a style function from an array of styles', function() {
var styleFunction = ol.style.createStyleFunction([style]);
var styleFunction = ol.style.Style.createFunction([style]);
expect(styleFunction()).to.eql([style]);
});
@@ -84,13 +84,13 @@ describe('ol.style.createStyleFunction()', function() {
var original = function() {
return [style];
};
var styleFunction = ol.style.createStyleFunction(original);
var styleFunction = ol.style.Style.createFunction(original);
expect(styleFunction).to.be(original);
});
it('throws on (some) unexpected input', function() {
expect(function() {
ol.style.createStyleFunction({bogus: 'input'});
ol.style.Style.createFunction({bogus: 'input'});
}).to.throwException();
});