OpenLayers OpenLayers

Ticket #1548: 1548_r1790_A0.patch

File 1548_r1790_A0.patch, 4.9 kB (added by pgiraud, 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                "Unknown": {fillColor: "green"}, 
     75                "Update": {fillColor: "blue"}, 
     76                "Delete": {fillColor: "red"}, 
     77                "Insert": {fillColor: "orange"} 
     78            } 
     79             
     80            styleMap.addUniqueValueRules("default", "state", lookup, context); 
     81            layer = new OpenLayers.Layer.Vector('Points', { 
     82                styleMap: styleMap 
     83            }); 
     84             
     85            layer.addFeatures(features); 
     86            map.addLayer(layer); 
    5087        } 
    5188    </script> 
    5289  </head> 
     
    5693    <div id="tags"></div> 
    5794 
    5895    <p id="shortdesc"> 
    59       Shows how to create a style based on unique feature attribute values. 
     96      Shows how to create a style based : 
     97      <ul>  
     98        <li>on unique feature attribute values (markers),</li> 
     99        <li>on feature state values (circles).</li> 
     100      </ul> 
    60101    </p> 
    61102 
    62103    <div id="map" class="smallmap"></div>