Sha256: 826cd747895c618124165c29cd0d2493ee4cc1919643adb114ebb0ef7d7e7c46

Contents?: true

Size: 1.36 KB

Versions: 9

Compression:

Stored size: 1.36 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.updateOrder();

        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.$el.children().each(function(index) {
      $(this).data('view').model.set('position', index);
    });

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pageflow-0.3.0 app/assets/javascripts/pageflow/editor/views/sortable_collection_view.js
pageflow-0.2.1 app/assets/javascripts/pageflow/editor/views/sortable_collection_view.js
pageflow-0.2.0 app/assets/javascripts/pageflow/editor/views/sortable_collection_view.js
pageflow-0.1.0 app/assets/javascripts/pageflow/editor/views/sortable_collection_view.js
pageflow-0.0.5 app/assets/javascripts/pageflow/editor/views/sortable_collection_view.js
pageflow-0.0.4 app/assets/javascripts/pageflow/editor/views/sortable_collection_view.js
pageflow-0.0.3 app/assets/javascripts/pageflow/editor/views/sortable_collection_view.js
pageflow-0.0.2 app/assets/javascripts/pageflow/editor/views/sortable_collection_view.js
pageflow-0.0.1 app/assets/javascripts/pageflow/editor/views/sortable_collection_view.js