Closed joric closed 3 months ago
Okay I've managed to do it in subclassing. Here's a gist https://gist.github.com/joric/e325686809ef305a8fd25642d19bbc73
The issue is that toolbar sends consequent disable/enable events on the second click on the same item. We just need to prevent enabling if the disable time was too recent. Also add map handler to close on an outside click, the same way as popups.
let ImmediateAction = L.Toolbar2.Action.extend({
initialize: function(map, myAction) {
L.Toolbar2.Action.prototype.initialize.call(this);
this.disableTime = Date.now();
map.on('click', () => {
this.disable();
});
},
enable: function() {
if (Date.now() - this.disableTime > 100) {
L.Toolbar2.Action.prototype.enable.call(this);
}
},
disable: function() {
if (this._enabled) {
this.disableTime = Date.now();
}
L.Toolbar2.Action.prototype.disable.call(this);
},
});
Then use ImmediateAction.extend
instead of L.Toolbar2.Action.extend
in the toolbar.
I'm using 0.4.0-alpha.2 from CDN.
I want to close submenu on a second click on the button. Looks like the second click sends removeHooks and then immediately sends addHooks again and submenu opens again. Is it a bug?
I also want to close submenus on a map click. This is straightforward but not pretty. Maybe there's a better way?
It also seems that closing a submenu is impossible without subclassing because we have to save the action somewhere.
Do we really need subclassing? Please tell me how to work with the submenus properly.