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