Skip to content

Commit ab4a193

Browse files
committed
chore: add a snippet for search form
1 parent 82d8ff0 commit ab4a193

File tree

21 files changed

+152
-96
lines changed

21 files changed

+152
-96
lines changed

ckanext/theming/themes/bare/templates/macros/ui/component.html

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,9 @@
3030
{%- endmacro %}
3131

3232

33-
{%- macro search_form(query, sorting, sorting_options, query_name="q", sorting_name="sort", hidden_filters=[] ) -%}
34-
{%- set facets = {
35-
'fields': fields_grouped,
36-
'search': search_facets,
37-
'titles': facet_titles,
38-
'translated_fields': translated_fields,
39-
'remove_field': remove_field }
40-
%}
41-
33+
{%- macro search_form(query, sorting, sorting_options, query_error, item_count, first_item_position, last_item_position, facets, facet_titles, active_filters, query_name="q", sorting_name="sort" ) -%}
4234
{%- call ui.util.call(ui.form, **kwargs) -%}
43-
{{ ui.input(query_name, label=_("Search"), value=query, type="search") }}
44-
{{ ui.select(sorting_name, label=_("Order by"), selected=sorting, options=sorting_options) if sorting }}
45-
{{ ui.form_actions(ui.button(_('Search'), type="submit")) }}
46-
47-
{%- for name, value in hidden_filters -%}
48-
{{ ui.hidden_input(name, value=value) }}
49-
{%- endfor %}
50-
35+
{%- include "macros/ui/snippets/search_form.html" -%}
5136
{%- endcall %}
5237
{%- endmacro %}
5338

ckanext/theming/themes/bare/templates/macros/ui/misc.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ <h5>{{ title }}</h5>
4141
</div>
4242
{%- endmacro %}
4343

44-
{%- macro pagination_info(first, last, total, template) -%}
45-
{% set template = template | default(_("Records {first} - {last} out of {total}")) %}
44+
{%- macro pagination_info(total, first, last, template) -%}
45+
{%- if not template and first is undefined or last is undefined -%}
46+
{% set template = ungettext("Found {total} result", "Found {total} results", total) %}
47+
{%- elif not template -%}
48+
{% set template = _("Results {first} - {last} out of {total}") %}
49+
{%- endif -%}
4650
{{ template.format(first=first, last=last, total=total) }}
4751
{%- endmacro %}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{% set sorting = sorting if sorting else [(_('Name Ascending'), 'name asc'), (_('Name Descending'), 'name desc')] %}
2+
3+
{% block search_input %}
4+
{{ ui.input(query_name, label=_("Search"), value=query, type="search") }}
5+
6+
{% block search_input_button %}
7+
{{ ui.button(_('Search'), type="submit") }}
8+
{% endblock %}
9+
{% endblock %}
10+
11+
{% block search_search_fields %}
12+
{%- if active_filters -%}
13+
{%- for name, value in active_filters -%}
14+
{{ ui.hidden_input(name, value=value) }}
15+
{%- endfor %}
16+
{%- endif %}
17+
{% endblock %}
18+
19+
{% block search_sortby %}
20+
{%- if sorting_options -%}
21+
{{ ui.select(name=sorting_name, label=_("Order by"), selected=sorting, options=sorting_options) }}
22+
{% block search_sortby_button %}
23+
{{ ui.button(_('Sort'), type="submit") }}
24+
{% endblock %}
25+
{%- endif %}
26+
{% endblock %}
27+
28+
{% block search_title %}
29+
{%- call ui.util.call(ui.heading, level=2) -%}
30+
{%- if not query_error %}
31+
{%- if item_count %}
32+
{{ ui.pagination_info(item_count, first_item_position, last_item_position) }}
33+
{%- else %}
34+
{{ _('No results found') }}
35+
{%- endif %}
36+
{%- else %}
37+
{{ _('Error: {error}').format(query_error) }}
38+
{%- endif %}
39+
{%- endcall %}
40+
41+
{% endblock %}
42+
43+
{% block search_facets %}
44+
{%- if active_filters -%}
45+
{%- call ui.util.call(ui.listing) -%}
46+
{%- for key, values in active_filters|groupby(0) -%}
47+
<div>
48+
<strong>{{ facet_titles.get(key, key) }}:</strong>
49+
{%- for _, value in values %}
50+
<del>
51+
{%- with label=h.list_dict_filter(facets[key]["items"], 'name', 'display_name', value) -%}
52+
{{ ui.link(label, h.remove_url_param(key, value)) }}
53+
{%- endwith %}
54+
</del>
55+
{% endfor %}
56+
</div>
57+
58+
{%- endfor %}
59+
{%- endcall %}
60+
{%- endif %}
61+
{% endblock %}

