django-orchestra-test/orchestra/contrib/mailboxes/widgets.py
2023-07-09 07:51:51 +00:00

34 lines
1.1 KiB
Python

import textwrap
from django import forms
class OpenCustomFilteringOnSelect(forms.Select):
def __init__(self, *args, **kwargs):
collapse = self.get_dynamic_collapse()
attrs = kwargs.get('attrs', {})
attrs.update({
'onClick': collapse,
'onChange': collapse,
})
attrs.update(kwargs.get('attrs', {}))
kwargs['attrs'] = attrs
super(OpenCustomFilteringOnSelect, self).__init__(*args, **kwargs)
def get_dynamic_collapse(self):
return textwrap.dedent("""\
value = this.options[this.selectedIndex].value;
fieldset = $(this).closest("fieldset");
fieldset = $(".collapse");
if ( value == 'CUSTOM' ) {
if (fieldset.hasClass("collapsed")) {
fieldset.removeClass("collapsed").trigger("show.fieldset", [$(this).attr("id")]);
}
} else {
if (! $(this).closest("fieldset").hasClass("collapsed")) {
fieldset.addClass("collapsed").trigger("hide.fieldset", [$(this).attr("id")]);
}
}
"""
)