Renamed the page_links and page_sizes properties to pages and page_size_options in the Pagination utility.

This commit is contained in:
依瑪貓 2023-02-04 12:51:30 +08:00
parent fd63149066
commit 16b2eb1c93
2 changed files with 19 additions and 19 deletions

View File

@ -22,7 +22,7 @@ First written: 2023/1/26
{% if pagination.is_paged %} {% if pagination.is_paged %}
<nav aria-label="Page navigation"> <nav aria-label="Page navigation">
<ul class="pagination"> <ul class="pagination">
{% for link in pagination.page_links %} {% for link in pagination.pages %}
{% if link.uri is none %} {% if link.uri is none %}
<li class="page-item disabled {% if not link.is_for_mobile %} d-none d-md-inline {% endif %}"> <li class="page-item disabled {% if not link.is_for_mobile %} d-none d-md-inline {% endif %}">
<span class="page-link"> <span class="page-link">
@ -42,7 +42,7 @@ First written: 2023/1/26
{{ pagination.page_size }} {{ pagination.page_size }}
</div> </div>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
{% for link in pagination.page_sizes %} {% for link in pagination.page_size_options %}
<li> <li>
<a class="dropdown-item {% if link.is_current %} active {% endif %}" href="{{ link.uri }}"> <a class="dropdown-item {% if link.is_current %} active {% endif %}" href="{{ link.uri }}">
{{ link.text }} {{ link.text }}

View File

@ -84,10 +84,10 @@ class Pagination(t.Generic[T]):
"""Whether there should be pagination.""" """Whether there should be pagination."""
self.list: list[T] = pagination.list self.list: list[T] = pagination.list
"""The items shown in the list""" """The items shown in the list"""
self.page_links: list[Link] = pagination.page_links self.pages: list[Link] = pagination.pages
"""The pagination links.""" """The pages."""
self.page_sizes: list[Link] = pagination.page_sizes self.page_size_options: list[Link] = pagination.page_size_options
"""The links to switch the number of items in a page.""" """The options to the number of items in a page."""
class AbstractPagination(t.Generic[T]): class AbstractPagination(t.Generic[T]):
@ -99,10 +99,10 @@ class AbstractPagination(t.Generic[T]):
"""Whether there should be pagination.""" """Whether there should be pagination."""
self.list: list[T] = [] self.list: list[T] = []
"""The items shown in the list""" """The items shown in the list"""
self.page_links: list[Link] = [] self.pages: list[Link] = []
"""The pagination links.""" """The pages."""
self.page_sizes: list[Link] = [] self.page_size_options: list[Link] = []
"""The links to switch the number of items in a page.""" """The options to the number of items in a page."""
class EmptyPagination(AbstractPagination[T]): class EmptyPagination(AbstractPagination[T]):
@ -112,8 +112,8 @@ class EmptyPagination(AbstractPagination[T]):
class NonEmptyPagination(AbstractPagination[T]): class NonEmptyPagination(AbstractPagination[T]):
"""The pagination with real data.""" """The pagination with real data."""
AVAILABLE_PAGE_SIZES: list[int] = [10, 100, 200] PAGE_SIZE_OPTIONS: list[int] = [10, 100, 200]
"""The available page sizes.""" """The page size options."""
def __init__(self, items: list[T], is_reversed: bool = False): def __init__(self, items: list[T], is_reversed: bool = False):
"""Constructs the pagination. """Constructs the pagination.
@ -144,8 +144,8 @@ class NonEmptyPagination(AbstractPagination[T]):
if upper_bound > len(items): if upper_bound > len(items):
upper_bound = len(items) upper_bound = len(items)
self.list = items[lower_bound:upper_bound] self.list = items[lower_bound:upper_bound]
self.page_links = self.__get_page_links() self.pages = self.__get_pages()
self.page_sizes = self.__get_page_sizes() self.page_size_options = self.__get_page_size_options()
def __get_page_size(self) -> int: def __get_page_size(self) -> int:
"""Returns the page size. """Returns the page size.
@ -188,7 +188,7 @@ class NonEmptyPagination(AbstractPagination[T]):
str(self.__total_pages))) str(self.__total_pages)))
return page_no return page_no
def __get_page_links(self) -> list[Link]: def __get_pages(self) -> list[Link]:
"""Returns the page links in the pagination navigation. """Returns the page links in the pagination navigation.
:return: The page links in the pagination navigation. :return: The page links in the pagination navigation.
@ -265,16 +265,16 @@ class NonEmptyPagination(AbstractPagination[T]):
return self.__uri_set("page-no", None) return self.__uri_set("page-no", None)
return self.__uri_set("page-no", str(page_no)) return self.__uri_set("page-no", str(page_no))
def __get_page_sizes(self) -> list[Link]: def __get_page_size_options(self) -> list[Link]:
"""Returns the available page sizes. """Returns the page size options.
:return: The available page sizes. :return: The page size options.
""" """
if not self.is_paged: if not self.is_paged:
return [] return []
return [Link(str(x), self.__uri_size(x), return [Link(str(x), self.__uri_size(x),
is_current=x == self.__page_size) is_current=x == self.__page_size)
for x in self.AVAILABLE_PAGE_SIZES] for x in self.PAGE_SIZE_OPTIONS]
def __uri_size(self, page_size: int) -> str: def __uri_size(self, page_size: int) -> str:
"""Returns the URI of a page size. """Returns the URI of a page size.