]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/StyleMap.js
forbird creation or deletion of item; forbids adding or deleting an image
[syp.git] / openlayers / lib / OpenLayers / StyleMap.js
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. */
4
5 /**
6  * @requires OpenLayers/Style.js
7  * @requires OpenLayers/Feature/Vector.js
8  */
9  
10 /**
11  * Class: OpenLayers.StyleMap
12  */
13 OpenLayers.StyleMap = OpenLayers.Class({
14     
15     /**
16      * Property: styles
17      * Hash of {<OpenLayers.Style>}, keyed by names of well known
18      * rendering intents (e.g. "default", "temporary", "select", "delete").
19      */
20     styles: null,
21     
22     /**
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.
27      */
28     extendDefault: true,
29     
30     /**
31      * Constructor: OpenLayers.StyleMap
32      * 
33      * Parameters:
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,
38      *           select, temporary)
39      * options - {Object} optional hash of additional options for this
40      *           instance
41      */
42     initialize: function (style, options) {
43         this.styles = {
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"])
52         };
53         
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]);
70                 } else {
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);
76                     break;
77                 }
78             }
79         }
80         OpenLayers.Util.extend(this, options);
81     },
82
83     /**
84      * Method: destroy
85      */
86     destroy: function() {
87         for(var key in this.styles) {
88             this.styles[key].destroy();
89         }
90         this.styles = null;
91     },
92     
93     /**
94      * Method: createSymbolizer
95      * Creates the symbolizer for a feature for a render intent.
96      * 
97      * Parameters:
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).
104      * 
105      * Returns:
106      * {Object} symbolizer hash
107      */
108     createSymbolizer: function(feature, intent) {
109         if(!feature) {
110             feature = new OpenLayers.Feature.Vector();
111         }
112         if(!this.styles[intent]) {
113             intent = "default";
114         }
115         feature.renderIntent = intent;
116         var defaultSymbolizer = {};
117         if(this.extendDefault && intent != "default") {
118             defaultSymbolizer = this.styles["default"].createSymbolizer(feature);
119         }
120         return OpenLayers.Util.extend(defaultSymbolizer,
121             this.styles[intent].createSymbolizer(feature));
122     },
123     
124     /**
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
130      * 
131      * Parameters:
132      * renderIntent - {String} rendering intent to add the rules to
133      * property     - {String} values of feature attributes to create the
134      *                rules for
135      * symbolizers  - {Object} Hash of symbolizers, keyed by the desired
136      *                property values 
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
140      *                will be used
141      */
142     addUniqueValueRules: function(renderIntent, property, symbolizers, context) {
143         var rules = [];
144         for (var value in symbolizers) {
145             rules.push(new OpenLayers.Rule({
146                 symbolizer: symbolizers[value],
147                 context: context,
148                 filter: new OpenLayers.Filter.Comparison({
149                     type: OpenLayers.Filter.Comparison.EQUAL_TO,
150                     property: property,
151                     value: value
152                 })
153             }));
154         }
155         this.styles[renderIntent].addRules(rules);
156     },
157
158     CLASS_NAME: "OpenLayers.StyleMap"
159 });