From a1e253484bdab076943d6c7430b0d7125b4e91dd Mon Sep 17 00:00:00 2001 From: erosselli <67162025+erosselli@users.noreply.github.com> Date: Sat, 8 Jun 2024 16:22:08 +0200 Subject: [PATCH] Fixed #12 - Added app labels to model list dialog --- .../static/admin/css/shortcuts.css | 1 + .../static/admin/js/shortcuts.js | 48 ++++++++++++++----- .../templates/admin/base.html | 43 ++++++++++------- .../templatetags/shortcuts.py | 21 ++++---- 4 files changed, 77 insertions(+), 36 deletions(-) diff --git a/django_admin_keyboard_shortcuts/static/admin/css/shortcuts.css b/django_admin_keyboard_shortcuts/static/admin/css/shortcuts.css index 9fd8aa3..94d946f 100644 --- a/django_admin_keyboard_shortcuts/static/admin/css/shortcuts.css +++ b/django_admin_keyboard_shortcuts/static/admin/css/shortcuts.css @@ -63,4 +63,5 @@ dialog { #model-list-dialog-search { margin: 0.5em 0em; + width: 12vw; } diff --git a/django_admin_keyboard_shortcuts/static/admin/js/shortcuts.js b/django_admin_keyboard_shortcuts/static/admin/js/shortcuts.js index 626afe7..9d8529c 100644 --- a/django_admin_keyboard_shortcuts/static/admin/js/shortcuts.js +++ b/django_admin_keyboard_shortcuts/static/admin/js/shortcuts.js @@ -56,15 +56,25 @@ function replaceModifiers() { // defined in django/contrib/admin/static/admin/js/nav_sidebar.js // If/when merged into core, we could try to reuse some parts function filterModelList() { - const options = []; const modelListDialog = document.getElementById('model-list-dialog'); if (!modelListDialog) { return; } - modelListDialog.querySelectorAll('li a').forEach((container) => { - options.push({title: container.innerHTML, node: container}); - }); + const appSections = []; + + modelListDialog.querySelectorAll('section').forEach(section => { + const options = []; + section.querySelectorAll('li a').forEach(container => options.push({ + title: container.innerHTML, + node: container + })); + + appSections.push({ + node: section, + options, + }); + }); function checkValue(event) { let filterValue = event.target.value; @@ -75,14 +85,30 @@ function filterModelList() { filterValue = ''; event.target.value = ''; // clear input } - for (const option of options) { - let displayValue = ''; - if (!filterValue || option.title.toLowerCase().indexOf(filterValue) === -1) { - displayValue = 'none' - } - option.node.parentNode.style.display = displayValue; - } + appSections.forEach(section => { + let matched = false; + // Check if any of the app models match the filter text + section.options.forEach((option) => { + let optionDisplay = ''; + if (option.title.toLowerCase().indexOf(filterValue) === -1) { + optionDisplay = 'none'; + } else { + matched = true; + } + // Set display in parent