-
Notifications
You must be signed in to change notification settings - Fork 6
/
Style.js
311 lines (282 loc) · 8.44 KB
/
Style.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
define([
"dojo/_base/declare", // declare
"dojo/_base/lang", // isString, isArray, getObject
"dojo/_base/array", // forEach
"./_base",
"dojo/colors"
], function(declare, lang, array, djeo){
var symbolizers = ["points", "lines"],
styleAttributes = {theme:1, name: 1, legend: 1},
noStyleMixin = {id:1, filter:1, styleClass:1, fid:1, composer: 1, composerOptions: 1, zoom: 1};
var filterPattern = /\$(?:(\w+)|\[([^\]]+)\])/g;
var Style = declare(null, {
// json style definition
def: null,
// features that have this style in their style attribute (i.e. inline style)
_features: null,
constructor: function(def, map) {
this.map = map;
this._features = {};
this._setDef(def);
if (def.id) map.styleById[def.id] = this;
},
set: function(def) {
var m = this.map;
var affectedFeatures = this.styleClass || this.fid ? {} : this._features;
// remove the style from styleByClassAndFid, styleByFid, styleByClass
if (this.styleClass) {
array.forEach(this.styleClass, function(_class){
// remove the style from styleByClassAndFid
if (this.fid) {
array.forEach(this.fid, function(_fid){
var entry = m.styleByClassAndFid[_class][_fid],
entryLength = entry.length;
for(var i=0; i<entryLength;i++) {
if (entry[i]==this) break;
}
if (i<entryLength) {
entry.splice(i,1);
var feature = m.getFeatureById(_fid);
if (feature && feature.styleClass) {
for (var i=0; i<feature.styleClass.length; i++) {
if (_class==feature.styleClass[i] && !affectedFeatures[feature.id]) {
affectedFeatures[feature.id] = feature;
break;
}
}
}
}
}, this);
}
else {
// remove the style from styleByClass
var entry = m.styleByClass[_class],
entryLength = entry.length;
for(var i=0; i<entryLength;i++) {
if (entry[i]==this) break;
}
if (i<entryLength) {
entry.splice(i,1);
var features = m.featuresByClass[_class];
if (features) {
array.forEach(features, function(f){
if (!affectedFeatures[f.id]) affectedFeatures[f.id] = f;
});
}
}
}
}, this);
}
else if (this.fid) {
// remove the style from styleByFid
array.forEach(this.fid, function(_fid){
var entry = m.styleByFid[_fid],
entryLength = entry.length;
for(var i=0; i<entryLength;i++) {
if (entry[i]==this) break;
}
if (i<entryLength) {
entry.splice(i,1);
var feature = m.getFeatureById(_fid);
if (feature && !affectedFeatures[feature.id]) affectedFeatures[feature.id] = feature;
}
}, this);
}
delete this.styleClass, this.fid, this.filter;
this._setDef(def);
// apply the style to the the affected features
this.map.renderFeatures(affectedFeatures);
},
_setDef: function(def) {
var m = this.map;
// mixin style attributes
for (attr in def) {
if (attr in styleAttributes) this[attr] = def[attr];
}
// prepare filter function
var filter = def.filter;
if (filter) {
if (lang.isString(filter)) {
// replace $attribute or $[attribute] with this.get('attribute')
filter = filter.replace(filterPattern, function(match, attr1, attr2){
return "this.get('" + (attr1||attr2) + "')";
});
filter = eval("_=function(){return "+filter+";}");
}
this.filter = filter;
}
// process zoom
if ("zoom" in def) {
var z = def.zoom;
if (lang.isArray(z)) {
if (z.length == 0) {
z = [-Infinity, Infinity];
}
else {
var z1 = z[0]===undefined ? -Infinity : z[0],
z2 = z[1]===undefined ? Infinity : z[1]
;
z = [z1, z2];
}
}
else {
z = [z, z];
}
this.zoom = z;
// notify map that there is a style depending on zoom
if (!m._hasZoomStyle) {
m._hasZoomStyle = true;
}
}
// prepare styleClass and fid
var styleClass = def.styleClass;
var fid = def.fid;
if (fid && !lang.isArray(fid)) fid = [fid];
if (styleClass) {
if (!lang.isArray(styleClass)) styleClass = [styleClass];
array.forEach(styleClass, function(_class){
// styleClass and fid simultaneously
if (fid) {
var byClassAndFid = m.styleByClassAndFid;
array.forEach(fid, function(_fid){
if (!byClassAndFid[_class]) byClassAndFid[_class] = {};
if (!byClassAndFid[_class][_fid]) byClassAndFid[_class][_fid] = [];
byClassAndFid[_class][_fid].push(this);
}, this);
}
// styleClass only
else {
if (!m.styleByClass[_class]) m.styleByClass[_class] = [];
m.styleByClass[_class].push(this);
}
}, this);
}
else if (fid) {
// fid only
array.forEach(fid, function(_fid){
if (!m.styleByFid[_fid]) m.styleByFid[_fid] = [];
m.styleByFid[_fid].push(this);
}, this);
}
if (styleClass) this.styleClass = styleClass;
if (fid) this.fid = fid;
// prepare composer
var composer = def.composer;
if (composer) {
if (lang.isString(composer)) {
composer = lang.getObject(composer);
}
this.composer = composer;
// features may need this attribute to be aware that the style has changed
this._updated = (new Date()).getTime();
this.composerOptions = def.composerOptions || def;
}
// prepare handler
var handler = def.handler;
if (handler) {
if (lang.isString(handler)) {
handler = lang.getObject(handler);
}
this.handler = handler;
}
// copy actual style attribute (e.g. color) to a separate javascript object
this.def = {};
for (attr in def) {
if (!(attr in styleAttributes) && !(attr in noStyleMixin)) this.def[attr] = def[attr];
}
},
destroy: function() {
delete this.def, this.filter, this.map, this._features, this.composer, this.composerOptions, this.handler;
}
});
djeo.calculateStyle = function(feature, theme) {
var styles = [];
// find all features participating in the style calculation
// features = feature itself + all its parents
var features = [],
parent = feature;
do {
features.push(parent);
parent = parent.parent;
}
while (parent != feature.map)
for (var i=features.length-1; i>=0; i--) {
appendFeatureStyle(features[i], styles, theme);
}
var engine = feature.map.engine,
zoom = engine._get_zoom && engine._get_zoom()
;
// now do actual style calculation
var resultStyle = {};
for (var i=0, num=styles.length; i<num; i++) {
// check if the style defines a zoom range
var style = styles[i],
z = style.zoom
;
if (z && zoom!==undefined && !(z[0]<=zoom && zoom<=z[1])) {
continue;
}
// evaluate filter
var applyStyle = style.filter ? evaluateFilter(style.filter, feature) : true;
if (applyStyle) {
styleMixin(resultStyle, style.def);
if (style.composer) {
style.composer(feature, style.composerOptions, resultStyle, style._updated);
}
}
};
// final adjustments
array.forEach(symbolizers, function(styleType){
// ensure that specific style is defined as array
var s = resultStyle[styleType];
if (s && !lang.isArray(s)) resultStyle[styleType] = [s];
})
return resultStyle;
}
var evaluateFilter = function(filter, feature) {
return filter.call(feature.map.useAttrs ? feature.attrs : feature);
}
var appendFeatureStyle = function(feature, styles, theme) {
// TODO: duplication of styleClass
var m = feature.map;
styleClass = feature.styleClass,
fid = feature.id;
if (styleClass) {
array.forEach(styleClass, function(_styleClass){
var byClassAndFid = m.styleByClassAndFid;
if (byClassAndFid[_styleClass] && byClassAndFid[_styleClass][fid]) {
append(byClassAndFid[_styleClass][fid], styles, theme);
}
else if (m.styleByClass[_styleClass]) append(m.styleByClass[_styleClass], styles, theme);
});
}
else if (m.styleByFid[fid]) append(m.styleByFid[fid], styles, theme);
if (feature.style) append(feature.style, styles, theme);
}
var append = function(/*Array*/what, /*Array*/to, theme) {
array.forEach(what, function(element){
if ( ( (!element.theme||element.theme=="normal") && (!theme||theme=="normal") ) || (element.theme==theme) ) {
if (element.def.reset) {
// clear all entries in the destination
for(var i=to.length-1; i>=0; i--) to.pop();
}
to.push(element);
}
});
}
var styleMixin = function(styleDef, styleAttrs) {
for (attr in styleAttrs) {
if (attr=="shape" && styleDef.img) {
// we've got a vector icon, override the currently set bitmap icon
delete styleDef.img;
}
else if (attr=="img" && styleDef.shape) {
// we've got a vector icon, override the currently set bitmap icon
delete styleDef.shape;
}
styleDef[attr] = styleAttrs[attr];
}
return styleDef;
}
return Style;
});