1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Style.js
7 * @requires OpenLayers/Feature/Vector.js
11 * Class: OpenLayers.StyleMap
13 OpenLayers.StyleMap = OpenLayers.Class({
17 * Hash of {<OpenLayers.Style>}, keyed by names of well known
18 * rendering intents (e.g. "default", "temporary", "select", "delete").
23 * Property: extendDefault
24 * {Boolean} if true, every render intent will extend the symbolizers
25 * specified for the "default" intent at rendering time. Otherwise, every
26 * rendering intent will be treated as a completely independent style.
31 * Constructor: OpenLayers.StyleMap
34 * style - {Object} Optional. Either a style hash, or a style object, or
35 * a hash of style objects (style hashes) keyed by rendering
36 * intent. If just one style hash or style object is passed,
37 * this will be used for all known render intents (default,
39 * options - {Object} optional hash of additional options for this
42 initialize: function (style, options) {
44 "default": new OpenLayers.Style(
45 OpenLayers.Feature.Vector.style["default"]),
46 "select": new OpenLayers.Style(
47 OpenLayers.Feature.Vector.style["select"]),
48 "temporary": new OpenLayers.Style(
49 OpenLayers.Feature.Vector.style["temporary"]),
50 "delete": new OpenLayers.Style(
51 OpenLayers.Feature.Vector.style["delete"])
54 // take whatever the user passed as style parameter and convert it
55 // into parts of stylemap.
56 if(style instanceof OpenLayers.Style) {
57 // user passed a style object
58 this.styles["default"] = style;
59 this.styles["select"] = style;
60 this.styles["temporary"] = style;
61 this.styles["delete"] = style;
62 } else if(typeof style == "object") {
63 for(var key in style) {
64 if(style[key] instanceof OpenLayers.Style) {
65 // user passed a hash of style objects
66 this.styles[key] = style[key];
67 } else if(typeof style[key] == "object") {
68 // user passsed a hash of style hashes
69 this.styles[key] = new OpenLayers.Style(style[key]);
71 // user passed a style hash (i.e. symbolizer)
72 this.styles["default"] = new OpenLayers.Style(style);
73 this.styles["select"] = new OpenLayers.Style(style);
74 this.styles["temporary"] = new OpenLayers.Style(style);
75 this.styles["delete"] = new OpenLayers.Style(style);
80 OpenLayers.Util.extend(this, options);
87 for(var key in this.styles) {
88 this.styles[key].destroy();
94 * Method: createSymbolizer
95 * Creates the symbolizer for a feature for a render intent.
98 * feature - {<OpenLayers.Feature>} The feature to evaluate the rules
99 * of the intended style against.
100 * intent - {String} The intent determines the symbolizer that will be
101 * used to draw the feature. Well known intents are "default"
102 * (for just drawing the features), "select" (for selected
103 * features) and "temporary" (for drawing features).
106 * {Object} symbolizer hash
108 createSymbolizer: function(feature, intent) {
110 feature = new OpenLayers.Feature.Vector();
112 if(!this.styles[intent]) {
115 feature.renderIntent = intent;
116 var defaultSymbolizer = {};
117 if(this.extendDefault && intent != "default") {
118 defaultSymbolizer = this.styles["default"].createSymbolizer(feature);
120 return OpenLayers.Util.extend(defaultSymbolizer,
121 this.styles[intent].createSymbolizer(feature));
125 * Method: addUniqueValueRules
126 * Convenience method to create comparison rules for unique values of a
127 * property. The rules will be added to the style object for a specified
128 * rendering intent. This method is a shortcut for creating something like
129 * the "unique value legends" familiar from well known desktop GIS systems
132 * renderIntent - {String} rendering intent to add the rules to
133 * property - {String} values of feature attributes to create the
135 * symbolizers - {Object} Hash of symbolizers, keyed by the desired
137 * context - {Object} An optional object with properties that
138 * symbolizers' property values should be evaluated
139 * against. If no context is specified, feature.attributes
142 addUniqueValueRules: function(renderIntent, property, symbolizers, context) {
144 for (var value in symbolizers) {
145 rules.push(new OpenLayers.Rule({
146 symbolizer: symbolizers[value],
148 filter: new OpenLayers.Filter.Comparison({
149 type: OpenLayers.Filter.Comparison.EQUAL_TO,
155 this.styles[renderIntent].addRules(rules);
158 CLASS_NAME: "OpenLayers.StyleMap"