{% load i18n %}

<div id="{{ inline_admin_formset.formset.prefix }}-group"
     class="inline-group inline-tabular{% if inline_admin_formset.opts.sortable %} sortable{% endif %}{% if inline_admin_formset.opts.classes %} {{ inline_admin_formset.opts.classes|join:" " }}{% endif %}" name="inlinegrouptabular">
    <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
    <ul class="inline-item-tools">
        {% block inline-item-tools %}
            {% if inline_admin_formset.opts.allow_add %}<li><a href="javascript://" class="addhandler" title="{% trans 'Add Item' %}"></a></li>{% endif %}
        {% endblock %}
    </ul>
    {{ inline_admin_formset.formset.management_form }}
    {{ inline_admin_formset.formset.non_form_errors }}
    {{ inline_admin_formset.opts.model.opts.order_with_respect_to }}
    <div class="items"> <!-- table, sortable container -->
        <div class="legend"> <!-- table header -->
            {% for field in inline_admin_formset.fields %}
                {% if not field.widget.is_hidden %}
                    <div class="form-cell {{ field.label|lower|slugify }}">{{ field.label|capfirst }}</div>
                {% endif %}
            {% endfor %}
            <div class="form-cell">&nbsp;</div>
        </div>
        {% for inline_admin_form in inline_admin_formset %}
        <div class="inline-related{% if inline_admin_form.original or inline_admin_form.show_url %} has_original{% endif %}{% if forloop.last %} empty-form{% endif %}"
             id="{{ inline_admin_formset.formset.prefix }}-{% if not forloop.last %}{{ forloop.counter0 }}{% else %}empty{% endif %}"> <!-- tbody, sortable items -->
            <!-- errors -->
            {% if inline_admin_form.form.non_field_errors %}
                <div>{{ inline_admin_form.form.non_field_errors }}</div>
            {% endif %}
            <!-- Hidden title for the related object - used by Cartridge. -->
            <h3 style="display: none;"><b class="inline_label">{{ inline_admin_formset.opts.verbose_name|title }} #{{ forloop.counter }}</b>&nbsp;&nbsp;{% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}</h3>
            <!-- hidden fields -->
            {% spaceless %}
            {% for fieldset in inline_admin_form %}
                {% for line in fieldset %}
                    {% for field in line %}
                        {% if field.field.is_hidden %} {{ field.field }} {% endif %}
                    {% endfor %}
                {% endfor %}
            {% endfor %}
            {% endspaceless %}
            <!-- fieldsets -->
            <div class="fieldset module"> <!-- table-row -->
            {% for fieldset in inline_admin_form %}
                {% for line in fieldset %}
                    {% for field in line %}
                        {% if not field.field.is_hidden %}
                            <div class="item form-cell {{ field.field.name }} {% if field.field.errors %} error{% endif %}">
                                {% if field.is_readonly %}
                                {{ field.contents }}
                                {% else %}
                                {{ field.field }}&nbsp;
                                {% endif %}
                                {{ field.field.errors.as_ul }}
                            </div>
                        {% endif %}
                    {% endfor %}
                {% endfor %}
            {% endfor %}
            <!-- delete/link/sort -->
            <div class="item form-cell inline-item-tools">
                <ul class="inline-item-tools">
                    {% if inline_admin_formset.formset.can_delete and inline_admin_form.original %}<li>{{ inline_admin_form.deletion_field.field }}<a href="javascript://" class="deletelink" title="{% trans 'Delete Item' %}"></a></li>{% endif %}
                    {% if inline_admin_form.show_url %}<li><a href="../../../r/{{ inline_admin_form.original.content_type_id }}/{{ inline_admin_form.original.id }}/" class="viewsitelink" title="{% trans 'View on Site' %}"></a></li>{% endif %}
                    {% comment %}{% if inline_admin_formset.opts.sortable %} <li><a href="javascript://" class="draghandler" title="{% trans 'Move Item' %}"></a></li>{% endif %}{% endcomment %}
                </ul>
            </div>
            {{ inline_admin_form.fk_field.field }}
            {% if inline_admin_form.needs_explicit_pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
        </div>
        </div>
        {% endfor %}
    </div>
</div>

<div class="sortablehelper">
    <h3><b>&nbsp;</b></h3>
</div>

<script type="text/javascript">

(function($) {
  var prefix = "{{ inline_admin_formset.formset.prefix|escapejs }}";
  var selector = "#" + prefix + "-group .inline-related"
  $(selector).stackedFormset(selector, {
    prefix: prefix,
    addText: "{% filter escapejs %}{% blocktrans with inline_admin_formset.opts.verbose_name|capfirst as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}{% endfilter %}",
    deleteText: "{% filter escapejs %}{% trans 'Delete Item' %}{% endfilter %}"
  });
  $(document).on("formset:added", function(event, $row, targetPrefix) {
    if (targetPrefix === prefix) {
        // Tweak the Delete link to resemble the ones defined in template
        var $deleteLink = $row.find(".inline-deletelink");
        $deleteLink
            .appendTo($row.find("ul.inline-item-tools"))
            .wrap("<li/>")
            .addClass("deletelink")
            .attr("title", $deleteLink.text())
            .text("");
        // Execute the TabularInlineAdded callback
        if ($.isFunction(window.__onTabularInlineAdded)) {
            window.__onTabularInlineAdded($row, targetPrefix);
        }
    }
  });
  $(document).on("formset:removed", function(event, $row, targetPrefix) {
    if (targetPrefix === prefix) {
        // Execute the StackedInlineRemoved callback
        if ($.isFunction(window.__onTabularInlineRemoved)) {
            window.__onTabularInlineRemoved($row, targetPrefix);
        }
    }
  });
})(django.jQuery);
</script>
