context can now be given as argument in StyleMap.addUniqueValueRules()

this, for example, allows user to access to properties that are not part of the feature attributes.
r=elemoine (closes #1548)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@7216 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
pgiraud
2008-05-19 13:34:35 +00:00
parent 34ff282397
commit 815e55888a
4 changed files with 77 additions and 2 deletions

View File

@@ -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);
}
</script>
</head>
@@ -56,7 +92,11 @@
<div id="tags"></div>
<p id="shortdesc">
Shows how to create a style based on unique feature attribute values.
Shows how to create a style based :
<ul>
<li>on unique feature attribute values (markers),</li>
<li>on feature state values (circles).</li>
</ul>
</p>
<div id="map" class="smallmap"></div>

View File

@@ -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;
},

View File

@@ -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,

View File

@@ -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);