Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67dc2dd

Browse files
authored
Merge pull request gridstack#514 from radiolips/feature/isNodeChangedPosition
Feature/is node changed position
2 parents f1cdbe2 + 64ea5c4 commit 67dc2dd

File tree

6 files changed

+108
-32
lines changed

6 files changed

+108
-32
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ Changes
482482
- add oneColumnModeClass option to grid.
483483
- remove 768px CSS styles, moved to grid-stack-one-column-mode class.
484484
- add max-width override on grid-stck-one-column-mode ([#462](https://github.com/troolee/gridstack.js/issues/462)).
485+
- add internal function`isNodeChangedPosition`, minor optimization to move/drag.
485486

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

dist/gridstack.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@
351351
};
352352

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

356359
if (!this.height && !hasLocked) {
@@ -406,7 +409,27 @@
406409
return clone.getGridHeight() <= this.height;
407410
};
408411

412+
GridStackEngine.prototype.isNodeChangedPosition = function(node, x, y, width, height) {
413+
if (typeof x != 'number') { x = node.x; }
414+
if (typeof y != 'number') { y = node.y; }
415+
if (typeof width != 'number') { width = node.width; }
416+
if (typeof height != 'number') { height = node.height; }
417+
418+
if (typeof node.maxWidth != 'undefined') { width = Math.min(width, node.maxWidth); }
419+
if (typeof node.maxHeight != 'undefined') { height = Math.min(height, node.maxHeight); }
420+
if (typeof node.minWidth != 'undefined') { width = Math.max(width, node.minWidth); }
421+
if (typeof node.minHeight != 'undefined') { height = Math.max(height, node.minHeight); }
422+
423+
if (node.x == x && node.y == y && node.width == width && node.height == height) {
424+
return false;
425+
}
426+
return true;
427+
};
428+
409429
GridStackEngine.prototype.moveNode = function(node, x, y, width, height, noPack) {
430+
if (!this.isNodeChangedPosition(node, x, y, width, height)) {
431+
return node;
432+
}
410433
if (typeof x != 'number') { x = node.x; }
411434
if (typeof y != 'number') { y = node.y; }
412435
if (typeof width != 'number') { width = node.width; }

dist/gridstack.min.js

Lines changed: 3 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/gridstack.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/gridstack-engine-spec.js

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('gridstack engine', function() {
1414

1515
beforeAll(function() {
1616
engine = new GridStackUI.Engine(12);
17-
})
17+
});
1818

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

3030
beforeAll(function() {
3131
engine = new GridStackUI.Engine(12);
32-
})
32+
});
3333

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

6262
it('should be true', function() {
6363
expect(engine.isAreaEmpty(0, 0, 3, 2)).toEqual(true);
@@ -108,14 +108,14 @@ describe('gridstack engine', function() {
108108
});
109109
});
110110

