Merge branch 'master' of git://github.com/melinath/philo
[philo.git] / templates / admin / philo / edit_inline / grappelli_tabular_attribute.html
1 {% load i18n adminmedia %}
2
3 <!-- group -->
4 <div class="group tabular{% if inline_admin_formset.opts.classes %} {{ inline_admin_formset.opts.classes|join:" " }}{% endif %}"
5         id="{{ inline_admin_formset.formset.prefix }}-group" >
6         <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
7         <ul class="tools">
8                 <li class="add-handler-container"><a href="javascript://" class="icon add-handler" title="{% trans 'Add Another' %}"> </a></li>
9         </ul>
10         {{ inline_admin_formset.formset.management_form }}
11         {{ inline_admin_formset.formset.non_form_errors }}
12         <!-- container -->
13         <div class="module table">
14                 <div class="module thead">
15                         <div class="tr">
16                                 {% for field in inline_admin_formset.fields %}
17                                         {% if not field.widget.is_hidden %}
18                                                 <div class="th {{ field.label|lower }}{% if field.required %} required{% endif %}">{{ field.label|capfirst }}</div>
19                                         {% endif %}
20                                 {% endfor %}
21                                 <div class="th content_type">Content type</div>
22                                 <div class="th value">Value</div>
23                                 {% if inline_admin_formset.formset.can_delete %}<div class="th">&nbsp;</div>{% endif %}
24                         </div>
25                 </div>
26                 {% for inline_admin_form in inline_admin_formset %}
27                         <!-- element -->
28                         <div class="module tbody{% if inline_admin_form.original or inline_admin_form.show_url %} has_original{% endif %}{% if forloop.last %} empty-form{% endif %}"
29                                 id="{{ inline_admin_formset.formset.prefix }}{% if not forloop.last %}{{ forloop.counter0 }}{% else %}-empty{% endif %}">
30                                 {% if inline_admin_form.form.non_field_errors %}
31                                         <ul class="errorlist"><li>{{ inline_admin_form.form.non_field_errors }}</li></ul>
32                                 {% endif %}
33                                 <h3 style="display: none;"><b>{{ inline_admin_formset.opts.verbose_name|title }} #{{ forloop.counter }}</b>&nbsp;&nbsp;{% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}</h3>
34                                 {% spaceless %}
35                                 {% for fieldset in inline_admin_form %}
36                                         {% for line in fieldset %}
37                                                 {% for field in line %}
38                                                         {% if field.is_hidden %} {{ field.field }} {% endif %}
39                                                 {% endfor %}
40                                         {% endfor %}
41                                 {% endfor %}
42                                 {% endspaceless %}
43                                 <div class="tr">
44                                         {% for fieldset in inline_admin_form %}
45                                                 {% for line in fieldset %}
46                                                         {% for field in line %}
47                                                                 <div class="td {{ field.field.name }} {% if field.field.errors %} error{% endif %}">
48                                                                         {% if field.is_readonly %}
49                                                                                 <p>{{ field.contents }}</p>
50                                                                         {% else %}
51                                                                                 {{ field.field }}
52                                                                                 {{ field.field.errors.as_ul }}
53                                                                         {% endif %}
54                                                                 </div>
55                                                         {% endfor %}
56                                                 {% endfor %}
57                                         {% endfor %}
58                                         {% with inline_admin_form.form.content_type as field %}<div class="td {{ field.name }}{% if field.errors %} error{% endif %}">{{ field }}{{ field.errors.as_ul }}</div>{% endwith %}
59                                         {% with inline_admin_form.form.value as field %}<div class="td {{ field.name }}{% if field.errors %} error{% endif %}">{{ field }}{{ field.errors.as_ul }}</div>{% endwith %}
60                                         <div class="td tools">
61                                                 {% spaceless %}
62                                                 <ul class="tools">
63                                                         {% 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' %}"> </a></li>{% endif %}
64                                                         {% if inline_admin_formset.opts.sortable_field_name %}
65                                                                 <li class="drag-handler-container"><a href="javascript://" class="icon drag-handler" title="{% trans 'Move Item' %}"></a></li>
66                                                         {% endif %}
67                                                         {% if inline_admin_formset.formset.can_delete %}<li class="delete-handler-container">{{ inline_admin_form.deletion_field.field }}<a href="javascript://" class="icon {% if inline_admin_form.original %}delete-handler{% else %}remove-handler{% endif %}" title="{% trans 'Remove' %}"> </a>{% endif %}
68                                                 </ul>
69                                                 {% endspaceless %}
70                                         </div>
71                                         {{ inline_admin_form.fk_field.field }}
72                                         {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
73                                 </div>
74                         </div>
75                 {% endfor %}
76         </div>
77         <div class="module add-item">
78                 <a href="javascript://" class="add-handler">{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}</a>
79                 <ul class="tools">
80                         <li class="add-handler-container"><a href="javascript://" class="icon add-handler" title="{% trans 'Add Item' %}"> </a></li>
81                 </ul><br clear="all" />
82         </div>
83 </div>
84
85 <script type="text/javascript">
86 (function($) {
87         $(document).ready(function($) {
88                 
89                 $("#{{ inline_admin_formset.formset.prefix }}-group").inline({
90                         prefix: "{{ inline_admin_formset.formset.prefix }}",
91                         deleteCssClass: "delete-handler",
92                         emptyCssClass: "empty-form",
93                         onAdded: tabular_onAdded
94                 });
95                 
96 {% if inline_admin_formset.opts.sortable_field_name %}
97                 /**
98                  * sortable inlines
99                  * uses onAdded() and onRemoved() of inline() call above
100                  * uses sortable_updateFormIndex() and is_form_filled() from change_from.html
101                  */
102                 
103                 // hide sortable_field(_name) from form
104                 // hide div.td.{{ field.name }}
105                 var position_nodes = $("#{{ inline_admin_formset.formset.prefix }}-group").find("div.td.{{ inline_admin_formset.opts.sortable_field_name }}");
106                 position_nodes.hide();
107                 
108                 // hide its header (div.th) too (hard)
109                 // "div.th.{{ inline_admin_formset.opts.sortable_field_name }}" is not correct because
110                 // its div.th.<field.label> (and not name, see line#18).
111                 
112                 // so let's get the "position/idx" the first position div
113                 var tabular_row = position_nodes.first().parent().children("div.td");
114                 // get the "position" (== i) in the "table"
115                 for (var i = 0; i < tabular_row.length; i++) {
116                         if ($(tabular_row[i]).hasClass("{{ inline_admin_formset.opts.sortable_field_name }}")) break;
117                 }
118                 // we have the same order in the header of the "table"
119                 // so delete the div at the "position" (== i)
120                 var position_header = $("#{{ inline_admin_formset.formset.prefix }}-group").find("div.th")[i];
121                 // and hide
122                 $(position_header).hide()
123                 
124         {% if errors %}
125                 // sort inline
126                 var container = $("#{{ inline_admin_formset.formset.prefix }}-group > div.table"),
127                         dynamic_forms = container.find("div.dynamic-form"),
128                         updated = false,
129                         curr_form,
130                         real_pos;
131                 
132                 // loop thru all inline forms
133                 for (var i = 0; i < dynamic_forms.length; i++) {
134                         curr_form = $(dynamic_forms[i]);
135                         // the real position according to the sort_field(_name)
136                         real_pos = curr_form.find("div.{{ inline_admin_formset.opts.sortable_field_name }}").find("input").val();
137                         // if there is none it's an empty inline (=> we are at the end)
138                         // TODO: klemens: maybe buggy. try continue?
139                         if (!real_pos) continue;
140                         
141                         real_pos = parseInt(real_pos, 10);
142                         
143                         // check if real position is not equal to the CURRENT position in the dom
144                         if (real_pos != container.find("div.dynamic-form").index(curr_form)) {
145                                 // move to correct postition
146                                 curr_form.insertBefore(container.find("div.dynamic-form")[real_pos]);
147                                 // to update the inline lables
148                                 updated = true;
149                         }
150                 }
151                 
152         {% endif %}
153                 
154                 $("#{{ inline_admin_formset.formset.prefix }}-group > div.table").sortable({
155                         // drag&drop the inlines with the drag-handler only
156                         handle: "a.drag-handler",
157                         // very scary magic after drap&drop operations
158                         // pretty similar to inline() widget's removeHandler()
159                         // but removeHandler() can remove the current form and just reorder the rest
160                         // if we would do the same after drag&drop we would loose some form values
161                         // because while looping inputs would have the same names and maybe overwrite each other.
162                         placeholder: 'ui-sortable-placeholder',
163                         forcePlaceholderSize: true,
164                         items: "div.dynamic-form",
165                         axis: "y",
166                         start: function(evt, ui) {
167                                 ui.item.hide()
168                                 ui.placeholder.height(ui.placeholder.height()-4);
169                                 //sadly we have to do this every time we start dragging
170                                 var template = "<div class='tr'>",
171                                         // minus 1 because we don't need the "sortable_field_name row"
172                                         len = ui.item.find("div.tr").children("div.td").length - 1;
173                                 
174                                 for (var i = 0; i < len; i++) {
175                                         template += "<div class='td'></div>"
176                                 }
177                                 
178                                 template += "</div>"
179                                 ui.placeholder.addClass("tbody module").append(template);
180                         },
181                         update: function(evt, ui) {
182                                 ui.item.show();
183                         },
184                         appendTo: 'body',
185                         forceHelperSize: true,
186                         containment: '#{{ inline_admin_formset.formset.prefix }}-group > div.table',
187                         tolerance: 'pointer',
188                         helper: function(evt, elem) {
189                                 var helper = $("<div class='module table' />");
190                                 helper.html(elem.clone());
191                                 return helper;
192                         },
193                 });
194                 
195                 // sets the new positions onSubmit (0 - n)
196                 $("#{{ opts.module_name }}_form").bind("submit", function(){
197                         var forms = $("#{{ inline_admin_formset.formset.prefix }}-group").find("div.dynamic-form"),
198                                 form,
199                                 idx = 0;
200                         for (var i = 0; i < forms.length; i++) {
201                                 form = $(forms[i]);
202                                 
203                                 if (is_form_filled(form)) {
204                                         form.find("div.{{ inline_admin_formset.opts.sortable_field_name }}").find("input").val(idx);
205                                         idx++;
206                                 }
207                         }
208                 });
209                 
210 {% endif %}
211         
212         });     
213 })(django.jQuery);
214 </script>
215