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

Skip to content

Commit c81413a

Browse files
authored
Merge pull request gridstack#1237 from adumesny/typescript
TS: return signature cleanup, bug fix
2 parents bcd7bd2 + 79667a6 commit c81413a

File tree

3 files changed

+162
-112
lines changed

3 files changed

+162
-112
lines changed

spec/gridstack-spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ describe('gridstack', function() {
886886
let grid = GridStack.init(options);
887887
grid.destroy();
888888
expect($('.grid-stack').length).toBe(0);
889-
expect(grid.engine).toBe(null);
889+
expect(grid.engine).toBe(undefined);
890890
});
891891
it('should cleanup gridstack but leave elements', function() {
892892
let options = {
@@ -897,7 +897,7 @@ describe('gridstack', function() {
897897
grid.destroy(false);
898898
expect($('.grid-stack').length).toBe(1);
899899
expect($('.grid-stack-item').length).toBe(2);
900-
expect(grid.engine).toBe(null);
900+
expect(grid.engine).toBe(undefined);
901901
grid.destroy();
902902
});
903903
});

src/gridstack-engine.ts

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,25 @@ export class GridStackEngine {
4444
this.nodes = nodes;
4545
}
4646

47-
public batchUpdate() {
48-
if (this.batchMode) return;
47+
public batchUpdate(): GridStackEngine {
48+
if (this.batchMode) return this;
4949
this.batchMode = true;
5050
this._prevFloat = this._float;
5151
this._float = true; // let things go anywhere for now... commit() will restore and possibly reposition
52+
return this;
5253
}
5354

54-
public commit() {
55-
if (!this.batchMode) return;
55+
public commit(): GridStackEngine {
56+
if (!this.batchMode) return this;
5657
this.batchMode = false;
5758
this._float = this._prevFloat;
5859
delete this._prevFloat;
5960
this._packNodes();
6061
this._notify();
62+
return this;
6163
}
6264

63-
private _fixCollisions(node: GridStackNode) {
65+
private _fixCollisions(node: GridStackNode): GridStackEngine {
6466
this._sortNodes(-1);
6567

6668
let nn = node;
@@ -70,7 +72,7 @@ export class GridStackEngine {
7072
}
7173
while (true) {
7274
let collisionNode = this.nodes.find( n => n !== node && Utils.isIntercepted(n, nn), {node: node, nn: nn});
73-
if (!collisionNode) { return; }
75+
if (!collisionNode) { return this }
7476
let moved;
7577
if (collisionNode.locked) {
7678
// if colliding with a locked item, move ourself instead
@@ -80,11 +82,11 @@ export class GridStackEngine {
8082
moved = this.moveNode(collisionNode, collisionNode.x, node.y + node.height,
8183
collisionNode.width, collisionNode.height, true);
8284
}
83-
if (!moved) { return; } // break inf loop if we couldn't move after all (ex: maxRow, fixed)
85+
if (!moved) { return this } // break inf loop if we couldn't move after all (ex: maxRow, fixed)
8486
}
8587
}
8688

87-
public isAreaEmpty(x: number, y: number, width: number, height: number) {
89+
public isAreaEmpty(x: number, y: number, width: number, height: number): boolean {
8890
let nn = {x: x || 0, y: y || 0, width: width || 1, height: height || 1};
8991
let collisionNode = this.nodes.find(n => {
9092
return Utils.isIntercepted(n, nn);
@@ -93,8 +95,8 @@ export class GridStackEngine {
9395
}
9496

9597
/** re-layout grid items to reclaim any empty space */
96-
public compact() {
97-
if (this.nodes.length === 0) { return; }
98+
public compact(): GridStackEngine {
99+
if (this.nodes.length === 0) { return this }
98100
this.batchUpdate();
99101
this._sortNodes();
100102
let copyNodes = this.nodes;
@@ -107,6 +109,7 @@ export class GridStackEngine {
107109
node._dirty = true; // force attr update
108110
});
109111
this.commit();
112+
return this;
110113
}
111114

112115
/** enable/disable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html) */
@@ -122,17 +125,18 @@ export class GridStackEngine {
122125
/** float getter method */
123126
public get float(): boolean { return this._float || false; }
124127

125-
private _sortNodes(dir?: -1 | 1) {
128+
private _sortNodes(dir?: -1 | 1): GridStackEngine {
126129
this.nodes = Utils.sort(this.nodes, dir, this.column);
130+
return this;
127131
}
128132

129-
private _packNodes() {
133+
private _packNodes(): GridStackEngine {
130134
this._sortNodes();
131135

132136
if (this.float) {
133137
this.nodes.forEach((n, i) => {
134138
if (n._updating || n._packY === undefined || n.y === n._packY) {
135-
return;
139+
return this;
136140
}
137141
let newY = n.y;
138142
while (newY >= n._packY) {
@@ -149,7 +153,7 @@ export class GridStackEngine {
149153
});
150154
} else {
151155
this.nodes.forEach((n, i) => {
152-
if (n.locked) { return; }
156+
if (n.locked) { return this }
153157
while (n.y > 0) {
154158
let newY = n.y - 1;
155159
let canBeMoved = i === 0;
@@ -170,14 +174,15 @@ export class GridStackEngine {
170174
}
171175
});
172176
}
177+
return this;
173178
}
174179

175180
/**
176181
* given a random node, makes sure it's coordinates/values are valid in the current grid
177182
* @param node to adjust
178183
* @param resizing if out of bound, resize down or move into the grid to fit ?
179184
*/
180-
public prepareNode(node: GridStackNode, resizing?: boolean) {
185+
public prepareNode(node: GridStackNode, resizing?: boolean): GridStackNode {
181186
node = node || {};
182187
// if we're missing position, have the grid position us automatically (before we set them to 0,0)
183188
if (node.x === undefined || node.y === undefined || node.x === null || node.y === null) {
@@ -240,7 +245,7 @@ export class GridStackEngine {
240245
return node;
241246
}
242247

243-
public getDirtyNodes(verify?: boolean) {
248+
public getDirtyNodes(verify?: boolean): GridStackNode[] {
244249
// compare original X,Y,W,H (or entire node?) instead as _dirty can be a temporary state
245250
if (verify) {
246251
let dirtNodes = [];
@@ -259,22 +264,24 @@ export class GridStackEngine {
259264
return this.nodes.filter(n => n._dirty);
260265
}
261266

262-
private _notify(nodes?: GridStackNode | GridStackNode[], detachNode?: boolean) {
263-
if (this.batchMode) { return; }
267+
private _notify(nodes?: GridStackNode | GridStackNode[], detachNode?: boolean): GridStackEngine {
268+
if (this.batchMode) { return this }
264269
detachNode = (detachNode === undefined ? true : detachNode);
265270
nodes = (nodes === undefined ? [] : (Array.isArray(nodes) ? nodes : [nodes]) );
266271
let dirtyNodes = nodes.concat(this.getDirtyNodes());
267272
if (this.onchange) {
268273
this.onchange(dirtyNodes, detachNode);
269274
}
275+
return this;
270276
}
271277

272-
public cleanNodes() {
273-
if (this.batchMode) { return; }
278+
public cleanNodes(): GridStackEngine {
279+
if (this.batchMode) { return this }
274280
this.nodes.forEach(n => { delete n._dirty; });
281+
return this;
275282
}
276283

277-
public addNode(node: GridStackNode, triggerAddEvent?: boolean) {
284+
public addNode(node: GridStackNode, triggerAddEvent?: boolean): GridStackNode {
278285
node = this.prepareNode(node);
279286

280287
node._id = node._id || GridStackEngine._idSeq++;
@@ -309,23 +316,25 @@ export class GridStackEngine {
309316
return node;
310317
}
311318

312-
public removeNode(node: GridStackNode, detachNode?: boolean) {
319+
public removeNode(node: GridStackNode, detachNode?: boolean): GridStackEngine {
313320
detachNode = (detachNode === undefined ? true : detachNode);
314321
this.removedNodes.push(node);
315322
node._id = null; // hint that node is being removed
316323
this.nodes = this.nodes.filter(n => n !== node);
317324
this._packNodes();
318325
this._notify(node, detachNode);
326+
return this;
319327
}
320328

321-
public removeAll(detachNode?: boolean) {
329+
public removeAll(detachNode?: boolean): GridStackEngine {
322330
delete this._layouts;
323-
if (this.nodes.length === 0) { return; }
331+
if (this.nodes.length === 0) { return this }
324332
detachNode = (detachNode === undefined ? true : detachNode);
325333
this.nodes.forEach(n => { n._id = null; }); // hint that node is being removed
326334
this.removedNodes = this.nodes;
327335
this.nodes = [];
328336
this._notify(this.removedNodes, detachNode);
337+
return this;
329338
}
330339

331340
public canMoveNode(node: GridStackNode, x: number, y: number, width?: number, height?: number): boolean {
@@ -369,7 +378,7 @@ export class GridStackEngine {
369378
return canMove;
370379
}
371380

372-
public canBePlacedWithRespectToHeight(node: GridStackNode) {
381+
public canBePlacedWithRespectToHeight(node: GridStackNode): boolean {
373382
if (!this.maxRow) {
374383
return true;
375384
}
@@ -384,7 +393,7 @@ export class GridStackEngine {
384393
return clone.getRow() <= this.maxRow;
385394
}
386395

387-
public isNodeChangedPosition(node: GridStackNode, x: number, y: number, width: number, height: number) {
396+
public isNodeChangedPosition(node: GridStackNode, x: number, y: number, width: number, height: number): boolean {
388397
if (typeof x !== 'number') { x = node.x; }
389398
if (typeof y !== 'number') { y = node.y; }
390399
if (typeof width !== 'number') { width = node.width; }
@@ -436,26 +445,28 @@ export class GridStackEngine {
436445
return this.nodes.reduce((memo, n) => Math.max(memo, n.y + n.height), 0);
437446
}
438447

439-
public beginUpdate(node: GridStackNode) {
440-
if (node._updating) return;
448+
public beginUpdate(node: GridStackNode): GridStackEngine {
449+
if (node._updating) return this;
441450
node._updating = true;
442451
this.nodes.forEach(n => { n._packY = n.y; });
452+
return this;
443453
}
444454

445-
public endUpdate() {
455+
public endUpdate(): GridStackEngine {
446456
let n = this.nodes.find(n => n._updating);
447457
if (n) {
448458
n._updating = false;
449459
this.nodes.forEach(n => { delete n._packY; });
450460
}
461+
return this;
451462
}
452463

453464
/** @internal called whenever a node is added or moved - updates the cached layouts */
454-
public layoutsNodesChange(nodes: GridStackNode[]) {
455-
if (!this._layouts || this._ignoreLayoutsNodeChange) return;
465+
public layoutsNodesChange(nodes: GridStackNode[]): GridStackEngine {
466+
if (!this._layouts || this._ignoreLayoutsNodeChange) return this;
456467
// remove smaller layouts - we will re-generate those on the fly... larger ones need to update
457468
this._layouts.forEach((layout, column) => {
458-
if (!layout || column === this.column) return;
469+
if (!layout || column === this.column) return this;
459470
if (column < this.column) {
460471
this._layouts[column] = undefined;
461472
}
@@ -464,7 +475,7 @@ export class GridStackEngine {
464475
// Note: we don't need to check against out of bound scaling/moving as that will be done when using those cache values.
465476
nodes.forEach(node => {
466477
let n = layout.find(l => l._id === node._id);
467-
if (!n) return; // no cache for new nodes. Will use those values.
478+
if (!n) return this; // no cache for new nodes. Will use those values.
468479
let ratio = column / this.column;
469480
// Y changed, push down same amount
470481
// TODO: detect doing item 'swaps' will help instead of move (especially in 1 column mode)
@@ -483,6 +494,7 @@ export class GridStackEngine {
483494
});
484495
}
485496
});
497+
return this;
486498
}
487499

488500
/**
@@ -494,8 +506,8 @@ export class GridStackEngine {
494506
* @param column new column number
495507
* @param nodes different sorted list (ex: DOM order) instead of current list
496508
*/
497-
public updateNodeWidths(oldColumn: number, column: number, nodes: GridStackNode[]) {
498-
if (!this.nodes.length || oldColumn === column) { return; }
509+
public updateNodeWidths(oldColumn: number, column: number, nodes: GridStackNode[]): GridStackEngine {
510+
if (!this.nodes.length || oldColumn === column) { return this }
499511

500512
// cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data
501513
let copy: Layout[] = [];
@@ -554,7 +566,7 @@ export class GridStackEngine {
554566
// ...and add any extra non-cached ones
555567
let ratio = column / oldColumn;
556568
nodes.forEach(node => {
557-
if (!node) return;
569+
if (!node) return this;
558570
node.x = (column === 1 ? 0 : Math.round(node.x * ratio));
559571
node.width = ((column === 1 || oldColumn === 1) ? 1 : (Math.round(node.width * ratio) || 1));
560572
newNodes.push(node);
@@ -571,17 +583,19 @@ export class GridStackEngine {
571583
}, this);
572584
this.commit();
573585
delete this._ignoreLayoutsNodeChange;
586+
return this;
574587
}
575588

576589
/** @internal called to save initial position/size */
577-
public saveInitial() {
590+
public saveInitial(): GridStackEngine {
578591
this.nodes.forEach(n => {
579592
n._origX = n.x;
580593
n._origY = n.y;
581594
n._origW = n.width;
582595
n._origH = n.height;
583596
delete n._dirty;
584597
});
598+
return this;
585599
}
586600

587601
// legacy method renames

0 commit comments

Comments
 (0)