Enhance case list item links and management actions: update hrefs for dynamic case numbering and improve button structure for better accessibility

This commit is contained in:
Ross
2025-11-16 23:03:19 +00:00
parent 5e0068d490
commit 9760be8f27
3 changed files with 67 additions and 11 deletions
@@ -3,8 +3,12 @@
<div class="flex-fill">
<div class="d-flex justify-content-between align-items-start">
<div>
<a class="h6 mb-0 text-decoration-none" href="{% url 'atlas:collection_case_view' pk=collection.pk case_number=forloop.counter0 %}">{{ casedetail.case.title }}</a>
<div class="small text-muted">Case {{ forloop.counter }} &mdash; <span class="text-truncate">{{ casedetail.case.description|default_if_none:""|truncatechars:80 }}</span></div>
<a class="h6 mb-0 text-decoration-none case-link"
href="{% url 'atlas:collection_case_view' pk=collection.pk case_number=forloop.counter0 %}"
data-case-base-href="{% url 'atlas:collection_case_view' pk=collection.pk case_number=0 %}">
{{ casedetail.case.title }}
</a>
<div class="small text-muted"><span class="case-number">Case {{ forloop.counter }}</span> &mdash; <span class="text-truncate">{{ casedetail.case.description|default_if_none:""|truncatechars:80 }}</span></div>
</div>
</div>
@@ -1,4 +1,4 @@
<span class="btn-group btn-group-sm me-2" role="group" aria-label="management-actions">
<div class="btn-group btn-group-sm me-2" role="group" aria-label="management-actions">
<a class="btn btn-outline-secondary" href="{% url 'atlas:collection_case_displaysetup' casedetail.collection.pk casedetail.case.pk %}" title="Setup default display">
<i class="bi bi-display"></i>
</a>
@@ -30,4 +30,13 @@
<i class="bi bi-check"></i>
</button>
{% endif %}
</span>
</div>
{# Remove button (HTMX) - only shown when the surrounding template has `can_edit` true #}
{% if can_edit %}
<form class="d-inline ms-2 m-0" hx-post="{% url 'atlas:remove_case_from_collection' casedetail.case.pk casedetail.collection.pk %}" hx-target="closest li" hx-swap="outerHTML" hx-confirm="Remove this case from the collection?">
<button type="submit" class="btn btn-outline-danger btn-sm case-remove" title="Remove this case from the collection">
<i class="bi bi-trash"></i>
</button>
</form>
{% endif %}
+50 -7
View File
@@ -155,17 +155,38 @@
for (var i = ul.children.length; i >= 0; i--) {
ul.appendChild(ul.children[Math.random() * i | 0]);
}
renumber();
}))
// helper to renumber visible cases and update links
function renumber() {
$("#full-question-list li").each((i, e) => {
// update Case N label
$(e).find('.case-number').text('Case ' + (i + 1));
// update link href based on data-case-base-href (replace trailing 0)
const a = $(e).find('.case-link');
if (a.length) {
const base = a.attr('data-case-base-href');
if (base) {
// replace trailing '/0' with new index
const newHref = base.replace(/\/0$/, '/' + i);
a.attr('href', newHref);
}
}
});
}
$("#full-question-list li").each((n, el) => {
// Add up/down arrows at the start of each <li>
// Add up/down arrows at the start of each <li>
const upBtn = $("<button class='move-up btn btn-sm' title='Move up'>&#8593;</button>").click(function(e){
e.preventDefault();
const li = $(this).closest("li");
const prev = li.prev();
if (prev.length) {
prev.before(li);
renumber();
}
});
const downBtn = $("<button class='move-down btn btn-sm' title='Move down'>&#8595;</button>").click(function(e){
@@ -174,16 +195,23 @@
const next = li.next();
if (next.length) {
next.after(li);
renumber();
}
});
$(el).prepend(downBtn).prepend(upBtn);
// Existing delete button logic
$(el).append($(
"<span class='exam-question-delete flex-col'><button title='Remove this question from the exam/collection'>Remove</button></span>"
).click(() => {
el.remove();
}));
// Prefer server-side HTMX remove button when present; otherwise keep a fallback
if ($(el).find('.case-remove').length === 0) {
// fallback: append an inline client-side remove button (for contexts without server-side remove)
$(el).append($(
"<span class='exam-question-delete flex-col'><button title='Remove this question from the exam/collection'>Remove</button></span>"
).click(() => {
el.remove();
renumber();
}));
} else {
// If HTMX remove exists it will handle the server call and swap; renumber after swap via htmx:afterSwap
}
});
$("#full-question-list").after($(
@@ -208,6 +236,7 @@
if (data.status == "success") {
toastr.info('Exam order changed.')
renumber();
}
})
.always(function () {
@@ -220,6 +249,20 @@
).click(() => {
location.reload();
}));
// Keep list numbering in sync after HTMX swaps that affect the case list
document.body.addEventListener('htmx:afterSwap', function(evt){
try {
const target = evt.detail && evt.detail.target ? evt.detail.target : null;
if (!target) return;
if (target.id === 'full-question-list' || (target.closest && target.closest('#full-question-list'))) {
setTimeout(function(){
if (typeof renumber === 'function') renumber();
}, 10);
}
} catch(e) {
console.error('htmx afterSwap renumber error', e);
}
});
});