Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ Changes
- add oneColumnModeClass option to grid.
- remove 768px CSS styles, moved to grid-stack-one-column-mode class.
- add max-width override on grid-stck-one-column-mode ([#462](https://github.com/troolee/gridstack.js/issues/462)).
- add internal function`isNodeChangedPosition`, minor optimization to move/drag.

#### v0.2.6 (2016-08-17)

Expand Down
23 changes: 23 additions & 0 deletions dist/gridstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@
};

GridStackEngine.prototype.canMoveNode = function(node, x, y, width, height) {
if (!this.isNodeChangedPosition(node, x, y, width, height)) {
return false;
}
var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; }));

if (!this.height && !hasLocked) {
Expand Down Expand Up @@ -406,7 +409,27 @@
return clone.getGridHeight() <= this.height;
};

GridStackEngine.prototype.isNodeChangedPosition = function(node, x, y, width, height) {
if (typeof x != 'number') { x = node.x; }
if (typeof y != 'number') { y = node.y; }
if (typeof width != 'number') { width = node.width; }
if (typeof height != 'number') { height = node.height; }

if (typeof node.maxWidth != 'undefined') { width = Math.min(width, node.maxWidth); }
if (typeof node.maxHeight != 'undefined') { height = Math.min(height, node.maxHeight); }
if (typeof node.minWidth != 'undefined') { width = Math.max(width, node.minWidth); }
if (typeof node.minHeight != 'undefined') { height = Math.max(height, node.minHeight); }

if (node.x == x && node.y == y && node.width == width && node.height == height) {
return false;
}
return true;
};

