-
Notifications
You must be signed in to change notification settings - Fork 6
/
Placemark.js
265 lines (229 loc) · 6.2 KB
/
Placemark.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
define([
"dojo/_base/declare", // declare
"dojo/_base/lang", // isString, isObject
"dojo/_base/array", // forEach
"./_base",
"./Feature",
"./util/_base",
"./util/bbox"
], function(declare, lang, array, djeo, Feature, u, bbox) {
var p = declare([Feature], {
type: "",
isPlacemark: true,
factory: null,
// keep active handles
handles: null,
baseShapes: null,
textShapes: null,
// store dynamical or engine specific information here
reg: null,
constructor: function(/* Object? */featureDef, /* Object? */kwArgs) {
this.handles = {};
this.baseShapes = [];
this.reg = {};
},
getType: function() {
var type;
if (this.coords) type = this.type;
else {
var geometry = this.map.getGeometryById(this.geometryId || this.id);
if (geometry) type = geometry.type;
}
return type;
},
getCoordsType: function() {
return this.getType();
},
isPoint: function() {
return (this.getCoordsType() == "Point");
},
isArea: function() {
var type = this.getCoordsType();
return (type == "Polygon" || type == "MultiPolygon");
},
isLine: function() {
var type = this.getCoordsType();
return (type == "LineString" || type == "MultiLineString");
},
getCoords: function() {
return this._getCoords();
},
_getCoords: function() {
var coords = this.coords;
if (!coords) {
var geometry = this.map.getGeometryById(this.geometryId || this.id);
if (geometry) coords = geometry.coords;
}
return coords;
},
_set_coords: function(coords) {
// convert coordinates to the map projection if it is relevant here
var _coords = this.map.getCoords(coords);
this.map.engine.factories.Placemark.setCoords(_coords, this);
delete this._bb;
this.coords = coords;
this._coords = _coords;
},
_get_coords: function() {
return this.getCoords();
},
render: function(theme, destroy) {
theme = this._checkState(theme);
this.map.engine.factories.Placemark.render(this, theme, destroy);
},
_render: function(theme, destroy) {
theme = this._checkState(theme);
if (destroy) {
this.parent._show(this, true, true);
}
this.map.engine.factories.Placemark._render(this, theme, destroy);
},
_checkState: function(theme) {
if (!theme && this.state) {
var states = this.map.states;
theme = this.state in states ? states[this.state] : this.state;
}
return theme;
},
remove: function() {
var map = this.map;
// remove from the registry of features
delete map.features[this.id];
// remove feature from the feature container
var siblings = this.parent.features;
for(var i=0,numSiblings=siblings.length; i<numSiblings; i++) {
if (siblings[i]==this) {
siblings.splice(i,1);
break;
}
}
map.engine.factories.Placemark.remove(this);
// disconnect all events
for(var handle in this.handles) {
this.disconnect(handle);
delete this.handles[handle];
}
// remove from style dependence
if (this.style) {
array.forEach(this.style, function(style){
delete style._features[this.id];
}, this);
delete this.style;
}
// remove from map.featuresByClass
if (this.styleClass) {
var featuresByClass = map.featuresByClass
array.forEach(this.styleClass, function(styleClass){
var features = featuresByClass[styleClass];
for(var i=0,numFeatures=features.length; i<numFeatures; i++) {
if (features[i]==this) {
features.splice(i,1);
break;
}
}
}, this);
}
},
show: function(show) {
if (show === undefined) show = true;
if (this.visible != show) {
// delegate to the parent
this.parent._show(this, show);
}
},
_show: function(show) {
this.map.engine.factories.Placemark.show(this, show);
this.visible = show;
},
getBbox: function() {
// summary:
// Returns the feature bounding box in the current map projection
var bb = this._bbox || this.bbox;
if (!bb) bb = bbox.get(this);
return bb;
},
onForHandle: function(handle, /* Object */kwArgs) {
if (this.invalid) return handle;
var handleObj = this.handles[handle];
if (!handleObj || !kwArgs.key) {
var events = kwArgs.events;
events = lang.isString(events) ? [events] : events;
// disconnect existing events for this handle
if (handleObj) {
var eventConnections = handleObj[3];
array.forEach(eventConnections, function(eventConnection){
if (eventConnection) this.map.engine.disconnect(eventConnection);
}, this);
}
var eventConnections = [];
// the format of handleObj is [events,context,method,eventConnections]
handleObj = [events, kwArgs.context, kwArgs.method, eventConnections];
var numEvents = 0; // number of connected events
array.forEach(events, function(event) {
if (djeo.events[event]) {
eventConnections.push(
this.map.engine.onForFeature(this, event, kwArgs.method, kwArgs.context)
);
numEvents++;
}
else eventConnections.push(null);
}, this);
if (!numEvents) return handle;
handle = handle || u.uid();
if (!this.handles[handle]) this.handles[handle] = handleObj;
}
// attach key-value
if (kwArgs.key) {
if (handleObj.length == 4) {
handleObj[4] = {}
}
handleObj[4][kwArgs.key] = kwArgs.value;
}
return handle;
},
disconnect: function(handle, key, keepIfKey) {
var handleObj = this.handles[handle];
if (!handleObj) return;
var disconnect = true;
if (key) {
if (handleObj.length == 4) return;
delete handleObj[4][key];
if (keepIfKey) {
// check if there are still keys attached to handleObj
for (var k in handleObj[4]) {
disconnect = false;
break;
}
}
}
if (disconnect) {
var eventConnections = handleObj[3];
array.forEach(eventConnections, function(eventConnection){
this.map.engine.disconnect(eventConnection);
}, this);
delete this.handles[handle];
}
},
_set_state: function(state) {
this.state = state;
this.render();
},
_set_orientation: function(o) {
var factory = this.map.engine.factories.Placemark;
var heading = lang.isObject(o) ? o.heading : o;
if (heading !== undefined) {
factory.setOrientation(heading, this);
this.orientation = heading;
}
}
});
// register the constructor
var ft = djeo.featureTypes;
ft.Placemark = p;
ft.Point = p;
ft.LineString = p;
ft.Polygon = p;
ft.MultiLineString = p;
ft.MultiPolygon = p;
return p;
});