Static ol.style.Style functions
This commit is contained in:
@@ -312,7 +312,7 @@ ol.inherits(ol.interaction.Draw, ol.interaction.Pointer);
|
|||||||
* @return {ol.StyleFunction} Styles.
|
* @return {ol.StyleFunction} Styles.
|
||||||
*/
|
*/
|
||||||
ol.interaction.Draw.getDefaultStyleFunction = function() {
|
ol.interaction.Draw.getDefaultStyleFunction = function() {
|
||||||
var styles = ol.style.createDefaultEditingStyles();
|
var styles = ol.style.Style.createDefaultEditing();
|
||||||
return function(feature, resolution) {
|
return function(feature, resolution) {
|
||||||
return styles[feature.getGeometry().getType()];
|
return styles[feature.getGeometry().getType()];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1034,7 +1034,7 @@ ol.interaction.Modify.prototype.updateSegmentIndices_ = function(
|
|||||||
* @return {ol.StyleFunction} Styles.
|
* @return {ol.StyleFunction} Styles.
|
||||||
*/
|
*/
|
||||||
ol.interaction.Modify.getDefaultStyleFunction = function() {
|
ol.interaction.Modify.getDefaultStyleFunction = function() {
|
||||||
var style = ol.style.createDefaultEditingStyles();
|
var style = ol.style.Style.createDefaultEditing();
|
||||||
return function(feature, resolution) {
|
return function(feature, resolution) {
|
||||||
return style[ol.geom.GeometryType.POINT];
|
return style[ol.geom.GeometryType.POINT];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -342,7 +342,7 @@ ol.interaction.Select.prototype.setMap = function(map) {
|
|||||||
* @return {ol.StyleFunction} Styles.
|
* @return {ol.StyleFunction} Styles.
|
||||||
*/
|
*/
|
||||||
ol.interaction.Select.getDefaultStyleFunction = function() {
|
ol.interaction.Select.getDefaultStyleFunction = function() {
|
||||||
var styles = ol.style.createDefaultEditingStyles();
|
var styles = ol.style.Style.createDefaultEditing();
|
||||||
ol.array.extend(styles[ol.geom.GeometryType.POLYGON],
|
ol.array.extend(styles[ol.geom.GeometryType.POLYGON],
|
||||||
styles[ol.geom.GeometryType.LINE_STRING]);
|
styles[ol.geom.GeometryType.LINE_STRING]);
|
||||||
ol.array.extend(styles[ol.geom.GeometryType.GEOMETRY_COLLECTION],
|
ol.array.extend(styles[ol.geom.GeometryType.GEOMETRY_COLLECTION],
|
||||||
|
|||||||
@@ -177,8 +177,8 @@ ol.layer.Vector.prototype.setRenderOrder = function(renderOrder) {
|
|||||||
* @api stable
|
* @api stable
|
||||||
*/
|
*/
|
||||||
ol.layer.Vector.prototype.setStyle = function(style) {
|
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 ?
|
this.styleFunction_ = style === null ?
|
||||||
undefined : ol.style.createStyleFunction(this.style_);
|
undefined : ol.style.Style.createFunction(this.style_);
|
||||||
this.changed();
|
this.changed();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -293,8 +293,8 @@ ol.source.ImageVector.prototype.renderFeature_ = function(feature, resolution, p
|
|||||||
* @api stable
|
* @api stable
|
||||||
*/
|
*/
|
||||||
ol.source.ImageVector.prototype.setStyle = function(style) {
|
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 ?
|
this.styleFunction_ = !style ?
|
||||||
undefined : ol.style.createStyleFunction(this.style_);
|
undefined : ol.style.Style.createFunction(this.style_);
|
||||||
this.changed();
|
this.changed();
|
||||||
};
|
};
|
||||||
|
|||||||
+11
-10
@@ -1,5 +1,6 @@
|
|||||||
goog.provide('ol.style.Style');
|
goog.provide('ol.style.Style');
|
||||||
|
|
||||||
|
goog.require('ol.asserts');
|
||||||
goog.require('ol.geom.GeometryType');
|
goog.require('ol.geom.GeometryType');
|
||||||
goog.require('ol.style.Circle');
|
goog.require('ol.style.Circle');
|
||||||
goog.require('ol.style.Fill');
|
goog.require('ol.style.Fill');
|
||||||
@@ -31,7 +32,7 @@ ol.style.Style = function(opt_options) {
|
|||||||
* @private
|
* @private
|
||||||
* @type {!ol.StyleGeometryFunction}
|
* @type {!ol.StyleGeometryFunction}
|
||||||
*/
|
*/
|
||||||
this.geometryFunction_ = ol.style.defaultGeometryFunction;
|
this.geometryFunction_ = ol.style.Style.defaultGeometryFunction;
|
||||||
|
|
||||||
if (options.geometry !== undefined) {
|
if (options.geometry !== undefined) {
|
||||||
this.setGeometry(options.geometry);
|
this.setGeometry(options.geometry);
|
||||||
@@ -159,7 +160,7 @@ ol.style.Style.prototype.setGeometry = function(geometry) {
|
|||||||
return /** @type {ol.geom.Geometry} */ (feature.get(geometry));
|
return /** @type {ol.geom.Geometry} */ (feature.get(geometry));
|
||||||
};
|
};
|
||||||
} else if (!geometry) {
|
} else if (!geometry) {
|
||||||
this.geometryFunction_ = ol.style.defaultGeometryFunction;
|
this.geometryFunction_ = ol.style.Style.defaultGeometryFunction;
|
||||||
} else if (geometry !== undefined) {
|
} else if (geometry !== undefined) {
|
||||||
this.geometryFunction_ = function() {
|
this.geometryFunction_ = function() {
|
||||||
return /** @type {ol.geom.Geometry} */ (geometry);
|
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.
|
* A style function, a single style, or an array of styles.
|
||||||
* @return {ol.StyleFunction} A style function.
|
* @return {ol.StyleFunction} A style function.
|
||||||
*/
|
*/
|
||||||
ol.style.createStyleFunction = function(obj) {
|
ol.style.Style.createFunction = function(obj) {
|
||||||
var styleFunction;
|
var styleFunction;
|
||||||
|
|
||||||
if (typeof obj === 'function') {
|
if (typeof obj === 'function') {
|
||||||
@@ -217,7 +218,7 @@ ol.style.createStyleFunction = function(obj) {
|
|||||||
* @type {Array.<ol.style.Style>}
|
* @type {Array.<ol.style.Style>}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.style.defaultStyle_ = null;
|
ol.style.Style.default_ = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -225,13 +226,13 @@ ol.style.defaultStyle_ = null;
|
|||||||
* @param {number} resolution Resolution.
|
* @param {number} resolution Resolution.
|
||||||
* @return {Array.<ol.style.Style>} Style.
|
* @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
|
// We don't use an immediately-invoked function
|
||||||
// and a closure so we don't get an error at script evaluation time in
|
// 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
|
// browsers that do not support Canvas. (ol.style.Circle does
|
||||||
// canvas.getContext('2d') at construction time, which will cause an.error
|
// canvas.getContext('2d') at construction time, which will cause an.error
|
||||||
// in such browsers.)
|
// in such browsers.)
|
||||||
if (!ol.style.defaultStyle_) {
|
if (!ol.style.Style.default_) {
|
||||||
var fill = new ol.style.Fill({
|
var fill = new ol.style.Fill({
|
||||||
color: 'rgba(255,255,255,0.4)'
|
color: 'rgba(255,255,255,0.4)'
|
||||||
});
|
});
|
||||||
@@ -239,7 +240,7 @@ ol.style.defaultStyleFunction = function(feature, resolution) {
|
|||||||
color: '#3399CC',
|
color: '#3399CC',
|
||||||
width: 1.25
|
width: 1.25
|
||||||
});
|
});
|
||||||
ol.style.defaultStyle_ = [
|
ol.style.Style.default_ = [
|
||||||
new ol.style.Style({
|
new ol.style.Style({
|
||||||
image: new ol.style.Circle({
|
image: new ol.style.Circle({
|
||||||
fill: fill,
|
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.
|
* Default styles for editing features.
|
||||||
* @return {Object.<ol.geom.GeometryType, Array.<ol.style.Style>>} Styles
|
* @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>>} */
|
/** @type {Object.<ol.geom.GeometryType, Array.<ol.style.Style>>} */
|
||||||
var styles = {};
|
var styles = {};
|
||||||
var white = [255, 255, 255, 1];
|
var white = [255, 255, 255, 1];
|
||||||
@@ -332,6 +333,6 @@ ol.style.createDefaultEditingStyles = function() {
|
|||||||
* for.
|
* for.
|
||||||
* @return {ol.geom.Geometry|ol.render.Feature|undefined} Geometry to render.
|
* @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();
|
return feature.getGeometry();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -76,10 +76,10 @@ describe('ol.layer.Vector', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('updates the internal style function', 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);
|
layer.setStyle(style);
|
||||||
expect(layer.getStyleFunction()).not.to.be(
|
expect(layer.getStyleFunction()).not.to.be(
|
||||||
ol.style.defaultStyleFunction);
|
ol.style.Style.defaultFunction);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('allows setting an null style', function() {
|
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() {
|
it('sets the default style when passing undefined', function() {
|
||||||
layer.setStyle(style);
|
layer.setStyle(style);
|
||||||
layer.setStyle(undefined);
|
layer.setStyle(undefined);
|
||||||
expect(layer.getStyle()).to.be(ol.style.defaultStyleFunction);
|
expect(layer.getStyle()).to.be(ol.style.Style.defaultFunction);
|
||||||
expect(layer.getStyleFunction()).to.be(ol.style.defaultStyleFunction);
|
expect(layer.getStyleFunction()).to.be(ol.style.Style.defaultFunction);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -107,7 +107,7 @@ describe('ol.layer.Vector', function() {
|
|||||||
source: source
|
source: source
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(layer.getStyle()).to.be(ol.style.defaultStyleFunction);
|
expect(layer.getStyle()).to.be(ol.style.Style.defaultFunction);
|
||||||
|
|
||||||
layer.setStyle(style);
|
layer.setStyle(style);
|
||||||
expect(layer.getStyle()).to.be(style);
|
expect(layer.getStyle()).to.be(style);
|
||||||
|
|||||||
@@ -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();
|
var style = new ol.style.Style();
|
||||||
|
|
||||||
it('creates a style function from a single style', function() {
|
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]);
|
expect(styleFunction()).to.eql([style]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates a style function from an array of styles', function() {
|
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]);
|
expect(styleFunction()).to.eql([style]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -84,13 +84,13 @@ describe('ol.style.createStyleFunction()', function() {
|
|||||||
var original = function() {
|
var original = function() {
|
||||||
return [style];
|
return [style];
|
||||||
};
|
};
|
||||||
var styleFunction = ol.style.createStyleFunction(original);
|
var styleFunction = ol.style.Style.createFunction(original);
|
||||||
expect(styleFunction).to.be(original);
|
expect(styleFunction).to.be(original);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws on (some) unexpected input', function() {
|
it('throws on (some) unexpected input', function() {
|
||||||
expect(function() {
|
expect(function() {
|
||||||
ol.style.createStyleFunction({bogus: 'input'});
|
ol.style.Style.createFunction({bogus: 'input'});
|
||||||
}).to.throwException();
|
}).to.throwException();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user