Changeset 6396
- Timestamp:
- 02/28/08 12:57:37 (6 months ago)
- Files:
-
- trunk/openlayers/examples/styles-unique.html (added)
- trunk/openlayers/lib/OpenLayers/Rule.js (modified) (2 diffs)
- trunk/openlayers/lib/OpenLayers/Style.js (modified) (6 diffs)
- trunk/openlayers/lib/OpenLayers/StyleMap.js (modified) (1 diff)
- trunk/openlayers/tests/Rule/test_Comparison.html (modified) (2 diffs)
- trunk/openlayers/tests/test_Style.html (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openlayers/lib/OpenLayers/Rule.js
r6131 r6396 23 23 /** 24 24 * Property: context 25 * {Object} An optional object with properties that the rule and its26 * symbolizers' property values should be evaluatad against. If no27 * context is specified, feature.attributes will be used25 * {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. 28 28 */ 29 29 context: null, … … 41 41 /** 42 42 * Property: symbolizer 43 * {Object} Hash of styles for this rule. Contains hashes of feature44 * s tyles. Keys are one or more of ["Point", "Line", "Polygon"]43 * {Object} Symbolizer or hash of symbolizers for this rule. If hash of 44 * symbolizers, keys are one or more of ["Point", "Line", "Polygon"] 45 45 */ 46 46 symbolizer: null, trunk/openlayers/lib/OpenLayers/Style.js
r6246 r6396 42 42 43 43 /** 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 /** 44 52 * Property: defaultStyle 45 53 * {Object} hash of style properties to use as default for merging … … 117 125 for(var i=0; i<rules.length; i++) { 118 126 rule = rules[i]; 119 context = rule.context;120 if (!context) {121 context = feature.attributes || feature.data;122 }123 127 // does the rule apply? 124 128 var applies = rule.evaluate(feature); … … 129 133 } else { 130 134 appliedRules = true; 131 this.applySymbolizer(rule, style, feature , context);135 this.applySymbolizer(rule, style, feature); 132 136 } 133 137 } … … 138 142 appliedRules = true; 139 143 for(var i=0; i<elseRules.length; i++) { 140 this.applySymbolizer(elseRules[i], style, feature , context);144 this.applySymbolizer(elseRules[i], style, feature); 141 145 } 142 146 } … … 159 163 * style - {Object} 160 164 * feature - {<OpenLayer.Feature.Vector>} 161 * context - {Object}162 165 * 163 166 * Returns: 164 167 * {Object} A style with new symbolizer applied. 165 168 */ 166 applySymbolizer: function(rule, style, feature , context) {169 applySymbolizer: function(rule, style, feature) { 167 170 var symbolizerPrefix = feature.geometry ? 168 171 this.getSymbolizerPrefix(feature.geometry) : 169 172 OpenLayers.Style.SYMBOLIZER_PREFIXES[0]; 170 173 171 var symbolizer = rule.symbolizer[symbolizerPrefix]; 172 174 var symbolizer = rule.symbolizer[symbolizerPrefix] || rule.symbolizer; 175 176 var context = this.context || feature.attributes || feature.data; 177 173 178 // merge the style with the current style 174 179 return this.createLiterals( … … 213 218 // check the default style 214 219 var style = this.defaultStyle; 215 for (var i in style) { 216 if (typeof style[i] == "string" && style[i].match(/\$\{\w+\}/)) { 217 propertyStyles[i] = true; 218 } 219 } 220 this.addPropertyStyles(propertyStyles, style); 220 221 221 222 // walk through all rules to check for properties in their symbolizer 222 223 var rules = this.rules; 223 var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES;224 var symbolizer, value; 224 225 for (var i=0; i<rules.length; i++) { 225 for (var s=0; s<prefixes.length; s++) { 226 style = rules[i].symbolizer[prefixes[s]]; 227 for (var j in style) { 228 if (typeof style[j] == "string" && 229 style[j].match(/\$\{\w+\}/)) { 230 propertyStyles[j] = true; 231 } 226 var symbolizer = rules[i].symbolizer; 227 for (var key in symbolizer) { 228 value = symbolizer[key]; 229 if (typeof value == "object") { 230 // symbolizer key is "Point", "Line" or "Polygon" 231 this.addPropertyStyles(propertyStyles, value); 232 } else { 233 // symbolizer is a hash of style properties 234 this.addPropertyStyles(propertyStyles, symbolizer); 235 break; 232 236 } 237 } 238 } 239 return propertyStyles; 240 }, 241 242 /** 243 * Method: addPropertyStyles 244 * 245 * Parameters: 246 * propertyStyles - {Object} hash to add new property styles to. Will be 247 * modified inline 248 * symbolizer - {Object} search this symbolizer for property styles 249 * 250 * Returns: 251 * {Object} propertyStyles hash 252 */ 253 addPropertyStyles: function(propertyStyles, symbolizer) { 254 var property; 255 for (var key in symbolizer) { 256 property = symbolizer[key]; 257 if (typeof property == "string" && 258 property.match(/\$\{\w+\}/)) { 259 propertyStyles[key] = true; 233 260 } 234 261 } trunk/openlayers/lib/OpenLayers/StyleMap.js
r6240 r6396 111 111 this.styles[intent].createSymbolizer(feature)); 112 112 }, 113 114 /** 115 * Method: addUniqueValueRules 116 * Convenience method to create comparison rules for unique values of a 117 * property. The rules will be added to the style object for a specified 118 * rendering intent. This method is a shortcut for creating something like 119 * the "unique value legends" familiar from well known desktop GIS systems 120 * 121 * Parameters: 122 * renderIntent - {String} rendering intent to add the rules to 123 * property - {String} values of feature attributes to create the 124 * rules for 125 * symbolizers - {Object} Hash of symbolizers, keyed by the desired 126 * property values 127 */ 128 addUniqueValueRules: function(renderIntent, property, symbolizers) { 129 var rules = []; 130 for (var value in symbolizers) { 131 rules.push(new OpenLayers.Rule.Comparison({ 132 type: OpenLayers.Rule.Comparison.EQUAL_TO, 133 property: property, 134 value: value, 135 symbolizer: symbolizers[value]})); 136 } 137 this.styles[renderIntent].addRules(rules); 138 }, 113 139 114 140 CLASS_NAME: "OpenLayers.StyleMap" trunk/openlayers/tests/Rule/test_Comparison.html
r6131 r6396 39 39 40 40 function test_Comparison_evaluate(t) { 41 t.plan( 3);41 t.plan(4); 42 42 43 43 var rule = new OpenLayers.Rule.Comparison({ … … 63 63 " evaluates to "+result.toString()+" correctly."); 64 64 } 65 rule.context = { 66 area: 4998 67 } 68 var result = rule.evaluate(); 69 t.eq(result, true, "evaluation against custom rule context works."); 65 70 } 66 71 </script> trunk/openlayers/tests/test_Style.html
r6240 r6396 117 117 function test_Style_context(t) { 118 118 t.plan(1); 119 var context = {120 foo: "bar",121 size: 10};122 119 var rule = new OpenLayers.Rule.Comparison({ 123 120 type: OpenLayers.Rule.Comparison.LESS_THAN, 124 context: context,125 121 property: "size", 126 122 value: 11, 127 symbolizer: {"Point": {externalGraphic: "${ foo}.png"}}});123 symbolizer: {"Point": {externalGraphic: "${img1}"}}}); 128 124 var style = new OpenLayers.Style(); 125 style.context = { 126 "img1": "myImage.png" 127 }; 129 128 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"); 133 } 134 135 function test_Style_findPropertyStyles(t) { 136 t.plan(4); 137 var rule1 = new OpenLayers.Rule({symbolizer: { 138 pointRadius: 3, 139 externalGraphic: "${foo}.bar" 140 }}); 141 var rule2 = new OpenLayers.Rule({symbolizer: {"Point": { 142 strokeWidth: "${foo}" 143 }}}); 144 var style = new OpenLayers.Style({ 145 strokeOpacity: 1, 146 strokeColor: "${foo}" 147 }); 148 style.addRules([rule1, rule2]); 149 var propertyStyles = style.findPropertyStyles(); 150 t.ok(propertyStyles.externalGraphic, "detected externalGraphic from rule correctly"); 151 t.ok(propertyStyles.strokeWidth, "detected strokeWidth from Point symbolizer correctly"); 152 t.ok(propertyStyles.strokeColor, "detected strokeColor from style correctly"); 153 t.eq(typeof propertyStyles.pointRadius, "undefined", "correctly detected pointRadius as non-property style"); 132 154 } 133 155
