farmOS-map icon indicating copy to clipboard operation
farmOS-map copied to clipboard

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

Open symbioquine opened this issue 2 years ago • 1 comments

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

symbioquine avatar Jul 27 '22 15:07 symbioquine

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');)

mstenta avatar Jul 27 '22 16:07 mstenta