Merge disabled_indicator into option_attrs

This commit is contained in:
Jeremy Stretch 2024-02-09 13:29:12 -05:00
parent 87d92f8ee4
commit f155e3c3d5
8 changed files with 25 additions and 14 deletions

View File

@ -30,7 +30,9 @@ def get_cable_form(a_type, b_type):
attrs[f'{cable_end}_terminations'] = DynamicModelMultipleChoiceField(
queryset=term_cls.objects.all(),
label=term_cls._meta.verbose_name.title(),
disabled_indicator='_occupied',
option_attrs={
'disabled': '_occupied',
},
query_params={
'device_id': f'$termination_{cable_end}_device',
'kind': 'physical', # Exclude virtual interfaces
@ -52,7 +54,9 @@ def get_cable_form(a_type, b_type):
attrs[f'{cable_end}_terminations'] = DynamicModelMultipleChoiceField(
queryset=term_cls.objects.all(),
label=_('Power Feed'),
disabled_indicator='_occupied',
option_attrs={
'disabled': '_occupied',
},
query_params={
'power_panel_id': f'$termination_{cable_end}_powerpanel',
}
@ -72,7 +76,9 @@ def get_cable_form(a_type, b_type):
attrs[f'{cable_end}_terminations'] = DynamicModelMultipleChoiceField(
queryset=term_cls.objects.all(),
label=_('Side'),
disabled_indicator='_occupied',
option_attrs={
'disabled': '_occupied',
},
query_params={
'circuit_id': f'$termination_{cable_end}_circuit',
}

View File

@ -426,7 +426,7 @@ class DeviceForm(TenancyForm, NetBoxModelForm):
widget=APISelect(
api_url='/api/dcim/racks/{{rack}}/elevation/',
attrs={
'disabled-indicator': 'device',
'ts-disabled-field': 'device',
'data-dynamic-params': '[{"fieldName":"face","queryParam":"face"}]'
},
)

Binary file not shown.

Binary file not shown.

View File

@ -34,6 +34,7 @@ export class DynamicTomSelect extends TomSelect {
// Override any field names set as widget attributes
this.valueField = this.input.getAttribute('ts-value-field') || this.settings.valueField;
this.labelField = this.input.getAttribute('ts-label-field') || this.settings.labelField;
this.disabledField = this.input.getAttribute('ts-disabled-field') || this.settings.disabledField;
this.parentField = this.input.getAttribute('ts-parent-field') || null;
this.depthField = this.input.getAttribute('ts-depth-field') || '_depth';
this.descriptionField = this.input.getAttribute('ts-description-field') || 'description';
@ -155,6 +156,9 @@ export class DynamicTomSelect extends TomSelect {
let parent: Dict = data[this.parentField] as Dict;
option['parent'] = parent[this.labelField];
}
if (data[this.disabledField]) {
option['disabled'] = data[this.disabledField];
}
return option
}

View File

@ -49,10 +49,6 @@ export function initDynamicSelects(): void {
// Disable local search (search is performed on the backend)
searchField: [],
// Reference the disabled-indicator attr on the <select> element to determine
// the name of the attribute which indicates whether an option should be disabled
disabledField: select.getAttribute('disabled-indicator') || undefined,
// Load options from API immediately on focus
preload: 'focus',

View File

@ -63,7 +63,7 @@ class DynamicModelChoiceMixin:
initial_params: A dictionary of child field references to use for selecting a parent field's initial value
null_option: The string used to represent a null selection (if any)
disabled_indicator: The name of the field which, if populated, will disable selection of the
choice (optional)
choice (DEPRECATED: pass `option_attrs={'disabled': '$fieldname'}` instead)
option_attrs: A mapping of <option> template variables to their API data keys (optional)
selector: Include an advanced object selection widget to assist the user in identifying the desired object
"""
@ -103,12 +103,13 @@ class DynamicModelChoiceMixin:
for var, accessor in self.option_attrs.items():
attrs[f'ts-{var}-field'] = accessor
# Set the disabled indicator, if any
# TODO: Remove in v4.1
# Legacy means of specifying the disabled indicator
if self.disabled_indicator is not None:
attrs['disabled-indicator'] = self.disabled_indicator
attrs['ts-disabled-field'] = self.disabled_indicator
# Attach any static query parameters
if (len(self.query_params) > 0):
if len(self.query_params) > 0:
widget.add_query_params(self.query_params)
# Include object selector?

View File

@ -108,7 +108,9 @@ class WirelessLinkForm(TenancyForm, NetBoxModelForm):
'kind': 'wireless',
'device_id': '$device_a',
},
disabled_indicator='_occupied',
option_attrs={
'disabled': '_occupied',
},
label=_('Interface')
)
site_b = DynamicModelChoiceField(
@ -148,7 +150,9 @@ class WirelessLinkForm(TenancyForm, NetBoxModelForm):
'kind': 'wireless',
'device_id': '$device_b',
},
disabled_indicator='_occupied',
option_attrs={
'disabled': '_occupied',
},
label=_('Interface')
)
comments = CommentField()