Skip to content

Commit

Permalink
Started HTMX integration on wind sliders
Browse files Browse the repository at this point in the history
Unfortunately ionrangeslider seems not to work with HTMX yet. See opened issue here: IonDen/ion.rangeSlider#792
  • Loading branch information
henhuy committed Apr 10, 2024
1 parent ac76115 commit 97916a9
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions digiplan/map/forms.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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),
)
Expand All @@ -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"
Expand Down

0 comments on commit 97916a9

Please sign in to comment.