OpenLayers OpenLayers

Ticket #1373: styleContext.patch

File styleContext.patch, 5.6 kB (added by ahocevar, 11 months ago)
  • lib/OpenLayers/Rule.js

    old new  
    2222     
    2323    /** 
    2424     * Property: context 
    25      * {Object} An optional object with properties that the rule and its 
    26      * symbolizers' property values should be evaluatad against. If no 
    27      * context is specified, feature.attributes will be used 
     25     * {Object} An optional object with properties that the rule should be 
     26     * evaluatad against. If no context is specified, feature.attributes will 
     27     * be used. 
    2828     */ 
    2929    context: null, 
    3030 
  • lib/OpenLayers/Style.js

    old new  
    4141    rules: null, 
    4242     
    4343    /** 
     44     * Property: context 
     45     * {Object} An optional object with properties that symbolizers' property 
     46     * values should be evaluatad against. If no context is specified, 
     47     * feature.attributes will be used 
     48     */ 
     49    context: null, 
     50 
     51    /** 
    4452     * Property: defaultStyle 
    4553     * {Object} hash of style properties to use as default for merging 
    4654     * rule-based style symbolizers onto. If no rules are defined, 
     
    116124        var appliedRules = false; 
    117125        for(var i=0; i<rules.length; i++) { 
    118126            rule = rules[i]; 
    119             context = rule.context; 
    120             if (!context) { 
    121                 context = feature.attributes || feature.data; 
    122             } 
    123127            // does the rule apply? 
    124128            var applies = rule.evaluate(feature); 
    125129             
     
    128132                    elseRules.push(rule); 
    129133                } else { 
    130134                    appliedRules = true; 
    131                     this.applySymbolizer(rule, style, feature, context); 
     135                    this.applySymbolizer(rule, style, feature); 
    132136                } 
    133137            } 
    134138        } 
     
    137141        if(appliedRules == false && elseRules.length > 0) { 
    138142            appliedRules = true; 
    139143            for(var i=0; i<elseRules.length; i++) { 
    140                 this.applySymbolizer(elseRules[i], style, feature, context); 
     144                this.applySymbolizer(elseRules[i], style, feature); 
    141145            } 
    142146        } 
    143147 
     
    158162     * rule - {OpenLayers.Rule} 
    159163     * style - {Object} 
    160164     * feature - {<OpenLayer.Feature.Vector>} 
    161      * context - {Object} 
    162165     * 
    163166     * Returns: 
    164167     * {Object} A style with new symbolizer applied. 
     
    163166     * Returns: 
    164167     * {Object} A style with new symbolizer applied. 
    165168     */ 
    166     applySymbolizer: function(rule, style, feature, context) { 
     169    applySymbolizer: function(rule, style, feature) { 
    167170        var symbolizerPrefix = feature.geometry ? 
    168171                this.getSymbolizerPrefix(feature.geometry) : 
    169172                OpenLayers.Style.SYMBOLIZER_PREFIXES[0]; 
     
    170173 
    171174        var symbolizer = rule.symbolizer[symbolizerPrefix]; 
    172175 
     176        var context = this.context || feature.attributes || feature.data; 
     177         
    173178        // merge the style with the current style 
    174179        return this.createLiterals( 
    175180                OpenLayers.Util.extend(style, symbolizer), context); 
     
    251256     * APIMethod: setDefaultStyle 
    252257     * Sets the default style for this style object. 
    253258     *  
    254      * Parameters: 
     259     * Parameters:the rule and its 
    255260     * style - {Object} Hash of style properties 
    256261     */ 
    257262    setDefaultStyle: function(style) { 
  • tests/Rule/test_Comparison.html

    old new  
    3838    } 
    3939     
    4040    function test_Comparison_evaluate(t) { 
    41         t.plan(3); 
     41        t.plan(4); 
    4242         
    4343        var rule = new OpenLayers.Rule.Comparison({ 
    4444                property: "area", 
     
    6262            t.eq(result, ruleResults[i], "feature "+i+ 
    6363                    " evaluates to "+result.toString()+" correctly."); 
    6464        } 
     65        rule.context = { 
     66            area: 4998 
     67        } 
     68        var result = rule.evaluate(); 
     69        t.eq(result, true, "evaluation against custom rule context works."); 
    6570    } 
    6671    </script>  
    6772</head>  
  • tests/test_Style.html

    old new  
    116116     
    117117    function test_Style_context(t) { 
    118118        t.plan(1); 
    119         var context = { 
    120             foo: "bar", 
    121             size: 10}; 
    122119        var rule = new OpenLayers.Rule.Comparison({ 
    123120            type: OpenLayers.Rule.Comparison.LESS_THAN, 
    124             context: context, 
    125121            property: "size", 
    126122            value: 11, 
    127             symbolizer: {"Point": {externalGraphic: "${foo}.png"}}}); 
     123            symbolizer: {"Point": {externalGraphic: "${img1}"}}}); 
    128124        var style = new OpenLayers.Style(); 
     125        style.context = { 
     126            "img1": "myImage.png" 
     127        }; 
    129128        style.addRules([rule]); 
    130         var styleHash = style.createSymbolizer(new OpenLayers.Feature.Vector()); 
    131         t.eq(styleHash.externalGraphic, "bar.png", "correctly evaluated rule against a custom context"); 
     129        var feature = new OpenLayers.Feature.Vector(); 
     130        feature.attributes = {size: 10}; 
     131        var styleHash = style.createSymbolizer(feature); 
     132        t.eq(styleHash.externalGraphic, "myImage.png", "correctly evaluated rule and calculated property styles from a custom context"); 
    132133    } 
    133134 
    134135    function test_Style_destroy(t) {