1

I am using an EasyButton for showing a sidebar in a Leaflet map. Now I want to restrict the functionalities inside the sidebar in that way, that they only are accessible when the sidebar is opened. My plan was to ask for the current state of the easybutton, but I apparently don't know exactly how.

Here the code for the easyButton:

navi_button = L.easyButton({
 states:[{
    stateName: 'openSidebar',
    icon: 'fa fa-route',
    title: 'Navigation öffnen',
    onClick: function(control){
        sidebar.show();
        control.state('closeSidebar');
        }
    },{
    stateName: 'closeSidebar',
    icon: 'fa fa-route',
    title: 'Navigation schließen',
    onClick: function(control){
        sidebar.hide();
        control.state('openSidebar');
        }

    }]
});
navi_button.addTo(map);

Then it should follow like that (incorrect code):

if (navi_button.state() ==  openSidebar){
...[sidebar functions]...
}
else{};

Is there a way to get the current state of an easybutton?

2

You can get state of control in navi_button._currentState.stateName property, so your check would be something like

if (navi_button._currentState.stateName == 'openSidebar'){
...[sidebar functions]...
}
else{};
  • Wouldn't it be better to use === in order to exactly check for equality? – Stefan Jan 3 at 16:16
  • Thank you! The approach I provided doesn't work though, but at least I can keep building on it – pascatl Jan 3 at 16:17
  • @Stefan Generally yes but in this case it's not really important. – TomazicM Jan 3 at 16:22
1

As of EasyButton 2.4.0 you will be able to use control.state() (without arguments) to retrieve the current control state.

Source: https://github.com/CliffCloud/Leaflet.EasyButton/issues/87#issuecomment-453200962

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.