diff options
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/forms.py | 67 | ||||
-rw-r--r-- | ishtar_common/templates/blocks/bs_field_snippet.html | 2 | ||||
-rw-r--r-- | ishtar_common/templates/blocks/bs_form_snippet.html | 2 |
3 files changed, 70 insertions, 1 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 24972ed3b..c9c88b805 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -617,6 +617,7 @@ class FormHeader(object): class IshtarForm(forms.Form, BSForm): TYPES = [] # FieldType list + CONDITIONAL_FIELDS = [] # dynamic conditions on field display PROFILE_FILTER = {} # profile key associated to field list HEADERS = {} # field key associated to FormHeader instance SITE_KEYS = {} # archaeological sites fields and associated translation key @@ -659,6 +660,72 @@ class IshtarForm(forms.Form, BSForm): self.current_header = self.HEADERS[key] return self.current_header + def extra_render(self): + return self.get_conditional() + + HIDE_JS_TEMPLATE = """ + var %(id)s_item_show_list = ['%(item_list)s']; + for (idx in %(id)s_item_show_list){ + $("#main_div-id_" + %(id)s_item_show_list[idx]).addClass("d-none"); + } + """ + CONDITIONAL_JS_TEMPLATE = """ + var %(id)s_check_list = ['%(check_id_list)s']; + var %(id)s_item_show_list = ['%(item_list)s']; + var %(id)s_hide_display = function(){ + var current_val = $("#id_%(name)s").val(); + console.log("#id_%(name)s"); + if (%(id)s_check_list.indexOf(current_val) != -1){ + for (idx in %(id)s_item_show_list){ + $("#main_div-id_" + %(id)s_item_show_list[idx]).removeClass("d-none"); + } + } else { + for (idx in %(id)s_item_show_list){ + $("#main_div-id_" + %(id)s_item_show_list[idx]).addClass("d-none"); + } + } + }; + + $("#id_%(name)s").change(%(id)s_hide_display); + setTimeout(function(){ + %(id)s_hide_display(); + }, 500); + """ + + def get_conditional(self): + if not self.CONDITIONAL_FIELDS or not self.TYPES: + return + + type_dict = dict([(typ.key, typ.model) for typ in self.TYPES]) + html = "" + for condition, target_names in self.CONDITIONAL_FIELDS: + condition_field, condition_attr, condition_val = condition + if condition_field not in type_dict: + continue + model = type_dict[condition_field] + condition_ids = [ + str(item.pk) for item in model.objects.filter( + **{condition_attr: condition_val}).all() + ] + name = self.prefix + "-" + condition_field + target_names = [ + self.prefix + "-" + name for name in target_names + ] + if not condition_ids: + html += self.HIDE_JS_TEMPLATE % { + "item_list": "','".join(target_names), + "id": name.replace("-", "_")} + continue + html += self.CONDITIONAL_JS_TEMPLATE % { + "id": name.replace("-", "_"), + "name": name, + "item_list": "','".join(target_names), + "check_id_list": "','".join(condition_ids), + } + if html: + html = "<script type='text/javascript'>" + html + "</script>" + return html + class TableSelect(IshtarForm): def __init__(self, *args, **kwargs): diff --git a/ishtar_common/templates/blocks/bs_field_snippet.html b/ishtar_common/templates/blocks/bs_field_snippet.html index b89727327..644f7f433 100644 --- a/ishtar_common/templates/blocks/bs_field_snippet.html +++ b/ishtar_common/templates/blocks/bs_field_snippet.html @@ -1,5 +1,5 @@ {% load i18n %} - <div class="form-group{% if not field.label %} no-label{% endif %} {% if field.field.widget.attrs.cols or force_large_col %}col-lg-12{% else %}col-lg-6{% endif %}{% if field.errors %} is-invalid{% endif %}{% if field.field.required %} required{% endif %}{% if force_large_col %} full-width{% endif %}" + <div id="main_div-{{field.auto_id}}" class="form-group{% if not field.label %} no-label{% endif %} {% if field.field.widget.attrs.cols or force_large_col %}col-lg-12{% else %}col-lg-6{% endif %}{% if field.errors %} is-invalid{% endif %}{% if field.field.required %} required{% endif %}{% if force_large_col %} full-width{% endif %}" data-alt-name="{{field.field.alt_name}}"> {% if field.label %}{{ field.label_tag }}{% endif %} {% if show_field_number or form.show_field_number %} diff --git a/ishtar_common/templates/blocks/bs_form_snippet.html b/ishtar_common/templates/blocks/bs_form_snippet.html index 98d9e0fe6..124ecfb5e 100644 --- a/ishtar_common/templates/blocks/bs_form_snippet.html +++ b/ishtar_common/templates/blocks/bs_form_snippet.html @@ -125,3 +125,5 @@ $(register_advanced_search); </script> {% endif %} + +{{form.extra_render|safe}} |