Changeset 7216 for trunk/openlayers
- Timestamp:
- 05/19/08 09:34:35 (3 months ago)
- Files:
-
- trunk/openlayers/examples/styles-unique.html (modified) (2 diffs)
- trunk/openlayers/lib/OpenLayers/Rule.js (modified) (1 diff)
- trunk/openlayers/lib/OpenLayers/StyleMap.js (modified) (1 diff)
- trunk/openlayers/tests/Rule.html (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openlayers/examples/styles-unique.html
r7095 r7216 48 48 layer.addFeatures(features); 49 49 map.addLayer(layer); 50 51 // create 20 random features with a random state property. 52 var features = new Array(20); 53 var states = [ 54 OpenLayers.State.UNKNOWN, 55 OpenLayers.State.UPDATE, 56 OpenLayers.State.DELETE, 57 OpenLayers.State.INSERT 58 ]; 59 for (var i=0; i<20; i++) { 60 features[i] = new OpenLayers.Feature.Vector( 61 new OpenLayers.Geometry.Point(Math.random()*360-180, Math.random()*180-90) 62 ); 63 features[i].state = states[parseInt(Math.random()*4)]; 64 } 65 66 var context = function(feature) { 67 return feature; 68 } 69 var styleMap = new OpenLayers.StyleMap(); 70 71 // create a lookup table with different symbolizers for the diffent 72 // state values 73 var lookup = {}; 74 lookup[OpenLayers.State.UNKNOWN] = {fillColor: "green"}; 75 lookup[OpenLayers.State.UPDATE] = {fillColor: "green"}; 76 lookup[OpenLayers.State.DELETE] = {fillColor: "red"}; 77 lookup[OpenLayers.State.INSERT] = {fillColor: "orange"}; 78 79 styleMap.addUniqueValueRules("default", "state", lookup, context); 80 layer = new OpenLayers.Layer.Vector('Points', { 81 styleMap: styleMap 82 }); 83 84 layer.addFeatures(features); 85 map.addLayer(layer); 50 86 } 51 87 </script> … … 57 93 58 94 <p id="shortdesc"> 59 Shows how to create a style based on unique feature attribute values. 95 Shows how to create a style based : 96 <ul> 97 <li>on unique feature attribute values (markers),</li> 98 <li>on feature state values (circles).</li> 99 </ul> 60 100 </p> 61 101 trunk/openlayers/lib/OpenLayers/Rule.js
r7173 r7216 174 174 context = feature.attributes || feature.data; 175 175 } 176 if (typeof this.context == "function") { 177 context = this.context(feature); 178 } 176 179 return context; 177 180 }, trunk/openlayers/lib/OpenLayers/StyleMap.js
r6834 r7216 131 131 * symbolizers - {Object} Hash of symbolizers, keyed by the desired 132 132 * property values 133 * context - {Object} An optional object with properties that 134 * symbolizers' property values should be evaluated 135 * against. If no context is specified, feature.attributes 136 * will be used 133 137 */ 134 addUniqueValueRules: function(renderIntent, property, symbolizers ) {138 addUniqueValueRules: function(renderIntent, property, symbolizers, context) { 135 139 var rules = []; 136 140 for (var value in symbolizers) { 137 141 rules.push(new OpenLayers.Rule({ 138 142 symbolizer: symbolizers[value], 143 context: context, 139 144 filter: new OpenLayers.Filter.Comparison({ 140 145 type: OpenLayers.Filter.Comparison.EQUAL_TO, trunk/openlayers/tests/Rule.html
r6719 r7216 14 14 t.eq(typeof rule.evaluate, "function", "rule has an evaluate function"); 15 15 } 16 17 function test_Rule_getContext(t) { 18 t.plan(2); 19 var rule, options; 20 21 var feature = { 22 attributes: { 23 'dude': 'hello' 24 }, 25 'foobar': 'world' 26 } 27 28 rule = new OpenLayers.Rule(); 29 var context = rule.getContext(feature); 30 t.eq(context.dude, "hello", "value returned by getContext is correct" 31 + " if no context is specified"); 32 33 var options = { 34 context: function(feature){ 35 return feature; 36 } 37 }; 38 rule = new OpenLayers.Rule(options); 39 var context = rule.getContext(feature); 40 t.eq(context.foobar, "world", "value returned by getContext is correct" 41 + " if a context is given in constructor options"); 42 } 16 43 17 44 function test_Rule_destroy(t) {
