Improve callback support
[gpl/argeo-suite.git] / org.argeo.app.geo.js / src / org.argeo.app.geo.js / OpenLayersMapPart.js
index 7c157eb19226a1716545aa4e11f09232060292fb..6eff99f2ff58d8fe3635474bdc2a0e0628585fff 100644 (file)
@@ -12,6 +12,7 @@ import { Point } from 'ol/geom.js';
 import VectorLayer from 'ol/layer/Vector.js';
 import GeoJSON from 'ol/format/GeoJSON.js';
 import GPX from 'ol/format/GPX.js';
+import Select from 'ol/interaction/Select.js';
 
 import MapPart from './MapPart.js';
 import { SentinelCloudless } from './OpenLayerTileSources.js';
@@ -20,6 +21,8 @@ import { SentinelCloudless } from './OpenLayerTileSources.js';
 export default class OpenLayersMapPart extends MapPart {
        /** The OpenLayers Map. */
        #map;
+       callbacks = {};
+
        // Constructor
        constructor() {
                super();
@@ -38,6 +41,8 @@ export default class OpenLayersMapPart extends MapPart {
                });
        }
 
+       /* GEOGRAPHICAL METHODS */
+
        setZoom(zoom) {
                this.#map.getView().setZoom(zoom);
        }
@@ -67,4 +72,32 @@ export default class OpenLayersMapPart extends MapPart {
                        source: vectorSource,
                }));
        }
+
+       /* CALLBACKS */
+       enableFeatureSingleClick() {
+               // we cannot use 'this' in the function provided to OpenLayers
+               let mapPart = this;
+               this.#map.on('singleclick', function(e) {
+                       let feature = null;
+                       // we chose only one
+                       e.map.forEachFeatureAtPixel(e.pixel, function(f) {
+                               feature = f;
+                       });
+                       if (feature !== null)
+                               mapPart.callbacks['onFeatureSingleClick'](feature.get('path'));
+               });
+       }
+
+       enableFeatureSelected() {
+               // we cannot use 'this' in the function provided to OpenLayers
+               let mapPart = this;
+               var select = new Select();
+               this.#map.addInteraction(select);
+               select.on('select', function(e) {
+                       if (e.selected.length > 0) {
+                               let feature = e.selected[0];
+                               mapPart.callbacks['onFeatureSelected'](feature.get('path'));
+                       }
+               });
+       }
 }