Skip to content

Commit

Permalink
Add sortable-group#prepare to stash itemPosition
Browse files Browse the repository at this point in the history
itemPosition causes synchronous layout recalculations. We want
it happening as little as possible. Now it is stashed on
_startDrag and cleared on complete.
  • Loading branch information
jgwhite committed Apr 29, 2015
1 parent 551aa30 commit 9c55c8b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
20 changes: 19 additions & 1 deletion addon/components/sortable-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,30 @@ export default Component.extend({
this.get('items').removeObject(item);
},

/**
Prepare for sorting.
Main purpose is to stash the current itemPosition so
we don’t incur expensive re-layouts.
@method prepare
*/
prepare() {
this._itemPosition = this.get('itemPosition');
},

/**
Update item positions.
@method update
*/
update() {
let sortedItems = this.get('sortedItems');
let y = this.get('itemPosition');
let y = this._itemPosition;

// Just in case we haven’t called prepare first.
if (y === undefined) {
y = this.get('itemPosition');
}

sortedItems.forEach(item => {
if (!get(item, 'isDragging')) {
Expand All @@ -80,6 +96,8 @@ export default Component.extend({
let items = this.get('sortedItems');
let models = items.mapBy('model');

delete this._itemPosition;

this.sendAction('onChange', models);
}
});
11 changes: 6 additions & 5 deletions addon/mixins/sortable-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ export default Mixin.create({
@type Number
*/
y: computed(function(_, value) {
if (arguments.length === 2) {
if (arguments.length === 2 && value !== this._y) {
this._y = value;
this._scheduleApplyPosition();
}

if (this._y !== undefined) {
return this._y;
} else {
return this.element.offsetTop;
if (this._y === undefined) {
this._y = this.element.offsetTop;
}

return this._y;
}),

/**
Expand Down Expand Up @@ -179,6 +179,7 @@ export default Mixin.create({
.on('mousemove touchmove', drag)
.on('mouseup touchend', drop);

this._tellGroup('prepare');
this.set('isDragging', true);
},

Expand Down

0 comments on commit 9c55c8b

Please sign in to comment.