Sha256: 0115b06604509589b13e7824ea43fa8797094615a4ef570c4a8c57c19ab55103

Contents?: true

Size: 1.43 KB

Versions: 76

Compression:

Stored size: 1.43 KB

Contents

pageflow.SortableCollectionView = pageflow.CollectionView.extend({
  render: function() {
    pageflow.CollectionView.prototype.render.call(this);

    this.$el.sortable({
      connectWith: this.options.connectWith,
      placeholder: 'sortable-placeholder',
      forcePlaceholderSize: true,
      delay: 200,

      update: _.bind(function(event, ui) {
        if (ui.item.parent().is(this.el)) {
          this.updateOrder();
        }
      }, this),

      receive: _.bind(function(event, ui) {
        var view = ui.item.data('view');

        this.reindexPositions();

        this.itemViews.add(view);
        this.collection.add(view.model);
      }, this),

      remove: _.bind(function(event, ui) {
        var view = ui.item.data('view');

        this.itemViews.remove(view);
        this.collection.remove(view.model);
      }, this)
    });

    return this;
  },

  addItem: function(item) {
    if (!this.itemViews.findByModel(item)) {
      pageflow.CollectionView.prototype.addItem.call(this, item);
    }
  },

  removeItem: function(item) {
    if (this.itemViews.findByModel(item)) {
      pageflow.CollectionView.prototype.removeItem.call(this, item);
    }
  },

  updateOrder: function() {
    this.reindexPositions();

    this.collection.sort();
    this.collection.saveOrder();
  },

  reindexPositions: function() {
    this.$el.children().each(function(index) {
      $(this).data('view').model.set('position', index);
    });
  }
});

Version data entries

76 entries across 76 versions & 1 rubygems

Version Path
pageflow-14.5.2 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-15.0.2 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-15.0.1 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-15.0.0 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.5.1 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.5.0 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-15.0.0.rc2 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-15.0.0.rc1 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-15.0.0.beta4 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-15.0.0.beta3 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.4.0 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-15.0.0.beta2 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-15.0.0.beta1 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.3.0 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.2.1 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.2.0 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.1.1 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.1.0 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.0.0 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js
pageflow-14.0.0.rc2 app/assets/javascripts/pageflow/ui/views/sortable_collection_view.js