inveniosoftware / inveniosoftware/flask-menu

Provide a method to make matching url segments in the default active_when function optional

Open
#83 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
54
Forks
43
PR merge metrics
No merged PRs in 30d

Description

## Is your feature request related to a problem? Please describe.
If I have a menu registered with, e.g.,
```
@app.route("/x")
@register_menu(app, '.a', 'Test')
def xyzzy():
#...
```
and my 404 page template includes the menu, then the resulting menu entry is displayed as active when navigating to nonexistent routes under /x (e.g,. /x/y).

I also would like the ability to restructure menus without having to change URLs (e.g., to have an /x/z route elsewhere on the site without the menu entry for the /x route being shown as active when I navigate to it--there can be reasons why the navigation menu hierarchy might be independent of the URL structure of the site.)

## Describe the solution you'd like
A simple method (perhaps a keyword argument in the Menu constructor) of restricting the default active_when function to comparing endpoints rather than trying to match the URL segments would be helpful. More generally, being able to do something like Menu(app=app, active_when=some_callable) to set a default active_when function for entries within that menu might provide more flexibility; this might similarly be useful for visible_when=.

## Describe alternatives you've considered
I might be missing something, but using the active_when= keyword argument to the @register_menu decorator appears to mitigate the problem, e.g.:
```
def aw(self):
return request.endpoint == self._endpoint
@register_menu(app, '.a', 'Test', active_when=aw)
#[...]
```
however, a) this requires specifying active_when= for every single menu entry, and b) as far as I see, the endpoint of the menu entry is not exposed by the API, so using the private attribute self._endpoint in the custom function is questionable.

## Additional context
Incidentally, exposing the endpoint in the MenuEntryMixin API might also be useful in and of itself. Right now I'm clumsily iterating through app.url_map.iter_rules() and calling rule.match() to try to find the endpoint from MenuEntryMixin.url; it would be much simpler if there were an API exposing self._endpoint.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.