ckanext/theming/themes/bare/templates/package/search.html

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,8 @@
1212
{"text": _('Last Modified'), "value": "metadata_modified desc"}
1313
] %}
1414

15-
{{ ui.search_form(q, sorting_options=sorting_options, sorting=sort_by_selected, hidden_filters=fields) }}
15+
{{ ui.search_form(q, sorting_options=sorting_options, sorting=sort_by_selected, hidden_filters=fields, facet_titles=facet_titles, facets=search_facets, active_filters=fields, item_count=page.item_count, first_item_position=page.first_item, last_item_position=page.last_item) }}
1616

17-
{%- call ui.util.call(ui.listing) -%}
18-
{%- for key, values in fields_grouped.items() -%}
19-
<div>
20-
<strong>{{ facet_titles.get(key, key) }}:</strong>
21-
{%- for value in values %}
22-
<del>
23-
{%- with label=h.list_dict_filter(search_facets[key]["items"], 'name', 'display_name', value) -%}
24-
{{ ui.link(label, h.remove_url_param(key, value)) }}
25-
{%- endwith %}
26-
</del>
27-
{% endfor %}
28-
</div>
29-
30-
{%- endfor %}
31-
{%- endcall %}
32-
<h2>
33-
{%- if not query_error %}
34-
{%- if page.item_count %}
35-
{{ ui.pagination_info(page.first_item, page.last_item, page.item_count) }}
36-
{%- else %}
37-
{{ _('No datasets found') }}
38-
{%- endif %}
39-
{%- else %}
40-
{{ _('Error') }}
41-
{%- endif %}
42-
</h2>
4317
{%- endblock %}
4418

4519
{%- block primary_content_inner %}

ckanext/theming/themes/bare/templates/package/snippets/_package_basic_fields.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{% endblock %}
2323

2424
{% block package_basic_fields_license %}
25-
{{ ui.select("license_id", label=_("License"), selected=data.license_id, errors=errors.license_id, options=h.license_options(existing_license_id)) }}
25+
{{ ui.select(name="license_id", label=_("License"), selected=data.license_id, errors=errors.license_id, options=h.license_options(existing_license_id)) }}
2626

2727
{{ h.license_options(existing_license_id)|map("dict")|list }}
2828
{% endblock %}
@@ -37,7 +37,7 @@
3737
{{ organizations_available|map(attribute="name")|list }}
3838
{% if organizations_available and h.can_update_owner_org(data, organizations_available) %}
3939
{% set existing_org = data.owner_org or data.group_id %}
40-
{{ ui.select("owner_org", label=h.humanize_entity_type('organization', default_group_type, 'default label') or _('Organization'), selected=data.owner_org, errors=errors.owner_org, options=[]) }}
40+
{{ ui.select(name="owner_org", label=h.humanize_entity_type('organization', default_group_type, 'default label') or _('Organization'), selected=data.owner_org, errors=errors.owner_org, options=[]) }}
4141

4242
<div class="form-group control-medium">
4343

ckanext/theming/themes/bs5/templates/macros/ui/component.html

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,6 @@ <h5 class="card-title">{{ res.name or res.description or res.url[:50] ~ '...' }}
7777
{%- endmacro %}
7878

