Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

Commit

Permalink
Merge pull request #64 from uberVU/64-find-left-most-position
Browse files Browse the repository at this point in the history
findLeftMostPositionForItem logic is flawed
  • Loading branch information
andrei-picus-hs committed Aug 21, 2015
2 parents 20531d6 + 9a273bb commit d20e419
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
20 changes: 20 additions & 0 deletions spec/gridCollisionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,24 @@ describe("Grid collisions", function() {
expectedItems[16] = {x: 9, y: 2};
expect(grid.items).toEqualPositions(expectedItems);
});

it("should check across the entire height of an item when searching for a " +
"position", function() {
var items = [
{x: 0, y: 2, w: 2, h: 2},
{x: 2, y: 1, w: 1, h: 2},
{x: 3, y: 1, w: 2, h: 2}
];

var grid = new GridList(GridList.cloneItems(items), {rows: 4});

helpers.addIndexesToItems(grid.items);
grid.moveItemToPosition(grid.items[2], [3, 0]);
helpers.sortItemsByIndex(grid.items);

var expectedItems = GridList.cloneItems(items);
expectedItems[2].y = 0;

expect(grid.items).toEqualPositions(expectedItems);
});
});
17 changes: 10 additions & 7 deletions src/gridList.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,17 @@ GridList.prototype = {
*/
var tail = 0,
otherItem,
i;
i, j;

for (i = 0; i < this.grid.length; i++) {
otherItem = this.grid[i][item.y];
if (!otherItem) {
continue;
}
if (this.items.indexOf(otherItem) < this.items.indexOf(item)) {
tail = otherItem.x + otherItem.w;
for (j = item.y; j < item.y + item.h; j++) {
otherItem = this.grid[i][j];
if (!otherItem) {
continue;
}
if (this.items.indexOf(otherItem) < this.items.indexOf(item)) {
tail = otherItem.x + otherItem.w;
}
}
}
return tail;
Expand Down

0 comments on commit d20e419

Please sign in to comment.