inline-sortable.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // make tabular- and stacked inlines sortable
  2. jQuery(function($) {
  3. $('div.inline-group.sortable').each(function() {
  4. var default_order_field = $(this).find('.default_order_field').attr('default_order_field');
  5. var order_input_field = 'input[name$="-' + default_order_field + '"]';
  6. // first, try with tabluar inlines
  7. var tabular_inlines = $(this).find('div.tabular table');
  8. tabular_inlines.sortable({
  9. handle: $(this).find('tbody .drag'),
  10. items: 'tr.form-row.has_original',
  11. axis: 'y',
  12. scroll: true,
  13. cursor: 'ns-resize',
  14. containment: $(this).find('tbody'),
  15. stop: function(event, dragged_rows) {
  16. var $result_list = $(this);
  17. $result_list.find('tbody tr').each(function(index) {
  18. $(this).removeClass('row1 row2').addClass(index % 2 ? 'row2' : 'row1');
  19. });
  20. $result_list.find('tbody tr.has_original').each(function(index) {
  21. $(this).find(order_input_field).val(index + 1);
  22. });
  23. }
  24. });
  25. if (tabular_inlines.length)
  26. return true;
  27. // else, try with stacked inlines
  28. $(this).find('.stacked-inline-list').each(function() {
  29. $(this).sortable({
  30. items: '.stacked-inline-list-item.has_original',
  31. axis: 'y',
  32. scroll: true,
  33. cursor: 'move',
  34. stop: function(event, dragged_rows) {
  35. var $result_list = $(this);
  36. $result_list.find('.stacked-inline-list-item.has_original .stacked-inline-list-item-link').each(function(index) {
  37. var id = $(this).data('inline-related-id');
  38. $('#' + id).find(order_input_field).val(index + 1);
  39. });
  40. }
  41. });
  42. });
  43. });
  44. });