[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Style.js |
---|
| 8 | * @requires OpenLayers/Feature/Vector.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.StyleMap |
---|
| 13 | */ |
---|
| 14 | OpenLayers.StyleMap = OpenLayers.Class({ |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * Property: styles |
---|
| 18 | * Hash of {<OpenLayers.Style>}, keyed by names of well known |
---|
| 19 | * rendering intents (e.g. "default", "temporary", "select", "delete"). |
---|
| 20 | */ |
---|
| 21 | styles: null, |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * Property: extendDefault |
---|
| 25 | * {Boolean} if true, every render intent will extend the symbolizers |
---|
| 26 | * specified for the "default" intent at rendering time. Otherwise, every |
---|
| 27 | * rendering intent will be treated as a completely independent style. |
---|
| 28 | */ |
---|
| 29 | extendDefault: true, |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Constructor: OpenLayers.StyleMap |
---|
| 33 | * |
---|
| 34 | * Parameters: |
---|
| 35 | * style - {Object} Optional. Either a style hash, or a style object, or |
---|
| 36 | * a hash of style objects (style hashes) keyed by rendering |
---|
| 37 | * intent. If just one style hash or style object is passed, |
---|
| 38 | * this will be used for all known render intents (default, |
---|
| 39 | * select, temporary) |
---|
| 40 | * options - {Object} optional hash of additional options for this |
---|
| 41 | * instance |
---|
| 42 | */ |
---|
| 43 | initialize: function (style, options) { |
---|
| 44 | this.styles = { |
---|
| 45 | "default": new OpenLayers.Style( |
---|
| 46 | OpenLayers.Feature.Vector.style["default"]), |
---|
| 47 | "select": new OpenLayers.Style( |
---|
| 48 | OpenLayers.Feature.Vector.style["select"]), |
---|
| 49 | "temporary": new OpenLayers.Style( |
---|
| 50 | OpenLayers.Feature.Vector.style["temporary"]), |
---|
| 51 | "delete": new OpenLayers.Style( |
---|
| 52 | OpenLayers.Feature.Vector.style["delete"]) |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | // take whatever the user passed as style parameter and convert it |
---|
| 56 | // into parts of stylemap. |
---|
| 57 | if(style instanceof OpenLayers.Style) { |
---|
| 58 | // user passed a style object |
---|
| 59 | this.styles["default"] = style; |
---|
| 60 | this.styles["select"] = style; |
---|
| 61 | this.styles["temporary"] = style; |
---|
| 62 | this.styles["delete"] = style; |
---|
| 63 | } else if(typeof style == "object") { |
---|
| 64 | for(var key in style) { |
---|
| 65 | if(style[key] instanceof OpenLayers.Style) { |
---|
| 66 | // user passed a hash of style objects |
---|
| 67 | this.styles[key] = style[key]; |
---|
| 68 | } else if(typeof style[key] == "object") { |
---|
| 69 | // user passsed a hash of style hashes |
---|
| 70 | this.styles[key] = new OpenLayers.Style(style[key]); |
---|
| 71 | } else { |
---|
| 72 | // user passed a style hash (i.e. symbolizer) |
---|
| 73 | this.styles["default"] = new OpenLayers.Style(style); |
---|
| 74 | this.styles["select"] = new OpenLayers.Style(style); |
---|
| 75 | this.styles["temporary"] = new OpenLayers.Style(style); |
---|
| 76 | this.styles["delete"] = new OpenLayers.Style(style); |
---|
| 77 | break; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | OpenLayers.Util.extend(this, options); |
---|
| 82 | }, |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * Method: destroy |
---|
| 86 | */ |
---|
| 87 | destroy: function() { |
---|
| 88 | for(var key in this.styles) { |
---|
| 89 | this.styles[key].destroy(); |
---|
| 90 | } |
---|
| 91 | this.styles = null; |
---|
| 92 | }, |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * Method: createSymbolizer |
---|
| 96 | * Creates the symbolizer for a feature for a render intent. |
---|
| 97 | * |
---|
| 98 | * Parameters: |
---|
| 99 | * feature - {<OpenLayers.Feature>} The feature to evaluate the rules |
---|
| 100 | * of the intended style against. |
---|
| 101 | * intent - {String} The intent determines the symbolizer that will be |
---|
| 102 | * used to draw the feature. Well known intents are "default" |
---|
| 103 | * (for just drawing the features), "select" (for selected |
---|
| 104 | * features) and "temporary" (for drawing features). |
---|
| 105 | * |
---|
| 106 | * Returns: |
---|
| 107 | * {Object} symbolizer hash |
---|
| 108 | */ |
---|
| 109 | createSymbolizer: function(feature, intent) { |
---|
| 110 | if(!feature) { |
---|
| 111 | feature = new OpenLayers.Feature.Vector(); |
---|
| 112 | } |
---|
| 113 | if(!this.styles[intent]) { |
---|
| 114 | intent = "default"; |
---|
| 115 | } |
---|
| 116 | feature.renderIntent = intent; |
---|
| 117 | var defaultSymbolizer = {}; |
---|
| 118 | if(this.extendDefault && intent != "default") { |
---|
| 119 | defaultSymbolizer = this.styles["default"].createSymbolizer(feature); |
---|
| 120 | } |
---|
| 121 | return OpenLayers.Util.extend(defaultSymbolizer, |
---|
| 122 | this.styles[intent].createSymbolizer(feature)); |
---|
| 123 | }, |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Method: addUniqueValueRules |
---|
| 127 | * Convenience method to create comparison rules for unique values of a |
---|
| 128 | * property. The rules will be added to the style object for a specified |
---|
| 129 | * rendering intent. This method is a shortcut for creating something like |
---|
| 130 | * the "unique value legends" familiar from well known desktop GIS systems |
---|
| 131 | * |
---|
| 132 | * Parameters: |
---|
| 133 | * renderIntent - {String} rendering intent to add the rules to |
---|
| 134 | * property - {String} values of feature attributes to create the |
---|
| 135 | * rules for |
---|
| 136 | * symbolizers - {Object} Hash of symbolizers, keyed by the desired |
---|
| 137 | * property values |
---|
| 138 | * context - {Object} An optional object with properties that |
---|
| 139 | * symbolizers' property values should be evaluated |
---|
| 140 | * against. If no context is specified, feature.attributes |
---|
| 141 | * will be used |
---|
| 142 | */ |
---|
| 143 | addUniqueValueRules: function(renderIntent, property, symbolizers, context) { |
---|
| 144 | var rules = []; |
---|
| 145 | for (var value in symbolizers) { |
---|
| 146 | rules.push(new OpenLayers.Rule({ |
---|
| 147 | symbolizer: symbolizers[value], |
---|
| 148 | context: context, |
---|
| 149 | filter: new OpenLayers.Filter.Comparison({ |
---|
| 150 | type: OpenLayers.Filter.Comparison.EQUAL_TO, |
---|
| 151 | property: property, |
---|
| 152 | value: value |
---|
| 153 | }) |
---|
| 154 | })); |
---|
| 155 | } |
---|
| 156 | this.styles[renderIntent].addRules(rules); |
---|
| 157 | }, |
---|
| 158 | |
---|
| 159 | CLASS_NAME: "OpenLayers.StyleMap" |
---|
| 160 | }); |
---|