1 | /* global ol */ |
---|
2 | |
---|
3 | var GeometryTypeControl = function(opt_options) { |
---|
4 | 'use strict'; |
---|
5 | // Map control to switch type when geometry type is unknown |
---|
6 | var options = opt_options || {}; |
---|
7 | |
---|
8 | var element = document.createElement('div'); |
---|
9 | element.className = 'switch-type type-' + options.type + ' ol-control ol-unselectable'; |
---|
10 | if (options.active) { |
---|
11 | element.className += " type-active"; |
---|
12 | } |
---|
13 | |
---|
14 | var self = this; |
---|
15 | var switchType = function(e) { |
---|
16 | e.preventDefault(); |
---|
17 | if (options.widget.currentGeometryType !== self) { |
---|
18 | options.widget.map.removeInteraction(options.widget.interactions.draw); |
---|
19 | options.widget.interactions.draw = new ol.interaction.Draw({ |
---|
20 | features: options.widget.featureCollection, |
---|
21 | type: options.type |
---|
22 | }); |
---|
23 | options.widget.map.addInteraction(options.widget.interactions.draw); |
---|
24 | var className = options.widget.currentGeometryType.element.className.replace(/ type-active/g, ''); |
---|
25 | options.widget.currentGeometryType.element.className = className; |
---|
26 | options.widget.currentGeometryType = self; |
---|
27 | element.className += " type-active"; |
---|
28 | } |
---|
29 | }; |
---|
30 | |
---|
31 | element.addEventListener('click', switchType, false); |
---|
32 | element.addEventListener('touchstart', switchType, false); |
---|
33 | |
---|
34 | ol.control.Control.call(this, { |
---|
35 | element: element |
---|
36 | }); |
---|
37 | }; |
---|
38 | ol.inherits(GeometryTypeControl, ol.control.Control); |
---|
39 | |
---|
40 | // TODO: allow deleting individual features (#8972) |
---|
41 | (function() { |
---|
42 | 'use strict'; |
---|
43 | var jsonFormat = new ol.format.GeoJSON(); |
---|
44 | |
---|
45 | function MapWidget(options) { |
---|
46 | this.map = null; |
---|
47 | this.interactions = {draw: null, modify: null}; |
---|
48 | this.typeChoices = false; |
---|
49 | this.ready = false; |
---|
50 | |
---|
51 | // Default options |
---|
52 | this.options = { |
---|
53 | default_lat: 0, |
---|
54 | default_lon: 0, |
---|
55 | default_zoom: 12, |
---|
56 | is_collection: options.geom_name.indexOf('Multi') > -1 || options.geom_name.indexOf('Collection') > -1 |
---|
57 | }; |
---|
58 | |
---|
59 | // Altering using user-provided options |
---|
60 | for (var property in options) { |
---|
61 | if (options.hasOwnProperty(property)) { |
---|
62 | this.options[property] = options[property]; |
---|
63 | } |
---|
64 | } |
---|
65 | if (!options.base_layer) { |
---|
66 | this.options.base_layer = new ol.layer.Tile({source: new ol.source.OSM()}); |
---|
67 | } |
---|
68 | |
---|
69 | this.map = this.createMap(); |
---|
70 | this.featureCollection = new ol.Collection(); |
---|
71 | this.featureOverlay = new ol.layer.Vector({ |
---|
72 | map: this.map, |
---|
73 | source: new ol.source.Vector({ |
---|
74 | features: this.featureCollection, |
---|
75 | useSpatialIndex: false // improve performance |
---|
76 | }), |
---|
77 | updateWhileAnimating: true, // optional, for instant visual feedback |
---|
78 | updateWhileInteracting: true // optional, for instant visual feedback |
---|
79 | }); |
---|
80 | |
---|
81 | // Populate and set handlers for the feature container |
---|
82 | var self = this; |
---|
83 | this.featureCollection.on('add', function(event) { |
---|
84 | var feature = event.element; |
---|
85 | feature.on('change', function() { |
---|
86 | self.serializeFeatures(); |
---|
87 | }); |
---|
88 | if (self.ready) { |
---|
89 | self.serializeFeatures(); |
---|
90 | if (!self.options.is_collection) { |
---|
91 | self.disableDrawing(); // Only allow one feature at a time |
---|
92 | } |
---|
93 | } |
---|
94 | }); |
---|
95 | |
---|
96 | var initial_value = document.getElementById(this.options.id).value; |
---|
97 | if (initial_value) { |
---|
98 | var features = jsonFormat.readFeatures('{"type": "Feature", "geometry": ' + initial_value + '}'); |
---|
99 | var extent = ol.extent.createEmpty(); |
---|
100 | features.forEach(function(feature) { |
---|
101 | this.featureOverlay.getSource().addFeature(feature); |
---|
102 | ol.extent.extend(extent, feature.getGeometry().getExtent()); |
---|
103 | }, this); |
---|
104 | // Center/zoom the map |
---|
105 | this.map.getView().fit(extent, this.map.getSize(), {maxZoom: this.options.default_zoom}); |
---|
106 | } else { |
---|
107 | this.map.getView().setCenter(this.defaultCenter()); |
---|
108 | } |
---|
109 | this.createInteractions(); |
---|
110 | if (initial_value && !this.options.is_collection) { |
---|
111 | this.disableDrawing(); |
---|
112 | } |
---|
113 | this.ready = true; |
---|
114 | } |
---|
115 | |
---|
116 | MapWidget.prototype.createMap = function() { |
---|
117 | var map = new ol.Map({ |
---|
118 | target: this.options.map_id, |
---|
119 | layers: [this.options.base_layer], |
---|
120 | view: new ol.View({ |
---|
121 | zoom: this.options.default_zoom |
---|
122 | }) |
---|
123 | }); |
---|
124 | return map; |
---|
125 | }; |
---|
126 | |
---|
127 | MapWidget.prototype.createInteractions = function() { |
---|
128 | // Initialize the modify interaction |
---|
129 | this.interactions.modify = new ol.interaction.Modify({ |
---|
130 | features: this.featureCollection, |
---|
131 | deleteCondition: function(event) { |
---|
132 | return ol.events.condition.shiftKeyOnly(event) && |
---|
133 | ol.events.condition.singleClick(event); |
---|
134 | } |
---|
135 | }); |
---|
136 | |
---|
137 | // Initialize the draw interaction |
---|
138 | var geomType = this.options.geom_name; |
---|
139 | if (geomType === "Unknown" || geomType === "GeometryCollection") { |
---|
140 | // Default to Point, but create icons to switch type |
---|
141 | geomType = "Point"; |
---|
142 | this.currentGeometryType = new GeometryTypeControl({widget: this, type: "Point", active: true}); |
---|
143 | this.map.addControl(this.currentGeometryType); |
---|
144 | this.map.addControl(new GeometryTypeControl({widget: this, type: "LineString", active: false})); |
---|
145 | this.map.addControl(new GeometryTypeControl({widget: this, type: "Polygon", active: false})); |
---|
146 | this.typeChoices = true; |
---|
147 | } |
---|
148 | this.interactions.draw = new ol.interaction.Draw({ |
---|
149 | features: this.featureCollection, |
---|
150 | type: geomType |
---|
151 | }); |
---|
152 | |
---|
153 | this.map.addInteraction(this.interactions.draw); |
---|
154 | this.map.addInteraction(this.interactions.modify); |
---|
155 | }; |
---|
156 | |
---|
157 | MapWidget.prototype.defaultCenter = function() { |
---|
158 | var center = [this.options.default_lon, this.options.default_lat]; |
---|
159 | if (this.options.map_srid) { |
---|
160 | return ol.proj.transform(center, 'EPSG:4326', this.map.getView().getProjection()); |
---|
161 | } |
---|
162 | return center; |
---|
163 | }; |
---|
164 | |
---|
165 | MapWidget.prototype.enableDrawing = function() { |
---|
166 | this.interactions.draw.setActive(true); |
---|
167 | if (this.typeChoices) { |
---|
168 | // Show geometry type icons |
---|
169 | var divs = document.getElementsByClassName("switch-type"); |
---|
170 | for (var i = 0; i !== divs.length; i++) { |
---|
171 | divs[i].style.visibility = "visible"; |
---|
172 | } |
---|
173 | } |
---|
174 | }; |
---|
175 | |
---|
176 | MapWidget.prototype.disableDrawing = function() { |
---|
177 | if (this.interactions.draw) { |
---|
178 | this.interactions.draw.setActive(false); |
---|
179 | if (this.typeChoices) { |
---|
180 | // Hide geometry type icons |
---|
181 | var divs = document.getElementsByClassName("switch-type"); |
---|
182 | for (var i = 0; i !== divs.length; i++) { |
---|
183 | divs[i].style.visibility = "hidden"; |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | }; |
---|
188 | |
---|
189 | MapWidget.prototype.clearFeatures = function() { |
---|
190 | this.featureCollection.clear(); |
---|
191 | // Empty textarea widget |
---|
192 | document.getElementById(this.options.id).value = ''; |
---|
193 | this.enableDrawing(); |
---|
194 | }; |
---|
195 | |
---|
196 | MapWidget.prototype.serializeFeatures = function() { |
---|
197 | // Three use cases: GeometryCollection, multigeometries, and single geometry |
---|
198 | var geometry = null; |
---|
199 | var features = this.featureOverlay.getSource().getFeatures(); |
---|
200 | if (this.options.is_collection) { |
---|
201 | if (this.options.geom_name === "GeometryCollection") { |
---|
202 | var geometries = []; |
---|
203 | for (var i = 0; i < features.length; i++) { |
---|
204 | geometries.push(features[i].getGeometry()); |
---|
205 | } |
---|
206 | geometry = new ol.geom.GeometryCollection(geometries); |
---|
207 | } else { |
---|
208 | geometry = features[0].getGeometry().clone(); |
---|
209 | for (var j = 1; j < features.length; j++) { |
---|
210 | switch(geometry.getType()) { |
---|
211 | case "MultiPoint": |
---|
212 | geometry.appendPoint(features[j].getGeometry().getPoint(0)); |
---|
213 | break; |
---|
214 | case "MultiLineString": |
---|
215 | geometry.appendLineString(features[j].getGeometry().getLineString(0)); |
---|
216 | break; |
---|
217 | case "MultiPolygon": |
---|
218 | geometry.appendPolygon(features[j].getGeometry().getPolygon(0)); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | } else { |
---|
223 | if (features[0]) { |
---|
224 | geometry = features[0].getGeometry(); |
---|
225 | } |
---|
226 | } |
---|
227 | document.getElementById(this.options.id).value = jsonFormat.writeGeometry(geometry); |
---|
228 | }; |
---|
229 | |
---|
230 | window.MapWidget = MapWidget; |
---|
231 | })(); |
---|