@@ -38,11 +38,6 @@ type Metadata = {
3838 controls : any [ ] ;
3939} ;
4040
41- // --- Constants
42- // TODO: Move to constants
43- // const TYPE_IDENTIFIER = "@@type";
44- // const GEOJSON_IDENTIFIER = "@@geojson";
45-
4641const jsonConverter = new JSONConverter ( ) ;
4742
4843// --- Use geographic coordinates (WGS-84) in all methods
@@ -51,14 +46,10 @@ useGeographic();
5146function parseLayerDef ( layerDef : JSONDef ) : Layer {
5247 const layer = jsonConverter . parse ( layerDef ) ;
5348 console . log ( "layerDef" , layerDef ) ;
54- // Use setProperties instead
5549 layer . setProperties ( {
5650 id : layerDef . id ,
5751 type : layerDef [ TYPE_IDENTIFIER ]
5852 } ) ;
59- // layer.set("id", layerDef.id);
60- // layer.set("type", layerDef[TYPE_IDENTIFIER]);
61-
6253 addGeojsonFeatures ( layer , layerDef . source [ GEOJSON_IDENTIFIER ] ) ;
6354 return layer ;
6455}
@@ -83,47 +74,28 @@ export default class MapWidget {
8374 this . _model = model ;
8475
8576 const view = jsonConverter . parse ( mapOptions . view ) as View ;
86- // let baseControls: Control[] = [];
87- // let baseLayers: Layer[] = [];
8877
8978 this . _container = mapElement ;
9079 this . _map = new Map ( {
9180 target : mapElement ,
9281 view : view ,
93- controls : [ ] , // defaultControls,
94- layers : [ ] // baseLayers,
82+ controls : [ ] ,
83+ layers : [ ]
9584 } ) ;
9685
97- // events
86+ // Add event listeners
9887 addEventListernersToMapWidget ( this ) ;
9988
89+ /*
10090 const d = new DrawControl()
10191 this._map.addControl(d);
10292 d.onAdd();
93+ */
10394
95+ // Add default controls
10496 for ( const defaultControl of defaultControls )
10597 this . _map . addControl ( defaultControl ) ;
106- /*
107- this._map.getLayers().on("add", (e) => {
108- const layer = e.element;
109- console.log("layer add", layer.getProperties());
110- });
111- */
112-
113- /*
114- this._map.on("loadend", () => this.updateMetadata());
11598
116- this._map.getControls().on("propertychange", (e) => {
117- this.updateMetadata();
118- console.log("control added or removed", this._metadata);
119- });
120-
121- this._map.getLayers().on("propertychange", (e) => {
122- this.updateMetadata();
123- console.log("layer added or removed", this._metadata);
124- });
125- */
126- // ---
12799
128100 // Add controls
129101 for ( let controlDef of mapOptions . controls || [ ] ) {
@@ -136,6 +108,7 @@ export default class MapWidget {
136108 }
137109 }
138110
111+ // --- Functions
139112 getElement ( ) : HTMLElement {
140113 return this . _container ;
141114 }
@@ -152,33 +125,8 @@ export default class MapWidget {
152125 return this . _model ;
153126 }
154127
155- /*
156- updateMetadata(): void {
157- const layers = this._map.getLayers().getArray().map(l => ({
158- id: l.get("id"),
159- type: l.get("type"),
160- // extent: l.getExtent()
161- // properties: l.getProperties()
162- }));
163- this._metadata.layers = layers;
164- const controls = this._map.getControls().getArray().map(c => ({
165- id: c.get("id"),
166- type: c.get("type"),
167- // properties: c.getProperties()
168- }));
169- this._metadata.controls = controls;
170- if (this._model) {
171- this._model.set("metadata", this._metadata);
172- this._model.save_changes();
173- console.log("model data updated", this._metadata, this._map.getLayers().getArray());
174- }
175- }
176- */
177-
178128 setViewFromSource ( layerId : string ) : void {
179129 const view = this . getLayer ( layerId ) ?. getSource ( ) ?. getView ( ) ;
180- // const source = layer?.getSource();
181- // const view = source?.getView();
182130 if ( view )
183131 this . _map . setView ( view ) ;
184132 }
@@ -205,14 +153,6 @@ export default class MapWidget {
205153 this . _map . getView ( ) . fit ( extent ) ;
206154 }
207155
208- // TODO: obsolete since `useGeographic()` does this for us
209- /*
210- fitBoundsFromLonLat(extentLonLat: any): void {
211- const exent = transformExtent(extentLonLat, "EPSG:4326", this._map.getView().getProjection());
212- this.fitBounds(exent);
213- }
214- */
215-
216156 setView ( viewDef : JSONDef ) : void {
217157 const view = jsonConverter . parse ( viewDef ) as View ;
218158 this . _map . setView ( view ) ;
@@ -242,18 +182,6 @@ export default class MapWidget {
242182 if ( layer . get ( "fitBounds" ) ) {
243183 const source = layer . getSource ( ) as VectorSource ;
244184 this . setExtentFromSource ( source ) ;
245- /*
246- if (source) {
247- if (isEmpty(source.getExtent())) {
248- source.on("featuresloadend", (e) => {
249- this._map.getView().fit(source.getExtent());
250- });
251- }
252- else {
253- this._map.getView().fit(source.getExtent());
254- }
255- }
256- */
257185 }
258186
259187 this . _map . addLayer ( layer ) ;
@@ -303,25 +231,14 @@ export default class MapWidget {
303231
304232 addControl ( controlDef : JSONDef ) : void {
305233 const control = jsonConverter . parse ( controlDef ) ;
306- // control.set("id", controlDef.id);
307- // control.set("type", controlDef[TYPE_IDENTIFIER])
308234 control . setProperties ( { id : controlDef . id , type : controlDef [ TYPE_IDENTIFIER ] } ) ;
309235 this . _map . addControl ( control ) ;
310- /*
311- this._metadata.controls.push({
312- id: control.get("id"),
313- type: controlDef[TYPE_IDENTIFIER],
314- });
315- */
316- // console.log("control", control.get("id"), "added", this._metadata);
317236 }
318237
319238 removeControl ( controlId : string ) : void {
320239 const control = this . getControl ( controlId ) ;
321240 if ( control ) {
322241 this . _map . removeControl ( control ) ;
323- // this._metadata.controls = this._metadata.controls.filter(item => item["id"] != controlId);
324- // console.log("control", controlId, "removed", this._metadata);
325242 }
326243 }
327244
@@ -351,6 +268,7 @@ export default class MapWidget {
351268 }
352269
353270 // See https://openlayers.org/en/latest/examples/draw-and-modify-features.html
271+ // TODO: Remove: Use DrawControl instead
354272 addDrawInteraction ( type : GeomType ) : void {
355273 if ( this . _draw )
356274 this . _map . removeInteraction ( this . _draw ) ;
0 commit comments