-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from GSA/add-pagination
Adds Pagination and Partial SPA-like page rerendering
- Loading branch information
Showing
31 changed files
with
1,055 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import math | ||
|
||
from database.interface import PAGINATE_ENTRIES_PER_PAGE | ||
|
||
|
||
class Pagination: | ||
def __init__(self, current: int = 1, count: int = 1): | ||
self.current = current | ||
self.count = count | ||
self.page_count = math.ceil(count / PAGINATE_ENTRIES_PER_PAGE) | ||
self.per_page = PAGINATE_ENTRIES_PER_PAGE | ||
|
||
def to_dict(self): | ||
return { | ||
"current": self.current, | ||
"count": self.count, | ||
"page_count": self.page_count, | ||
"page_label": "Page", | ||
"per_page": self.per_page, | ||
"next": {"label": "Next"}, | ||
"previous": {"label": "Previous"}, | ||
"last_item": { | ||
"label": "Last page", | ||
}, | ||
} | ||
|
||
def update_current(self, current: int) -> dict: | ||
self.current = int(current) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,9 @@ ul.menu { | |
.usa-card__img img { | ||
padding: 10px; | ||
} | ||
|
||
.usa-pagination { | ||
&__item { | ||
cursor: pointer; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{% from 'components/pagination/pagination_arrow.html' import pagination_arrow %} | ||
|
||
{% set overflow %} | ||
<li class="usa-pagination__item usa-pagination__overflow" aria-label="ellipsis indicating non-visible pages"> | ||
<span> | ||
… | ||
</span> | ||
</li> | ||
{% endset %} | ||
|
||
<nav aria-label="Pagination" class="usa-pagination"> | ||
<ul class="usa-pagination__list"> | ||
{% if pagination['page_count'] > 7 and pagination['current'] > 1 %} | ||
{{ pagination_arrow('previous', pagination, data.htmx_vars) }} | ||
{% endif %} | ||
|
||
{% include "components/pagination/pagination_numbers.html" %} | ||
|
||
{% if pagination['page_count'] > 7 and pagination['current'] < pagination['page_count'] %} | ||
{{ pagination_arrow('next', pagination, data.htmx_vars) }} | ||
{% endif %} | ||
</ul> | ||
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{# | ||
The full pagination data object is passed so we can access current state, aria labels, and text labels. | ||
#} | ||
{% macro pagination_arrow(direction, pagination, htmx_vars) %} | ||
{% set page_var = ((pagination.current - 1) if direction == 'previous' else (pagination.current + 1)) | string() %} | ||
{% set placeholder_link = htmx_vars.endpoint_url + "?page=" + page_var %} | ||
|
||
{% set link_attrs = { | ||
'class': 'usa-pagination__link usa-pagination__' ~ direction ~ '-page', | ||
'aria_label': pagination[direction]['label'] ~ ' ' ~ pagination.page_label | lower | ||
} %} | ||
|
||
<li class="usa-pagination__item usa-pagination__arrow"> | ||
<a | ||
hx-get="{{ placeholder_link | default("javascript:void(0);") }}" | ||
hx-target="{{htmx_vars.target_div}}" | ||
class="{{ link_attrs.class }}" | ||
aria-label="{{ link_attrs.aria_label }}" | ||
> | ||
{% if direction == 'previous' %} | ||
<svg class="usa-icon" aria-hidden="true" role="img"> | ||
{# Global variable not applying #} | ||
<use xlink:href="/assets/uswds/img/sprite.svg#navigate_before"></use> | ||
</svg> | ||
{% endif %} | ||
<span class="usa-pagination__link-text"> | ||
{{ pagination[direction]['label'] }} | ||
</span> | ||
{% if direction == 'next' %} | ||
<svg class="usa-icon" aria-hidden="true" role="img"> | ||
{# Global variable not applying #} | ||
<use xlink:href="/assets/uswds/img/sprite.svg#navigate_next"></use> | ||
</svg> | ||
{% endif %} | ||
</a> | ||
</li> | ||
{% endmacro %} |
23 changes: 23 additions & 0 deletions
23
app/templates/components/pagination/pagination_button.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{% macro pagination_button(item, pager_opts, htmx_vars) %} | ||
{% set is_current = (item == pager_opts.current) %} | ||
{% set is_last = (item == pager_opts.total) %} | ||
{% set labels = pager_opts.aria_labels %} | ||
{# HTMX page vars#} | ||
{% set item_str = item | string() %} | ||
{% set placeholder_link = htmx_vars.endpoint_url + "?page=" + item_str %} | ||
|
||
{# Display: "Last page, page X" if last item. Otherwise "Page X" #} | ||
{% set aria_label = (labels.last ~ " " ~ labels.page_label | lower if is_last else labels.page_label) ~ " " ~ item %} | ||
|
||
<li class="usa-pagination__item usa-pagination__page-no"> | ||
{# Global variable placeholder_link doesn't work for some reason. #} | ||
<a | ||
hx-get="{{ placeholder_link | default("javascript:void(0);") }}" | ||
hx-target="{{htmx_vars.target_div}}" | ||
class="usa-pagination__button {{ "usa-current" if is_current}}" | ||
aria-label="{{ aria_label }}" | ||
{% if is_current %}aria-current="page"{% endif %}> | ||
{{ item }} | ||
</a> | ||
</li> | ||
{% endmacro %} |
65 changes: 65 additions & 0 deletions
65
app/templates/components/pagination/pagination_numbers.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{% from "components/pagination/pagination_button.html" import pagination_button %} | ||
|
||
{# Add +1 to first_five / last_five due to how ranges work in jinja #} | ||
{% set pager_ranges = { | ||
'default': range(pagination.current - 1, pagination.current + 1), | ||
'last_item': pagination.page_count, | ||
'first_five': range(1, 5 + 1), | ||
'last_five': range(pagination.page_count - 4, pagination.page_count + 1), | ||
} | ||
%} | ||
|
||
{% set pager_button_opts = { | ||
'current': pagination.current, | ||
'total': pagination.page_count, | ||
'aria_labels': { | ||
'page_label': pagination.page_label, | ||
'previous': pagination.previous.label, | ||
'next': pagination.next.label, | ||
'last': pagination.last_item.label | ||
} | ||
} %} | ||
|
||
{# Page numbers #} | ||
{# List all items if less than 7 #} | ||
{% if pagination.page_count <= 7 %} | ||
{% for item in range(1, pagination.page_count) %} | ||
{{ pagination_button(item, pager_button_opts, data.htmx_vars) }} | ||
{% endfor %} | ||
{# User is at the start of a long dataset #} | ||
{# Example: 1, 2, 3, *4*, 5 … 8 #} | ||
{# Doesn't apply when user gets to 5 of 8 #} | ||
{% elif pagination.current <= 4 and pagination.page_count >= 7 %} | ||
{% for item in pager_ranges.first_five %} | ||
{{ pagination_button(item, pager_button_opts, data.htmx_vars) }} | ||
{% endfor %} | ||
|
||
{{ overflow | trim | safe }} | ||
|
||
{{ pagination_button(pager_ranges.last_item, pager_button_opts, data.htmx_vars) }} | ||
|
||
{# When user is close to the end of dataset #} | ||
{# Example: 1 … 4, *5*, 6, 7, 8 #} | ||
{% elif pagination.current >= pagination.page_count - 3 %} | ||
{{ pagination_button(1, pager_button_opts, data.htmx_vars) }} | ||
|
||
{{ overflow | trim | safe }} | ||
{% for item in pager_ranges.last_five %} | ||
{{ pagination_button(item, pager_button_opts, data.htmx_vars) }} | ||
{% endfor %} | ||
{# Default case: Current - 1, Current, Current + 1 #} | ||
{# Example: 1 … 21, *22*, 23 … 50 #} | ||
{# Example: 1 … 4, *5*, 6 … 9 #} | ||
{% else %} | ||
{{ pagination_button(1, pager_button_opts, data.htmx_vars) }} | ||
|
||
{{ overflow | trim | safe }} | ||
|
||
{% for item in pager_ranges.default %} | ||
{{ pagination_button(item, pager_button_opts, data.htmx_vars) }} | ||
{% endfor %} | ||
|
||
{{ overflow | trim | safe }} | ||
|
||
{{ pagination_button(pager_ranges.last_item, pager_button_opts, data.htmx_vars) }} | ||
{% endif %} |
Oops, something went wrong.
65a69f4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
65a69f4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
65a69f4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
65a69f4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.