Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to determine if an edit interaction is active #184

Open
paul121 opened this issue Sep 27, 2022 · 1 comment
Open

Add ability to determine if an edit interaction is active #184

paul121 opened this issue Sep 27, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@paul121
Copy link
Member

paul121 commented Sep 27, 2022

Right now I'm not sure if there is an easy way to determine if any of the edit interactions are currently "active". This would be useful when adding custom behaviors that should/should not do certain things when a user is using interactions from the edit control, like drawing on the map.

An interaction object is added to instance.edit the first time it is turned on, but is never removed. This can be used to see if an interaction has been used by looking on the instance.edit control eg: instance.edit.drawInteraction or instance.edit.modifyInteraction, but because it is never removed, it cannot be used to determine if the interaction is "active".

Interestingly, interaction objects do have a getActive() method, but our edit control does not use this: https://openlayers.org/en/latest/apidoc/module-ol_interaction_Interaction-Interaction.html#getActive Instead of making the interaction "inactive" it removes the interaction from the map, but keeps the interaction object itself at instance.edit.*. The interaction object continues to report getActive() == true even after the interaction was removed from the map.

After a quick look at this I see two possible approaches:

  1. Remove the interaction object from instance.edit after removing the interaction from the map. This way the presence of the interaction object can be used to determine if it is active.

    disableAll() {
    const interactions = [
    'drawInteraction',
    'modifyInteraction',
    'selectInteraction',
    'snapInteraction',
    'translateInteraction',
    ];
    interactions.forEach((interaction) => {
    if (this[interaction]) {
    if (interaction === 'selectInteraction') {
    this[interaction].getFeatures().clear();
    }
    this.getMap().removeInteraction(this[interaction]);
    }
    });
    this.toggleActiveButton(false, false);
    this.toggleDeleteButton(false);
    this.eventListeners.disable.forEach(({ cb, format }) => {
    cb(format.writeFeatures(this.getFeatures(), projection));
    });
    }

  2. Refactor the edit control to use the "active" state of interactions instead of adding/removing interactions from the map. At first glance it seems like this might be feasible, but I'm not 100% sure. See also Edit interactions remain selected after programatically disabling active state #183

Finally, it would be nice if the edit control provided some helper methods to determine if any part of the edit control "is active". This could be a convenience method wrapped around one of the approaches described above. For example:

# Determine if any edit interaction is being used.
let editing = instance.edit.getActive();

# Determine if the draw interaction is being used.
let drawing = instance.edit.getActive('draw');

# Determine if the modify interaction is being used.
let modifying = instance.edit.getActive('modify');
@paul121 paul121 added the enhancement New feature or request label Sep 27, 2022
@symbioquine
Copy link
Collaborator

I feel that we should follow the patterns that OpenLayers has established and keep farmOS-map's behavior as unsurprising as possible when OL functionality is used programmatically.

In practice I think that means you're highlighting some real issues here @paul121.

IMHO we should;

  • Call interaction.setActive(false) and set instance.edit.drawInteraction to null when finishing/cancelling drawing
  • Observe the interaction's active property and honor programmatic changes to it
  • Probably provide some sort of convenience method(s) like you're describing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants