-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtoolset.js
More file actions
170 lines (156 loc) · 7.02 KB
/
toolset.js
File metadata and controls
170 lines (156 loc) · 7.02 KB
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
(function ($) {
"use strict";
window.Mapbender.Digitizer = Mapbender.Digitizer || {};
Mapbender.Digitizer.Toolset = function(owner) {
this.owner = owner;
};
Mapbender.Digitizer.Toolset.prototype = {
iconMap_: {
drawCircle: "icon-draw-circle",
drawDonut: "icon-draw-donut",
drawEllipse: "icon-draw-ellipse",
drawLine: "icon-draw-line",
drawPoint: "icon-draw-point",
drawPolygon: "icon-draw-polygon",
drawRectangle: "icon-draw-rectangle",
modifyFeature: "icon-modify-feature",
moveFeature: "icon-move-feature"
},
/**
* @param {Object} schema
* @return {Array<String>}
* @static
*/
getValidToolNames: function(schema) {
switch (schema.featureType.geomType) {
case 'point':
case 'multipoint':
return ['drawPoint', 'moveFeature'];
case 'line':
case 'multiline':
return ['drawLine', 'modifyFeature', 'moveFeature'];
case 'polygon':
case 'multipolygon':
return ['drawPolygon', 'drawRectangle', 'drawCircle', 'drawEllipse', 'drawDonut', 'modifyFeature', 'moveFeature'];
default:
// everything
return [
'drawPoint',
'drawLine',
'drawPolygon', 'drawRectangle', 'drawCircle', 'drawEllipse', 'drawDonut',
'modifyFeature', 'moveFeature'
];
}
},
pause: function() {
// @todo: reintegrate with FeatureEditor :)
$('button.-fn-toggle-tool', this.owner.element).prop('disabled', true);
},
resume: function() {
// @todo: reintegrate with FeatureEditor :)
$('button.-fn-toggle-tool', this.owner.element).prop('disabled', false);
},
getDefaultGeometryToolNames: function(schema) {
return this.getValidToolNames(schema);
},
normalizeToolSet_: function(schema) {
var seen = {};
var toolSpecs = [];
var subSchemas = this.owner.expandCombination(schema);
var addMove = false;
var self = this;
for (var s = 0; s < subSchemas.length; ++s) {
var validNames = this.getValidToolNames(subSchemas[s]);
var subSchemaTools = (subSchemas[s].toolset || this.getDefaultGeometryToolNames(subSchemas[s])).map(function(tc) {
var obj = (typeof tc === 'string') && {type: tc} || Object.assign({}, tc);
obj.schema = obj.schema || subSchemas[s].schemaName;
return obj;
}).filter(function(toolSpec) {
return (!seen[toolSpec.type])
&& self.checkToolAccess_(subSchemas[s], toolSpec.type)
&& -1 !== validNames.indexOf(toolSpec.type)
;
});
for (var t = 0; t < subSchemaTools.length; ++t) {
var toolSpec = subSchemaTools[t];
if (!seen[toolSpec.type]) {
toolSpecs.push(toolSpec);
seen[toolSpec.type] = true;
}
}
}
return toolSpecs;
},
renderGeometryToolButtons: function(schema) {
var toolSpecs = this.normalizeToolSet_(schema);
var buttons = [];
for (var i = 0; i < toolSpecs.length; ++i) {
var toolName = toolSpecs[i].type;
var iconClass = this.iconMap_[toolName];
var $icon = $(document.createElement('span')).addClass(iconClass);
var tooltip = Mapbender.trans(toolSpecs[i].label || ('mb.digitizer.toolset.' + toolName));
var btnSchema = this.owner.options.schemes[toolSpecs[i].schema] || schema;
var $button = $(document.createElement('button'))
.attr({
type: 'button',
'data-toolname': toolName,
title: tooltip
})
.addClass('-fn-toggle-tool btn btn-outline-secondary')
.append($icon)
.data({
schema: btnSchema
});
buttons.push($button);
}
return buttons;
},
checkToolAccess_: function(schema, toolName) {
var isModify = -1 !== ['drawDonut', 'moveFeature', 'modifyFeature'].indexOf(toolName);
if (isModify) {
return schema.allowEdit && schema.allowDigitize;
} else {
return schema.allowCreate;
}
},
registerEvents: function() {
var widget = this.owner;
widget.element.on('click', '.-fn-visibility-all', function() {
var state = !!$(this).attr('data-visibility');
widget.renderer.forAllSchemaFeatures(widget._getCurrentSchema(), function (feature) {
if (widget.getItemSchema(feature).allowChangeVisibility) {
feature.set('hidden', !state);
}
});
});
// Add an event listener for exporting the currently selected features
widget.element.on('click', '.-fn-export-selected', function() {
// Get the selected features
var exportFeatures = widget.tableRenderer.selectedFeatures || [];
// Check if there are any features to export
if (exportFeatures.length === 0) {
$.notify(Mapbender.trans('mb.digitizer.feature.export.notFound'));
return;
}
// Create a new array of features that include geometry and properties
var strippedFeatures = exportFeatures.map(function(originalFeature) {
var geometryClone = originalFeature.getGeometry().clone();
var properties = originalFeature.get("data");
delete properties.geometry; // Remove the geometry property to avoid duplication
return new ol.Feature({
geometry: geometryClone,
...properties // Add the properties to the new feature
});
});
var format = new ol.format.GeoJSON();
var dataProjection = 'EPSG:4326';
// Write features as GeoJSON, specifying only geometry
var geojson = format.writeFeaturesObject(strippedFeatures, {
dataProjection: dataProjection,
featureProjection: widget.mbMap.getModel().olMap.getView().getProjection()
});
widget.createExportData(geojson);
});
}
};
})(jQuery);