From ac8bf88db359b7bb3f2e63ec7b892339c4843131 Mon Sep 17 00:00:00 2001 From: Jacob Olson Date: Wed, 11 Nov 2026 10:33:24 -0700 Subject: [PATCH] Type-to-filter dropdowns for the aliquot and sample forms Part of #209. Co-Authored-By: Claude Sonnet 5.5 --- krill/sample/forms.py | 11 +- krill/sample/tests/test_combobox.py | 225 +++++++++++++++++++ krill/sample/urls.py | 3 +- krill/sample/views/search.py | 43 ++++ krill/sample/widgets.py | 57 +++++ krill/static/krill/js/combobox.js | 329 ++++++++++++++++++++++++++++ krill/static/krill/style.css | 95 ++++++++ krill/templates/base/layout.html | 1 + krill/templates/sample/create.html | 2 + 9 files changed, 764 insertions(+), 2 deletions(-) create mode 100644 krill/sample/tests/test_combobox.py create mode 100644 krill/sample/widgets.py create mode 100644 krill/static/krill/js/combobox.js diff --git a/krill/sample/forms.py b/krill/sample/forms.py index 4c0c06c..551c1d7 100644 --- a/krill/sample/forms.py +++ b/krill/sample/forms.py @@ -2,6 +2,7 @@ from .models.sample import Sample from .models.aliquot import Aliquot, AliquotLocation, AliquotType, AliquotDisposition from .models.source import Source +from .widgets import RemoteSelect, aliquot_choice_label, mark_comboboxes class SampleForm(forms.ModelForm): class Meta: @@ -26,6 +27,10 @@ class Meta: 'notes': forms.Textarea(attrs={'rows': 4}), } + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + mark_comboboxes(self) + def clean(self): cleaned = super().clean() warn, alert = cleaned.get('low_count_warn'), cleaned.get('low_count_alert') @@ -72,6 +77,7 @@ class Meta: ] widgets = { 'sample_date': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'), + 'parent': RemoteSelect('sample:aliquot_options'), } help_texts = { 'parent': 'Parent aliquot, if this is a derivative', @@ -85,8 +91,10 @@ def __init__(self, *args, **kwargs): from storage.models.storage import Box super().__init__(*args, **kwargs) self.fields['box'].queryset = Box.objects.all().order_by('name') - self.fields['parent'].queryset = Aliquot.objects.select_related('sample').all() + self.fields['parent'].queryset = Aliquot.objects.select_related('sample', 'aliquot_type') + self.fields['parent'].label_from_instance = aliquot_choice_label self.fields['disposition'].required = True + mark_comboboxes(self) def clean(self): """Validate box assignment fields""" @@ -177,3 +185,4 @@ def __init__(self, *args, **kwargs): from storage.models.storage import Box super().__init__(*args, **kwargs) self.fields['box'].queryset = Box.objects.all() + mark_comboboxes(self) diff --git a/krill/sample/tests/test_combobox.py b/krill/sample/tests/test_combobox.py new file mode 100644 index 0000000..ff427fc --- /dev/null +++ b/krill/sample/tests/test_combobox.py @@ -0,0 +1,225 @@ +import datetime + +from django.contrib.staticfiles import finders +from django.db import connection +from django.test.utils import CaptureQueriesContext +from django.urls import reverse + +from ..forms import AliquotForm, AliquotMoveForm, SampleForm +from ..models.aliquot import Aliquot, AliquotType +from ..models.sample import Sample +from ..widgets import RemoteSelect, aliquot_choice_label +from .test_views import SampleViewTest + + +class ComboboxFixture(SampleViewTest): + def setUp(self): + super().setUp() + Aliquot.objects.all().delete() + self.url = reverse('sample:aliquot_options') + self.client.force_login(self.user) + + def make_aliquot(self, sample=None, **kwargs): + return Aliquot.objects.create( + sample=sample or self.sample, aliquot_type=kwargs.pop('aliquot_type', self.aliquot_type), + disposition=self.stored_disposition, **kwargs) + + def search(self, q): + return self.client.get(self.url, {'q': q}).json() + + +class AliquotLabelTest(ComboboxFixture): + def test_label_has_sample_number_type_passage_and_date(self): + a = self.make_aliquot(passage='p8', sample_date=datetime.date(2026, 3, 5)) + self.assertEqual(aliquot_choice_label(a), f'Test Sample #{a.pk} · Test Type · p8 · 2026-03-05') + + def test_label_skips_what_is_missing(self): + a = Aliquot.objects.create(sample=self.sample, disposition=self.stored_disposition) + self.assertEqual(aliquot_choice_label(a), f'Test Sample #{a.pk}') + + +class SearchEndpointTest(ComboboxFixture): + def test_requires_login(self): + self.client.logout() + self.assertEqual(self.client.get(self.url, {'q': 'x'}).status_code, 302) + + def test_blank_query_returns_nothing(self): + self.make_aliquot() + for q in ('', ' '): + with self.subTest(q=repr(q)): + self.assertEqual(self.search(q), {'results': [], 'more': False}) + + def test_matches_sample_name_ignoring_case(self): + a = self.make_aliquot() + self.make_aliquot(sample=Sample.objects.create(name='Other', source=self.source)) + self.assertEqual([r['id'] for r in self.search('test sam')['results']], [a.pk]) + + def test_matches_passage_and_type(self): + by_passage = self.make_aliquot(passage='p8') + other_type = AliquotType.objects.create(name='Pellet') + by_type = self.make_aliquot(aliquot_type=other_type) + self.assertEqual([r['id'] for r in self.search('p8')['results']], [by_passage.pk]) + self.assertEqual([r['id'] for r in self.search('pellet')['results']], [by_type.pk]) + + def test_every_word_must_match_so_sample_and_passage_combine(self): + hit = self.make_aliquot(passage='p8') + self.make_aliquot(passage='p9') + self.make_aliquot(sample=Sample.objects.create(name='Other', source=self.source), passage='p8') + # 'test' would also match every aliquot through its type, 'Test Type'; 'sample' is only in the name + self.assertEqual([r['id'] for r in self.search('sample p8')['results']], [hit.pk]) + + def test_aliquot_number_with_or_without_hash(self): + target = self.make_aliquot() + self.make_aliquot() + for q in (str(target.pk), f'#{target.pk}'): + with self.subTest(q=q): + self.assertEqual(self.search(q)['results'][0]['id'], target.pk) + + def test_an_exact_number_is_listed_first_even_when_others_also_match(self): + sample = Sample.objects.create(name='Line 7', source=self.source) # "7" also matches this name + many = [self.make_aliquot(sample=sample) for _ in range(25)] + target = self.make_aliquot(sample=self.sample) + number = str(many[3].pk) + self.assertEqual(self.search(number)['results'][0]['id'], many[3].pk) + self.assertNotEqual(target.pk, many[3].pk) + + def test_limit_and_more_flag(self): + for _ in range(25): + self.make_aliquot() + data = self.search('test') + self.assertEqual(len(data['results']), 20) + self.assertTrue(data['more']) + + def test_no_more_flag_at_the_limit(self): + for _ in range(20): + self.make_aliquot() + data = self.search('test') + self.assertEqual(len(data['results']), 20) + self.assertFalse(data['more']) + + def test_deleted_aliquots_are_not_offered(self): + gone = self.make_aliquot() + gone.deleted = True + gone.save() + self.assertEqual(self.search('test')['results'], []) + + def test_results_are_labelled_and_ordered_by_sample_then_newest(self): + zebra = Sample.objects.create(name='Zebra', source=self.source) + a = self.make_aliquot(sample=zebra) + older, newer = self.make_aliquot(passage='p1'), self.make_aliquot(passage='p1') + results = self.search('')['results'] or self.search('p1')['results'] + self.assertEqual([r['id'] for r in results], [newer.pk, older.pk]) + self.assertEqual(results[0]['label'], f'Test Sample #{newer.pk} · Test Type · p1') + self.assertEqual(self.search('zebra')['results'][0]['id'], a.pk) + + def test_the_query_count_does_not_grow_with_the_results(self): + self.make_aliquot() + with CaptureQueriesContext(connection) as few: + self.search('test') + for _ in range(15): + self.make_aliquot() + with CaptureQueriesContext(connection) as many: + self.search('test') + self.assertEqual(len(few), len(many)) + + +class RemoteSelectFormTest(ComboboxFixture): + def test_the_parent_select_renders_only_a_blank_option(self): + for _ in range(30): + self.make_aliquot() + html = str(AliquotForm()['parent']) + self.assertEqual(html.count('Test Sample #{parent.pk} · Test Type · p8', html) + + def test_an_edited_aliquot_shows_its_current_parent(self): + parent = self.make_aliquot(passage='p3') + child = self.make_aliquot(parent=parent) + html = str(AliquotForm(instance=child)['parent']) + self.assertIn(f'', html) + select = html[html.index('name="parent"'):] + select = select[:select.index('')] + self.assertEqual(select.count('//', ModelDetailView.as_view(), name='detail'), path('aliquot//', AliquotDetailView.as_view(), name='aliquot_detail'), # Backward-compatibility redirect: tube// -> aliquot// diff --git a/krill/sample/views/search.py b/krill/sample/views/search.py index 81e91ca..196ffc2 100644 --- a/krill/sample/views/search.py +++ b/krill/sample/views/search.py @@ -6,6 +6,7 @@ from ..models.sample import Sample from ..models.aliquot import Aliquot, AliquotType, AliquotLocation from ..models.source import Source +from ..widgets import aliquot_choice_label @login_required @@ -127,3 +128,45 @@ def aliquot_search(request): 'total_count': aliquots.count(), } return render(request, 'sample/search.html', context) + + +OPTION_LIMIT = 20 + + +@login_required +def aliquot_options(request): + """JSON for the Parent aliquot picker: aliquots matching what was typed. + + Every word typed must match something (sample name, type or passage), so "HeLa p8" finds the p8 + aliquots of HeLa. A word that is a number, with or without a leading #, also matches the aliquot + number, and an aliquot with exactly that number is listed first. At most OPTION_LIMIT results come + back, with `more` set when there are others, so the page never loads thousands of options. + """ + query = request.GET.get('q', '').strip() + if not query: + return JsonResponse({'results': [], 'more': False}) + + aliquots = Aliquot.objects.filter(deleted=False).select_related('sample', 'aliquot_type') + words = query.split() + for word in words: + match = ( + Q(sample__name__icontains=word) | + Q(aliquot_type__name__icontains=word) | + Q(passage__icontains=word) + ) + number = word.lstrip('#') + if number.isdigit(): + match |= Q(pk=int(number)) + aliquots = aliquots.filter(match) + + exact = [] + only = words[0].lstrip('#') if len(words) == 1 else '' + if only.isdigit(): + exact = list(aliquots.filter(pk=int(only))) + aliquots = aliquots.exclude(pk=int(only)) + + rows = exact + list(aliquots.order_by('sample__name', '-pk')[:OPTION_LIMIT + 1]) + return JsonResponse({ + 'results': [{'id': a.pk, 'label': aliquot_choice_label(a)} for a in rows[:OPTION_LIMIT]], + 'more': len(rows) > OPTION_LIMIT, + }) diff --git a/krill/sample/widgets.py b/krill/sample/widgets.py new file mode 100644 index 0000000..9ef294c --- /dev/null +++ b/krill/sample/widgets.py @@ -0,0 +1,57 @@ +"""Form helpers for the type-to-filter dropdowns (see static/krill/js/combobox.js).""" +from django import forms +from django.urls import reverse_lazy + + +def aliquot_choice_label(aliquot): + """How an aliquot is named in a picker: enough to tell similar ones apart. + + Needs aliquot.sample and aliquot.aliquot_type loaded (select_related) to avoid a query per row. + Example: "HeLa #1234 · Cells · p8 · 2026-03-05" + """ + parts = [f'{aliquot.sample.name} #{aliquot.pk}'] + if aliquot.aliquot_type_id: + parts.append(aliquot.aliquot_type.name) + if aliquot.passage: + parts.append(aliquot.passage) + if aliquot.sample_date: + parts.append(f'{aliquot.sample_date:%Y-%m-%d}') + return ' · '.join(parts) + + +class RemoteSelect(forms.Select): + """A select for a very long list: it renders only its blank and current choices. + + The browser searches the rest through `search_url` (combobox.js). Validation still uses the + field's full queryset, so any valid value that is posted is accepted. Without JavaScript the + current value is kept but a new one cannot be chosen. + """ + + def __init__(self, search_url_name, attrs=None): + attrs = {**(attrs or {}), 'data-combobox': '1', 'data-search-url': reverse_lazy(search_url_name)} + super().__init__(attrs) + + def optgroups(self, name, value, attrs=None): + iterator = self.choices + if not hasattr(iterator, 'queryset'): # not a model choice field: behave normally + return super().optgroups(name, value, attrs) + selected = [v for v in value if v not in (None, '')] + groups = [(None, [self.create_option(name, '', '---------', False, 0, attrs=attrs)], 0)] + if selected: + for index, obj in enumerate(iterator.queryset.filter(pk__in=selected), start=1): + option = self.create_option( + name, obj.pk, iterator.field.label_from_instance(obj), True, index, attrs=attrs) + groups.append((None, [option], index)) + return groups + + +def mark_comboboxes(form): + """Switch on type-to-filter for every dropdown in a form. + + combobox.js leaves short lists as plain selects, so lists that are small today only change + when they grow. + """ + for field in form.fields.values(): + widget = field.widget + if isinstance(widget, forms.Select) and not isinstance(widget, forms.SelectMultiple): + widget.attrs.setdefault('data-combobox', '1') diff --git a/krill/static/krill/js/combobox.js b/krill/static/krill/js/combobox.js new file mode 100644 index 0000000..cf5cab5 --- /dev/null +++ b/krill/static/krill/js/combobox.js @@ -0,0 +1,329 @@ +// Type-to-filter for is replaced by a text box with a list under it. The original are filtered as you type. Every word +// typed must appear in an option ("hela p8" finds "HeLa #12 · Cells · p8"). +// Remote lists A back so the field never disappears + function undo(select) { + var wrapper = select.closest('.combobox'); + if (wrapper && wrapper.parentNode) { + wrapper.parentNode.insertBefore(select, wrapper); + wrapper.parentNode.removeChild(wrapper); + } + select.classList.remove('combobox-native'); + select.removeAttribute('aria-hidden'); + select.removeAttribute('tabindex'); + delete select.dataset.comboboxReady; + if (select.id) { + Array.prototype.forEach.call( + document.querySelectorAll('label[data-combobox-for="' + select.id + '"]'), + function (label) { label.setAttribute('for', select.id); } + ); + } + } + + function init(select) { + try { + enhance(select); + } catch (error) { + undo(select); + if (window.console) { console.error('Type-to-filter could not start for', select.name, error); } + } + } + + function enhance(select) { + if (select.dataset.comboboxReady) { return; } + var searchUrl = select.getAttribute('data-search-url'); + if (!searchUrl && select.options.length - 1 < MIN_OPTIONS) { return; } + select.dataset.comboboxReady = '1'; + + var id = 'combobox-' + (++counter); + var wrapper = document.createElement('div'); + wrapper.className = 'combobox'; + select.parentNode.insertBefore(wrapper, select); + wrapper.appendChild(select); + select.classList.add('combobox-native'); + select.tabIndex = -1; + select.setAttribute('aria-hidden', 'true'); + + var input = document.createElement('input'); + input.type = 'text'; + input.id = id + '-input'; + input.className = 'combobox-input'; + input.autocomplete = 'off'; + input.spellcheck = false; + input.disabled = select.disabled; + input.setAttribute('role', 'combobox'); + input.setAttribute('aria-autocomplete', 'list'); + input.setAttribute('aria-haspopup', 'listbox'); + input.setAttribute('aria-expanded', 'false'); + input.setAttribute('aria-controls', id + '-list'); + input.placeholder = searchUrl ? 'Type to search' : 'Type to filter'; + if (select.getAttribute('aria-describedby')) { + input.setAttribute('aria-describedby', select.getAttribute('aria-describedby')); + } + + var clear = document.createElement('button'); + clear.type = 'button'; + clear.className = 'combobox-clear'; + clear.setAttribute('aria-label', 'Clear selection'); + clear.textContent = '×'; + + var list = document.createElement('ul'); + list.id = id + '-list'; + list.className = 'combobox-list'; + list.setAttribute('role', 'listbox'); + list.hidden = true; + + var status = document.createElement('div'); + status.className = 'combobox-status'; + status.setAttribute('role', 'status'); + status.setAttribute('aria-live', 'polite'); + + wrapper.appendChild(input); + wrapper.appendChild(clear); + wrapper.appendChild(list); + wrapper.appendChild(status); + + // The label points at the hidden select; send clicks to the text box instead + var label = select.id ? document.querySelector('label[for="' + select.id + '"]') : null; + if (label) { + label.setAttribute('data-combobox-for', select.id); + label.setAttribute('for', input.id); + list.setAttribute('aria-label', label.textContent.replace(/[:*\s]+$/, '')); + } + + var items = []; // what the list is showing: [{value, label}] + var active = -1; // index of the highlighted item + var timer = null; + var request = null; + + function selectedLabel() { + var option = select.options[select.selectedIndex]; + return option && option.value !== '' ? option.text : ''; + } + + function syncFromSelect() { + input.value = selectedLabel(); + clear.hidden = select.value === '' || select.disabled; + } + + function isOpen() { return !list.hidden; } + + function setActive(index) { + var nodes = list.querySelectorAll('[role="option"]'); + if (active >= 0 && nodes[active]) { + nodes[active].removeAttribute('aria-selected'); + nodes[active].classList.remove('is-active'); + } + active = index; + if (active >= 0 && nodes[active]) { + nodes[active].setAttribute('aria-selected', 'true'); + nodes[active].classList.add('is-active'); + input.setAttribute('aria-activedescendant', nodes[active].id); + nodes[active].scrollIntoView({ block: 'nearest' }); + } else { + input.removeAttribute('aria-activedescendant'); + } + } + + function open() { + list.hidden = false; + input.setAttribute('aria-expanded', 'true'); + } + + function close() { + list.hidden = true; + input.setAttribute('aria-expanded', 'false'); + input.removeAttribute('aria-activedescendant'); + active = -1; + if (request) { request.abort(); request = null; } + clearTimeout(timer); + } + + function render(newItems, message) { + items = newItems; + list.textContent = ''; + newItems.forEach(function (item, index) { + var li = document.createElement('li'); + li.id = id + '-option-' + index; + li.className = 'combobox-option'; + li.setAttribute('role', 'option'); + li.dataset.index = index; + li.textContent = item.label; + if (item.value === select.value) { li.classList.add('is-current'); } + list.appendChild(li); + }); + if (message) { + var note = document.createElement('li'); + note.className = 'combobox-message'; + note.setAttribute('role', 'presentation'); + note.textContent = message; + list.appendChild(note); + } + active = -1; + input.removeAttribute('aria-activedescendant'); + status.textContent = newItems.length + ? newItems.length + (newItems.length === 1 ? ' result' : ' results') + ' available' + : (message || ''); + open(); + } + + function localOptions() { + return Array.prototype.slice.call(select.options) + .filter(function (o) { return o.value !== ''; }) + .map(function (o) { return { value: o.value, label: o.text }; }); + } + + function showLocal(text) { + var words = text.toLowerCase().split(/\s+/).filter(Boolean); + var matches = localOptions().filter(function (item) { + var haystack = item.label.toLowerCase(); + return words.every(function (w) { return haystack.indexOf(w) !== -1; }); + }); + var shown = matches.slice(0, LOCAL_LIMIT); + var message = null; + if (!matches.length) { message = 'No matches'; } + else if (matches.length > shown.length) { message = 'Keep typing to narrow the ' + matches.length + ' matches'; } + render(shown, message); + } + + function showRemote(text) { + clearTimeout(timer); + if (request) { request.abort(); request = null; } + if (!text.trim()) { + render([], 'Type a sample name, passage or aliquot number'); + return; + } + timer = setTimeout(function () { + request = typeof AbortController === 'function' ? new AbortController() : null; + fetch(searchUrl + (searchUrl.indexOf('?') === -1 ? '?' : '&') + 'q=' + encodeURIComponent(text), { + credentials: 'same-origin', + headers: { 'X-Requested-With': 'XMLHttpRequest' }, + signal: request ? request.signal : undefined + }).then(function (response) { + if (!response.ok) { throw new Error('bad status'); } + return response.json(); + }).then(function (data) { + var found = data.results.map(function (r) { return { value: String(r.id), label: r.label }; }); + render(found, !found.length ? 'No matches' : (data.more ? 'Keep typing to narrow the results' : null)); + }).catch(function (error) { + if (error && error.name === 'AbortError') { return; } + render([], 'Search is unavailable. Try again.'); + }); + }, DEBOUNCE_MS); + } + + function search(text) { + if (searchUrl) { showRemote(text); } else { showLocal(text); } + } + + function choose(item) { + if (searchUrl) { + // The select only holds the current choice, so replace it + while (select.options.length > 1) { select.remove(1); } + select.add(new Option(item.label, item.value, true, true)); + } + select.value = item.value; + close(); + syncFromSelect(); + select.dispatchEvent(new Event('change', { bubbles: true })); + } + + function clearChoice() { + if (searchUrl) { while (select.options.length > 1) { select.remove(1); } } + select.value = ''; + syncFromSelect(); + select.dispatchEvent(new Event('change', { bubbles: true })); + input.focus(); + } + + input.addEventListener('focus', function () { + input.select(); + // Browsing: show everything for a local list; a remote list waits for typing + if (searchUrl) { render([], 'Type a sample name, passage or aliquot number'); } else { showLocal(''); } + }); + + input.addEventListener('input', function () { search(input.value); }); + + input.addEventListener('keydown', function (event) { + var count = items.length; + if (event.key === 'ArrowDown') { + event.preventDefault(); + if (!isOpen()) { search(input.value === selectedLabel() ? '' : input.value); return; } + if (count) { setActive((active + 1) % count); } + } else if (event.key === 'ArrowUp') { + event.preventDefault(); + if (isOpen() && count) { setActive(active <= 0 ? count - 1 : active - 1); } + } else if (event.key === 'Enter') { + if (isOpen() && active >= 0 && items[active]) { + event.preventDefault(); // choosing must not also submit the form + choose(items[active]); + } else if (isOpen() && count === 1) { + event.preventDefault(); + choose(items[0]); + } + } else if (event.key === 'Escape') { + if (isOpen()) { + event.preventDefault(); + event.stopPropagation(); + close(); + syncFromSelect(); + } + } + }); + + // Keep focus in the text box while the list is clicked, so blur does not close it first + list.addEventListener('mousedown', function (event) { event.preventDefault(); }); + list.addEventListener('click', function (event) { + var node = event.target.closest('[role="option"]'); + if (node && items[node.dataset.index]) { choose(items[node.dataset.index]); } + }); + list.addEventListener('mousemove', function (event) { + var node = event.target.closest('[role="option"]'); + if (node && Number(node.dataset.index) !== active) { setActive(Number(node.dataset.index)); } + }); + clear.addEventListener('click', clearChoice); + + input.addEventListener('blur', function () { + close(); + syncFromSelect(); // text that was typed but not chosen is dropped + }); + + // Another script changed the select (for example create.html clearing the box field) + select.addEventListener('change', syncFromSelect); + if (select.form) { + select.form.addEventListener('reset', function () { setTimeout(syncFromSelect, 0); }); + } + + syncFromSelect(); + } + + function initAll(root) { + Array.prototype.forEach.call((root || document).querySelectorAll('select[data-combobox]'), init); + } + + window.krillCombobox = { init: init, initAll: initAll }; + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', function () { initAll(); }); + } else { + initAll(); + } +})(); diff --git a/krill/static/krill/style.css b/krill/static/krill/style.css index 8f76829..086c2b5 100644 --- a/krill/static/krill/style.css +++ b/krill/static/krill/style.css @@ -2672,6 +2672,101 @@ main table tbody tr:last-child td{ gap: 0.5rem; } +/* Type-to-filter dropdown (static/krill/js/combobox.js) */ +.combobox { + position: relative; +} + +/* Keep the real