Refactored sorting JS files to be includes so that server-side variables may be passed to them.

Added 'after_sorting_js_callback_name' attribute to SortableAdminBase.
Added callback to be executed after sorting for each of the possible sorting scenarios.
Added custom template examples to add a callback to be executed when sorting is finished.
This commit is contained in:
Brandon Taylor
2018-06-18 11:40:24 -04:00
parent fa8a5f12a8
commit e35f36b25a
10 changed files with 270 additions and 193 deletions
@@ -1,47 +0,0 @@
(function($){
$(function() {
jQuery('.sortable').sortable({
axis : 'y',
containment : 'parent',
tolerance : 'pointer',
items : 'li',
stop : function(event, ui) {
var indexes = [],
lineItems = ui.item.parent().find('> li');
lineItems.each(function(i) {
indexes.push($(this).find(':hidden[name="pk"]').val());
});
$.ajax({
url: ui.item.find('a.admin_sorting_url').attr('href'),
type: 'POST',
data: { indexes: indexes.join(',') },
success: function() {
// set icons based on position
lineItems.each(function(index, element) {
var icon = $(element).find('a.admin_sorting_url .fa');
icon.removeClass('fa-sort-desc fa-sort-asc fa-sort');
if (index === 0) {
icon.addClass('fa fa-sort-desc');
}
else if (index == lineItems.length - 1) {
icon.addClass('fa fa-sort-asc');
}
else {
icon.addClass('fa fa-sort');
}
});
ui.item.effect('highlight', {}, 1000);
}
});
}
}).click(function(e){
e.preventDefault();
});
});
})(django.jQuery);
@@ -1,70 +0,0 @@
(function($){
$(function() {
var sorting_urls = $(':hidden[name="admin_sorting_url"]');
if (sorting_urls.length > 0)
{
var sortable_inline_groups = sorting_urls.closest('.inline-group')
var sortable_inline_rows = sortable_inline_groups.find('.inline-related');
sortable_inline_groups.addClass('sortable')
sortable_inline_rows.addClass('sortable');
sortable_inline_groups.sortable({
axis : 'y',
containment : 'parent',
create: function(event, ui) {
$('.inline-related :checkbox').unbind();
},
tolerance : 'pointer',
items : '.inline-related',
stop : function(event, ui)
{
if ($('.inline-deletelink').length > 0) {
$(ui.sender).sortable('cancel');
alert($('#localized_save_before_reorder_message').val());
return false;
}
var indexes = [];
ui.item.parent().children('.inline-related').each(function(i)
{
var index_value = $(this).find(':hidden[name$="-id"]').val();
if (index_value !== "" && index_value !== undefined) {
indexes.push(index_value);
}
});
$.ajax({
url: ui.item.parent().find(':hidden[name="admin_sorting_url"]').val(),
type: 'POST',
data: { indexes : indexes.join(',') },
success: function() {
var fieldsets = ui.item.find('fieldset'),
highlightedSelector = fieldsets.filter('.collapsed').length === fieldsets.length ? 'h3' : '.form-row',
icons = ui.item.parent().find('h3 > .fa');
// set icons based on position
icons.removeClass('fa-sort-desc fa-sort-asc fa-sort');
icons.each(function(index, element) {
var icon = $(element);
if (index === 0) {
icon.addClass('fa fa-sort-desc');
}
else if (index == icons.length - 1) {
icon.addClass('fa fa-sort-asc');
}
else {
icon.addClass('fa fa-sort');
}
});
ui.item.find(highlightedSelector).effect('highlight', {}, 1000);
}
});
}
});
}
});
})(django.jQuery);
@@ -1,69 +0,0 @@
(function($){
$(function() {
var sorting_urls = $(':hidden[name="admin_sorting_url"]');
if (sorting_urls.length)
{
var sortable_inline_group = sorting_urls.closest('.inline-group');
var tabular_inline_rows = sortable_inline_group.find('.tabular table tbody tr');
tabular_inline_rows.addClass('sortable');
sortable_inline_group.find('.tabular.inline-related').sortable({
axis : 'y',
containment : 'parent',
create: function(event, ui) {
$('td.delete :checkbox').unbind();
},
tolerance : 'pointer',
items : 'tr:not(.add-row)',
stop : function(event, ui) {
if ($('.inline-deletelink').length > 0) {
$(ui.sender).sortable('cancel');
alert($('#localized_save_before_reorder_message').val());
return false;
}
var indexes = [];
ui.item.parent().children('tr').each(function(i)
{
var index_value = $(this).find('.original :input:first').val();
if (index_value !== '' && index_value !== undefined) {
indexes.push(index_value);
}
});
$.ajax({
url: ui.item.parent().find(':hidden[name="admin_sorting_url"]').val(),
type: 'POST',
data: { indexes : indexes.join(',') },
success: function() {
// set icons based on position
var icons = ui.item.parent().find('.fa');
icons.removeClass('fa-sort-desc fa-sort-asc fa-sort');
icons.each(function(index, element) {
var icon = $(element);
if (index === 0) {
icon.addClass('fa fa-sort-desc');
}
else if (index == icons.length - 1) {
icon.addClass('fa fa-sort-asc');
}
else {
icon.addClass('fa fa-sort');
}
});
// highlight sorted row, then re-stripe table
ui.item.effect('highlight', {}, 1000);
tabular_inline_rows.removeClass('row1 row2');
$('.tabular table tbody tr:odd').addClass('row2');
$('.tabular table tbody tr:even').addClass('row1');
}
});
}
});
}
});
})(django.jQuery);