OpenLayers OpenLayers

Ticket #1548: 1548_r1790_A1.patch

File 1548_r1790_A1.patch, 5.0 kB (added by elemoine, 8 months ago)
  • tests/Rule.html

    old new  
    1313        t.eq(rule.foo, "bar", "constructor sets options correctly");  
    1414        t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");  
    1515    } 
     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    } 
    1643 
    1744    function test_Rule_destroy(t) { 
    1845        t.plan(1); 
  • lib/OpenLayers/StyleMap.js

    old new  
    130130     *                rules for 
    131131     * symbolizers  - {Object} Hash of symbolizers, keyed by the desired 
    132132     *                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 
    133137     */ 
    134     addUniqueValueRules: function(renderIntent, property, symbolizers) { 
     138    addUniqueValueRules: function(renderIntent, property, symbolizers, context) { 
    135139        var rules = []; 
    136140        for (var value in symbolizers) { 
    137141            rules.push(new OpenLayers.Rule({ 
    138142                symbolizer: symbolizers[value], 
     143                context: context, 
    139144                filter: new OpenLayers.Filter.Comparison({ 
    140145                    type: OpenLayers.Filter.Comparison.EQUAL_TO, 
    141146                    property: property, 
  • lib/OpenLayers/Rule.js

    old new  
    173173        if (!context) { 
    174174            context = feature.attributes || feature.data; 
    175175        } 
     176        if (typeof this.context == "function") { 
     177            context = this.context(feature); 
     178        } 
    176179        return context; 
    177180    }, 
    178181         
  • examples/styles-unique.html

    old new  
    4747             
    4848            layer.addFeatures(features); 
    4949            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); 
    5086        } 
    5187    </script> 
    5288  </head> 
     
    5692    <div id="tags"></div> 
    5793 
    5894    <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> 
    60100    </p> 
    61101 
    62102    <div id="map" class="smallmap"></div>