Skip to content
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
4 changes: 2 additions & 2 deletions krill/sample/tests/test_checked_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _list(self, **params):

def test_pills_start_off_with_aria_state(self):
response = self._list()
self.assertContains(response, 'aria-label="Checked out by me, off. Select to turn on"')
self.assertContains(response, 'aria-label="Checked out by me, 0 aliquots, off. Select to turn on"')
self.assertContains(response, 'aria-label="Hide exhausted, off. Select to turn on"')
self.assertNotContains(response, 'toggle-pill on')

Expand Down Expand Up @@ -163,7 +163,7 @@ def test_checked_out_pill_counts_my_in_use_aliquots_only(self):
checked_out_by=owner, checked_out_at=timezone.now())
response = self._list()
self.assertEqual(response.context['checked_out_count'], 2)
self.assertContains(response, 'Checked out by me · 2')
self.assertContains(response, 'Checked out by me · 2')

def test_search_button_has_aria_label_and_singular_count(self):
response = self._list(q='Test Sample')
Expand Down
145 changes: 145 additions & 0 deletions krill/sample/tests/test_results_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import re

from django.contrib.auth import get_user_model
from django.contrib.staticfiles import finders
from django.urls import reverse
from django.utils import timezone

from ..models.aliquot import Aliquot, AliquotDisposition
from .test_views import SampleViewTest

User = get_user_model()


class ResultsBarTest(SampleViewTest):
"""The pills live in the bar directly above the table, with the count and view toggle at the right."""

def setUp(self):
super().setUp()
self.client.force_login(self.user)
self.url = reverse('sample:sample_list')
self.in_use = AliquotDisposition.objects.get_or_create(name='In Use', defaults={'disposition_type': 'in_use'})[0]

def html(self, **params):
return self.client.get(self.url, {'type': 'aliquot', **params}).content.decode()

@staticmethod
def bar_of(html):
"""The results bar: from its id to the table, or to the empty-state block when nothing matches."""
start = html.index('id="results-bar"')
ends = [html.index(marker, start) for marker in ('<!-- Table view -->', 'class="no-results"') if marker in html[start:]]
return html[start:min(ends)]

def bar(self, **params):
return self.bar_of(self.html(**params))

def checked_out(self, n=2):
for _ in range(n):
Aliquot.objects.create(sample=self.sample, disposition=self.in_use,
checked_out_by=self.user, checked_out_at=timezone.now())

def test_the_results_bar_has_an_id(self):
self.assertEqual(self.html().count('id="results-bar"'), 1)

def test_the_pills_are_in_the_results_bar_not_the_toolbar(self):
html = self.html()
bar_start = html.index('id="results-bar"')
toolbar = html[html.index('class="toolbar-row"'):bar_start]
self.assertNotIn('toggle-pill', toolbar)
bar = self.bar_of(html)
self.assertEqual(len(re.findall(r'class="toggle-pill(?: on)?"', bar)), 2)
self.assertIn('Hide exhausted', bar)
self.assertIn('Checked out by me', bar)

def test_pills_come_first_then_the_count_and_the_view_toggle(self):
bar = self.bar()
order = ['toggle-pills', 'result-count', 'view-toggle']
positions = [bar.index(token) for token in order]
self.assertEqual(positions, sorted(positions))

def test_the_toggle_links_keep_the_page_in_place_with_an_anchor(self):
response = self.client.get(self.url, {'type': 'aliquot', 'q': 'x', 'sort': 'passage', 'page': '1'})
for key in ('checked_out_toggle_url', 'hide_finished_toggle_url'):
with self.subTest(link=key):
url = response.context[key]
self.assertTrue(url.endswith('#results-bar'), url)
self.assertIn('q=x', url) # still keeps the other parameters
self.assertIn('sort=passage', url)
self.assertNotIn('page=', url) # and still goes back to page 1
html = response.content.decode()
self.assertRegex(html, r'href="\?[^"]*checked_out_by=me[^"]*#results-bar"')
self.assertRegex(html, r'href="\?[^"]*hide_finished=1[^"]*#results-bar"')

def test_turning_a_pill_off_keeps_the_list_type_and_the_anchor(self):
response = self.client.get(self.url, {'type': 'aliquot', 'hide_finished': '1'})
self.assertEqual(response.context['hide_finished_toggle_url'], '?type=aliquot#results-bar')

def test_the_count_shows_in_the_checked_out_pill_only_while_it_is_off(self):
self.checked_out(2)
off = self.bar()
self.assertRegex(off, r'Checked out by me &middot; 2')
on = self.bar(checked_out_by='me')
pill = on[on.index('Checked out by me'):]
pill = pill[:pill.index('</a>')]
self.assertNotIn('&middot;', pill)
self.assertNotRegex(pill, r'\d')

