Add selector nomenclature for conditional field visibility
A selector is a nomenclature whose value decides which other nomenclatures appear beneath it: picking "Ship" reveals the ship fields, "Warehouse" reveals the warehouse ones. Previously every vehicle nomenclature rendered at once, so the operator had to know which were relevant and a vehicle could silently carry both a ship and a warehouse. Visibility is one rule - a nomenclature is hidden if some selector option reveals it and that selector's value is not the revealing option - kept in nomenclatures/visibility.py and mirrored in services/nomenclatureVisibility.js. A nomenclature no selector references always shows, so installations without selectors are unchanged. The serializer, not the UI, enforces the invariant: a value for a hidden nomenclature is rejected, as is a selector value that matches no option. The frontend clearing values on switch and pruning hidden keys before save is convenience on top of that. Selector values join lookups in the quick-fill combination projection, so applying a saved combination sets the source and its revealed fields in one click rather than collapsing "Ship + cargo" and "Warehouse + cargo" into one indistinguishable row. Configuration rejects nesting (a selector revealing another selector) and double-reveal (one nomenclature revealed by two selectors, leaving its visibility ambiguous). The admin UI filters both out of the reveal list as well, so an invalid configuration cannot be built in the first place. Note: introducing a selector over existing data hides values already entered - vehicles predating the selector have no selector key, so anything it reveals becomes invisible until a source is chosen. The values remain stored. Worth a data check before configuring the first selector. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
"""The one rule deciding which nomenclatures are visible for a given extra_data.
|
||||
|
||||
A nomenclature is hidden if it is revealed by some selector option and that
|
||||
selector's current value is not the option revealing it. Anything no selector
|
||||
references is always visible, so installations without selectors are unchanged.
|
||||
|
||||
Mirrored in frontend/src/utils/nomenclatureVisibility.js - keep the two in step.
|
||||
"""
|
||||
|
||||
from .models import Nomenclature, SelectorOption
|
||||
|
||||
|
||||
def build_reveal_map(nomenclatures=None):
|
||||
"""{revealed_code: {selector_code: {value, ...}}} for the given nomenclatures."""
|
||||
options = (
|
||||
SelectorOption.objects
|
||||
.select_related('selector')
|
||||
.prefetch_related('reveals')
|
||||
)
|
||||
if nomenclatures is not None:
|
||||
codes = {n.code for n in nomenclatures}
|
||||
options = [o for o in options if o.selector.code in codes]
|
||||
|
||||
reveal_map = {}
|
||||
for option in options:
|
||||
for revealed in option.reveals.all():
|
||||
reveal_map.setdefault(revealed.code, {}).setdefault(
|
||||
option.selector.code, set()
|
||||
).add(option.value)
|
||||
return reveal_map
|
||||
|
||||
|
||||
def is_visible(code, extra_data, reveal_map):
|
||||
"""True if the nomenclature `code` should be shown/accepted for this data."""
|
||||
controllers = reveal_map.get(code)
|
||||
if not controllers:
|
||||
return True
|
||||
for selector_code, revealing_values in controllers.items():
|
||||
if extra_data.get(selector_code) in revealing_values:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def visible_codes(codes, extra_data, reveal_map=None):
|
||||
"""Filter `codes` down to those visible for `extra_data`."""
|
||||
if reveal_map is None:
|
||||
reveal_map = build_reveal_map()
|
||||
return [c for c in codes if is_visible(c, extra_data or {}, reveal_map)]
|
||||
Reference in New Issue
Block a user