mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 04:22:01 -06:00
#1033: Convert ColorSelect to a templatized widget
This commit is contained in:
parent
1dcb0b52e2
commit
d2bd4a213b
@ -118,25 +118,15 @@ class SmallTextarea(forms.Textarea):
|
|||||||
|
|
||||||
|
|
||||||
class ColorSelect(forms.Select):
|
class ColorSelect(forms.Select):
|
||||||
|
"""
|
||||||
|
Extends the built-in Select widget to colorize each <option>.
|
||||||
|
"""
|
||||||
|
option_template_name = 'colorselect_option.html'
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
kwargs['choices'] = COLOR_CHOICES
|
kwargs['choices'] = COLOR_CHOICES
|
||||||
super(ColorSelect, self).__init__(*args, **kwargs)
|
super(ColorSelect, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def render_option(self, selected_choices, option_value, option_label):
|
|
||||||
if option_value is None:
|
|
||||||
option_value = ''
|
|
||||||
option_value = force_text(option_value)
|
|
||||||
if option_value in selected_choices:
|
|
||||||
selected_html = mark_safe(' selected')
|
|
||||||
if not self.allow_multiple_selected:
|
|
||||||
# Only allow for a single selection.
|
|
||||||
selected_choices.remove(option_value)
|
|
||||||
else:
|
|
||||||
selected_html = ''
|
|
||||||
return format_html('<option value="{}"{} style="background-color: #{}">{}</option>',
|
|
||||||
option_value, selected_html, option_value, force_text(option_label))
|
|
||||||
|
|
||||||
|
|
||||||
class SelectWithDisabled(forms.Select):
|
class SelectWithDisabled(forms.Select):
|
||||||
"""
|
"""
|
||||||
|
1
netbox/utilities/templates/colorselect_option.html
Normal file
1
netbox/utilities/templates/colorselect_option.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<option value="{{ widget.value }}"{% include "django/forms/widgets/attrs.html" %} style="background-color: #{{ widget.value }}">{{ widget.label }}</option>
|
Loading…
Reference in New Issue
Block a user