farmOS / farmOS-map

farmOS-map is an OpenLayers wrapper library designed for agricultural mapping needs. It can be used in any project that has similar requirements.
https://farmOS.github.io/farmOS-map
MIT License
35 stars 21 forks source link

Layer switcher not necessarily updated when layers are removed from the map #174

Open symbioquine opened 1 year ago

symbioquine commented 1 year ago

We probably need to update this to honor layers being removed(/changed?): https://github.com/farmOS/farmOS-map/blob/777758f7218499e2211c5d75c1faf3dbea87904a/src/behavior/layerSwitcherInSidePanel.js#L46-L49

Also relevant: https://github.com/walkermatt/ol-layerswitcher/issues/98

mstenta commented 1 year ago

Here's some behavior code to replicate against - this is where we discovered the issue. The behavior handles copying WKT from the input field under the map into the drawing layer.

(function () {
  farmOS.map.behaviors.wkt_refresh = {
    attach: function (instance) {

      // Get the wkt input element.
      var wkt = document.getElementById('edit-mymap-value');

      // Run a handleInput() callback when input changes.
      wkt.oninput = handleInput;
      function handleInput(e) {
        if (wkt.value) {

          // Clear features from the current edit layer's source.
          instance.edit.layer.getSource().clear();

          // Add a new temporary invisible WKT layer.
          var layer = instance.addLayer('wkt', {
            title: 'WKT',
            wkt: wkt.value,
            visible: false,
          });

          // Copy features from the WKT layer to the edit layer.
          instance.edit.layer.getSource().addFeatures(layer.getSource().getFeatures());

          // Remove the temporary WKT layer.
          instance.map.removeLayer(layer);

          // Zoom to the edit layer.
          instance.zoomToLayer(instance.edit.layer);
        } else if (wkt.value === '') {

          // Clear features from the current edit layer's source.
          instance.edit.layer.getSource().clear();
        }
      }
    },

    // Make sure this runs after farmOS.map.behaviors.wkt.
    weight: 101
  };
}());

(Note this currently assumes a map named mymap for the ID matching in var wkt = document.getElementById('edit-mymap-value');)