Add isNodeChangedPosition
.
This commit is contained in:
parent
01a27c6ed9
commit
64ea5c4cf6
|
@ -482,6 +482,7 @@ Changes
|
||||||
- add oneColumnModeClass option to grid.
|
- add oneColumnModeClass option to grid.
|
||||||
- remove 768px CSS styles, moved to grid-stack-one-column-mode class.
|
- 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 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)
|
#### v0.2.6 (2016-08-17)
|
||||||
|
|
||||||
|
|
23
dist/gridstack.js
vendored
23
dist/gridstack.js
vendored
|
@ -351,6 +351,9 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
GridStackEngine.prototype.canMoveNode = function(node, x, y, width, height) {
|
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; }));
|
var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; }));
|
||||||
|
|
||||||
if (!this.height && !hasLocked) {
|
if (!this.height && !hasLocked) {
|
||||||
|
@ -406,7 +409,27 @@
|
||||||
return clone.getGridHeight() <= this.height;
|
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) {
|
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 x != 'number') { x = node.x; }
|
||||||
if (typeof y != 'number') { y = node.y; }
|
if (typeof y != 'number') { y = node.y; }
|
||||||
if (typeof width != 'number') { width = node.width; }
|
if (typeof width != 'number') { width = node.width; }
|
||||||
|
|
10
dist/gridstack.min.js
vendored
10
dist/gridstack.min.js
vendored
File diff suppressed because one or more lines are too long
2
dist/gridstack.min.map
vendored
2
dist/gridstack.min.map
vendored
File diff suppressed because one or more lines are too long
|
@ -14,7 +14,7 @@ describe('gridstack engine', function() {
|
||||||
|
|
||||||
beforeAll(function() {
|
beforeAll(function() {
|
||||||
engine = new GridStackUI.Engine(12);
|
engine = new GridStackUI.Engine(12);
|
||||||
})
|
});
|
||||||
|
|
||||||
it('should be setup properly', function() {
|
it('should be setup properly', function() {
|
||||||
expect(engine.width).toEqual(12);
|
expect(engine.width).toEqual(12);
|
||||||
|
@ -29,7 +29,7 @@ describe('gridstack engine', function() {
|
||||||
|
|
||||||
beforeAll(function() {
|
beforeAll(function() {
|
||||||
engine = new GridStackUI.Engine(12);
|
engine = new GridStackUI.Engine(12);
|
||||||
})
|
});
|
||||||
|
|
||||||
it('should prepare a node', function() {
|
it('should prepare a node', function() {
|
||||||
expect(engine._prepareNode({}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1}));
|
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.nodes = [
|
||||||
engine._prepareNode({x: 3, y: 2, width: 3, height: 2})
|
engine._prepareNode({x: 3, y: 2, width: 3, height: 2})
|
||||||
];
|
];
|
||||||
})
|
});
|
||||||
|
|
||||||
it('should be true', function() {
|
it('should be true', function() {
|
||||||
expect(engine.isAreaEmpty(0, 0, 3, 2)).toEqual(true);
|
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;
|
var engine;
|
||||||
|
|
||||||
beforeAll(function () {
|
beforeAll(function() {
|
||||||
engine = new GridStackUI.Engine(12)
|
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);
|
expect(engine.float).toEqual(false);
|
||||||
engine.batchUpdate();
|
engine.batchUpdate();
|
||||||
expect(engine._updateCounter).toBeGreaterThan(0);
|
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;
|
var engine;
|
||||||
|
|
||||||
beforeAll(function () {
|
beforeAll(function() {
|
||||||
engine = new GridStackUI.Engine(12, null, true)
|
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);
|
expect(engine.float).toEqual(true);
|
||||||
engine.batchUpdate();
|
engine.batchUpdate();
|
||||||
expect(engine._updateCounter).toBeGreaterThan(0);
|
expect(engine._updateCounter).toBeGreaterThan(0);
|
||||||
|
@ -150,8 +150,8 @@ describe('gridstack engine', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
spy = {
|
spy = {
|
||||||
callback: function () {}
|
callback: function() {}
|
||||||
}
|
};
|
||||||
spyOn(spy, 'callback');
|
spyOn(spy, 'callback');
|
||||||
|
|
||||||
engine = new GridStackUI.Engine(12, spy.callback, true);
|
engine = new GridStackUI.Engine(12, spy.callback, true);
|
||||||
|
@ -204,19 +204,19 @@ describe('gridstack engine', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('test _packNodes', function () {
|
describe('test _packNodes', function() {
|
||||||
describe('using not float mode', function () {
|
describe('using not float mode', function() {
|
||||||
var engine;
|
var engine;
|
||||||
|
|
||||||
var findNode = function (engine, id) {
|
var findNode = function(engine, id) {
|
||||||
return _.find(engine.nodes, function(i) { return i._id === id });
|
return _.find(engine.nodes, function(i) { return i._id === id; });
|
||||||
}
|
};
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function() {
|
||||||
engine = new GridStackUI.Engine(12, null, false);
|
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 = [
|
engine.nodes = [
|
||||||
{x: 0, y: 0, width: 1, height: 1, _id: 1},
|
{x: 0, y: 0, width: 1, height: 1, _id: 1},
|
||||||
];
|
];
|
||||||
|
@ -227,7 +227,7 @@ describe('gridstack engine', function() {
|
||||||
expect(findNode(engine, 1)._dirty).toBeFalsy();
|
expect(findNode(engine, 1)._dirty).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pack one node correctly', function () {
|
it('should pack one node correctly', function() {
|
||||||
engine.nodes = [
|
engine.nodes = [
|
||||||
{x: 0, y: 1, width: 1, height: 1, _id: 1},
|
{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}));
|
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 = [
|
engine.nodes = [
|
||||||
{x: 0, y: 1, width: 1, height: 1, _id: 1},
|
{x: 0, y: 1, width: 1, height: 1, _id: 1},
|
||||||
{x: 0, y: 5, width: 1, height: 1, _id: 2},
|
{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}));
|
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 = [
|
engine.nodes = [
|
||||||
{x: 0, y: 5, width: 1, height: 1, _id: 1},
|
{x: 0, y: 5, width: 1, height: 1, _id: 1},
|
||||||
{x: 0, y: 1, width: 1, height: 1, _id: 2},
|
{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}));
|
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 = [
|
engine.nodes = [
|
||||||
{x: 0, y: 1, width: 1, height: 1, _id: 1, locked: true},
|
{x: 0, y: 1, width: 1, height: 1, _id: 1, locked: true},
|
||||||
{x: 0, y: 5, width: 1, height: 1, _id: 2},
|
{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) {
|
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; }));
|
var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; }));
|
||||||
|
|
||||||
if (!this.height && !hasLocked) {
|
if (!this.height && !hasLocked) {
|
||||||
|
@ -406,7 +409,27 @@
|
||||||
return clone.getGridHeight() <= this.height;
|
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) {
|
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 x != 'number') { x = node.x; }
|
||||||
if (typeof y != 'number') { y = node.y; }
|
if (typeof y != 'number') { y = node.y; }
|
||||||
if (typeof width != 'number') { width = node.width; }
|
if (typeof width != 'number') { width = node.width; }
|
||||||
|
|
Loading…
Reference in a new issue