Merge pull request #1690 from tschaub/style

Accept a style option and provide setStyle and getStyle methods.
This commit is contained in:
Tim Schaub
2014-02-14 13:44:15 -07:00
31 changed files with 575 additions and 190 deletions

View File

@@ -389,7 +389,8 @@
* drawing finish (default is 12).
* @property {ol.geom.GeometryType} type Drawing type ('Point', 'LineString',
* 'Polygon', 'MultiPoint', 'MultiLineString', or 'MultiPolygon').
* @property {ol.feature.StyleFunction|undefined} styleFunction Style function.
* @property {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined} style
* Style for sketch features.
* @todo stability experimental
*/
@@ -541,7 +542,7 @@
* @property {number|undefined} opacity Opacity. 0-1. Default is `1`.
* @property {number|undefined} saturation Saturation.
* @property {ol.source.Vector} source Source.
* @property {ol.feature.StyleFunction|undefined} styleFunction Style function.
* @property {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined} style Layer style.
* @property {boolean|undefined} visible Visibility. Default is `true` (visible).
* @todo stability experimental
*/
@@ -575,7 +576,7 @@
* @typedef {Object} olx.FeatureOverlayOptions
* @property {Array.<ol.Feature>|ol.Collection|undefined} features Features.
* @property {ol.Map|undefined} map Map.
* @property {ol.feature.StyleFunction|undefined} styleFunction Style function.
* @property {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined} style Feature style.
*/
/**
@@ -745,8 +746,8 @@
* new canvases will be created for these resolutions only.
* @property {ol.source.Vector} source The vector source from which the vector
* features drawn in canvas elements are read.
* @property {ol.feature.StyleFunction|undefined} styleFunction Style function
* providing the styles to use when rendering features to the canvas.
* @property {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined} style
* Style to use when rendering features to the canvas.
*/
/**

View File

@@ -1,5 +1,8 @@
@exportSymbol ol.Feature
@exportProperty ol.Feature.prototype.getGeometryName
@exportProperty ol.Feature.prototype.setGeometryName
@exportProperty ol.Feature.prototype.getId
@exportProperty ol.Feature.prototype.getStyle
@exportProperty ol.Feature.prototype.getStyleFunction
@exportProperty ol.Feature.prototype.setGeometryName
@exportProperty ol.Feature.prototype.setId
@exportProperty ol.Feature.prototype.setStyle

View File

@@ -10,14 +10,6 @@ goog.require('ol.geom.Geometry');
goog.require('ol.style.Style');
/**
* @enum {string}
*/
ol.FeatureProperty = {
STYLE_FUNCTION: 'styleFunction'
};
/**
* @constructor
@@ -42,6 +34,20 @@ ol.Feature = function(opt_geometryOrValues) {
*/
this.geometryName_ = 'geometry';
/**
* User provided style.
* @private
* @type {ol.style.Style|Array.<ol.style.Style>|
* ol.feature.FeatureStyleFunction}
*/
this.style_ = null;
/**
* @private
* @type {ol.feature.FeatureStyleFunction|undefined}
*/
this.styleFunction_;
/**
* @private
* @type {goog.events.Key}
@@ -51,9 +57,6 @@ ol.Feature = function(opt_geometryOrValues) {
goog.events.listen(
this, ol.Object.getChangeEventType(this.geometryName_),
this.handleGeometryChanged_, false, this);
goog.events.listen(
this, ol.Object.getChangeEventType(ol.FeatureProperty.STYLE_FUNCTION),
this.handleStyleFunctionChange_, false, this);
if (goog.isDefAndNotNull(opt_geometryOrValues)) {
if (opt_geometryOrValues instanceof ol.geom.Geometry) {
@@ -103,18 +106,23 @@ ol.Feature.prototype.getGeometryName = function() {
};
/**
* @return {ol.style.Style|Array.<ol.style.Style>|
* ol.feature.FeatureStyleFunction} User provided style.
* @todo stability experimental
*/
ol.Feature.prototype.getStyle = function() {
return this.style_;
};
/**
* @return {ol.feature.FeatureStyleFunction|undefined} Style function.
* @todo stability experimental
*/
ol.Feature.prototype.getStyleFunction = function() {
return /** @type {ol.feature.FeatureStyleFunction|undefined} */ (
this.get(ol.FeatureProperty.STYLE_FUNCTION));
return this.styleFunction_;
};
goog.exportProperty(
ol.Feature.prototype,
'getStyleFunction',
ol.Feature.prototype.getStyleFunction);
/**
@@ -142,14 +150,6 @@ ol.Feature.prototype.handleGeometryChanged_ = function() {
};
/**
* @private
*/
ol.Feature.prototype.handleStyleFunctionChange_ = function() {
this.dispatchChangeEvent();
};
/**
* @param {ol.geom.Geometry|undefined} geometry Geometry.
* @todo stability experimental
@@ -164,17 +164,15 @@ goog.exportProperty(
/**
* @param {ol.feature.FeatureStyleFunction|undefined} styleFunction Style
* function
* @param {ol.style.Style|Array.<ol.style.Style>|
* ol.feature.FeatureStyleFunction} style Feature style.
* @todo stability experimental
*/
ol.Feature.prototype.setStyleFunction = function(styleFunction) {
this.set(ol.FeatureProperty.STYLE_FUNCTION, styleFunction);
ol.Feature.prototype.setStyle = function(style) {
this.style_ = style;
this.styleFunction_ = ol.feature.createFeatureStyleFunction(style);
this.dispatchChangeEvent();
};
goog.exportProperty(
ol.Feature.prototype,
'setStyleFunction',
ol.Feature.prototype.setStyleFunction);
/**
@@ -247,3 +245,70 @@ ol.feature.defaultStyleFunction = function(feature, resolution) {
}
return featureStyleFunction.call(feature, resolution);
};
/**
* Convert the provided object into a feature style function. Functions passed
* through unchanged. Arrays of ol.style.Style or single style objects wrapped
* in a new feature style function.
* @param {ol.feature.FeatureStyleFunction|Array.<ol.style.Style>|
* ol.style.Style} obj A feature style function, a single style, or an array
* of styles.
* @return {ol.feature.FeatureStyleFunction} A style function.
*/
ol.feature.createFeatureStyleFunction = function(obj) {
/**
* @type {ol.feature.FeatureStyleFunction}
*/
var styleFunction;
if (goog.isFunction(obj)) {
styleFunction = /** @type {ol.feature.FeatureStyleFunction} */ (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 = goog.functions.constant(styles);
}
return styleFunction;
};
/**
* 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 = goog.functions.constant(styles);
}
return styleFunction;
};

View File

@@ -1,7 +1,9 @@
@exportSymbol ol.FeatureOverlay
@exportProperty ol.FeatureOverlay.prototype.addFeature
@exportProperty ol.FeatureOverlay.prototype.getFeatures
@exportProperty ol.FeatureOverlay.prototype.getStyle
@exportProperty ol.FeatureOverlay.prototype.getStyleFunction
@exportProperty ol.FeatureOverlay.prototype.setFeatures
@exportProperty ol.FeatureOverlay.prototype.setMap
@exportProperty ol.FeatureOverlay.prototype.setStyleFunction
@exportProperty ol.FeatureOverlay.prototype.setStyle
@exportProperty ol.FeatureOverlay.prototype.removeFeature

View File

@@ -52,11 +52,18 @@ ol.FeatureOverlay = function(opt_options) {
*/
this.postComposeListenerKey_ = null;
/**
* @private
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction}
*/
this.style_ = null;
/**
* @private
* @type {ol.feature.StyleFunction|undefined}
*/
this.styleFunction_ = undefined;
this.styleFunction_ = goog.isDef(options.style) ?
ol.feature.createStyleFunction(options.style) : undefined;
if (goog.isDef(options.features)) {
if (goog.isArray(options.features)) {
@@ -69,10 +76,6 @@ ol.FeatureOverlay = function(opt_options) {
this.setFeatures(new ol.Collection());
}
if (goog.isDef(options.styleFunction)) {
this.setStyleFunction(options.styleFunction);
}
if (goog.isDef(options.map)) {
this.setMap(options.map);
}
@@ -238,16 +241,33 @@ ol.FeatureOverlay.prototype.setMap = function(map) {
/**
* @param {ol.feature.StyleFunction} styleFunction Style function.
* Set the style for features. This can be a single style object, an array
* of styles, or a function that takes a feature and resolution and returns
* an array of styles.
* @param {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction} style
* Overlay style.
* @todo stability experimental
*/
ol.FeatureOverlay.prototype.setStyleFunction = function(styleFunction) {
this.styleFunction_ = styleFunction;
ol.FeatureOverlay.prototype.setStyle = function(style) {
this.style_ = style;
this.styleFunction_ = ol.feature.createStyleFunction(style);
this.requestRenderFrame_();
};
/**
* Get the style for features. This returns whatever was passed to the `style`
* option at construction or to the `setStyle` method.
* @return {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction}
* Overlay style.
*/
ol.FeatureOverlay.prototype.getStyle = function() {
return this.style_;
};
/**
* Get the style function.
* @return {ol.feature.StyleFunction|undefined} Style function.
*/
ol.FeatureOverlay.prototype.getStyleFunction = function() {

View File

@@ -1448,7 +1448,7 @@ ol.format.KML.prototype.readPlacemark_ = function(node, objectStack) {
} else {
featureStyleFunction = ol.format.KML.makeFeatureStyleFunction_(style);
}
feature.setStyleFunction(featureStyleFunction);
feature.setStyle(featureStyleFunction);
return feature;
};

View File

@@ -146,9 +146,11 @@ ol.interaction.Draw = function(options) {
* @type {ol.FeatureOverlay}
* @private
*/
this.overlay_ = new ol.FeatureOverlay();
this.overlay_.setStyleFunction(goog.isDef(options.styleFunction) ?
options.styleFunction : ol.interaction.Draw.getDefaultStyleFunction());
this.overlay_ = new ol.FeatureOverlay({
style: goog.isDef(options.style) ?
options.style : ol.interaction.Draw.getDefaultStyleFunction()
});
};
goog.inherits(ol.interaction.Draw, ol.interaction.Interaction);

View File

@@ -1 +1,4 @@
@exportSymbol ol.layer.Vector
@exportProperty ol.layer.Vector.prototype.getStyle
@exportProperty ol.layer.Vector.prototype.getStyleFunction
@exportProperty ol.layer.Vector.prototype.setStyle

View File

@@ -1,5 +1,6 @@
goog.provide('ol.layer.Vector');
goog.require('goog.object');
goog.require('ol.feature');
goog.require('ol.layer.Layer');
@@ -8,8 +9,7 @@ goog.require('ol.layer.Layer');
* @enum {string}
*/
ol.layer.VectorProperty = {
RENDER_GEOMETRY_FUNCTIONS: 'renderGeometryFunctions',
STYLE_FUNCTION: 'styleFunction'
RENDER_GEOMETRY_FUNCTIONS: 'renderGeometryFunctions'
};
@@ -25,11 +25,28 @@ ol.layer.Vector = function(opt_options) {
var options = goog.isDef(opt_options) ?
opt_options : /** @type {olx.layer.VectorOptions} */ ({});
goog.base(this, /** @type {olx.layer.LayerOptions} */ (options));
var baseOptions = /** @type {olx.layer.LayerOptions} */
(goog.object.clone(options));
// FIXME veryify this
if (goog.isDef(options.styleFunction)) {
this.setStyleFunction(options.styleFunction);
delete baseOptions.style;
goog.base(this, baseOptions);
/**
* User provided style.
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction}
* @private
*/
this.style_ = null;
/**
* Style function for use within the library.
* @type {ol.feature.StyleFunction}
* @private
*/
this.styleFunction_;
if (goog.isDef(options.style)) {
this.setStyle(options.style);
}
};
@@ -51,17 +68,24 @@ goog.exportProperty(
/**
* @return {ol.feature.StyleFunction|undefined} Style function.
* Get the style for features. This returns whatever was passed to the `style`
* option at construction or to the `setStyle` method.
* @return {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction}
* Layer style.
*/
ol.layer.Vector.prototype.getStyle = function() {
return this.style_;
};
/**
* Get the style function.
* @return {ol.feature.StyleFunction} Layer style function.
* @todo stability experimental
*/
ol.layer.Vector.prototype.getStyleFunction = function() {
return /** @type {ol.feature.StyleFunction|undefined} */ (
this.get(ol.layer.VectorProperty.STYLE_FUNCTION));
return this.styleFunction_;
};
goog.exportProperty(
ol.layer.Vector.prototype,
'getStyleFunction',
ol.layer.Vector.prototype.getStyleFunction);
/**
@@ -81,16 +105,15 @@ goog.exportProperty(
/**
* If the styles are changed by setting a new style function or by changing the
* value returned by the style function then `dispatchChangeEvent` should be
* called on the layer for the layer to be refreshed on the screen.
* @param {ol.feature.StyleFunction|undefined} styleFunction Style function.
* Set the style for features. This can be a single style object, an array
* of styles, or a function that takes a feature and resolution and returns
* an array of styles.
* @param {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction} style
* Layer style.
* @todo stability experimental
*/
ol.layer.Vector.prototype.setStyleFunction = function(styleFunction) {
this.set(ol.layer.VectorProperty.STYLE_FUNCTION, styleFunction);
ol.layer.Vector.prototype.setStyle = function(style) {
this.style_ = style;
this.styleFunction_ = ol.feature.createStyleFunction(style);
this.dispatchChangeEvent();
};
goog.exportProperty(
ol.layer.Vector.prototype,
'setStyleFunction',
ol.layer.Vector.prototype.setStyleFunction);

View File

@@ -44,8 +44,9 @@ ol.source.ImageVector = function(options) {
* @private
* @type {!ol.feature.StyleFunction}
*/
this.styleFunction_ = goog.isDef(options.styleFunction) ?
options.styleFunction : ol.feature.defaultStyleFunction;
this.styleFunction_ = goog.isDef(options.style) ?
ol.feature.createStyleFunction(options.style) :
ol.feature.defaultStyleFunction;
/**
* @private