Sha256: 2312ee4ee8aa8243880356865405567cecc240916b3e765c378991221e7bfb2d

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

(function($) {
  $(document).ready(function() {
    $('.handle').closest('tbody').activeAdminSortableTable();
  });

  $.fn.activeAdminSortableTable = function() {
    this.sortable({
      handle: ".handle",
      update: function(event, ui) {
        var item = ui.item.find('[data-sort-url]');
        var url = item.data('sort-url');
        var customParams = {};
        if (typeof item.data('sort-custom-params') === 'object') {
          customParams = item.data('sort-custom-params');
        }

        var nextElement = ui.item[0].nextElementSibling;

        var nextPosition = $(nextElement).find('[data-position]').data('position');
        var currentPosition = ui.item.find('[data-position]').data('position');

        if(nextPosition === undefined || nextPosition > currentPosition) {
          // moved down
          var previousElement = ui.item[0].previousElementSibling;
          var previousPosition = $(previousElement).find('[data-position]').data('position');
          var newPosition = previousPosition;
        } else {
          // moved up
          var newPosition = nextPosition;
        }

        $.ajax({
          url: url,
          type: 'post',
          data: $.extend(customParams, { position: newPosition }),
          error: function () { console.error('Saving sortable error'); },
          success: function () {
            location.href = location.href;
          },
          async: false
        });
      }
    });

    this.disableSelection();
  }
})(jQuery);

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activeadmin_sortable_table-1.3.0 app/assets/javascripts/activeadmin_sortable_table.js
activeadmin_sortable_table-1.2.0 app/assets/javascripts/activeadmin_sortable_table.js
activeadmin_sortable_table-1.1.3 app/assets/javascripts/activeadmin_sortable_table.js