111-
describe('test batchUpdate/commit', function () {
111+
describe('test batchUpdate/commit', function() {
112112
var engine;
113113

114-
beforeAll(function () {
115-
engine = new GridStackUI.Engine(12)
114+
beforeAll(function() {
115+
engine = new GridStackUI.Engine(12);
116116
});
117117

118-
it('should work on not float grids', function () {
118+
it('should work on not float grids', function() {
119119
expect(engine.float).toEqual(false);
120120
engine.batchUpdate();
121121
expect(engine._updateCounter).toBeGreaterThan(0);
@@ -126,14 +126,14 @@ describe('gridstack engine', function() {
126126
});
127127
});
128128

129-
describe('test batchUpdate/commit', function () {
129+
describe('test batchUpdate/commit', function() {
130130
var engine;
131131

132-
beforeAll(function () {
133-
engine = new GridStackUI.Engine(12, null, true)
132+
beforeAll(function() {
133+
engine = new GridStackUI.Engine(12, null, true);
134134
});
135135

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

151151
beforeEach(function() {
152152
spy = {
153-
callback: function () {}
154-
}
153+
callback: function() {}
154+
};
155155
spyOn(spy, 'callback');
156156

157157
engine = new GridStackUI.Engine(12, spy.callback, true);
@@ -204,19 +204,19 @@ describe('gridstack engine', function() {
204204
});
205205
});
206206

207-
describe('test _packNodes', function () {
208-
describe('using not float mode', function () {
207+
describe('test _packNodes', function() {
208+
describe('using not float mode', function() {
209209
var engine;
210210

211-
var findNode = function (engine, id) {
212-
return _.find(engine.nodes, function(i) { return i._id === id });
213-
}
211+
var findNode = function(engine, id) {
212+
return _.find(engine.nodes, function(i) { return i._id === id; });
213+
};
214214

215-
beforeEach(function () {
215+
beforeEach(function() {
216216
engine = new GridStackUI.Engine(12, null, false);
217217
});
218218

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

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

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

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

264-
it('should respect locked nodes', function () {
264+
it('should respect locked nodes', function() {
265265
engine.nodes = [
266266
{x: 0, y: 1, width: 1, height: 1, _id: 1, locked: true},
267267
{x: 0, y: 5, width: 1, height: 1, _id: 2},
@@ -275,4 +275,37 @@ describe('gridstack engine', function() {
275275
});
276276
});
277277
});
278+
279+
describe('test isNodeChangedPosition', function() {
280+
var engine;
281+
282+
beforeAll(function() {
283+
engine = new GridStackUI.Engine(12);
284+
});
285+
286+
it('should return true for changed x', function() {
287+
var widget = { x: 1, y: 2, width: 3, height: 4 };
288+
expect(engine.isNodeChangedPosition(widget, 2, 2)).toEqual(true);
289+
});
290+
291+
it('should return true for changed y', function() {
292+
var widget = { x: 1, y: 2, width: 3, height: 4 };
293+
expect(engine.isNodeChangedPosition(widget, 1, 1)).toEqual(true);
294+
});
295+
296+
it('should return true for changed width', function() {
297+
var widget = { x: 1, y: 2, width: 3, height: 4 };
298+
expect(engine.isNodeChangedPosition(widget, 2, 2, 4, 4)).toEqual(true);
299+
});
300+
301+
it('should return true for changed height', function() {
302+
var widget = { x: 1, y: 2, width: 3, height: 4 };
303+
expect(engine.isNodeChangedPosition(widget, 1, 2, 3, 3)).toEqual(true);
304+
});
305+
306+
it('should return false for unchanged position', function() {
307+
var widget = { x: 1, y: 2, width: 3, height: 4 };
308+
expect(engine.isNodeChangedPosition(widget, 1, 2, 3, 4)).toEqual(false);
309+
});
310+
});
278311
});

src/gridstack.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@
351351
};
352352

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

356359
if (!this.height && !hasLocked) {
@@ -406,7 +409,27 @@
406409
return clone.getGridHeight() <= this.height;
407410
};
408411

412+
GridStackEngine.prototype.isNodeChangedPosition = function(node, x, y, width, height) {
413+
if (typeof x != 'number') { x = node.x; }
414+
if (typeof y != 'number') { y = node.y; }
415+
if (typeof width != 'number') { width = node.width; }
416+
if (typeof height != 'number') { height = node.height; }
417+
418+
if (typeof node.maxWidth != 'undefined') { width = Math.min(width, node.maxWidth); }
419+
if (typeof node.maxHeight != 'undefined') { height = Math.min(height, node.maxHeight); }
420+
if (typeof node.minWidth != 'undefined') { width = Math.max(width, node.minWidth); }
421+
if (typeof node.minHeight != 'undefined') { height = Math.max(height, node.minHeight); }
422+
423+
if (node.x == x && node.y == y && node.width == width && node.height == height) {
424+
return false;
425+
}
426+
return true;
427+
};
428+
409429
GridStackEngine.prototype.moveNode = function(node, x, y, width, height, noPack) {
430+
if (!this.isNodeChangedPosition(node, x, y, width, height)) {
431+
return node;
432+
}
410433
if (typeof x != 'number') { x = node.x; }
411434
if (typeof y != 'number') { y = node.y; }
412435
if (typeof width != 'number') { width = node.width; }

0 commit comments

Comments
 (0)