def test_the_hidden_count_is_still_in_the_accessible_label(self):
self.checked_out(1)
self.assertIn('aria-label="Checked out by me, 1 aliquot, off. Select to turn on"', self.bar())
self.checked_out(1)
self.assertIn('aria-label="Checked out by me, 2 aliquots, off. Select to turn on"', self.bar())
self.assertIn('aria-label="Checked out by me, on. Select to turn off"', self.bar(checked_out_by='me'))

def test_chips_sit_between_the_toolbar_and_the_results_bar_and_only_when_active(self):
html = self.html(f_sample='test', f_passage='p8')
positions = [html.index('class="toolbar-row"'), html.index('class="filter-chips"'),
html.index('id="results-bar"')]
self.assertEqual(positions, sorted(positions))
self.assertNotIn('class="filter-chips"', self.html())

def test_clear_all_is_in_the_chips_row_not_the_results_bar(self):
html = self.html(f_sample='test', f_passage='p8') # matches nothing, so no table is rendered
chips = html[html.index('class="filter-chips"'):html.index('id="results-bar"')]
self.assertIn('Clear all', chips)
self.assertNotIn('Clear all', self.bar_of(html))

def test_the_samples_list_keeps_its_own_pill_where_it_was(self):
html = self.client.get(self.url).content.decode()
self.assertNotIn('toggle-pill', self.bar_of(html)) # no pills in the Samples results bar
self.assertIn('Low count &middot;', html) # the Low count pill is still on the page

def test_the_bar_exists_on_every_list_for_the_anchor_and_count(self):
for params in ({}, {'type': 'source'}, {'type': 'aliquot-type'}):
with self.subTest(params=params):
html = self.client.get(self.url, params).content.decode()
self.assertIn('id="results-bar"', html)
self.assertIn('result-count', html)


class ResultsBarStyleTest(SampleViewTest):
def setUp(self):
super().setUp()
self.client.force_login(self.user)

def test_pills_are_at_least_32px_tall(self):
css = open(finders.find('style.css') or finders.find('krill/style.css')).read()
rule = css[css.index('.toggle-pill {'):]
rule = rule[:rule.index('}')]
self.assertIn('min-height: 32px', rule)

def test_the_bar_is_a_wrapping_row_centred_with_pills_left_and_info_right(self):
html = self.client.get(reverse('sample:sample_list'), {'type': 'aliquot'}).content.decode()
rule = html[html.index('.results-header {'):]
rule = rule[:rule.index('}')]
for needed in ('display: flex', 'justify-content: space-between', 'align-items: center', 'flex-wrap: wrap'):
with self.subTest(declaration=needed):
self.assertIn(needed, rule)
info = html[html.index('.results-info {'):]
self.assertIn('margin-left: auto', info[:info.index('}')])

def test_narrow_screens_wrap_instead_of_stacking_into_a_column(self):
html = self.client.get(reverse('sample:sample_list'), {'type': 'aliquot'}).content.decode()
media = html[html.index('@media (max-width: 768px)'):]
media = media[:media.index('\n}\n')]
self.assertNotRegex(media, r'\.results-header\s*\{[^}]*flex-direction:\s*column')
14 changes: 9 additions & 5 deletions krill/sample/views/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,20 @@ def _checked_out_by_everyone(self):
return False
return UserRole.get_or_create_for_user(self.request.user).role in ('lab_manager', 'lab_admin')

def _toggle_url(self, param, value):
"""Link that flips one yes/no filter, keeps every other parameter, and goes back to page 1."""
def _toggle_url(self, param, value, anchor=''):
"""Link that flips one yes/no filter, keeps every other parameter, and goes back to page 1.

With an anchor the link ends in #anchor, so the page keeps its place after the toggle.
"""
params = self.request.GET.copy()
params.pop('page', None)
if params.get(param) == value:
params.pop(param)
else:
params[param] = value
query = params.urlencode()
return f'?{query}' if query else self.request.path
url = f'?{query}' if query else self.request.path
return f'{url}#{anchor}' if anchor else url

