Skip to content
This repository has been archived by the owner on Aug 2, 2018. It is now read-only.

Commit

Permalink
Fix gap calculations #17
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalec committed Jun 23, 2014
1 parent e2a43e9 commit 87419bd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = function(grunt) {
},
files: [
'*.html',
'src/*.html',
'examples/**/*.html',
'tests/*.js'
],
Expand Down
12 changes: 8 additions & 4 deletions src/packer.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ Packer.prototype.placeAt = function( rectangle, slot ) {
*/
Packer.prototype.placed = function( rectangle ) {
if(this.gap){
rectangle.width += this.gap;
rectangle.height += this.gap;
rectangle.width += 2 * this.gap;
rectangle.height += 2 * this.gap;
rectangle.x -= this.gap;
rectangle.y -= this.gap;
}
// update slots
var revisedSlots = [];
Expand All @@ -118,8 +120,10 @@ Packer.prototype.placed = function( rectangle ) {
}
}
if(this.gap){
rectangle.width -= this.gap;
rectangle.height -= this.gap;
rectangle.width -= 2 * this.gap;
rectangle.height -= 2 * this.gap;
rectangle.x += this.gap;
rectangle.y += this.gap;
}
// stretch container
this.minWidth = Math.max( rectangle.x + rectangle.width, this.minWidth );
Expand Down
47 changes: 47 additions & 0 deletions tests/packageSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,53 @@ describe('Package', function() {
expect(packedTree.items[1]).to.have.property("x").equal(0, "should not fit in second column");
expect(packedTree.items[1]).to.have.property("y").equal(60, "second row should be 10 (gap) lower");
});
it('in root (more complicated example (issue #17)', function() {
var setup = {
"gap": 25,
"items": [

{
"index": 0,
"priority": 0.9,
"height": 25,
"width": 200,
},
{
"index": 1,
"priority": 0.8,
"height": 75,
"width": 100
},
{
"index": 2,
"priority": 0.7,
"height": 25,
"width": 100
},
{
"index": 3,
"priority": 0.6,
"height": 25,
"width": 100
}
],
"name": "root",
"width": 350
};
var pkg = new Package(setup);
var packedTree = pkg.packItems();
expect(packedTree.items[0]).to.have.property("x").equal(0);
expect(packedTree.items[0]).to.have.property("y").equal(0);

expect(packedTree.items[1]).to.have.property("x").equal(225, "should fit 25 after #0");
expect(packedTree.items[1]).to.have.property("y").equal(0);

expect(packedTree.items[2]).to.have.property("x").equal(0);
expect(packedTree.items[2]).to.have.property("y").equal(50, "should fit 25 below #0");

expect(packedTree.items[3]).to.have.property("x").equal(0, "should not fit between #1 and #2, should be below");
expect(packedTree.items[3]).to.have.property("y").equal(100);
});
it('in containers', function() {
var setup = {
width: 150,
Expand Down

0 comments on commit 87419bd

Please sign in to comment.