@@ -44,23 +44,25 @@ export class GridStackEngine {
44
44
this . nodes = nodes ;
45
45
}
46
46
47
- public batchUpdate ( ) {
48
- if ( this . batchMode ) return ;
47
+ public batchUpdate ( ) : GridStackEngine {
48
+ if ( this . batchMode ) return this ;
49
49
this . batchMode = true ;
50
50
this . _prevFloat = this . _float ;
51
51
this . _float = true ; // let things go anywhere for now... commit() will restore and possibly reposition
52
+ return this ;
52
53
}
53
54
54
- public commit ( ) {
55
- if ( ! this . batchMode ) return ;
55
+ public commit ( ) : GridStackEngine {
56
+ if ( ! this . batchMode ) return this ;
56
57
this . batchMode = false ;
57
58
this . _float = this . _prevFloat ;
58
59
delete this . _prevFloat ;
59
60
this . _packNodes ( ) ;
60
61
this . _notify ( ) ;
62
+ return this ;
61
63
}
62
64
63
- private _fixCollisions ( node : GridStackNode ) {
65
+ private _fixCollisions ( node : GridStackNode ) : GridStackEngine {
64
66
this . _sortNodes ( - 1 ) ;
65
67
66
68
let nn = node ;
@@ -70,7 +72,7 @@ export class GridStackEngine {
70
72
}
71
73
while ( true ) {
72
74
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 }
74
76
let moved ;
75
77
if ( collisionNode . locked ) {
76
78
// if colliding with a locked item, move ourself instead
@@ -80,11 +82,11 @@ export class GridStackEngine {
80
82
moved = this . moveNode ( collisionNode , collisionNode . x , node . y + node . height ,
81
83
collisionNode . width , collisionNode . height , true ) ;
82
84
}
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)
84
86
}
85
87
}
86
88
87
- public isAreaEmpty ( x : number , y : number , width : number , height : number ) {
89
+ public isAreaEmpty ( x : number , y : number , width : number , height : number ) : boolean {
88
90
let nn = { x : x || 0 , y : y || 0 , width : width || 1 , height : height || 1 } ;
89
91
let collisionNode = this . nodes . find ( n => {
90
92
return Utils . isIntercepted ( n , nn ) ;
@@ -93,8 +95,8 @@ export class GridStackEngine {
93
95
}
94
96
95
97
/** 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 }
98
100
this . batchUpdate ( ) ;
99
101
this . _sortNodes ( ) ;
100
102
let copyNodes = this . nodes ;
@@ -107,6 +109,7 @@ export class GridStackEngine {
107
109
node . _dirty = true ; // force attr update
108
110
} ) ;
109
111
this . commit ( ) ;
112
+ return this ;
110
113
}
111
114
112
115
/** enable/disable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html) */
@@ -122,17 +125,18 @@ export class GridStackEngine {
122
125
/** float getter method */
123
126
public get float ( ) : boolean { return this . _float || false ; }
124
127
125
- private _sortNodes ( dir ?: - 1 | 1 ) {
128
+ private _sortNodes ( dir ?: - 1 | 1 ) : GridStackEngine {
126
129
this . nodes = Utils . sort ( this . nodes , dir , this . column ) ;
130
+ return this ;
127
131
}
128
132
129
- private _packNodes ( ) {
133
+ private _packNodes ( ) : GridStackEngine {
130
134
this . _sortNodes ( ) ;
131
135
132
136
if ( this . float ) {
133
137
this . nodes . forEach ( ( n , i ) => {
134
138
if ( n . _updating || n . _packY === undefined || n . y === n . _packY ) {
135
- return ;
139
+ return this ;
136
140
}
137
141
let newY = n . y ;
138
142
while ( newY >= n . _packY ) {
@@ -149,7 +153,7 @@ export class GridStackEngine {
149
153
} ) ;
150
154
} else {
151
155
this . nodes . forEach ( ( n , i ) => {
152
- if ( n . locked ) { return ; }
156
+ if ( n . locked ) { return this }
153
157
while ( n . y > 0 ) {
154
158
let newY = n . y - 1 ;
155
159
let canBeMoved = i === 0 ;
@@ -170,14 +174,15 @@ export class GridStackEngine {
170
174
}
171
175
} ) ;
172
176
}
177
+ return this ;
173
178
}
174
179
175
180
/**
176
181
* given a random node, makes sure it's coordinates/values are valid in the current grid
177
182
* @param node to adjust
178
183
* @param resizing if out of bound, resize down or move into the grid to fit ?
179
184
*/
180
- public prepareNode ( node : GridStackNode , resizing ?: boolean ) {
185
+ public prepareNode ( node : GridStackNode , resizing ?: boolean ) : GridStackNode {
181
186
node = node || { } ;
182
187
// if we're missing position, have the grid position us automatically (before we set them to 0,0)
183
188
if ( node . x === undefined || node . y === undefined || node . x === null || node . y === null ) {
@@ -240,7 +245,7 @@ export class GridStackEngine {
240
245
return node ;
241
246
}
242
247
243
- public getDirtyNodes ( verify ?: boolean ) {
248
+ public getDirtyNodes ( verify ?: boolean ) : GridStackNode [ ] {
244
249
// compare original X,Y,W,H (or entire node?) instead as _dirty can be a temporary state
245
250
if ( verify ) {
246
251
let dirtNodes = [ ] ;
@@ -259,22 +264,24 @@ export class GridStackEngine {
259
264
return this . nodes . filter ( n => n . _dirty ) ;
260
265
}
261
266
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 }
264
269
detachNode = ( detachNode === undefined ? true : detachNode ) ;
265
270
nodes = ( nodes === undefined ? [ ] : ( Array . isArray ( nodes ) ? nodes : [ nodes ] ) ) ;
266
271
let dirtyNodes = nodes . concat ( this . getDirtyNodes ( ) ) ;
267
272
if ( this . onchange ) {
268
273
this . onchange ( dirtyNodes , detachNode ) ;
269
274
}
275
+ return this ;
270
276
}
271
277
272
- public cleanNodes ( ) {
273
- if ( this . batchMode ) { return ; }
278
+ public cleanNodes ( ) : GridStackEngine {
279
+ if ( this . batchMode ) { return this }
274
280
this . nodes . forEach ( n => { delete n . _dirty ; } ) ;
281
+ return this ;
275
282
}
276
283
277
- public addNode ( node : GridStackNode , triggerAddEvent ?: boolean ) {
284
+ public addNode ( node : GridStackNode , triggerAddEvent ?: boolean ) : GridStackNode {
278
285
node = this . prepareNode ( node ) ;
279
286
280
287
node . _id = node . _id || GridStackEngine . _idSeq ++ ;
@@ -309,23 +316,25 @@ export class GridStackEngine {
309
316
return node ;
310
317
}
311
318
312
- public removeNode ( node : GridStackNode , detachNode ?: boolean ) {
319
+ public removeNode ( node : GridStackNode , detachNode ?: boolean ) : GridStackEngine {
313
320
detachNode = ( detachNode === undefined ? true : detachNode ) ;
314
321
this . removedNodes . push ( node ) ;
315
322
node . _id = null ; // hint that node is being removed
316
323
this . nodes = this . nodes . filter ( n => n !== node ) ;
317
324
this . _packNodes ( ) ;
318
325
this . _notify ( node , detachNode ) ;
326
+ return this ;
319
327
}
320
328
321
- public removeAll ( detachNode ?: boolean ) {
329
+ public removeAll ( detachNode ?: boolean ) : GridStackEngine {
322
330
delete this . _layouts ;
323
- if ( this . nodes . length === 0 ) { return ; }
331
+ if ( this . nodes . length === 0 ) { return this }
324
332
detachNode = ( detachNode === undefined ? true : detachNode ) ;
325
333
this . nodes . forEach ( n => { n . _id = null ; } ) ; // hint that node is being removed
326
334
this . removedNodes = this . nodes ;
327
335
this . nodes = [ ] ;
328
336
this . _notify ( this . removedNodes , detachNode ) ;
337
+ return this ;
329
338
}
330
339
331
340
public canMoveNode ( node : GridStackNode , x : number , y : number , width ?: number , height ?: number ) : boolean {
@@ -369,7 +378,7 @@ export class GridStackEngine {
369
378
return canMove ;
370
379
}
371
380
372
- public canBePlacedWithRespectToHeight ( node : GridStackNode ) {
381
+ public canBePlacedWithRespectToHeight ( node : GridStackNode ) : boolean {
373
382
if ( ! this . maxRow ) {
374
383
return true ;
375
384
}
@@ -384,7 +393,7 @@ export class GridStackEngine {
384
393
return clone . getRow ( ) <= this . maxRow ;
385
394
}
386
395
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 {
388
397
if ( typeof x !== 'number' ) { x = node . x ; }
389
398
if ( typeof y !== 'number' ) { y = node . y ; }
390
399
if ( typeof width !== 'number' ) { width = node . width ; }
@@ -436,26 +445,28 @@ export class GridStackEngine {
436
445
return this . nodes . reduce ( ( memo , n ) => Math . max ( memo , n . y + n . height ) , 0 ) ;
437
446
}
438
447
439
- public beginUpdate ( node : GridStackNode ) {
440
- if ( node . _updating ) return ;
448
+ public beginUpdate ( node : GridStackNode ) : GridStackEngine {
449
+ if ( node . _updating ) return this ;
441
450
node . _updating = true ;
442
451
this . nodes . forEach ( n => { n . _packY = n . y ; } ) ;
452
+ return this ;
443
453
}
444
454
445
- public endUpdate ( ) {
455
+ public endUpdate ( ) : GridStackEngine {
446
456
let n = this . nodes . find ( n => n . _updating ) ;
447
457
if ( n ) {
448
458
n . _updating = false ;
449
459
this . nodes . forEach ( n => { delete n . _packY ; } ) ;
450
460
}
461
+ return this ;
451
462
}
452
463
453
464
/** @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 ;
456
467
// remove smaller layouts - we will re-generate those on the fly... larger ones need to update
457
468
this . _layouts . forEach ( ( layout , column ) => {
458
- if ( ! layout || column === this . column ) return ;
469
+ if ( ! layout || column === this . column ) return this ;
459
470
if ( column < this . column ) {
460
471
this . _layouts [ column ] = undefined ;
461
472
}
@@ -464,7 +475,7 @@ export class GridStackEngine {
464
475
// Note: we don't need to check against out of bound scaling/moving as that will be done when using those cache values.
465
476
nodes . forEach ( node => {
466
477
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.
468
479
let ratio = column / this . column ;
469
480
// Y changed, push down same amount
470
481
// TODO: detect doing item 'swaps' will help instead of move (especially in 1 column mode)
@@ -483,6 +494,7 @@ export class GridStackEngine {
483
494
} ) ;
484
495
}
485
496
} ) ;
497
+ return this ;
486
498
}
487
499
488
500
/**
@@ -494,8 +506,8 @@ export class GridStackEngine {
494
506
* @param column new column number
495
507
* @param nodes different sorted list (ex: DOM order) instead of current list
496
508
*/
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 }
499
511
500
512
// cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data
501
513
let copy : Layout [ ] = [ ] ;
@@ -554,7 +566,7 @@ export class GridStackEngine {
554
566
// ...and add any extra non-cached ones
555
567
let ratio = column / oldColumn ;
556
568
nodes . forEach ( node => {
557
- if ( ! node ) return ;
569
+ if ( ! node ) return this ;
558
570
node . x = ( column === 1 ? 0 : Math . round ( node . x * ratio ) ) ;
559
571
node . width = ( ( column === 1 || oldColumn === 1 ) ? 1 : ( Math . round ( node . width * ratio ) || 1 ) ) ;
560
572
newNodes . push ( node ) ;
@@ -571,17 +583,19 @@ export class GridStackEngine {
571
583
} , this ) ;
572
584
this . commit ( ) ;
573
585
delete this . _ignoreLayoutsNodeChange ;
586
+ return this ;
574
587
}
575
588
576
589
/** @internal called to save initial position/size */
577
- public saveInitial ( ) {
590
+ public saveInitial ( ) : GridStackEngine {
578
591
this . nodes . forEach ( n => {
579
592
n . _origX = n . x ;
580
593
n . _origY = n . y ;
581
594
n . _origW = n . width ;
582
595
n . _origH = n . height ;
583
596
delete n . _dirty ;
584
597
} ) ;
598
+ return this ;
585
599
}
586
600
587
601
// legacy method renames
0 commit comments