Skip to content

Commit

Permalink
Introduce htmx_boost template tag; enable HTMX for user menu
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Mar 25, 2024
1 parent 16ceaef commit c74fd4f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 45 deletions.
44 changes: 42 additions & 2 deletions netbox/templates/base/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,48 @@ <h1 class="navbar-brand navbar-brand-autodark">
<i class="mdi mdi-lightbulb-on"></i>
</button>
</div>

{# User menu #}
{% include 'inc/user_menu.html' %}
{% if request.user.is_authenticated %}
<div class="nav-item dropdown">
<a href="#" class="nav-link d-flex lh-1 text-reset p-0" data-bs-toggle="dropdown" aria-label="Open user menu">
<div class="d-xl-block ps-2">
<div>{{ request.user }}</div>
<div class="mt-1 small text-secondary">{% if request.user.is_staff %}Staff{% else %}User{% endif %}</div>
</div>
</a>
<div class="dropdown-menu dropdown-menu-end dropdown-menu-arrow" {% htmx_boost %}>
{% if config.DJANGO_ADMIN_ENABLED and request.user.is_staff %}
<a class="dropdown-item" href="{% url 'admin:index' %}">
<i class="mdi mdi-cog"></i> {% trans "Django Admin" %}
</a>
{% endif %}
<a href="{% url 'account:profile' %}" class="dropdown-item">
<i class="mdi mdi-account"></i> {% trans "Profile" %}
</a>
<a href="{% url 'account:bookmarks' %}" class="dropdown-item">
<i class="mdi mdi-bookmark"></i> {% trans "Bookmarks" %}
</a>
<a href="{% url 'account:preferences' %}" class="dropdown-item">
<i class="mdi mdi-wrench"></i> {% trans "Preferences" %}
</a>
<a href="{% url 'account:usertoken_list' %}" class="dropdown-item">
<i class="mdi mdi-key"></i> {% trans "API Tokens" %}
</a>
<div class="dropdown-divider"></div>
<a href="{% url 'logout' %}" class="dropdown-item">
<i class="mdi mdi-logout-variant"></i> {% trans "Log Out" %}
</a>
</div>
</div>
{% else %}
<div class="btn-group ps-2">
<a class="btn btn-primary" type="button" href="{% url 'login' %}?next={{ request.path }}">
<i class="mdi mdi-login-variant"></i> {% trans "Log In" %}
</a>
</div>
{% endif %}
{# /User menu #}
</div>

{# Search box #}
Expand All @@ -79,7 +119,7 @@ <h1 class="navbar-brand navbar-brand-autodark">

{# Page content #}
<div class="page-wrapper">
<div id="page-content"{% if htmx_navigation %} hx-boost="true" hx-target="#page-content" hx-select="#page-content" hx-swap="outerHTML show:window:top"{% endif %}>
<div id="page-content" {% htmx_boost %}>

{# Page header #}
{% block header %}
Expand Down
41 changes: 0 additions & 41 deletions netbox/templates/inc/user_menu.html

This file was deleted.

3 changes: 2 additions & 1 deletion netbox/utilities/templates/navigation/menu.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% load helpers %}
{% load navigation %}

<ul class="navbar-nav pt-lg-2"{% if htmx_navigation %} hx-boost="true" hx-target="#page-content" hx-select="#page-content" hx-swap="outerHTML show:window:top"{% endif %}>
<ul class="navbar-nav pt-lg-2" {% htmx_boost %}>
{% for menu, groups in nav_items %}
<li class="nav-item dropdown">

Expand Down
18 changes: 17 additions & 1 deletion netbox/utilities/templatetags/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

__all__ = (
'nav',
'htmx_boost',
)


register = template.Library()


@register.inclusion_tag("navigation/menu.html", takes_context=True)
def nav(context: Context) -> Dict:
def nav(context):
"""
Render the navigation menu.
"""
Expand Down Expand Up @@ -43,3 +44,18 @@ def nav(context: Context) -> Dict:
'nav_items': nav_items,
'htmx_navigation': context['htmx_navigation']
}


@register.simple_tag(takes_context=True)
def htmx_boost(context, target='#page-content', select='#page-content'):
if not context.get('htmx_boost'):
return ''
hx_params = {
'hx-boost': 'true',
'hx-target': f'#{target}',
'hx-select': f'#{select}',
'hx-swap': 'outerHTML show:window:top',
}
return ' '.join([
f'{k}="{v}"' for k, v in hx_params.items()
])

0 comments on commit c74fd4f

Please sign in to comment.