diff --git a/docs/source/getting-started.rst b/docs/source/getting-started.rst index 4202b51..a90f870 100644 --- a/docs/source/getting-started.rst +++ b/docs/source/getting-started.rst @@ -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 @@ -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')), @@ -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)), ) ) diff --git a/keg_auth/libs/navigation.py b/keg_auth/libs/navigation.py index 69ed12b..3bc2db5 100644 --- a/keg_auth/libs/navigation.py +++ b/keg_auth/libs/navigation.py @@ -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')) ) ) @@ -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] @@ -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 = {} diff --git a/keg_auth/templates/keg_auth/navigation.html b/keg_auth/templates/keg_auth/navigation.html index 3a34832..2b6e3b9 100644 --- a/keg_auth/templates/keg_auth/navigation.html +++ b/keg_auth/templates/keg_auth/navigation.html @@ -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 %} - + {{ render_link_text(node) }} {% elif node.sub_nodes %} @@ -18,7 +21,8 @@ {%- endmacro %} {% macro render_group(node, expand_to_current) %} - {{ render_link_text(node) }} diff --git a/keg_auth/tests/test_views.py b/keg_auth/tests/test_views.py index 27db8ee..0f9b865 100644 --- a/keg_auth/tests/test_views.py +++ b/keg_auth/tests/test_views.py @@ -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) diff --git a/keg_auth_ta/events.py b/keg_auth_ta/events.py index 1d02e37..47cf368 100644 --- a/keg_auth_ta/events.py +++ b/keg_auth_ta/events.py @@ -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',