-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEngine.js
388 lines (348 loc) · 10.9 KB
/
Engine.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
define([
"dojo/_base/declare", // declare
"dojo/has", // has
"dojo/_base/lang", // mixin, hitch, isArray, isString, isObject
"dojo/on",
"dojo/_base/array", // forEach
"dojo/Evented", //emit
"dojo/Deferred",
"./_base"
], function(declare, has, lang, on, array, Evented, Deferred, djeo){
var defaultCenter = [0,0],
defaultZoom = 3
;
return declare([Evented], {
// summary:
// The base class for engines. An engine class is supposed to be a singleton
// type: String
// Type of the engine. Example values: "djeo", "gmaps", "ge"
type: '',
// factories: Object
// A registry of factories. The term "factory" should be understood as
// a realization of actual functionalities of basic djeo classes
factories: null,
// initialized: Boolean
// Specifies if the engine is initialized
initialized: false,
// canRenderModels: Boolean
// Specifies if the engine can render 3D models
canRenderModels: false,
// _require: Function
// Reference to the context-sensitive require of the actual Engine instance
_require: null,
// ignoredDependencies: Object
// Ignored dependencies (e.g default implementation is used)
ignoredDependencies: null,
// _layerCtrs: Object
// Registry of layer constructors. Used to store costructors resolved from layer modules via require call
// in the _onEngineReady functions of djeo/Map during djeo/Map initialization.
// We need to resolve modules there to simplify things
// (e.g. so set a proper map projection before rendering of features)
_layerCtrs: null,
constructor: function(kwArgs){
lang.mixin(this, kwArgs);
this.factories = {};
this.ignoredDependencies = {};
// layers stuff
this.layers = [];
this._layerCtrs = {};
this._layerReg = {};
},
initialize: function(/* Function */readyFunction) {
// summary:
// Initializes the engine and calls the supplied function.
// Should be implemented in the inherited class
},
_initBasicFactories: function(placemarkFactory) {
var f = this.factories;
f.Placemark = placemarkFactory;
f.Point = placemarkFactory;
f.LineString = placemarkFactory;
f.Polygon = placemarkFactory;
f.MultiLineString = placemarkFactory;
f.MultiPolygon = placemarkFactory;
},
matchModuleId: function(dependency) {
if (this.ignoredDependencies[dependency]) return null;
// calculate moduleId
var moduleId = this._require.toAbsMid("./");
// treat the case of a build when "./" is resolved to "pack/main"
if (moduleId.length>5 && moduleId.substr(moduleId.length-5, 5)=="/main") {
moduleId = moduleId.substring(0, moduleId.length-4);
}
moduleId += dependency;
return moduleId;
},
createContainer: function(parentContainer, featureType) {
},
appendChild: function(/* Object */parent, /* Object */child) {
// summary:
// Appends a child to the parent. Should be implemented in the inherited class
},
prepare: function() {
// summary:
// Normally called by this.render() method.
// Should include some preparatory code for rendering.
// Should be implemented in the inherited class
},
_initCamera: function() {
var map = this.map,
extent = map.extent,
zoom,
// do we need to project center to the map's projection?
projectCenter = true,
deferred
;
// the following attribute checked in the onzoom_changed function
// if this._renderingDisabled == true, then no rendering due to style change will occur in the onzoom_changed
this._renderingDisabled = true;
if (extent) {
// converting extent to the map's projection
var lb = this.map.getCoords([extent[0], extent[1]]),
rt = this.map.getCoords([extent[2], extent[3]])
;
deferred = this.zoomTo([lb[0], lb[1], rt[0], rt[1]]);
}
else {
// finding map center
var center = map.center || (map.lookAt && map.lookAt.coords),
bbox
;
if (!center || !("zoom" in map)) {
bbox = map.getBbox();
}
if ("zoom" in map) {
zoom = map.zoom;
if (!center) {
if (bbox) {
center = [(bbox[2] + bbox[0])/2, (bbox[3] + bbox[1])/2];
// bbox is already in the map's projection
projectCenter = false;
}
else {
center = [defaultCenter[0], defaultCenter[1]];
}
}
}
else {
if (center || !bbox) {
zoom = defaultZoom;
if (!bbox) {
center = [defaultCenter[0], defaultCenter[1]];
}
}
// If center is not set bbox has been calculated,
// the center of the bounding box will be used as a map center
}
if (center && projectCenter) {
center = map.getCoords(center);
}
// now actually set the camera
if (center) {
deferred = this._setCamera({center: center, zoom: zoom});
}
else {
deferred = this.zoomTo(bbox);
}
}
this._renderingDisabled = false;
return deferred;
},
getFactory: function(/* String */dependency) {
// summary:
// Looks up a factory for the given type.
// If the factory is not found, tries to load the factory class and instantiate it
if (!dependency || this.ignoredDependencies[dependency]) return null;
var factories = this.factories;
if (!factories[dependency]) {
var moduleId = this.matchModuleId(dependency);
if (moduleId) {
var factoryClass = require(moduleId);
factories[dependency] = new factoryClass({engine: this});
}
}
return factories[dependency];
},
getTopContainer: function() {
// summary:
// Returns the top container
// Should be implemented in the inherited class
// returns:
// The top level container
},
connect: function(feature, event, context, method) {
// summary:
// Adds a listener for the event for the specified feature
// Should be implemented in the inherited class
// returns: Array
// An array that holds connection parameters that are specific for the engine
},
normalizeCallback: function(feature, event, method, context) {
// summary:
// Normalizes callback function for events
// A particular map engine may provide different implementation of the function
return function(nativeEvent){
method.call(context, {
type: event,
nativeEvent: nativeEvent,
feature: feature
});
};
},
render: function(/* String? */theme, /* Boolean? */destroy) {
// summary:
// Default implementation of the render method of djeo/Map
// theme:
// See description in the render method of djeo/Map
// destroy:
// See description in the render method of djeo/Map
var map = this.map;
map._calculateViewport();
this.prepare();
// wait till zooming is ready
var cameraDeferred = this._initCamera(),
resultDeferred
;
if (cameraDeferred) {
resultDeferred = new Deferred();
cameraDeferred.then(function(){
var renderDeferred = map.document.render(theme, destroy);
if (renderDeferred) {
renderDeferred.then(function(){
resultDeferred.resolve();
});
}
else {
resultDeferred.resolve();
}
})
}
else {
resultDeferred = map.document.render(theme, destroy);
}
return resultDeferred;
},
renderFeatures: function(/* Array|Object */features, /* String? */theme, /* Boolean? */destroy) {
// summary:
// Default implementation of the renderFeatures method of djeo/Map
if (lang.isString(features)) features = [features];
if (lang.isArray(features)) {
array.forEach(features, function(feature){
if (lang.isString(feature)) feature = this.getFeatureById(feature);
if (feature) feature._render(theme, destroy);
}, this);
}
else {
for(var fid in features) {
// TODO: avoid double rendering
features[fid]._render(theme, destroy);
}
}
},
renderContainer: function(container, theme, destroy) {
if (destroy) {
// reset numVisibleFeatures
container.numVisibleFeatures = 0;
}
this._renderContainer(container, theme, destroy);
},
_renderContainer: function(container, theme, destroy) {
if (!container.visible) return;
if (container.features.length == 0 && destroy) {
container.parent.numVisibleFeatures++;
}
array.forEach(container.features, function(feature){
if (feature.isContainer || feature.visible) {
feature._render(theme, destroy);
}
});
},
enableLayer: function(/* String|Object */layer, enabled) {
if (enabled === undefined) enabled = true;
if (enabled) {
if (lang.isString(layer)) {
// we've got a layer id
var colonIndex = layer.indexOf(":"),
classId = (colonIndex > 0) ? layer.substring(0, colonIndex) : layer
;
classId = classId.toLowerCase();
layer = (colonIndex > 0) ? classId + ":" + layer.substring(colonIndex+1) : classId;
// check if the layer already has been enabled
if (this._layerReg[layer]) return;
// check if know the layer class id
if (!this._supportedLayers[classId]) return;
var layerDef = this._supportedLayers[classId];
var kwArgs = layerDef[1];
if (colonIndex > 0) {
kwArgs.paramStr = layer.substring(colonIndex+1);
}
if (this._layerCtrs[classId]) {
// layer constructor is already available
// proceed directly to layer initialization
this._createLayer(layer, this._layerCtrs[classId], kwArgs);
}
else {
// load layer module and its factory
this._require(["djeo/"+layerDef[0], "./"+layerDef[0]], lang.hitch(this, function(layerCtor){
this._layerCtrs[classId] = layerCtor;
this._createLayer(layer, layerCtor, kwArgs);
}));
}
}
else {
// we've got a layer instance
// check if the layer already has been enabled
for (var i=0; i<this.layers.length; i++) {
if (this.layers[i] === layer) return;
}
layer.startup(this.map);
this._checkLayerProjection(layer);
this.layers.push(layer);
}
}
},
_createLayer: function(/* String */layerId, /* Function */layerCtor, kwArgs) {
if (!kwArgs) {
kwArgs = {};
}
var layer = new layerCtor(kwArgs);
layer.startup(this.map);
this._checkLayerProjection(layer);
this.layers.push(layer);
this._layerReg[layerId] = layer;
},
_checkLayerProjection: function(layer) {
// TODO: check if the layer supports bottom layer projection
var layers = this.layers;
if (layers.length == 0) {
// it will be the bottom layer
// so set map's projection to the layer's one
if (layer.projection) {
this.map.projection = layer.projection;
}
}
},
isValidLayerId: function(/* String */layerId) {
var classId = djeo.getLayerClassId(layerId.toLowerCase());
return classId in this._supportedLayers;
},
getLayerModuleId: function(/* String */layerId) {
var classId = djeo.getLayerClassId(layerId.toLowerCase());
return this._supportedLayers[classId][0];
},
setLayerConstructor: function(/* String */layerId, /* Function */ctr) {
var classId = djeo.getLayerClassId(layerId.toLowerCase());
this._layerCtrs[classId] = ctr;
},
onzoom_changed: function() {
if (!this._renderingDisabled && this.map._hasZoomStyle && !this.map._customZoomRendering) {
this.map.document.render();
}
// "zoom_changed" is emited here automatically (see documentation for dojo/Evented)
},
_appendDiv: function(div) {
// we append the div directly to this.map.container
this.map.container.appendChild(div);
}
});
});