forked from vvoovv/djeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeatureContainer.js
229 lines (205 loc) · 5.33 KB
/
FeatureContainer.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
define([
"dojo/_base/declare", // declare
"dojo/_base/lang", // isString, isArray
"dojo/_base/array", // forEach
"./_base",
"./Feature",
"./util/_base",
"./util/bbox"
], function(declare, lang, array, djeo, Feature, u, bbox){
var fc = declare([Feature], {
type: "FeatureContainer",
isContainer: true,
features: null,
handles: null,
numVisibleFeatures: 0,
constructor: function(featureDef, kwArgs) {
this._init();
this.handles = {};
},
_init: function() {
if (this.features) {
var features = this.features;
this.features = [];
this.addFeatures(features, true, true);
}
else this.features = [];
},
show: function(show) {
if (show === undefined) show = true;
if (this.features.length == 0) {
// just notify parent
this.parent._show(this, show, true);
}
array.forEach(this.features, function(feature){
feature.show(show);
}, this);
if (this.visible != show) {
this.visible = show;
}
},
_show: function(feature, show, attrOnly){
// if attrOnly==true don't call feature._show();
if (show) {
this.numVisibleFeatures++;
if (!this.visible) {
// set visibility to true
this.visible = true;
}
if (this.numVisibleFeatures == 1) {
// notify parent
this.parent._show(this, true, true);
}
}
else {
this.numVisibleFeatures--;
if (this.numVisibleFeatures==0){
// notify parent
this.parent._show(this, false, true);
}
}
if (!attrOnly) {
feature._show(show);
}
},
addFeatures: function(/* Array */features, ignoreEvents, preventRendering) {
if (!lang.isArray(features)) features = [features];
var addedFeatures = [];
array.forEach(features, function(feature){
if (feature.declaredClass) { // derived from djeo.Feature
feature.setMap(this.map);
feature.setParent(this);
this.features.push(feature);
}
else {
var featureType = feature.type ? feature.type : ( feature.features ? "FeatureContainer" : "Placemark" );
var ctor = djeo.featureTypes[featureType];
if (ctor) {
feature = new ctor(feature, {map: this.map, parent: this});
this.features.push(feature);
}
}
if (feature.declaredClass) {
feature.setMap(this.map);
feature.setParent(this);
this.map.registerFeature(feature);
addedFeatures.push(feature);
}
}, this);
if (!preventRendering) {
this.map.renderFeatures(addedFeatures, null, true);
}
if (!ignoreEvents) {
// attach parent's events to the feature
array.forEach(addedFeatures, function(feature) {
for(var handle in this.handles) {
var handleObj = this.handles[handle];
if (handleObj.keys) {
for (var key in handleObj.keys) {
feature.onForHandle(handle, {
events: handleObj.events,
method: handleObj.method,
context: handleObj.context,
key: key,
value: handleObj.keys[key]
});
}
}
else {
feature.onForHandle(handle, {
events: handleObj.events,
method: handleObj.method,
context: handleObj.context
});
}
}
}, this);
}
return addedFeatures;
},
removeFeatures: function(features) {
if (!lang.isArray(features)) features = [features];
var removedFeatures = [];
array.forEach(features, function(feature){
feature = feature.declaredClass ? feature : this.map.getFeatureById(feature);
if (feature) feature.remove();
}, this);
return removedFeatures;
},
remove: function() {
var features = this.features;
for(var i=features.length-1; i>=0; i--) {
features[i].remove();
}
},
getBbox: function() {
if (this.features.length == 0) return null;
var bb = [Infinity,Infinity,-Infinity,-Infinity];
array.forEach(this.features, function(feature){
bbox.extend(bb, feature.getBbox());
}, this);
return bb;
},
render: function(theme, destroy) {
return this.map.engine.renderContainer(this, theme, destroy);
},
_render: function(theme, destroy) {
return this.map.engine._renderContainer(this, theme, destroy);
},
getContainer: function() {
if (!this.container) {
this.container = this.map.engine.createContainer(this);
}
return this.container;
},
onForHandle: function(handle, kwArgs) {
if (kwArgs.events) {
kwArgs.events = lang.isString(kwArgs.events) ? [kwArgs.events] : kwArgs.events;
}
handle = handle || u.uid();
array.forEach(this.features, function(feature) {
feature.onForHandle(handle, kwArgs);
});
var handles = this.handles,
handleObj = handles[handle]
;
if (!handleObj) {
handleObj = {
events: kwArgs.events,
method: kwArgs.method,
context: kwArgs.context
}
handles[handle] = handleObj;
}
var key = kwArgs.key
if (key) {
if (!handleObj.keys) handleObj.keys = {};
handleObj.keys[key] = kwArgs.value;
}
return handle;
},
disconnect: function(handle, key, keepIfKey) {
var deleteHandle = true;
array.forEach(this.features, function(feature) {
feature.disconnect(handle, key, keepIfKey);
if (keepIfKey && feature.handles[handle]) {
deleteHandle = false;
}
});
if (deleteHandle) {
delete this.handles[handle];
}
else {
var handleObj = this.handles[handle];
if (handleObj &&handleObj.keys) {
// Can keys object be empty? I don't think so
delete handleObj.keys[key];
}
}
}
});
// register the constructor
djeo.featureTypes.FeatureContainer = fc;
djeo.featureTypes.FeatureCollection = fc;
return fc;
});