From 815e55888acbc36155a204cbe753795d100a2516 Mon Sep 17 00:00:00 2001 From: pgiraud Date: Mon, 19 May 2008 13:34:35 +0000 Subject: [PATCH] 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 --- examples/styles-unique.html | 42 ++++++++++++++++++++++++++++++++++++- lib/OpenLayers/Rule.js | 3 +++ lib/OpenLayers/StyleMap.js | 7 ++++++- tests/Rule.html | 27 ++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) 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 : +

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