{%- 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 %}
    {%- if modal -%}
      {# content added to modal-content #}
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        {% block header_text %}
            <h3>{{ _gettext('Editing %(path)s', path=path) }}</h3>
        {% endblock %}
      </div>
      <div class="modal-body">
        {% call check_error(error) %}
          {{ lib.render_form(form, dir_url, action=request.url, is_modal=True) }}
        {% endcall %}
      </div>
    {%- else -%}
      {{ self.header_text() }}
      {% call check_error(error) %}
        {{ lib.render_form(form, dir_url) }}
      {% endcall %}
    {%- endif -%}
{% endblock %}

{% block tail %}
    {%- if modal -%}
    <script>
    // fixes "remote modal shows same content every time", avoiding the flicker
    $('body').on('hidden.bs.modal', '.modal', function() {
        $(this).removeData('bs.modal').find(".modal-content").empty();
    });
    </script>
    {%- endif -%}
{% endblock %}
