diff --git a/wagtail/admin/templates/wagtailadmin/pages/listing/_table_headers_explore.html b/wagtail/admin/templates/wagtailadmin/pages/listing/_table_headers_explore.html index 2af07e545..2d52120c5 100644 --- a/wagtail/admin/templates/wagtailadmin/pages/listing/_table_headers_explore.html +++ b/wagtail/admin/templates/wagtailadmin/pages/listing/_table_headers_explore.html @@ -29,43 +29,31 @@ ordering: the current sort parameter {% endif %} - {% if sortable %} - - {% trans 'Title' %} - - {% else %} - {% trans 'Title' %} - {% endif %} + {% trans 'Title' as title_label %} + {% table_header_label label=title_label sortable=sortable sort_field='title' %} {% if show_parent %} - {% trans 'Parent' %} + + {% trans 'Parent' as parent_label %} + {% table_header_label label=parent_label sortable=0 %} + {% endif %} - {% if sortable %} - - {% trans 'Updated' %} - - {% else %} - {% trans 'Updated' %} - {% endif %} + {% trans 'Updated' as updated_label %} + {% table_header_label label=updated_label sortable=sortable sort_field='latest_revision_created_at' %} + {% trans 'Type' as type_label %} + {% if sortable and sortable_by_type %} - - {% trans 'Type' %} - + {% table_header_label label=type_label sortable=1 sort_field='content_type' %} {% else %} - {% trans 'Type' %} + {% table_header_label label=type_label sortable=0 %} {% endif %} - {% if sortable %} - - {% trans 'Status' %} - - {% else %} - {% trans 'Status' %} - {% endif %} + {% trans 'Status' as status_label %} + {% table_header_label label=status_label sortable=sortable sort_field='live' %} diff --git a/wagtail/admin/templatetags/wagtailadmin_tags.py b/wagtail/admin/templatetags/wagtailadmin_tags.py index 0359e3ad5..576ced02c 100644 --- a/wagtail/admin/templatetags/wagtailadmin_tags.py +++ b/wagtail/admin/templatetags/wagtailadmin_tags.py @@ -8,7 +8,7 @@ from django.contrib.messages.constants import DEFAULT_TAGS as MESSAGE_TAGS from django.template.defaultfilters import stringfilter from django.template.loader import render_to_string from django.templatetags.static import static -from django.utils.html import conditional_escape +from django.utils.html import conditional_escape, format_html from django.utils.safestring import mark_safe from wagtail.admin.menu import admin_menu @@ -289,6 +289,52 @@ def querystring(context, **kwargs): return '?' + querydict.urlencode() +@register.simple_tag(takes_context=True) +def table_header_label(context, label=None, sortable=True, ordering=None, sort_context_var='ordering', sort_param='ordering', sort_field=None): + """ + A label to go in a table header cell, optionally with a 'sort' link that alternates between + forward and reverse sorting + + label = label text + ordering = current active ordering. If not specified, we will fetch it from the template context variable + given by sort_context_var. (We don't fetch it from the URL because that wouldn't give the view method + the opportunity to set a default) + sort_param = URL parameter that indicates the current active ordering + sort_field = the value for sort_param that indicates that sorting is currently on this column. + For example, if sort_param='ordering' and sort_field='title', then a URL parameter of + ordering=title indicates that the listing is ordered forwards on this column, and a URL parameter + of ordering=-title indicated that the listing is ordered in reverse on this column + To disable sorting on this column, set sortable=False or leave sort_field unspecified. + """ + if not sortable or not sort_field: + # render label without a sort link + return label + + if ordering is None: + ordering = context.get(sort_context_var) + reverse_sort_field = "-%s" % sort_field + + if ordering == sort_field: + # currently ordering forwards on this column; link should change to reverse ordering + url = querystring(context, **{sort_param: reverse_sort_field}) + classname = "icon icon-arrow-down-after teal" + + elif ordering == reverse_sort_field: + # currently ordering backwards on this column; link should change to forward ordering + url = querystring(context, **{sort_param: sort_field}) + classname = "icon icon-arrow-up-after teal" + + else: + # not currently ordering on this column; link should change to forward ordering + url = querystring(context, **{sort_param: sort_field}) + classname = "icon icon-arrow-down-after" + + return format_html( + '{label}', + url=url, classname=classname, label=label + ) + + @register.simple_tag(takes_context=True) def pagination_querystring(context, page_number, page_key=DEFAULT_PAGE_KEY): """