Skip to content

Commit

Permalink
Merge pull request #2394 from tonio/style_order
Browse files Browse the repository at this point in the history
Give precedence to feature styles
  • Loading branch information
bartvde committed Jul 25, 2014
2 parents 10ffc89 + 4b8ceca commit f468239
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 182 deletions.
24 changes: 12 additions & 12 deletions externs/olx.js
Expand Up @@ -1718,7 +1718,7 @@ olx.interaction.DragZoomOptions.prototype.style;
* snapTolerance: (number|undefined),
* type: ol.geom.GeometryType,
* minPointsPerRing: (number|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined),
* geometryName: (string|undefined),
* condition: (ol.events.ConditionType|undefined)}}
* @api
Expand Down Expand Up @@ -1765,7 +1765,7 @@ olx.interaction.DrawOptions.prototype.minPointsPerRing;

/**
* Style for sketch features.
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined}
* @type {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined}
*/
olx.interaction.DrawOptions.prototype.style;

Expand Down Expand Up @@ -1846,7 +1846,7 @@ olx.interaction.KeyboardZoomOptions.prototype.delta;
/**
* @typedef {{deleteCondition: (ol.events.ConditionType|undefined),
* pixelTolerance: (number|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined),
* features: ol.Collection}}
* @api
*/
Expand All @@ -1873,7 +1873,7 @@ olx.interaction.ModifyOptions.prototype.pixelTolerance;

/**
* FeatureOverlay style.
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined}
* @type {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined}
*/
olx.interaction.ModifyOptions.prototype.style;

Expand Down Expand Up @@ -1931,7 +1931,7 @@ olx.interaction.PinchZoomOptions.prototype.duration;
* @typedef {{addCondition: (ol.events.ConditionType|undefined),
* condition: (ol.events.ConditionType|undefined),
* layers: (Array.<ol.layer.Layer>|function(ol.layer.Layer): boolean|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined),
* removeCondition: (ol.events.ConditionType|undefined),
* toggleCondition: (ol.events.ConditionType|undefined)}}
* @api
Expand Down Expand Up @@ -1976,7 +1976,7 @@ olx.interaction.SelectOptions.prototype.layers;

/**
* Style for the selected features (those in the FeatureOverlay).
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined}
* @type {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined}
*/
olx.interaction.SelectOptions.prototype.style;

Expand Down Expand Up @@ -2501,7 +2501,7 @@ olx.layer.TileOptions.prototype.useInterimTilesOnError;
* opacity: (number|undefined),
* saturation: (number|undefined),
* source: ol.source.Vector,
* style: (ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined),
* visible: (boolean|undefined)}}
* @api
*/
Expand Down Expand Up @@ -2583,7 +2583,7 @@ olx.layer.VectorOptions.prototype.source;

/**
* Layer style.
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined}
* @type {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined}
*/
olx.layer.VectorOptions.prototype.style;

Expand All @@ -2598,7 +2598,7 @@ olx.layer.VectorOptions.prototype.visible;
/**
* @typedef {{features: (Array.<ol.Feature>|ol.Collection|undefined),
* map: (ol.Map|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined)}}
* style: (ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined)}}
* @api
*/
olx.FeatureOverlayOptions;
Expand All @@ -2620,7 +2620,7 @@ olx.FeatureOverlayOptions.prototype.map;

/**
* Feature style.
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined}
* @type {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined}
*/
olx.FeatureOverlayOptions.prototype.style;

Expand Down Expand Up @@ -3549,7 +3549,7 @@ olx.source.ImageCanvasOptions.prototype.state;
* ratio: (number|undefined),
* resolutions: (Array.<number>|undefined),
* source: ol.source.Vector,
* style: (ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined)}}
* style: (ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined)}}
* @api
*/
olx.source.ImageVectorOptions;
Expand Down Expand Up @@ -3602,7 +3602,7 @@ olx.source.ImageVectorOptions.prototype.source;

/**
* Style to use when rendering features to the canvas.
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction|undefined}
* @type {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined}
*/
olx.source.ImageVectorOptions.prototype.style;

