{% load i18n %}

<div id="{{ inline_admin_formset.formset.prefix }}-group"
     class="inline-group inline-stacked{% if inline_admin_formset.opts.sortable %} sortable{% endif %}{% if inline_admin_formset.opts.classes %} {{ inline_admin_formset.opts.classes|join:" " }}{% endif %}" name="inlinegroup">
    <h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2>
    <ul class="inline-item-tools">
        {% block inline-item-tools %}
            <li><a href="javascript://" class="openhandler" title="{% trans 'Open All Items' %}"></a></li>
            <li><a href="javascript://" class="closehandler" title="{% trans 'Close All Items' %}"></a></li>
            {% 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 }}
    <div class="items"> <!-- sortable container -->
        {% for inline_admin_form in inline_admin_formset %}
        <div id="{{ inline_admin_formset.formset.prefix }}-{% if not forloop.last %}{{ forloop.counter0 }}{% else %}empty{% endif %}"
             class="inline-related collapse-closed{% if inline_admin_form.original or inline_admin_form.show_url %} has_original{% endif %}{% if forloop.last %} empty-form{% endif %}"> <!-- sortable items -->
            <!-- headline, original, delete/link/sort -->
            <h3>
                {% for fieldset in inline_admin_form %}
                    {% for line in fieldset %}
                        {% for field in line %}
                            {% if field.field.name == "_order" %} {{ field.field }} &nbsp; {% endif %}
                        {% endfor %}
                    {% endfor %}
                {% endfor %}
                <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>
            <ul class="inline-item-tools">
                {% 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' %}" target="_blank"></a></li>{% else %}<li>&nbsp;</li>{% endif %}
                {% if inline_admin_formset.opts.sortable %}<li><a href="javascript://" class="draghandler" title="Move Item"></a></li>{% else %}<li>&nbsp;</li>{% endif %}
                {% 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>{% else %}<li>&nbsp;</li>{% endif %}
            </ul>
            <!-- fieldsets -->
            {% if inline_admin_form.form.non_field_errors %}{{ inline_admin_form.form.non_field_errors }}{% endif %}
            {% for fieldset in inline_admin_form %}
                {% include "admin/includes/fieldset.html" %}
            {% endfor %}
            {% if inline_admin_form.needs_explicit_pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
            {{ inline_admin_form.fk_field.field }}
        </div>
        {% endfor %}
        {{ inline_admin_formset.extra_forms }}
    </div>
</div>

<div class="sortablehelper">
    <h3><b>Sortable Helper</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 StackedInlineAdded callback
        if ($.isFunction(window.__onStackedInlineAdded)) {
            window.__onStackedInlineAdded($row, targetPrefix);
        }
    }
  });
  $(document).on("formset:removed", function(event, $row, targetPrefix) {
    if (targetPrefix === prefix) {
        // Execute the StackedInlineRemoved callback
        if ($.isFunction(window.__onStackedInlineRemoved)) {
            window.__onStackedInlineRemoved($row, targetPrefix);
        }
    }
  });
})(django.jQuery);
</script>