def get_paginate_by(self, queryset):
if self.request.GET.get('page_size') == '20':
Expand Down Expand Up @@ -225,8 +229,8 @@ def get_context_data(self, **kwargs):
context['hide_finished'] = self.request.GET.get('hide_finished') == '1'
context['checked_out_by_me'] = self.request.GET.get('checked_out_by') == 'me'
context['checked_out_all'] = self._checked_out_by_everyone()
context['checked_out_toggle_url'] = self._toggle_url('checked_out_by', 'me')
context['hide_finished_toggle_url'] = self._toggle_url('hide_finished', '1')
context['checked_out_toggle_url'] = self._toggle_url('checked_out_by', 'me', anchor='results-bar')
context['hide_finished_toggle_url'] = self._toggle_url('hide_finished', '1', anchor='results-bar')
context['checked_out_count'] = Aliquot.objects.filter(
checked_out_by=self.request.user,
disposition__disposition_type='in_use',
Expand Down
6 changes: 6 additions & 0 deletions krill/static/krill/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2832,6 +2832,8 @@ main table tbody tr:last-child td{
.toggle-pill {
display: inline-flex;
align-items: center;
box-sizing: border-box;
min-height: 32px;
gap: 0.25rem;
padding: 0.3rem 0.8rem;
border: 1px solid var(--color-gray-border);
Expand All @@ -2845,6 +2847,10 @@ main table tbody tr:last-child td{
white-space: nowrap;
}

.results-header .toggle-pills {
margin-top: 0;
}

.toggle-pill:hover {
border-color: var(--color-primary);
}
Expand Down
45 changes: 23 additions & 22 deletions krill/templates/sample/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ <h1>{{ model_name }}</h1>
{% include 'sample/_filter_fields.html' with fields=filter_bar.more variant='panel' %}
</div>
</form>
<div class="toggle-pills">
<a href="{{ checked_out_toggle_url }}" class="toggle-pill{% if checked_out_by_me %} on{% endif %}"
aria-label="Checked out by me, {% if checked_out_by_me %}on. Select to turn off{% else %}off. Select to turn on{% endif %}">
{% if checked_out_by_me %}<span class="material-icons-round">check</span>{% endif %}
Checked out by me · {{ checked_out_count }}
</a>
<a href="{{ hide_finished_toggle_url }}" class="toggle-pill{% if hide_finished %} on{% endif %}"
aria-label="Hide exhausted, {% if hide_finished %}on. Select to turn off{% else %}off. Select to turn on{% endif %}">
{% if hide_finished %}<span class="material-icons-round">check</span>{% endif %}
Hide exhausted
</a>
</div>
{% include 'sample/_filter_chips.html' %}
</div>
{% else %}
Expand Down Expand Up @@ -131,7 +119,21 @@ <h1>{{ model_name }}</h1>

<!-- Results -->
<div class="search-results">
<div class="results-header">
<div class="results-header" id="results-bar">
{% if model_type == 'aliquot' %}
<div class="toggle-pills results-pills">
<a href="{{ checked_out_toggle_url }}" class="toggle-pill{% if checked_out_by_me %} on{% endif %}"
aria-label="Checked out by me, {% if checked_out_by_me %}on. Select to turn off{% else %}{{ checked_out_count }} aliquot{{ checked_out_count|pluralize }}, off. Select to turn on{% endif %}">
{% if checked_out_by_me %}<span class="material-icons-round">check</span>{% endif %}
Checked out by me{% if not checked_out_by_me %} &middot; {{ checked_out_count }}{% endif %}
</a>
<a href="{{ hide_finished_toggle_url }}" class="toggle-pill{% if hide_finished %} on{% endif %}"
aria-label="Hide exhausted, {% if hide_finished %}on. Select to turn off{% else %}off. Select to turn on{% endif %}">
{% if hide_finished %}<span class="material-icons-round">check</span>{% endif %}
Hide exhausted
</a>
</div>
{% endif %}
<div class="results-info">
<span class="result-count">{{ paginator.count }} {% if paginator.count == 1 %}{{ model_name|singular|lower }}{% else %}{{ model_name|lower }}{% endif %} found</span>
{% if search_query %}
Expand Down Expand Up @@ -614,8 +616,11 @@ <h3>No {{ model_name|lower }} found</h3>
padding: 20px;
border-bottom: 1px solid var(--color-light);
display: flex;
justify-content: flex-end; /* the page heading above already names the list */
align-items: center;
justify-content: space-between;
align-items: center; /* pills, count and the view toggle share one centre line */
flex-wrap: wrap;
gap: 12px 20px;
scroll-margin-top: 1rem; /* the toggle links jump here; leave a little room above */
}

.results-header h2 {
Expand All @@ -625,7 +630,9 @@ <h3>No {{ model_name|lower }} found</h3>

.results-info {
display: flex;
gap: 20px;
flex-wrap: wrap;
gap: 8px 20px;
margin-left: auto; /* always at the right, with or without pills on the left */
color: var(--color-dark-variant);
font-size: 0.9rem;
align-items: center;
Expand Down Expand Up @@ -753,12 +760,6 @@ <h3>No {{ model_name|lower }} found</h3>
grid-template-columns: 1fr;
}

.results-header {
flex-direction: column;
gap: 15px;
align-items: flex-start;
}

.form-actions {
flex-direction: column;
}
Expand Down
Loading