From 0153b099894dfaea36b2320bfa0bcfb3a45a5cad Mon Sep 17 00:00:00 2001 From: Hendrik Huyskens Date: Tue, 26 Mar 2024 17:03:10 +0100 Subject: [PATCH] Started HTMX integration on wind sliders Unfortunately ionrangeslider seems not to work with HTMX yet. See opened issue here: https://github.com/IonDen/ion.rangeSlider/issues/792 --- digiplan/map/forms.py | 58 +++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/digiplan/map/forms.py b/digiplan/map/forms.py index e0652de0..bf522799 100644 --- a/digiplan/map/forms.py +++ b/digiplan/map/forms.py @@ -1,6 +1,7 @@ from itertools import count # noqa: D100 from django.forms import BooleanField, Form, IntegerField, TextInput, renderers +from django.shortcuts import reverse from django.utils.safestring import mark_safe from django_mapengine import legend @@ -66,33 +67,36 @@ def __init__(self, parameters, additional_parameters=None, **kwargs) -> None: # super().__init__(**kwargs) self.fields = {item["name"]: item["field"] for item in self.generate_fields(parameters, additional_parameters)} - @staticmethod - def generate_fields(parameters, additional_parameters=None): # noqa: ANN001, ANN205, D102 + def get_field_attrs(self, name: str, parameters: dict) -> dict: # noqa: ARG002 + """Set up field attributes from parameters.""" + attrs = { + "class": parameters["class"], + "data-min": parameters["min"], + "data-max": parameters["max"], + "data-from": parameters["start"], + "data-grid": "true" if "grid" in parameters and parameters["grid"] else "false", + "data-has-sidepanel": "true" if "sidepanel" in parameters else "false", + "data-color": parameters["color"] if "color" in parameters else "", + } + if "to" in parameters: + attrs["data-to"] = parameters["to"] + if "step" in parameters: + attrs["data-step"] = parameters["step"] + if "from-min" in parameters: + attrs["data-from-min"] = parameters["from-min"] + if "from-max" in parameters: + attrs["data-from-max"] = parameters["from-max"] + return attrs + + def generate_fields(self, parameters: dict, additional_parameters: dict | None = None) -> dict: + """Create fields from config parameters.""" if additional_parameters is not None: charts.merge_dicts(parameters, additional_parameters) for name, item in parameters.items(): if item["type"] == "slider": - attrs = { - "class": item["class"], - "data-min": item["min"], - "data-max": item["max"], - "data-from": item["start"], - "data-grid": "true" if "grid" in item and item["grid"] else "false", - "data-has-sidepanel": "true" if "sidepanel" in item else "false", - "data-color": item["color"] if "color" in item else "", - } - if "to" in item: - attrs["data-to"] = item["to"] - if "step" in item: - attrs["data-step"] = item["step"] - if "from-min" in item: - attrs["data-from-min"] = item["from-min"] - if "from-max" in item: - attrs["data-from-max"] = item["from-max"] - field = IntegerField( label=item["label"], - widget=TextInput(attrs=attrs), + widget=TextInput(attrs=self.get_field_attrs(name, item)), help_text=item["tooltip"], required=item.get("required", True), ) @@ -115,13 +119,23 @@ def generate_fields(parameters, additional_parameters=None): # noqa: ANN001, AN class EnergyPanelForm(PanelForm): # noqa: D101 template_name = "forms/panel_energy.html" - def __init__(self, parameters, additional_parameters=None, **kwargs) -> None: # noqa: D107, ANN001 + def __init__(self, parameters, additional_parameters=None, **kwargs) -> None: # noqa: ANN001 + """Overwrite init function to add initial key results for detail panels.""" super().__init__(parameters, additional_parameters, **kwargs) for technology in ("wind_2018", "wind_2024", "wind_2027", "pv_ground", "pv_roof"): key_results = menu.detail_key_results(technology) for key, value in key_results.items(): self.extra_content[f"{technology}_key_result_{key}"] = value + def get_field_attrs(self, name: str, parameters: dict) -> dict: + """Add HTMX attributes to wind and pv detail sliders.""" + detail_slider_targets = {"s_w_6": "wind_key_results_2024", "s_w_7": "wind_key_results_2027"} + attrs = super().get_field_attrs(name, parameters) + if name in detail_slider_targets: + attrs["hx-get"] = reverse("map:detail_key_results") + attrs["hx-target"] = detail_slider_targets[name] + return attrs + class HeatPanelForm(PanelForm): # noqa: D101 template_name = "forms/panel_heat.html"