diff --git a/hshassets/assets/js/common.js b/hshassets/assets/js/common.js new file mode 100644 index 0000000000000000000000000000000000000000..f6462f991ea41f3b12be3c50c6b3b85e6ba4e8f1 --- /dev/null +++ b/hshassets/assets/js/common.js @@ -0,0 +1,6 @@ + +$('[data-submit-only-once="true"]').submit(function(event){ + $(this).find("button").addClass("is-loading"); + $(this).find(":submit").attr("disabled","disabled"); +}); + diff --git a/hshassets/forms/utils.py b/hshassets/forms/utils.py index efc23b885684345d7dd2e0d02297188b66bc4d38..90dd7bc4c9ef10f91cab6c385c70799a0d74d104 100644 --- a/hshassets/forms/utils.py +++ b/hshassets/forms/utils.py @@ -103,11 +103,12 @@ class BulmaFieldMarkup(object): return cls.div_control(content) -def render_form_generics(context, rendered_fields, rendered_errors, submit_text, submit_class): +def render_form_generics(context, rendered_fields, rendered_errors, submit_text, submit_class, submit_only_once=True): csrf_field = defaulttags.CsrfTokenNode().render(context) + submit_only_once = "true" if submit_only_once else "false" return format_html( """ - <form method="post"> + <form method="post" data-submit-only-once="{submit_only_once}"> {rendered_errors} {rendered_fields} {csrf_field} diff --git a/hshassets/static/hshassets/js/common.js b/hshassets/static/hshassets/js/common.js new file mode 100644 index 0000000000000000000000000000000000000000..2824f19747dde30a7a6a040d8383824686027f21 --- /dev/null +++ b/hshassets/static/hshassets/js/common.js @@ -0,0 +1,7 @@ + +$('[data-submit-only-once="true"]').submit(function(event){ + $(this).find("button").addClass("is-loading"); + $(this).find(":submit").attr("disabled","disabled"); +}); + + diff --git a/hshassets/static/hshassets/js/common.min.js b/hshassets/static/hshassets/js/common.min.js new file mode 100644 index 0000000000000000000000000000000000000000..894dd68b9aa22e8ca329362209a6bbb4fa370c4f --- /dev/null +++ b/hshassets/static/hshassets/js/common.min.js @@ -0,0 +1 @@ +$('[data-submit-only-once="true"]').submit(function(event){$(this).find("button").addClass("is-loading");$(this).find(":submit").attr("disabled","disabled");}); \ No newline at end of file diff --git a/hshassets/templates/hshassets/layout/base.html b/hshassets/templates/hshassets/layout/base.html index 80db6af178211945af4202d6fabfa1ba67bd9892..3b0c235501599fca5eadc5d6f40223531c66dbbe 100644 --- a/hshassets/templates/hshassets/layout/base.html +++ b/hshassets/templates/hshassets/layout/base.html @@ -30,8 +30,10 @@ {% block script %} {% if debug %} <script type="text/javascript" src="{% static 'hshassets/js/jquery-3.2.1.js' %}"></script> + <script type="text/javascript" src="{% static 'hshassets/js/common.js' %}"></script> {% else %} <script type="text/javascript" src="{% static 'hshassets/js/jquery-3.2.1.min.js' %}"></script> + <script type="text/javascript" src="{% static 'hshassets/js/common.min.js' %}"></script> {% endif %} {% endblock %} {% block post-script %}{% endblock %} diff --git a/hshassets/templatetags/bulma.py b/hshassets/templatetags/bulma.py index 7ff599067ec623099c524a991db26d0a82702c55..9990c5375d8a2e0b34c4724df83b634e216e737d 100644 --- a/hshassets/templatetags/bulma.py +++ b/hshassets/templatetags/bulma.py @@ -43,13 +43,13 @@ def bulma_form_errors(form): @register.simple_tag(takes_context=True) -def bulma_form(context, form, submit_text="OK", submit_class="button is-outlined"): +def bulma_form(context, form, submit_text="OK", submit_class="button is-outlined", submit_only_once=True): """Renders whole form, including errors, csrf and a submit button.""" fields = bulma_form_fields(form) errors = bulma_form_errors(form) - return render_form_generics(context, fields, errors, submit_text, submit_class) + return render_form_generics(context, fields, errors, submit_text, submit_class, submit_only_once) def render_layout(elements, form):