summaryrefslogtreecommitdiff
path: root/app/lib/grappelli/templates/admin/edit_inline/stacked.html
blob: 7112ba246ee0de4b9cb875c4b59c1b35be261b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{% load i18n adminmedia %}

<!-- group -->
<div class="group stacked {% if inline_admin_formset.opts.classes %} {{ inline_admin_formset.opts.classes|join:" " }}{% endif %}"
    id="{{ inline_admin_formset.formset.prefix }}-group">
    <h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2>
    <ul class="tools">
        <li class="open-handler-container"><a href="javascript://" class="icon open-handler" title="{% trans 'Open All Items' %}"></a></li>
        <li class="close-handler-container"><a href="javascript://" class="icon close-handler" title="{% trans 'Close All Items' %}"></a></li>
        <li class="add-handler-container"><a href="javascript://" class="icon add-handler" title="{% trans 'Add Item' %}"> </a></li>
    </ul>
    {{ inline_admin_formset.formset.management_form }}
    {{ inline_admin_formset.formset.non_form_errors }}
    <!-- container -->
    <div class="items">
        {% for inline_admin_form in inline_admin_formset %}
            <!-- element -->
            <div class="module collapse closed{% 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 forloop.last %}-empty{% else %}{{ forloop.counter0 }}{% endif %}">
                <h3>{{ inline_admin_formset.opts.verbose_name|title }} #{{ forloop.counter }}&nbsp;&nbsp;{% if inline_admin_form.original %}{{ inline_admin_form.original }}{% endif %}</h3>
                <ul class="tools">
                    {% if inline_admin_form.show_url %}<li class="viewsite-link-container"><a href="../../../r/{{ inline_admin_form.original.content_type_id }}/{{ inline_admin_form.original.id }}/" class="icon viewsite-link" title="{% trans 'View on Site' %}" target="_blank"></a></li>{% endif %}
                    {% if inline_admin_formset.opts.sortable_field_name %}
                        <li class="drag-handler-container"><a href="javascript://" class="icon drag-handler" title="{% trans 'Move Item' %}"></a></li>
                    {% endif %}
                    {% if inline_admin_formset.formset.can_delete %}
                        {% if inline_admin_form.original %}
                            <li class="delete-handler-container">{{ inline_admin_form.deletion_field.field }}<a href="javascript://" class="icon delete-handler" title="{% trans 'Delete Item' %}"></a></li>
                        {% else %}
                            <li class="remove-handler-container">{{ inline_admin_form.deletion_field.field }}<a href="javascript://" class="icon remove-handler" title="{% trans 'Delete Item' %}"></a></li>
                        {% endif %}
                    {% endif %}
                </ul>
                {% if inline_admin_form.form.non_field_errors %}
                    <ul class="errorlist">
                        <li>{{ inline_admin_form.form.non_field_errors }}</li>
                    </ul>
                {% endif %}
                {% for fieldset in inline_admin_form %}
                    {% include "admin/includes/fieldset_inline.html" %}
                {% endfor %}
                {{ inline_admin_form.pk_field.field }}
                {{ inline_admin_form.fk_field.field }}
            </div>
        {% endfor %}
        {{ inline_admin_formset.extra_forms }}
    </div>
    <div class="module add-item">
        <a href="javascript://" class="add-handler">{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}</a>
        <ul class="tools">
            <li class="add-handler-container"><a href="javascript://" class="icon add-handler" title="{% trans 'Add Item' %}"> </a></li>
        </ul><br clear="all" />
    </div>
</div>

<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        
        // call for inline() widget
        $("#{{ inline_admin_formset.formset.prefix }}-group").inline({
            prefix: "{{ inline_admin_formset.formset.prefix }}",
            deleteCssClass: "delete-handler",
            emptyCssClass: "empty-form",
            onRemoved: stacked_onRemoved,
            onAdded: stacked_onAdded,
        });
        
{% if inline_admin_formset.opts.sortable_field_name %}
        /**
         * sortable inlines
         * uses onAdded() and onRemoved() of inline() call above
         */
        
        $("#{{ inline_admin_formset.formset.prefix }}-group").find("div.row.{{ inline_admin_formset.opts.sortable_field_name }}").hide();
        
    {% if errors %}
        // sort inline
        var container = $("#{{ inline_admin_formset.formset.prefix }}-group > div.items"),
            dynamic_forms = container.find("div.dynamic-form"),
            updated = false,
            curr_form,
            real_pos;
        
        // loop thru all inline forms
        for (var i = 0; i < dynamic_forms.length; i++) {
            curr_form = $(dynamic_forms[i]);
            // the real position according to the sort_field(_name)
            real_pos = curr_form.find("div.{{ inline_admin_formset.opts.sortable_field_name }}").find("input").val();
            // if there is none it's an empty inline (=> we are at the end)
            // TODO: klemens: maybe buggy. try continue?
            if (!real_pos) continue;
            
            real_pos = parseInt(real_pos, 10);
            
            // check if real position is not equal to the CURRENT position in the dom
            if (real_pos != container.find("div.dynamic-form").index(curr_form)) {
                // move to correct postition
                curr_form.insertBefore(container.find("div.dynamic-form")[real_pos]);
                // to update the inline lables
                updated = true;
            }
        }
        if (updated) {
            stacked_updateInlineLabel($(dynamic_forms[0]));
        }
        
    {% endif %}
        
        // activate sortable feature for this inline
        $("#{{ inline_admin_formset.formset.prefix }}-group > div.items").sortable({
            // drag&drop the inlines with the drag-handler only
            handle: "a.drag-handler",
            // very scary magic after drap&drop operations
            // pretty similar to inline() widget's removeHandler()
            // but removeHandler() can remove the current form and just reorder the rest
            // if we would do the same after drag&drop we would loose some form values
            // because while looping inputs would have the same names and maybe overwrite each other.
            placeholder: 'ui-sortable-placeholder module',
            forcePlaceholderSize: true,
            items: "div.dynamic-form",
            axis: "y",
            containment: 'parent',
            tolerance: 'pointer',
            update: function(evt, ui) {
                stacked_updateInlineLabel(ui.item);
            }
        });
        
        // sets the new positions onSubmit (0 - n)
        $("#{{ opts.module_name }}_form").bind("submit", function() {
            var forms = $("#{{ inline_admin_formset.formset.prefix }}-group").find("div.dynamic-form"),
                form,
                idx = 0;
            for (var i = 0; i < forms.length; i++) {
                form = $(forms[i]);
                if (is_form_filled(form)) {
                    form.find("div.{{ inline_admin_formset.opts.sortable_field_name }}").find("input").val(idx);
                    idx++;
                }
            }
        });
        
{% endif %}
    });
})(django.jQuery);
</script>