Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some simple htmx improvements #151

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airlock/static/htmx-1.9.10.min.js

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions airlock/templates/file_browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@

<div style="flex-basis: 25%">
{% #card %}
<ul id="tree" class="tree root" hx-target="#selected-contents" hx-select="#selected-contents" hx-swap="outerHTML" hx-push-url="true">
<ul id="tree" class="tree root"
hx-boost="true"
hx-target="#selected-contents"
hx-select="#selected-contents"
hx-swap="outerHTML"
hx-push-url="true"
hx-on:htmx:after-request="setTreeSelection(this, event)"
>
{% include "file_browser/tree.html" with path=root %}
</ul>
{% /card %}
Expand Down Expand Up @@ -220,16 +227,18 @@
{% endblock full_width_content %}

{% block extra_js %}
<script type="text/javascript" src="{% static 'vendor/htmx.min.js' %}"></script>
<script type="text/javascript" src="{% static 'htmx-1.9.10.min.js' %}"></script>
{% django_htmx_script %}

<script type="text/javascript">
// keep the selected class up to date in the tree on the client side
function setTreeSelection(target, event) {
function setTreeSelection(tree, event) {
// target here is the hx-get link that has been clicked on

// remove class from currently selected node
document.querySelector('#tree .selected')?.classList.remove('selected');
tree.querySelector('.selected')?.classList.remove('selected');

let target = event.srcElement;

// set current selected
target.classList.add('selected');
Expand Down
4 changes: 2 additions & 2 deletions airlock/templates/file_browser/tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% if child.is_directory %}
<details {% if child.expanded %}open{% endif %}>
<summary>
<a class="{{ child.html_classes }}" hx-get="{{ child.url }}" href="{{ child.url }}" hx-on="htmx:afterRequest: setTreeSelection(this, event)">{{ child.display }}</a>
<a class="{{ child.html_classes }}" href="{{ child.url }}">{{ child.display }}</a>
</summary>
{% if child.children %}
<ul class="tree">
Expand All @@ -12,7 +12,7 @@
{% endif %}
</details>
{% else %}
<a class="{{ child.html_classes }}" hx-get="{{ child.url }}" href="{{ child.url }}" hx-on="htmx:afterRequest: setTreeSelection(this, event)">{{ child.display }}</a>
<a class="{{ child.html_classes }}" href="{{ child.url }}">{{ child.display }}</a>
{% endif %}
</li>
{% endfor %}
Loading