GridStackEngine.prototype.moveNode = function(node, x, y, width, height, noPack) {
if (!this.isNodeChangedPosition(node, x, y, width, height)) {
return node;
}
if (typeof x != 'number') { x = node.x; }
if (typeof y != 'number') { y = node.y; }
if (typeof width != 'number') { width = node.width; }
Expand Down
10 changes: 3 additions & 7 deletions dist/gridstack.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gridstack.min.map

Large diffs are not rendered by default.

81 changes: 57 additions & 24 deletions spec/gridstack-engine-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('gridstack engine', function() {

beforeAll(function() {
engine = new GridStackUI.Engine(12);
})
});

it('should be setup properly', function() {
expect(engine.width).toEqual(12);
Expand All @@ -29,7 +29,7 @@ describe('gridstack engine', function() {

beforeAll(function() {
engine = new GridStackUI.Engine(12);
})
});

it('should prepare a node', function() {
expect(engine._prepareNode({}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1}));
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('gridstack engine', function() {
engine.nodes = [
engine._prepareNode({x: 3, y: 2, width: 3, height: 2})
];
})
});

it('should be true', function() {
expect(engine.isAreaEmpty(0, 0, 3, 2)).toEqual(true);
Expand Down Expand Up @@ -108,14 +108,14 @@ describe('gridstack engine', function() {
});
});

describe('test batchUpdate/commit', function () {
describe('test batchUpdate/commit', function() {
var engine;

beforeAll(function () {
engine = new GridStackUI.Engine(12)
beforeAll(function() {
engine = new GridStackUI.Engine(12);
});

it('should work on not float grids', function () {
it('should work on not float grids', function() {
expect(engine.float).toEqual(false);
engine.batchUpdate();
expect(engine._updateCounter).toBeGreaterThan(0);
Expand All @@ -126,14 +126,14 @@ describe('gridstack engine', function() {
});
});

describe('test batchUpdate/commit', function () {
describe('test batchUpdate/commit', function() {
var engine;

beforeAll(function () {
engine = new GridStackUI.Engine(12, null, true)
beforeAll(function() {
engine = new GridStackUI.Engine(12, null, true);
});

it('should work on float grids', function () {
it('should work on float grids', function() {
expect(engine.float).toEqual(true);
engine.batchUpdate();
expect(engine._updateCounter).toBeGreaterThan(0);
Expand All @@ -150,8 +150,8 @@ describe('gridstack engine', function() {

beforeEach(function() {
spy = {
callback: function () {}
}
callback: function() {}
};
spyOn(spy, 'callback');

engine = new GridStackUI.Engine(12, spy.callback, true);
Expand Down Expand Up @@ -204,19 +204,19 @@ describe('gridstack engine', function() {
});
});

describe('test _packNodes', function () {
describe('using not float mode', function () {
describe('test _packNodes', function() {
describe('using not float mode', function() {
var engine;

var findNode = function (engine, id) {
return _.find(engine.nodes, function(i) { return i._id === id });
}
var findNode = function(engine, id) {
return _.find(engine.nodes, function(i) { return i._id === id; });
};

beforeEach(function () {
beforeEach(function() {
engine = new GridStackUI.Engine(12, null, false);
});

it('shouldn\'t pack one node with y coord eq 0', function () {
it('shouldn\'t pack one node with y coord eq 0', function() {
engine.nodes = [
{x: 0, y: 0, width: 1, height: 1, _id: 1},
];
Expand All @@ -227,7 +227,7 @@ describe('gridstack engine', function() {
expect(findNode(engine, 1)._dirty).toBeFalsy();
});

it('should pack one node correctly', function () {
it('should pack one node correctly', function() {
engine.nodes = [
{x: 0, y: 1, width: 1, height: 1, _id: 1},
];
Expand All @@ -237,7 +237,7 @@ describe('gridstack engine', function() {
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1, _dirty: true}));
});

it('should pack nodes correctly', function () {
it('should pack nodes correctly', function() {
engine.nodes = [
{x: 0, y: 1, width: 1, height: 1, _id: 1},
{x: 0, y: 5, width: 1, height: 1, _id: 2},
Expand All @@ -249,7 +249,7 @@ describe('gridstack engine', function() {
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 1, height: 1, _dirty: true}));
});

it('should pack nodes correctly', function () {
it('should pack nodes correctly', function() {
engine.nodes = [
{x: 0, y: 5, width: 1, height: 1, _id: 1},
{x: 0, y: 1, width: 1, height: 1, _id: 2},
Expand All @@ -261,7 +261,7 @@ describe('gridstack engine', function() {
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 1, height: 1, _dirty: true}));
});

it('should respect locked nodes', function () {
it('should respect locked nodes', function() {
engine.nodes = [
{x: 0, y: 1, width: 1, height: 1, _id: 1, locked: true},
{x: 0, y: 5, width: 1, height: 1, _id: 2},
Expand All @@ -275,4 +275,37 @@ describe('gridstack engine', function() {
});
});
});

describe('test isNodeChangedPosition', function() {
var engine;

beforeAll(function() {
engine = new GridStackUI.Engine(12);
});

it('should return true for changed x', function() {
var widget = { x: 1, y: 2, width: 3, height: 4 };
expect(engine.isNodeChangedPosition(widget, 2, 2)).toEqual(true);
});

it('should return true for changed y', function() {
var widget = { x: 1, y: 2, width: 3, height: 4 };
expect(engine.isNodeChangedPosition(widget, 1, 1)).toEqual(true);
});

it('should return true for changed width', function() {
var widget = { x: 1, y: 2, width: 3, height: 4 };
expect(engine.isNodeChangedPosition(widget, 2, 2, 4, 4)).toEqual(true);
});

it('should return true for changed height', function() {
var widget = { x: 1, y: 2, width: 3, height: 4 };
expect(engine.isNodeChangedPosition(widget, 1, 2, 3, 3)).toEqual(true);
});

it('should return false for unchanged position', function() {
var widget = { x: 1, y: 2, width: 3, height: 4 };
expect(engine.isNodeChangedPosition(widget, 1, 2, 3, 4)).toEqual(false);
});
});
});
23 changes: 23 additions & 0 deletions src/gridstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@
};

GridStackEngine.prototype.canMoveNode = function(node, x, y, width, height) {
if (!this.isNodeChangedPosition(node, x, y, width, height)) {
return false;
}
var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; }));

if (!this.height && !hasLocked) {
Expand Down Expand Up @@ -406,7 +409,27 @@
return clone.getGridHeight() <= this.height;
};

GridStackEngine.prototype.isNodeChangedPosition = function(node, x, y, width, height) {
if (typeof x != 'number') { x = node.x; }
if (typeof y != 'number') { y = node.y; }
if (typeof width != 'number') { width = node.width; }
if (typeof height != 'number') { height = node.height; }

if (typeof node.maxWidth != 'undefined') { width = Math.min(width, node.maxWidth); }
if (typeof node.maxHeight != 'undefined') { height = Math.min(height, node.maxHeight); }
if (typeof node.minWidth != 'undefined') { width = Math.max(width, node.minWidth); }
if (typeof node.minHeight != 'undefined') { height = Math.max(height, node.minHeight); }

if (node.x == x && node.y == y && node.width == width && node.height == height) {
return false;
}
return true;
};

GridStackEngine.prototype.moveNode = function(node, x, y, width, height, noPack) {
if (!this.isNodeChangedPosition(node, x, y, width, height)) {
return node;
}
if (typeof x != 'number') { x = node.x; }
if (typeof y != 'number') { y = node.y; }
if (typeof width != 'number') { width = node.width; }
Expand Down