7979
{%- macro search_form(query, sorting, sorting_options, query_name="q", sorting_name="sort", active_filters=[] ) -%}
80-
{%- set facets = {
81-
'fields': fields_grouped,
82-
'search': search_facets,
83-
'titles': facet_titles,
84-
'translated_fields': translated_fields,
85-
'remove_field': remove_field }
86-
%}
87-
8880
{%- call ui.util.call(ui.form, **kwargs) -%}
8981
{{ ui.input(query_name, label=_("Search"), value=query, type="search", class="mb-2") }}
9082
{{ ui.select(sorting_name, label=_("Order by"), selected=sorting, options=sorting_options) if sorting }}

ckanext/theming/themes/bs5/templates/macros/ui/misc.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ <h4 class="alert-heading">{{ title }}</h4>
5454
</div>
5555
{%- endmacro %}
5656

57-
{%- macro pagination_info(first, last, total, template) -%}
58-
{% set template = template | default(_("Records {first} - {last} out of {total}")) %}
57+
{%- macro pagination_info(total, first, last, template) -%}
58+
{%- if not template and first is undefined or last is undefined -%}
59+
{% set template = ungettext("Found {total} result", "Found {total} results", total) %}
60+
{%- elif not template -%}
61+
{% set template = _("Results {first} - {last} out of {total}") %}
62+
{%- endif -%}
5963
{{ template.format(first=first, last=last, total=total) }}
6064
{%- endmacro %}

ckanext/theming/themes/bs5/templates/package/edit.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
{{ ui.form_start(method="POST", action="", attrs={"class": "dataset-form"}) }}
55
{{ h.csrf_input() }}
66

7-
{{ ui.input("name", "field-name", label=_('Name'), value=data.name or "", errors=errors.name or []) }}
8-
{{ ui.input("title", "field-title", label=_('Title'), value=data.title or "", errors=errors.title or []) }}
9-
{{ ui.textarea("notes", "field-notes", label=_('Description'), value=data.notes or "", errors=errors.notes or []) }}
7+
{{ ui.input(name="name", id="field-name", label=_('Name'), value=data.name or "", errors=errors.name or []) }}
8+
{{ ui.input(name="title", id="field-title", label=_('Title'), value=data.title or "", errors=errors.title or []) }}
9+
{{ ui.textarea(name="notes", id="field-notes", label=_('Description'), value=data.notes or "", errors=errors.notes or []) }}
1010

11-
{{ ui.select("license_id", "field-license",
11+
{{ ui.select(name="license_id", id="field-license",
1212
label=_('License'),
1313
options=h.license_options(),
1414
selected=data.license_id) }}

ckanext/theming/themes/bs5/templates/package/new.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
{{ ui.form_start(method="POST", action="", attrs={"class": "dataset-form"}) }}
1717
{{ h.csrf_input() }}
1818

19-
{{ ui.input("name", "field-name", label=_('Name'), value=data.name or "", errors=errors.name) }}
20-
{{ ui.input("title", "field-title", label=_('Title'), value=data.title or "", errors=errors.title) }}
21-
{{ ui.textarea("notes", "field-notes", label=_('Description'), value=data.notes or "", errors=errors.notes) }}
19+
{{ ui.input(name="name", id="field-name", label=_('Name'), value=data.name or "", errors=errors.name) }}
20+
{{ ui.input(name="title", id="field-title", label=_('Title'), value=data.title or "", errors=errors.title) }}
21+
{{ ui.textarea(name="notes", id="field-notes", label=_('Description'), value=data.notes or "", errors=errors.notes) }}
2222

23-
{{ ui.select("license_id", "field-license",
23+
{{ ui.select(name="license_id", id="field-license",
2424
label=_('License'),
2525
options=h.license_options(),
2626
selected=data.license_id) }}

ckanext/theming/themes/bs5/templates/package/search.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
{%- if sorting %}
4242
<div class="mb-3">
4343
<label for="field-order-by" class="form-label">{{ _('Order by') }}</label>
44-
{{ ui.select("sort", "field-order-by", label="", options=sorting, selected=sort_by_selected, class="form-select") }}
44+
{{ ui.select(name="sort", id="field-order-by", label="", options=sorting, selected=sort_by_selected, class="form-select") }}
4545
{{ ui.button(_('Go'), type="submit", style="secondary") }}
4646
</div>
4747
{%- endif %}

0 commit comments

Comments
 (0)