|
| 1 | +from django import template |
| 2 | +from .helpers import create_content_wrapper |
| 3 | + |
| 4 | +register = template.Library() |
| 5 | + |
| 6 | + |
| 7 | +@register.inclusion_tag("components/Filter/SearchFilters.html") |
| 8 | +def search_filters( |
| 9 | + search_form, |
| 10 | + search_filter_categories, |
| 11 | + search_filter_tags, |
| 12 | + search_filter_organizations, |
| 13 | + **kwargs |
| 14 | +): |
| 15 | + """ |
| 16 | + Renders the actions in a filterable table. |
| 17 | +
|
| 18 | + Usage: |
| 19 | + {% search_filters search_form form_id='form_id' search_filter_categories search_filter_tags search_filter_organizations %} |
| 20 | +
|
| 21 | + Available options: |
| 22 | + + search_form: BaseForm | The form that contains the fields with choices. |
| 23 | + + form_id: str | The id of the form. |
| 24 | + + search_filter_categories: bool | The config bool that defines if the categorie choices are rendered. |
| 25 | + + search_filter_tags: bool | The config bool that defines if the tag choices are rendered. |
| 26 | + + search_filter_organizations: bool | The config bool that defines if the organization choices are rendered. |
| 27 | + """ |
| 28 | + |
| 29 | + open_filter_index = 0 |
| 30 | + |
| 31 | + # Determine the initial open state based on active filters and their choices |
| 32 | + filters = [ |
| 33 | + (search_filter_categories, len(search_form["categories"].field.choices), 0), |
| 34 | + (search_filter_tags, len(search_form["tags"].field.choices), 1), |
| 35 | + ( |
| 36 | + search_filter_organizations, |
| 37 | + len(search_form["organizations"].field.choices), |
| 38 | + 2, |
| 39 | + ), |
| 40 | + ] |
| 41 | + |
| 42 | + # Set initial_open based on which filter is active and has choices |
| 43 | + for filter_active, choices_len, index in filters: |
| 44 | + if filter_active and choices_len != 0: |
| 45 | + open_filter_index = index |
| 46 | + break # Stop once we find the first active filter with choices |
| 47 | + |
| 48 | + kwargs["search_form"] = search_form |
| 49 | + kwargs["search_filter_categories"] = search_filter_categories |
| 50 | + kwargs["search_filter_tags"] = search_filter_tags |
| 51 | + kwargs["search_filter_organizations"] = search_filter_organizations |
| 52 | + kwargs["open_filter_index"] = open_filter_index |
| 53 | + |
| 54 | + return {**kwargs} |
| 55 | + |
| 56 | + |
| 57 | +@register.inclusion_tag("components/Filter/SearchFiltersMobile.html") |
| 58 | +def search_filters_mobile( |
| 59 | + search_form, |
| 60 | + search_filter_categories, |
| 61 | + search_filter_tags, |
| 62 | + search_filter_organizations, |
| 63 | + **kwargs |
| 64 | +): |
| 65 | + """ |
| 66 | + Renders the actions in a filterable table. |
| 67 | +
|
| 68 | + Usage: |
| 69 | + {% search_filters search_form form_id='form_id' search_filter_categories search_filter_tags search_filter_organizations %} |
| 70 | +
|
| 71 | + Available options: |
| 72 | + + search_form: BaseForm | The form that contains the fields with choices. |
| 73 | + + form_id: str | The id of the form. |
| 74 | + + search_filter_categories: bool | The config bool that defines if the categorie choices are rendered. |
| 75 | + + search_filter_tags: bool | The config bool that defines if the tag choices are rendered. |
| 76 | + + search_filter_organizations: bool | The config bool that defines if the organization choices are rendered. |
| 77 | + """ |
| 78 | + |
| 79 | + open_filter_index = 0 |
| 80 | + |
| 81 | + # Determine the initial open state based on active filters and their choices |
| 82 | + filters = [ |
| 83 | + (search_filter_categories, len(search_form["categories"].field.choices), 0), |
| 84 | + (search_filter_tags, len(search_form["tags"].field.choices), 1), |
| 85 | + ( |
| 86 | + search_filter_organizations, |
| 87 | + len(search_form["organizations"].field.choices), |
| 88 | + 2, |
| 89 | + ), |
| 90 | + ] |
| 91 | + |
| 92 | + # Set initial_open based on which filter is active and has choices |
| 93 | + for filter_active, choices_len, index in filters: |
| 94 | + if filter_active and choices_len != 0: |
| 95 | + open_filter_index = index |
| 96 | + break # Stop once we find the first active filter with choices |
| 97 | + |
| 98 | + kwargs["search_form"] = search_form |
| 99 | + kwargs["search_filter_categories"] = search_filter_categories |
| 100 | + kwargs["search_filter_tags"] = search_filter_tags |
| 101 | + kwargs["search_filter_organizations"] = search_filter_organizations |
| 102 | + kwargs["open_filter_index"] = open_filter_index |
| 103 | + kwargs["append_to_checkbox_id"] = kwargs.get('append_to_checkbox_id', None) |
| 104 | + |
| 105 | + return {**kwargs} |
| 106 | + |
| 107 | + |
| 108 | +@register.inclusion_tag("components/Filter/Filter.html") |
| 109 | +def filter(field, **kwargs): |
| 110 | + """ |
| 111 | + Renders the actions in a filterable table. |
| 112 | +
|
| 113 | + Usage: |
| 114 | + {% filter field=search_form.tags form_id=form_id open_filter_index=1 index=1 %} |
| 115 | +
|
| 116 | + Available options: |
| 117 | + + field: Field | The field that needs to be rendered. |
| 118 | + + form_id: str | The id of the form. |
| 119 | + + open_filter_index: int | The index of the filter that should render open. |
| 120 | + + index: int | The index of the current filter. |
| 121 | + + checkbox_as_tag: bool | Boolean indicating if the checkbox should render as a tag. |
| 122 | + """ |
| 123 | + |
| 124 | + kwargs["initial_open"] = kwargs["open_filter_index"] is kwargs["index"] |
| 125 | + checkbox_as_tag = kwargs.get('checkbox_as_tag', False) |
| 126 | + |
| 127 | + return {"field": field, "checkbox_as_tag": checkbox_as_tag, **kwargs} |
| 128 | + |
| 129 | + |
| 130 | +@register.tag |
| 131 | +def render_filter_modal(parser, token): |
| 132 | + """ |
| 133 | + This is used to render in a grid. |
| 134 | +
|
| 135 | + Usage: |
| 136 | + {% render_filter_modal %} |
| 137 | + <div>Some content here</div> |
| 138 | + <div>Some content here too</div> |
| 139 | + {% endrender_filter_modal %} |
| 140 | +
|
| 141 | + Variables: |
| 142 | + - compact: bool | Whether to use compact mode (no gutters). |
| 143 | +
|
| 144 | + Extra context: |
| 145 | + - contents: string (HTML) | this is the context between the render_grid and endrender_grid tags |
| 146 | + """ |
| 147 | + return create_content_wrapper("render_filter_modal", "components/Filter/FilterModal.html")( |
| 148 | + parser, token |
| 149 | + ) |
0 commit comments