Skip to content

Commit

Permalink
add class and code options to NavItems
Browse files Browse the repository at this point in the history
- class will apply to the whole li tag
- code allows better conditional rendering for custom templates

fixes #146
  • Loading branch information
guruofgentoo committed Feb 22, 2022
1 parent 71e6b10 commit 2842cc2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
11 changes: 10 additions & 1 deletion docs/source/getting-started.rst
Expand Up @@ -306,6 +306,14 @@ by an instance-level ``check_auth`` method, that will not be used by the navigat
- NavItems can specify an icon to display in the menu item by passing an ``icon_class`` string to the
NavItem constructor. e.g., ``NavItem('Title', NavURL(...), icon_class='fas fa-shopping-cart')``.

- NavItems can be given a ``class_`` kwarg that will be applied to the whole ``li`` tag in the default
render. This applies to both group items and the menu links themselves.

- NavItems can also be provided a ``code`` kwarg, which is useful when doing custom templating to render
the menu. The code is a code-only tag for the menu that can remain the same even if the menu wording
changes. For example, the code could be used in a conditional template block to render certain menu
items differently from the rest.

Example:

.. code-block:: python
Expand All @@ -324,6 +332,7 @@ Example:
'Nesting',
NavItem('Secret1', NavURL('private.secret1')),
NavItem('Secret1 Class', NavURL('private.secret1-class')),
class_='my-nest-class',
),
NavItem('Permissions On Stock Methods', NavURL('private.secret2')),
NavItem('Permissions On Methods', NavURL('private.someroute')),
Expand All @@ -333,7 +342,7 @@ Example:
'private.secret3', requires_permissions='permission3'
)),
NavItem('User Manage', NavURL('auth.user:add')),
NavItem('Logout', NavURL('auth.logout')),
NavItem('Logout', NavURL('auth.logout'), code='i-am-different'),
NavItem('Login', NavURL('auth.login', requires_anonymous=True)),
)
)
Expand Down
9 changes: 6 additions & 3 deletions keg_auth/libs/navigation.py
Expand Up @@ -153,11 +153,12 @@ class NavItem(object):
NavItem('Users', NavURL('auth.user:list')),
NavItem('Groups', NavURL('auth.group:list')),
nav_group='admin',
icon_class='fas fa-briefcase'
icon_class='fas fa-briefcase',
class_='my-menu-group'
),
NavItem(
'Reports',
NavItem('Frequency', NavURL('frequency-report')),
NavItem('Frequency', NavURL('frequency-report'), code='frequency'),
NavItem('Financial', NavURL('money-report', requires_permissions='secret-perm'))
)
)
Expand All @@ -167,7 +168,7 @@ class NavItemType(object):
STEM = 0
LEAF = 1

def __init__(self, *args, nav_group=None, icon_class=None):
def __init__(self, *args, nav_group=None, icon_class=None, class_=None, code=None):
self.label = None
if len(args) and (isinstance(args[0], str) or is_lazy_string(args[0])):
self.label = args[0]
Expand All @@ -176,6 +177,8 @@ def __init__(self, *args, nav_group=None, icon_class=None):
self.sub_nodes = None
self.nav_group = nav_group
self.icon_class = icon_class
self.class_ = class_
self.code = code

# cache permission-related items
self._is_permitted = {}
Expand Down
8 changes: 6 additions & 2 deletions keg_auth/templates/keg_auth/navigation.html
Expand Up @@ -5,9 +5,12 @@
{% macro render_node(node, expand_to_current) -%}
{# render a node (and its children, if appropriate) #}
{% set NODE_LEAF = 1 %}
{% set has_classes = node.has_current_route or node.class_ %}

{% if node.node_type == NODE_LEAF %}
<li{% if node.has_current_route %} class="nav-current"{% endif %}>
<li{% if has_classes %} class="
{%- if node.has_current_route %}nav-current {% endif %}
{%- if node.class_ %}{{ node.class_ }}{% endif %}"{% endif %}>
<a href="{{ node.route.url }}">{{ render_link_text(node) }}</a>
</li>
{% elif node.sub_nodes %}
Expand All @@ -18,7 +21,8 @@
{%- endmacro %}

{% macro render_group(node, expand_to_current) %}
<a class="menu-header group-header" data-toggle="collapse" href="#navgroup-{{ node.nav_group }}"
<a class="menu-header group-header {{- (' ' + node.class_) if node.class_ }}"
data-toggle="collapse" href="#navgroup-{{ node.nav_group }}"
{% if expand_to_current and node.has_current_route %}aria-expanded="true"{% endif %}>
{{ render_link_text(node) }}
<b class="caret"></b>
Expand Down
14 changes: 14 additions & 0 deletions keg_auth/tests/test_views.py
Expand Up @@ -130,6 +130,20 @@ def test_navigation_group(self):
nav_el = resp.pyquery('#navigation')
assert nav_el('[href="#navgroup-auth"]').attr('aria-expanded') != 'true'

def test_navigation_group_class(self):
user = ents.User.testing_create(permissions=[self.perm_auth, self.perm1, self.perm2])
client = AuthTestApp(flask.current_app, user=user)
resp = client.get('/')
nav_el = resp.pyquery('#navigation')
assert nav_el('.group-header.my-group-class').text() == 'Sub-Menu'

def test_navigation_link_class(self):
user = ents.User.testing_create(permissions=[self.perm_auth, self.perm1, self.perm2])
client = AuthTestApp(flask.current_app, user=user)
resp = client.get('/')
nav_el = resp.pyquery('#navigation')
assert nav_el('.my-link-class').text() == 'Secret View'

def test_navigation_icon(self):
user = ents.User.testing_create(permissions=[self.perm_auth])
client = AuthTestApp(flask.current_app, user=user)
Expand Down
3 changes: 2 additions & 1 deletion keg_auth_ta/events.py
Expand Up @@ -19,7 +19,8 @@ def init_navigation(app):
NavItem(
'Sub-Menu',
NavItem('User Manage', NavURL('private.secret2')),
NavItem('Secret View', NavURL('private.secret_nested')),
NavItem('Secret View', NavURL('private.secret_nested'), class_='my-link-class'),
class_='my-group-class'
),
NavItem(
'Menu-Group',
Expand Down

0 comments on commit 2842cc2

Please sign in to comment.