Fix Jauge always reading the object's most recent row
A Jauge could only target a whole object, not a specific record — three gauges pointing at the same "value" field (e.g. Réputation/ Trésorerie/Confiance in one "jauge" object) all silently showed the most recent row's value, with no way to tell them apart. Add "Enregistrement (ligne) à suivre" (row_id) so a Jauge targets one specific row, and "Champ contenant le nom" (champ_nom) to show a label above the bar — both as dropdowns populated from the object's actual fields/rows (previously "Champ numérique à afficher" was free text the user had to type correctly by hand). No regression: without row_id the widget still falls back to the latest row, exactly as before. Extract data_definition_options() (rows+fields for a definition) out of routes/screens/screen_edit.py so controls_with_values.py can reuse it server-side for the initial render; a small client-side handler (bindJaugeDefinitionSelect) repopulates the same selects live when the tracked object is changed without leaving the panel. Verified live with Playwright: picking "jauge" then "Confiance" then "value"/"name" in the panel renders an 80%-filled, green-leaning bar labelled "Confiance" on /play — not the 20%/50% of the other rows.
This commit is contained in:
@@ -22,12 +22,15 @@ def _interpolate_color(low_hex, high_hex, ratio):
|
||||
return "#{:02x}{:02x}{:02x}".format(*mixed)
|
||||
|
||||
|
||||
def _read_latest_field_value(slug, definition_id, field_name):
|
||||
"""Lit la valeur du champ demandé sur la ligne la PLUS RÉCENTE de
|
||||
l'objet visé — même convention que le filtre de Répéteur (1.2) : un
|
||||
objet utilisé comme jauge de partie (Réputation, Trésorerie...) ne
|
||||
garde qu'une seule ligne, mise à jour en place."""
|
||||
if not definition_id or not slug:
|
||||
def _read_field_value(slug, definition_id, row_id, field_name):
|
||||
"""Lit la valeur du champ demandé sur l'enregistrement visé (row_id) de
|
||||
l'objet suivi — quand un objet contient plusieurs enregistrements (ex:
|
||||
Réputation/Trésorerie/Confiance dans un même objet "jauge"), row_id est
|
||||
ce qui distingue lequel suivre. Repli sur la ligne la PLUS RÉCENTE si
|
||||
row_id est absent ou introuvable (jauges créées avant ce réglage, ou
|
||||
ligne supprimée depuis) — comportement historique, jamais de régression
|
||||
pour un objet à une seule ligne."""
|
||||
if not definition_id or not field_name or not slug:
|
||||
return None
|
||||
definition = db.get_definition(slug, int(definition_id))
|
||||
if not definition:
|
||||
@@ -35,8 +38,13 @@ def _read_latest_field_value(slug, definition_id, field_name):
|
||||
rows = db.list_rows(slug, definition)
|
||||
if not rows:
|
||||
return None
|
||||
row = None
|
||||
if row_id:
|
||||
row = next((r for r in rows if str(r["id"]) == str(row_id)), None)
|
||||
if row is None:
|
||||
row = rows[0]
|
||||
col = db.slugify(field_name).replace("-", "_")
|
||||
return rows[0].get(col)
|
||||
return row.get(col)
|
||||
|
||||
|
||||
def _render_jauge(el, meta, slug, children_map=None, ctx=None, parent_flex_direction=None):
|
||||
@@ -48,9 +56,16 @@ def _render_jauge(el, meta, slug, children_map=None, ctx=None, parent_flex_direc
|
||||
"Modifier une donnée"), donc toujours à jour sans action supplémentaire
|
||||
à poser dans le graphe de logique."""
|
||||
attrs = el.get("attributes") or {}
|
||||
style = _style_string(el, " overflow:hidden; position:relative; background:#1c2130; border-radius:6px;", parent_flex_direction)
|
||||
# display:flex column : le nom (optionnel) empile AU-DESSUS de la barre
|
||||
# plutôt que de se superposer dedans — voir name_html plus bas. Le fond/
|
||||
# bordure/overflow vivent maintenant sur le wrapper de la barre
|
||||
# (bar_wrap_style), pas ici, pour que le nom reste en dehors de la zone
|
||||
# rognée par "overflow:hidden".
|
||||
style = _style_string(el, " display:flex; flex-direction:column; gap:4px;", parent_flex_direction)
|
||||
definition_id = attrs.get("_definition_id")
|
||||
row_id = attrs.get("_row_id")
|
||||
field_name = (attrs.get("_champ") or "").strip()
|
||||
field_name_nom = (attrs.get("_champ_nom") or "").strip()
|
||||
try:
|
||||
min_v = float(attrs.get("_min", "0") or 0)
|
||||
except (TypeError, ValueError):
|
||||
@@ -63,7 +78,7 @@ def _render_jauge(el, meta, slug, children_map=None, ctx=None, parent_flex_direc
|
||||
couleur_haute = attrs.get("_couleur_haute") or "#4caf50"
|
||||
afficher_valeur = attrs.get("_afficher_valeur") == "1"
|
||||
|
||||
raw_value = _read_latest_field_value(slug, definition_id, field_name) if field_name else None
|
||||
raw_value = _read_field_value(slug, definition_id, row_id, field_name) if field_name else None
|
||||
try:
|
||||
value = float(raw_value)
|
||||
except (TypeError, ValueError):
|
||||
@@ -82,6 +97,15 @@ def _render_jauge(el, meta, slug, children_map=None, ctx=None, parent_flex_direc
|
||||
f'color:#e8eaf0; text-shadow:0 1px 2px rgba(0,0,0,.6);">{html_lib.escape(str(raw_value if raw_value is not None else "—"))}</div>'
|
||||
)
|
||||
|
||||
name_html = ""
|
||||
if field_name_nom:
|
||||
nom_value = _read_field_value(slug, definition_id, row_id, field_name_nom)
|
||||
if nom_value not in (None, ""):
|
||||
name_html = (
|
||||
f'<div style="flex:0 0 auto; font-family:system-ui,sans-serif; font-size:12px; '
|
||||
f'color:#e8eaf0; text-align:center;">{html_lib.escape(str(nom_value))}</div>'
|
||||
)
|
||||
|
||||
# La classe "jaugeBar" est repérée côté client (play.html, refreshRuntimeData)
|
||||
# pour savoir que ce wrapper doit être régénéré après toute action
|
||||
# "Modifier une donnée" — sinon la jauge resterait figée sur sa valeur
|
||||
@@ -91,7 +115,11 @@ def _render_jauge(el, meta, slug, children_map=None, ctx=None, parent_flex_direc
|
||||
f'<div class="jaugeBar" style="position:absolute; left:0; top:0; bottom:0; width:{percent}%; '
|
||||
f'background-color:{fill_color}; transition:width .3s ease, background-color .3s ease;"></div>'
|
||||
)
|
||||
bar_wrap = (
|
||||
f'<div style="position:relative; overflow:hidden; background:#1c2130; border-radius:6px; '
|
||||
f'flex:1 1 auto; min-height:0;">{bar}{label_html}</div>'
|
||||
)
|
||||
return (
|
||||
f'<div id="{_auto_id(el)}" data-element-id="{el["id"]}" style="{html_lib.escape(style)}">'
|
||||
f'{bar}{label_html}</div>'
|
||||
f'{name_html}{bar_wrap}</div>'
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user