/* dynamic_inlines_with_sort.js */
/* Created in May 2009 by Hannes Rydén */
/* Use, distribute and modify freely */
// "Add"-link html code. Defaults to Django's "+" image icon, but could use text instead.
add_link_html = '';
// "Delete"-link html code. Defaults to Django's "x" image icon, but could use text instead.
delete_link_html = '
';
position_field = 'position'; // Name of inline model field (integer) used for ordering. Defaults to "position".
jQuery(function($) {
// This script is applied to all TABULAR inlines
$('div.inline-group div.tabular').each(function() {
table = $(this).find('table');
// Hide initial extra row and prepare it to be used as a template for new rows
add_template = table.find('tr:last');
add_template.addClass('add_template').hide();
table.prepend(add_template);
// Hide initial deleted rows
table.find('td.delete input:checkbox:checked').parent('td').parent('tr').addClass('deleted_row').hide();
// "Add"-button in bottom of inline for adding new rows
$(this).find('fieldset').after('' + add_link_html + '');
$(this).find('a.add').click(function(){
old_item = $(this).parent().find('table tr.add_template')
new_item = old_item.clone(true);
create_delete_button(new_item.find('td.delete'));
new_item.removeClass('add_template').show();
$(this).parent().find('table').append(new_item);
update_positions($(this).parent().find('table'), true);
// Place for special code to re-enable javascript widgets after clone (e.g. an ajax-autocomplete field)
// Fictive example: new_item.find('.autocomplete').each(function() { $(this).triggerHandler('autocomplete'); });
}).removeAttr('href').css('cursor', 'pointer');
// "Delete"-buttons for each row that replaces the default checkbox
table.find('tr:not(.add_template) td.delete').each(function() {
create_delete_button($(this));
});
// Drag and drop functionality - only used if a position field exists
if (position_field != '' && table.find('td').is('.' + position_field))
{
// Hide "position"-field (both td:s and th:s)
$(this).find('td.' + position_field).hide();
td_pos_field_index = table.find('tbody tr td').index($(this).find('td.' + position_field));
$(this).find('th:eq(' + (td_pos_field_index-1) + ')').hide();
// Hide "original"-field and set any colspan to 1 (why show in the first case?)
$(this).find('td.original').hide();
$(this).find('th[colspan]').removeAttr('colspan');
// Make table sortable using jQuery UI Sortable
table.sortable({
items: 'tr:has(td)',
tolerance: 'pointer',
axis: 'y',
cancel: 'input,button,select,a',
helper: 'clone',
update: function() {
update_positions($(this));
}
});
// Re-order