{%- if not modal -%}
  {% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}

{% macro check_error(error) %}
    {% if error %}
      <span>{{ _gettext('This file cannot be edited for now.') }}</span>
    {% else %}
      {{ caller() }}
    {% endif %}
{% endmacro %}

{% block body %}
    {% call check_error(error) %}
      {%- if modal -%}
        {# content added to modal-content #}
        {{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
      {%- else -%}
        {% block header_text -%}
          <h3>{{ _gettext('Editing %(path)s', path=path) }}<h3>
        {%- endblock %}
        {{ lib.render_form(form, dir_url) }}
      {%- endif -%}
    {% endcall %}
{% endblock %}

{% block tail %}
    {%- if modal -%}
    <script>
    // fill the header of modal dynamically
    $('.modal-header h3').html('{{ self.header_text() }}');

    // fixes "remote modal shows same content every time"
    $('.modal').on('hidden', function() {
        $(this).removeData('modal');
    });
    </script>
    {%- endif -%}
{% endblock %}