Expand Down
96 changes: 0 additions & 96 deletions src/ol/feature.js
Expand Up @@ -291,69 +291,6 @@ ol.Feature.prototype.setGeometryName = function(name) {
ol.feature.FeatureStyleFunction;


/**
* Default style function for features.
* @param {number} resolution Resolution.
* @return {Array.<ol.style.Style>} Style.
* @this {ol.Feature}
*/
ol.feature.defaultFeatureStyleFunction = function(resolution) {
var fill = new ol.style.Fill({
color: 'rgba(255,255,255,0.4)'
});
var stroke = new ol.style.Stroke({
color: '#3399CC',
width: 1.25
});
var styles = [
new ol.style.Style({
image: new ol.style.Circle({
fill: fill,
stroke: stroke,
radius: 5
}),
fill: fill,
stroke: stroke
})
];

// now that we've run it the first time,
// replace the function with a constant version
ol.feature.defaultFeatureStyleFunction =
/** @type {function(this:ol.Feature):Array.<ol.style.Style>} */(
function(resolution) {
return styles;
});

return styles;
};


/**
* A function that takes an {@link ol.Feature} and a `{number}` representing
* the view's resolution. The function should return an array of
* {@link ol.style.Style}. This way e.g. a vector layer can be styled.
*
* @typedef {function(ol.Feature, number): Array.<ol.style.Style>}
* @api
*/
ol.feature.StyleFunction;


/**
* @param {ol.Feature} feature Feature.
* @param {number} resolution Resolution.
* @return {Array.<ol.style.Style>} Style.
*/
ol.feature.defaultStyleFunction = function(feature, resolution) {
var featureStyleFunction = feature.getStyleFunction();
if (!goog.isDef(featureStyleFunction)) {
featureStyleFunction = ol.feature.defaultFeatureStyleFunction;
}
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
Expand Down Expand Up @@ -388,39 +325,6 @@ ol.feature.createFeatureStyleFunction = function(obj) {
};


/**
* 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;
};


/**
* Default styles for editing features.
* @return {Object.<ol.geom.GeometryType, Array.<ol.style.Style>>} Styles
Expand Down
21 changes: 10 additions & 11 deletions src/ol/featureoverlay.js
Expand Up @@ -8,9 +8,9 @@ goog.require('goog.object');
goog.require('ol.Collection');
goog.require('ol.CollectionEventType');
goog.require('ol.Feature');
goog.require('ol.feature');
goog.require('ol.render.EventType');
goog.require('ol.renderer.vector');
goog.require('ol.style.Style');



Expand Down Expand Up @@ -63,19 +63,18 @@ ol.FeatureOverlay = function(opt_options) {

/**
* @private
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction}
* @type {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction}
*/
this.style_ = null;

/**
* @private
* @type {ol.feature.StyleFunction|undefined}
* @type {ol.style.StyleFunction|undefined}
*/
this.styleFunction_ = undefined;

if (goog.isDef(options.style)) {
this.setStyle(options.style);
}
this.setStyle(goog.isDef(options.style) ?
options.style : ol.style.defaultStyleFunction);

if (goog.isDef(options.features)) {
if (goog.isArray(options.features)) {
Expand Down Expand Up @@ -169,7 +168,7 @@ ol.FeatureOverlay.prototype.handleMapPostCompose_ = function(event) {
}
var styleFunction = this.styleFunction_;
if (!goog.isDef(styleFunction)) {
styleFunction = ol.feature.defaultStyleFunction;
styleFunction = ol.style.defaultStyleFunction;
}
var replayGroup = /** @type {ol.render.IReplayGroup} */
(event.replayGroup);
Expand Down Expand Up @@ -270,21 +269,21 @@ ol.FeatureOverlay.prototype.setMap = function(map) {
* 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
* @param {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction} style
* Overlay style.
* @api
*/
ol.FeatureOverlay.prototype.setStyle = function(style) {
this.style_ = style;
this.styleFunction_ = ol.feature.createStyleFunction(style);
this.styleFunction_ = ol.style.createStyleFunction(style);
this.render_();
};


/**
* 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}
* @return {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction}
* Overlay style.
* @api
*/
Expand All @@ -295,7 +294,7 @@ ol.FeatureOverlay.prototype.getStyle = function() {

/**
* Get the style function.
* @return {ol.feature.StyleFunction|undefined} Style function.
* @return {ol.style.StyleFunction|undefined} Style function.
* @api
*/
ol.FeatureOverlay.prototype.getStyleFunction = function() {
Expand Down
2 changes: 1 addition & 1 deletion src/ol/interaction/drawinteraction.js
Expand Up @@ -205,7 +205,7 @@ goog.inherits(ol.interaction.Draw, ol.interaction.Pointer);


/**
* @return {ol.feature.StyleFunction} Styles.
* @return {ol.style.StyleFunction} Styles.
*/
ol.interaction.Draw.getDefaultStyleFunction = function() {
var styles = ol.feature.createDefaultEditingStyles();
Expand Down
2 changes: 1 addition & 1 deletion src/ol/interaction/modifyinteraction.js
Expand Up @@ -769,7 +769,7 @@ ol.interaction.Modify.prototype.updateSegmentIndices_ = function(


/**
* @return {ol.feature.StyleFunction} Styles.
* @return {ol.style.StyleFunction} Styles.
*/
ol.interaction.Modify.getDefaultStyleFunction = function() {
var style = ol.feature.createDefaultEditingStyles();
Expand Down
2 changes: 1 addition & 1 deletion src/ol/interaction/selectinteraction.js
Expand Up @@ -204,7 +204,7 @@ ol.interaction.Select.prototype.setMap = function(map) {


/**
* @return {ol.feature.StyleFunction} Styles.
* @return {ol.style.StyleFunction} Styles.
*/
ol.interaction.Select.getDefaultStyleFunction = function() {
var styles = ol.feature.createDefaultEditingStyles();
Expand Down
19 changes: 9 additions & 10 deletions src/ol/layer/vectorlayer.js
@@ -1,8 +1,8 @@
goog.provide('ol.layer.Vector');

goog.require('goog.object');
goog.require('ol.feature');
goog.require('ol.layer.Layer');
goog.require('ol.style.Style');


/**
Expand Down Expand Up @@ -39,21 +39,20 @@ ol.layer.Vector = function(opt_options) {

/**
* User provided style.
* @type {ol.style.Style|Array.<ol.style.Style>|ol.feature.StyleFunction}
* @type {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction}
* @private
*/
this.style_ = null;

/**
* Style function for use within the library.
* @type {ol.feature.StyleFunction|undefined}
* @type {ol.style.StyleFunction|undefined}
* @private
*/
this.styleFunction_ = undefined;

if (goog.isDef(options.style)) {
this.setStyle(options.style);
}
this.setStyle(goog.isDefAndNotNull(options.style) ?
options.style : ol.style.defaultStyleFunction);

};
goog.inherits(ol.layer.Vector, ol.layer.Layer);
Expand All @@ -72,7 +71,7 @@ ol.layer.Vector.prototype.getRenderOrder = 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}
* @return {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction}
* Layer style.
* @api
*/
Expand All @@ -83,7 +82,7 @@ ol.layer.Vector.prototype.getStyle = function() {

/**
* Get the style function.
* @return {ol.feature.StyleFunction|undefined} Layer style function.
* @return {ol.style.StyleFunction|undefined} Layer style function.
* @api
*/
ol.layer.Vector.prototype.getStyleFunction = function() {
Expand All @@ -104,12 +103,12 @@ ol.layer.Vector.prototype.setRenderOrder = function(renderOrder) {
* 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
* @param {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction} style
* Layer style.
* @api
*/
ol.layer.Vector.prototype.setStyle = function(style) {
this.style_ = style;
this.styleFunction_ = ol.feature.createStyleFunction(style);
this.styleFunction_ = ol.style.createStyleFunction(style);
this.dispatchChangeEvent();
};

0 comments on commit f468239

Please sign in to comment.