Commit 64ea5c4c authored by d's avatar d
Browse files

Add `isNodeChangedPosition`.

parent 01a27c6e
......@@ -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)
......
......@@ -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) {
......@@ -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; }
......
This diff is collapsed.
This diff is collapsed.
......@@ -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);
......@@ -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}));
......@@ -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);
......@@ -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);
......@@ -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);
......@@ -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);
......@@ -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},
];
......@@ -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},
];
......@@ -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},
......@@ -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},
......@@ -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},
......@@ -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);
});
});
});
......@@ -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) {
......@@ -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; }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment