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:
@@ -47,6 +47,42 @@
|
|||||||
|
|
||||||
layer.addFeatures(features);
|
layer.addFeatures(features);
|
||||||
map.addLayer(layer);
|
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>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
@@ -56,7 +92,11 @@
|
|||||||
<div id="tags"></div>
|
<div id="tags"></div>
|
||||||
|
|
||||||
<p id="shortdesc">
|
<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>
|
</p>
|
||||||
|
|
||||||
<div id="map" class="smallmap"></div>
|
<div id="map" class="smallmap"></div>
|
||||||
|
|||||||
@@ -173,6 +173,9 @@ OpenLayers.Rule = OpenLayers.Class({
|
|||||||
if (!context) {
|
if (!context) {
|
||||||
context = feature.attributes || feature.data;
|
context = feature.attributes || feature.data;
|
||||||
}
|
}
|
||||||
|
if (typeof this.context == "function") {
|
||||||
|
context = this.context(feature);
|
||||||
|
}
|
||||||
return context;
|
return context;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -130,12 +130,17 @@ OpenLayers.StyleMap = OpenLayers.Class({
|
|||||||
* rules for
|
* rules for
|
||||||
* symbolizers - {Object} Hash of symbolizers, keyed by the desired
|
* symbolizers - {Object} Hash of symbolizers, keyed by the desired
|
||||||
* property values
|
* 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 = [];
|
var rules = [];
|
||||||
for (var value in symbolizers) {
|
for (var value in symbolizers) {
|
||||||
rules.push(new OpenLayers.Rule({
|
rules.push(new OpenLayers.Rule({
|
||||||
symbolizer: symbolizers[value],
|
symbolizer: symbolizers[value],
|
||||||
|
context: context,
|
||||||
filter: new OpenLayers.Filter.Comparison({
|
filter: new OpenLayers.Filter.Comparison({
|
||||||
type: OpenLayers.Filter.Comparison.EQUAL_TO,
|
type: OpenLayers.Filter.Comparison.EQUAL_TO,
|
||||||
property: property,
|
property: property,
|
||||||
|
|||||||
@@ -14,6 +14,33 @@
|
|||||||
t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");
|
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) {
|
function test_Rule_destroy(t) {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user