diff --git a/examples/styles-unique.html b/examples/styles-unique.html
index cfc08fd2a4..4ccad086fb 100644
--- a/examples/styles-unique.html
+++ b/examples/styles-unique.html
@@ -47,6 +47,42 @@
layer.addFeatures(features);
map.addLayer(layer);
+
+ // create 20 random features with a random state property.
+ var features = new Array(20);
+ var states = [
+ OpenLayers.State.UNKNOWN,
+ OpenLayers.State.UPDATE,
+ OpenLayers.State.DELETE,
+ OpenLayers.State.INSERT
+ ];
+ for (var i=0; i<20; i++) {
+ features[i] = new OpenLayers.Feature.Vector(
+ new OpenLayers.Geometry.Point(Math.random()*360-180, Math.random()*180-90)
+ );
+ features[i].state = states[parseInt(Math.random()*4)];
+ }
+
+ var context = function(feature) {
+ return feature;
+ }
+ var styleMap = new OpenLayers.StyleMap();
+
+ // create a lookup table with different symbolizers for the diffent
+ // state values
+ var lookup = {};
+ lookup[OpenLayers.State.UNKNOWN] = {fillColor: "green"};
+ lookup[OpenLayers.State.UPDATE] = {fillColor: "green"};
+ lookup[OpenLayers.State.DELETE] = {fillColor: "red"};
+ lookup[OpenLayers.State.INSERT] = {fillColor: "orange"};
+
+ styleMap.addUniqueValueRules("default", "state", lookup, context);
+ layer = new OpenLayers.Layer.Vector('Points', {
+ styleMap: styleMap
+ });
+
+ layer.addFeatures(features);
+ map.addLayer(layer);
}
@@ -56,7 +92,11 @@
- Shows how to create a style based on unique feature attribute values.
+ Shows how to create a style based :
+
+ - on unique feature attribute values (markers),
+ - on feature state values (circles).
+
diff --git a/lib/OpenLayers/Rule.js b/lib/OpenLayers/Rule.js
index 54ec5fec8a..f1fb695192 100644
--- a/lib/OpenLayers/Rule.js
+++ b/lib/OpenLayers/Rule.js
@@ -173,6 +173,9 @@ OpenLayers.Rule = OpenLayers.Class({
if (!context) {
context = feature.attributes || feature.data;
}
+ if (typeof this.context == "function") {
+ context = this.context(feature);
+ }
return context;
},
diff --git a/lib/OpenLayers/StyleMap.js b/lib/OpenLayers/StyleMap.js
index 88f33a5bbf..75b880567d 100644
--- a/lib/OpenLayers/StyleMap.js
+++ b/lib/OpenLayers/StyleMap.js
@@ -130,12 +130,17 @@ OpenLayers.StyleMap = OpenLayers.Class({
* rules for
* symbolizers - {Object} Hash of symbolizers, keyed by the desired
* property values
+ * context - {Object} An optional object with properties that
+ * symbolizers' property values should be evaluated
+ * against. If no context is specified, feature.attributes
+ * will be used
*/
- addUniqueValueRules: function(renderIntent, property, symbolizers) {
+ addUniqueValueRules: function(renderIntent, property, symbolizers, context) {
var rules = [];
for (var value in symbolizers) {
rules.push(new OpenLayers.Rule({
symbolizer: symbolizers[value],
+ context: context,
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: property,
diff --git a/tests/Rule.html b/tests/Rule.html
index 840a516c25..d27d88a7cf 100644
--- a/tests/Rule.html
+++ b/tests/Rule.html
@@ -13,6 +13,33 @@
t.eq(rule.foo, "bar", "constructor sets options correctly");
t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");
}
+
+ function test_Rule_getContext(t) {
+ t.plan(2);
+ var rule, options;
+
+ var feature = {
+ attributes: {
+ 'dude': 'hello'
+ },
+ 'foobar': 'world'
+ }
+
+ rule = new OpenLayers.Rule();
+ var context = rule.getContext(feature);
+ t.eq(context.dude, "hello", "value returned by getContext is correct"
+ + " if no context is specified");
+
+ var options = {
+ context: function(feature){
+ return feature;
+ }
+ };
+ rule = new OpenLayers.Rule(options);
+ var context = rule.getContext(feature);
+ t.eq(context.foobar, "world", "value returned by getContext is correct"
+ + " if a context is given in constructor options");
+ }
function test_Rule_destroy(t) {
t.plan(1);