This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Editor.js
603 lines (532 loc) · 20.6 KB
/
Editor.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/**
* @copyright 2011 geOps
* @license https://github.com/geops/ole/blob/master/license.txt
* @link https://github.com/geops/ole
*/
/**
* @requires Editor/Control/EditorPanel.js
*/
/**
* Class: OpenLayers.Editor
* The OpenLayers Editor provides basic methods and informations for map editing.
* Highlevel functions are implemented in different controls and can be
* activated by the editor constructor.
*
* @constructor
* @param {OpenLayers.Map} map
* @param {Object=} options
*/
OpenLayers.Editor = OpenLayers.Class({
/**
* Property: map
* {<OpenLayers.Map>} this gets set in the constructor.
*/
map: null,
/**
* Property: id
* {String} Unique identifier for the Editor.
*/
id: null,
/**
* Property: editLayer
* {<OpenLayers.Layer.Vector>} Editor workspace.
*/
editLayer: null,
/**
* Property: editorPanel
* {<OpenLayers.Editor.Control.EditorPanel>} Contains icons for active controls
* and gets set by startEditMode() and unset by stopEditMode().
*/
editorPanel: null,
/**
* Property: editMode
* {Boolean} The editor is active.
*/
editMode: false,
/**
* Property: dialog
* {<OpenLayers.Editor.Control.Dialog>} ...
*/
dialog: null,
/**
* Property: status
* @type {function(string, string)} Function to display states, receives status type and message
*/
showStatus: function (status, message) {
if (status === 'error') {
alert(message);
}
},
/**
* Property: activeControls
* {Array} ...
*/
activeControls: [],
/**
* Property: editorControls
* {Array} Contains names of all available editor controls. In particular
* this information is needed by this EditorPanel.
*/
editorControls: ['CleanFeature', 'DeleteFeature', 'DeleteAllFeatures', 'Dialog', 'DrawHole', 'DrawRegular',
'DrawPolygon', 'DrawPath', 'DrawPoint', 'DrawText', 'EditorPanel', 'ImportFeature',
'MergeFeature', 'SnappingSettings', 'SplitFeature', 'CADTools',
'TransformFeature', 'ContextMenu'],
/**
* Geometry types available for editing
* {Array}
*/
featureTypes: ['text', 'point', 'path', 'polygon', 'regular'],
/**
* Property: sourceLayers
* {Array} ...
*/
sourceLayers: [],
/**
* Property: parameters
* {Object} ...
*/
params: {},
geoJSON: new OpenLayers.Format.GeoJSON(),
/**
* Property: options
* {Object} ...
*/
options: {},
/**
* Property: URL of processing service.
* {String}
*/
oleUrl: '',
/**
* Instantiated controls
* {Objects}
*/
controls: {},
/**
* Property: undoRedoActive
* {Boolean} Indicates if the UndoRedo control is active. Only read on
* initialization right now. Default is true.
*/
undoRedoActive: true,
/**
* @param {OpenLayers.Map} map A map that shall be equipped with an editor; can be left undefined in which case a map is created.
* @param {Object} options
*/
initialize: function (map, options) {
OpenLayers.Util.extend(this, options);
if (map instanceof OpenLayers.Map) {
this.map = map;
} else {
this.map = new OpenLayers.Map();
}
if (!options) {
options = {};
}
if (!options.dialog) {
this.dialog = new OpenLayers.Editor.Control.Dialog();
this.map.addControl(this.dialog);
}
this.id = OpenLayers.Util.createUniqueID('OpenLayers.Editor_');
if (options.editLayer) {
this.editLayer = options.editLayer
} else {
this.editLayer = new OpenLayers.Layer.Vector('Editor', {
displayInLayerSwitcher: false
});
}
if (options.styleMap) {
this.editLayer.styleMap = options.styleMap;
} else {
this.editLayer.styleMap = new OpenLayers.StyleMap({
'default': new OpenLayers.Style({
fillColor: '#07f',
fillOpacity: 0.8,
strokeColor: '#037',
strokeWidth: 2,
graphicZIndex: 1,
pointRadius: 5
}),
// defaultLabel and selectLabel Styles are needed for DrawText Control
'defaultLabel': new OpenLayers.Style({
fillColor: '#07f',
fillOpacity: 0.8,
strokeColor: '#037',
strokeWidth: 2,
graphicZIndex: 11,
pointRadius: 0,
cursor: 'default',
label: '${label}',
fontColor: '#000000',
fontSize: "11px",
fontFamily: "Verdana, Arial, Helvetica, sans-serif",
fontWeight: "bold",
// labelAlign: "cm",
// labelXOffset: 0,
// labelYOffset: 0,
labelOutlineColor: '#FFFFFF',
labelOutlineWidth: 4,
labelSelect: true
}),
'select': new OpenLayers.Style({
fillColor: '#fc0',
strokeColor: '#f70',
graphicZIndex: 2
}),
// defaultLabel and selectLabel Styles are needed for DrawText Control
'selectLabel': new OpenLayers.Style({
pointRadius: 5,
label: '${label}',
fontColor: 'black',
fontSize: "11px",
fontFamily: "Verdana, Arial, Helvetica, sans-serif",
fontWeight: "bold",
labelAlign: "cm",
labelXOffset: "${xOffset}",
labelYOffset: "${yOffset}",
fillColor: '#fc0',
strokeColor: '#f70',
labelOutlineColor: '#fc0',
labelOutlineWidth: 6,
graphicZIndex: 2
}),
'temporary': new OpenLayers.Style({
fillColor: '#fc0',
fillOpacity: 0.8,
strokeColor: '#f70',
strokeWidth: 2,
graphicZIndex: 2,
pointRadius: 5
})
});
}
var selectionContext = {
editor: this,
layer: this.editLayer,
controls: [
'OpenLayers.Editor.Control.DeleteFeature',
'OpenLayers.Editor.Control.CleanFeature',
'OpenLayers.Editor.Control.MergeFeature',
'OpenLayers.Editor.Control.SplitFeature'
]};
this.editLayer.events.register('featureselected', selectionContext, this.selectionChanged);
this.editLayer.events.register('featureunselected', selectionContext, this.selectionChanged);
for (var i = 0, il = this.featureTypes.length; i < il; i++) {
if (this.featureTypes[i] == 'polygon') {
this.activeControls.push('DrawPolygon');
}
else if (this.featureTypes[i] == 'path') {
this.activeControls.push('DrawPath');
}
else if (this.featureTypes[i] == 'point') {
this.activeControls.push('DrawPoint');
}
else if (this.featureTypes[i] == 'regular') {
this.activeControls.push('DrawRegular');
}
else if (this.featureTypes[i] == 'text') {
this.activeControls.push('DrawText');
}
}
for (var i = 0, il = this.sourceLayers.length; i < il; i++) {
var selectionContext = {
editor: this,
layer: this.sourceLayers[i],
controls: ['OpenLayers.Editor.Control.ImportFeature']
};
this.sourceLayers[i].events.register('featureselected', selectionContext, this.selectionChanged);
this.sourceLayers[i].events.register('featureunselected', selectionContext, this.selectionChanged);
this.sourceLayers[i].styleMap = new OpenLayers.StyleMap({
'default': new OpenLayers.Style({
fillColor: '#0c0',
fillOpacity: 0.8,
strokeColor: '#070',
strokeWidth: 2,
graphicZIndex: 1,
pointRadius: 5
}),
'select': new OpenLayers.Style({
fillColor: '#fc0',
strokeColor: '#f70',
graphicZIndex: 2
}),
'temporary': new OpenLayers.Style({
fillColor: '#fc0',
fillOpacity: 0.8,
strokeColor: '#f70',
strokeWidth: 2,
graphicZIndex: 2,
pointRadius: 5
})
});
this.map.addLayer(this.sourceLayers[i]);
}
this.map.editor = this;
this.map.addLayer(this.editLayer);
this.map.addControl(new OpenLayers.Editor.Control.LayerSettings(this));
if (this.undoRedoActive) {
this.map.addControl(new OpenLayers.Editor.Control.UndoRedo(this.editLayer));
}
this.addEditorControls();
return this;
},
/**
* Enable or disable controls that depend on selected features.
*
* Requires an active SelectFeature control and the following context variables:
* - editor: this
* - layer: The layer with selected features.
* - controls: An array of class names.
*/
selectionChanged: function () {
var selectFeature = this.editor.editorPanel.getControlsByClass('OpenLayers.Control.SelectFeature')[0];
if (this.layer.selectedFeatures.length > 0 && selectFeature && selectFeature.active) {
// enable controls
for (var ic = 0, lic = this.controls.length; ic < lic; ic++) {
var control = this.editor.editorPanel.getControlsByClass(this.controls[ic])[0];
if (control) {
OpenLayers.Element.removeClass(control.panel_div, 'oleControlDisabled');
}
}
} else {
// disable controls
for (var ic = 0, lic = this.controls.length; ic < lic; ic++) {
var control = this.editor.editorPanel.getControlsByClass(this.controls[ic])[0];
if (control) {
OpenLayers.Element.addClass(control.panel_div, 'oleControlDisabled');
}
}
}
this.editor.editorPanel.redraw();
},
/**
* Makes the toolbar appear and allows editing
*/
startEditMode: function () {
this.editMode = true;
this.editorPanel.activate();
},
/**
* Hides the toolbar and prevents editing
*/
stopEditMode: function () {
this.editMode = false;
this.editorPanel.deactivate();
},
/**
* Initializes configured controls and shows them
*/
addEditorControls: function () {
var control = null, controls = [];
var editor = this;
for (var i = 0, len = editor.activeControls.length; i < len; i++) {
control = editor.activeControls[i];
if (OpenLayers.Util.indexOf(editor.editorControls, control) > -1) {
controls.push(new OpenLayers.Editor.Control[control](
editor.editLayer, editor.options[control]
));
}
switch (control) {
case 'Separator':
controls.push(new OpenLayers.Control.Button({
displayClass: 'olControlSeparator'
}));
break;
case 'Navigation':
controls.push(new OpenLayers.Control.Navigation(
OpenLayers.Util.extend(
{title: OpenLayers.i18n('oleNavigation')},
editor.options.Navigation)
));
break;
case 'DragFeature':
controls.push(new OpenLayers.Editor.Control.DragFeature(editor.editLayer,
OpenLayers.Util.extend({}, editor.options.DragFeature)
));
break;
case 'ModifyFeature':
controls.push(new OpenLayers.Control.ModifyFeature(editor.editLayer,
OpenLayers.Util.extend(
{title: OpenLayers.i18n('oleModifyFeature')},
editor.options.ModifyFeature)
));
break;
case 'SelectFeature':
controls.push(new OpenLayers.Control.SelectFeature(
editor.sourceLayers.concat([editor.editLayer]),
OpenLayers.Util.extend(
{
title: OpenLayers.i18n('oleSelectFeature'),
clickout: true,
toggle: false,
multiple: false,
hover: false,
toggleKey: "ctrlKey",
multipleKey: "ctrlKey",
box: true
},
editor.options.SelectFeature)
));
break;
case 'DownloadFeature':
controls.push(new OpenLayers.Editor.Control.DownloadFeature(editor.editLayer,
OpenLayers.Util.extend({}, this.DownloadFeature)
));
break;
case 'UploadFeature':
controls.push(new OpenLayers.Editor.Control.UploadFeature(editor.editLayer,
OpenLayers.Util.extend({}, this.UploadFeature)
));
break;
default:
// Allow externally-defined controls, developers are responsible for providing an
// OpenLayers.Editor.Control[control] class and corresponding CSS.
// See FeatureStyler-example: lib.heron-mc.org/heron/latest/examples/vectorstyler
// (would otherwise require Heron/GXP/GeoExt).
if (OpenLayers.Util.indexOf(editor.editorControls, control) == -1 && OpenLayers.Editor.Control[control]) {
controls.push(new OpenLayers.Editor.Control[control](editor.editLayer,
OpenLayers.Util.extend({}, editor.options[control])
));
}
break;
}
// Save instance in editor's controls mapping
this.controls[control] = controls[controls.length - 1];
}
// Add toolbar to map
this.editorPanel = this.createEditorPanel(controls);
editor.map.addControl(this.editorPanel);
},
/**
* Adds a control to the editor and its panel
* @param {OpenLayers.Editor.Control} control
*/
addEditorControl: function (control) {
this.controls[control.CLASS_NAME] = control;
this.editorPanel.addControls([control]);
this.map.addControl(control);
},
/**
* Instantiates the container which displays the tools.
* To be called by OLE only and intended to be overridden by subclasses that want to display something else instead of the default toolbar
* @param {Array.<OpenLayers.Control>} controls Editing controls
* @return {OpenLayers.Editor.Control.EditorPanel} Widget to display editing tools
*/
createEditorPanel: function (controls) {
// remove controls from context menu
if (this.controls['ContextMenu']) {
var ctrls = this.controls['ContextMenu'].contextMenuControls || [];
var i = ctrls.length;
while (i--) {
var pos = controls.indexOf(this.controls[ctrls[i]]);
if (~pos) {
controls.splice(pos, 1);
}
}
controls.splice(controls.indexOf(this.controls['ContextMenu']), 1);
}
var editorPanel = new OpenLayers.Editor.Control.EditorPanel(this);
editorPanel.addControls(controls);
return editorPanel;
},
status: function (options) {
if (options.type == 'error') {
alert(options.content);
}
},
/**
* Destroys existing features and loads the provided one into editor
* @param {Array.<OpenLayers.Feature.Vector>} features
*/
loadFeatures: function (features) {
this.editLayer.destroyFeatures();
if (features) {
this.editLayer.addFeatures(features);
this.map.zoomToExtent(this.editLayer.getDataExtent());
}
},
/**
* Callback to update selected feature with result of server side processing
*/
requestComplete: function (response) {
var responseJSON = new OpenLayers.Format.JSON().read(response.responseText);
this.map.editor.stopWaiting();
if (!responseJSON) {
this.showStatus('error', OpenLayers.i18n('oleNoJSON'))
} else if (responseJSON.error) {
this.showStatus('error', responseJSON.message)
} else {
if (responseJSON.params) {
OpenLayers.Util.extend(this.params, responseJSON.params);
}
if (responseJSON.geo) {
var geo = this.geoJSON.read(responseJSON.geo);
this.editLayer.removeFeatures(this.editLayer.selectedFeatures);
this.editLayer.addFeatures(this.toFeatures(geo));
this.editLayer.events.triggerEvent('featureselected');
}
}
},
/**
* Flattens multipolygons and returns a list of their features
* @param {Object|Array} multiPolygon Geometry or list of geometries to flatten. Geometries can be of types
* OpenLayers.Geometry.MultiPolygon, OpenLayers.Geometry.Collection,
* OpenLayers.Geometry.Polygon.
* @return {Array} List for features of type OpenLayers.Feature.Vector.
*/
toFeatures: function (multiPolygon) {
if (multiPolygon === null || typeof(multiPolygon) !== 'object') {
throw new Error('Parameter does not match expected type.');
}
var features = [];
if (!(multiPolygon instanceof Array)) {
multiPolygon = [multiPolygon];
}
for (var i = 0, li = multiPolygon.length; i < li; i++) {
if (multiPolygon[i].geometry.CLASS_NAME === 'OpenLayers.Geometry.MultiPolygon' ||
multiPolygon[i].geometry.CLASS_NAME === 'OpenLayers.Geometry.Collection') {
for (var j = 0, lj = multiPolygon[i].geometry.components.length; j < lj; j++) {
features.push(new OpenLayers.Feature.Vector(
multiPolygon[i].geometry.components[j]
));
}
} else if (multiPolygon[i].geometry.CLASS_NAME === 'OpenLayers.Geometry.Polygon') {
features.push(new OpenLayers.Feature.Vector(multiPolygon[i].geometry));
}
}
return features;
},
toMultiPolygon: function (features) {
var components = [];
for (var i = 0, l = features.length; i < l; i++) {
if (features[i].geometry.CLASS_NAME === 'OpenLayers.Geometry.Polygon') {
components.push(features[i].geometry);
}
}
return new OpenLayers.Geometry.MultiPolygon(components);
},
startWaiting: function (panel_div) {
OpenLayers.Element.addClass(panel_div, 'olEditorWaiting');
OpenLayers.Element.addClass(this.map.div, 'olEditorWaiting');
this.waitingDiv = panel_div;
},
stopWaiting: function () {
OpenLayers.Element.removeClass(this.waitingDiv, 'olEditorWaiting');
OpenLayers.Element.removeClass(this.map.div, 'olEditorWaiting');
},
CLASS_NAME: 'OpenLayers.Editor'
});
/**
* @constructor
*/
OpenLayers.Editor.Control = OpenLayers.Class(OpenLayers.Control, {
initialize: function (options) {
OpenLayers.Control.prototype.initialize.apply(this, [options]);
},
CLASS_NAME: 'OpenLayers.Editor.Control'
});
/**
* Version number of OpenLayers Editor.
* @const
* @type {string}
*/
OpenLayers.Editor.VERSION_NUMBER="1.0-beta4";