Skip to content

Commit 4ea6fbd

Browse files
authored
Merge pull request #1999 from adumesny/master
added batchUpdate(false) to replace commit()
2 parents a7b4895 + 6f3bb44 commit 4ea6fbd

File tree

12 files changed

+63
-60
lines changed

12 files changed

+63
-60
lines changed

demo/react-hooks.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ <h2>Uncontrolled stack</h2>
7171
grid.batchUpdate()
7272
grid.removeAll(false)
7373
items.forEach(({ id }) => grid.makeWidget(refs.current[id].current))
74-
grid.commit()
74+
grid.batchUpdate(false)
7575
}, [items])
7676

7777
return (

doc/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change log
55
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
66
**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*
77

8+
- [TBD](#tbd)
89
- [5.1.1 (2022-06-16)](#511-2022-06-16)
910
- [5.1.0 (2022-05-21)](#510-2022-05-21)
1011
- [5.0.0 (2022-01-10)](#500-2022-01-10)
@@ -68,6 +69,9 @@ Change log
6869

6970
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
7071

72+
## TBD
73+
* changed `commit()` to be `batchUpdate(false)` to make it easier to turn batch on/off. updated doc. old API remains for now
74+
7175
## 5.1.1 (2022-06-16)
7276
* fix v5.1.0 regression [#1973](https://github.com/gridstack/gridstack.js/issues/1973) DnD Snap to Animation
7377

doc/README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ gridstack.js API
3030
- [`GridStack.registerEngine(engineClass: typeof GridStackEngine)`](#gridstackregisterengineengineclass-typeof-gridstackengine)
3131
- [API](#api)
3232
- [`addWidget(el?: GridStackWidget | GridStackElement, options?: GridStackWidget)`](#addwidgetel-gridstackwidget--gridstackelement-options-gridstackwidget)
33-
- [`batchUpdate()`](#batchupdate)
33+
- [`batchUpdate(flag = true)`](#batchupdateflag--true)
3434
- [`compact()`](#compact)
3535
- [`cellHeight(val: number, update = true)`](#cellheightval-number-update--true)
3636
- [`cellWidth()`](#cellwidth)
37-
- [`commit()`](#commit)
3837
- [`column(column: number, layout: ColumnOptions = 'moveScale')`](#columncolumn-number-layout-columnoptions--movescale)
3938
- [`destroy([removeDOM])`](#destroyremovedom)
4039
- [`disable()`](#disable)
@@ -348,9 +347,9 @@ grid.addWidget({w: 3, content: 'hello'});
348347
grid.addWidget('<div class="grid-stack-item"><div class="grid-stack-item-content">hello</div></div>', {w: 3});
349348
```
350349

351-
### `batchUpdate()`
350+
### `batchUpdate(flag = true)`
352351

353-
starts batch updates. You will see no changes until `commit()` method is called.
352+
use before calling a bunch of `addWidget()` to prevent un-necessary relayouts in between (more efficient) and get a single event callback. You will see no changes until `batchUpdate(false)` is called.
354353

355354
### `compact()`
356355

@@ -368,10 +367,6 @@ grid.cellHeight(grid.cellWidth() * 1.2);
368367

369368
Gets current cell width (grid width / # of columns).
370369

371-
### `commit()`
372-
373-
Ends batch updates. Updates DOM nodes. You must call it after `batchUpdate()`.
374-
375370
### `column(column: number, layout: ColumnOptions = 'moveScale')`
376371

377372
set the number of columns in the grid. Will update existing widgets to conform to new number of columns,

spec/e2e/html/810-many-columns.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ <h1>Many Columns demo</h1>
3838
for (; count <= COLUMNS;) {
3939
this.addNewWidget();
4040
}
41-
grid.commit();
41+
grid.batchUpdate(false);
4242
</script>
4343
</body>
4444
</html>

spec/gridstack-engine-spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ describe('gridstack engine', function() {
186186
engine.batchUpdate(); // double for code coverage
187187
expect(engine.batchMode).toBeTrue();
188188
expect(engine.float).toEqual(true);
189-
engine.commit();
190-
engine.commit();
189+
engine.batchUpdate(false);
190+
engine.batchUpdate(false);
191191
expect(engine.batchMode).not.toBeTrue();
192192
expect(engine.float).not.toBeTrue;
193193
});
@@ -197,7 +197,7 @@ describe('gridstack engine', function() {
197197
engine.batchUpdate();
198198
expect(engine.batchMode).toBeTrue();
199199
expect(engine.float).toEqual(true);
200-
engine.commit();
200+
engine.batchUpdate(false);
201201
expect(engine.batchMode).not.toBeTrue();
202202
expect(engine.float).toEqual(true);
203203
});
@@ -214,7 +214,7 @@ describe('gridstack engine', function() {
214214
engine.batchUpdate();
215215
expect(engine.batchMode).toBeTrue();
216216
expect(engine.float).toEqual(true);
217-
engine.commit();
217+
engine.batchUpdate(false);
218218
expect(engine.batchMode).not.toBeTrue();
219219
expect(engine.float).toEqual(true);
220220
});

spec/gridstack-spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,8 @@ describe('gridstack', function() {
433433
let el1 = grid.addWidget({w:1, h:1});
434434
let el2 = grid.addWidget({x:2, y:0, w:2, h:1});
435435
let el3 = grid.addWidget({x:1, y:0, w:1, h:2});
436-
grid.commit();
437-
grid.commit();
436+
grid.batchUpdate(false);
437+
grid.batchUpdate(false);
438438

439439
// items are item1[1x1], item3[1x1], item2[2x1]
440440
expect(parseInt(el1.getAttribute('gs-x'))).toBe(0);
@@ -1848,6 +1848,15 @@ describe('gridstack', function() {
18481848
expect((grid as any).willItFit(0, 0, 1, 3, false)).toBe(true);
18491849
expect((grid as any).willItFit(0, 0, 1, 4, false)).toBe(false);
18501850
});
1851+
it('warning if OLD commit() is called', function() {
1852+
let grid = GridStack.init();
1853+
grid.batchUpdate(true);
1854+
expect(grid.engine.batchMode).toBe(true);
1855+
grid.commit(); // old API
1856+
expect(grid.engine.batchMode).toBe(false);
1857+
// expect(console.warn).toHaveBeenCalledWith('gridstack.js: Function `setGridWidth` is deprecated in v0.5.3 and has been replaced with `column`. It will be **completely** removed in v1.0');
1858+
});
1859+
18511860
/* saving as example
18521861
it('warning if OLD setGridWidth is called', function() {
18531862
let grid: any = GridStack.init();

src/gridstack-engine.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,19 @@ export class GridStackEngine {
5454
this.onChange = opts.onChange;
5555
}
5656

57-
public batchUpdate(): GridStackEngine {
58-
if (this.batchMode) return this;
59-
this.batchMode = true;
60-
this._prevFloat = this._float;
61-
this._float = true; // let things go anywhere for now... commit() will restore and possibly reposition
62-
return this.saveInitial(); // since begin update (which is called multiple times) won't do this
63-
}
64-
65-
public commit(): GridStackEngine {
66-
if (!this.batchMode) return this;
67-
this.batchMode = false;
68-
this._float = this._prevFloat;
69-
delete this._prevFloat;
70-
return this._packNodes()
71-
._notify();
57+
public batchUpdate(flag = true): GridStackEngine {
58+
if (!!this.batchMode === flag) return this;
59+
this.batchMode = flag;
60+
if (flag) {
61+
this._prevFloat = this._float;
62+
this._float = true; // let things go anywhere for now... will restore and possibly reposition later
63+
this.saveInitial(); // since begin update (which is called multiple times) won't do this
64+
} else {
65+
this._float = this._prevFloat;
66+
delete this._prevFloat;
67+
this._packNodes()._notify();
68+
}
69+
return this;
7270
}
7371

7472
// use entire row for hitting area (will use bottom reverse sorted first) if we not actively moving DOWN and didn't already skip
@@ -252,7 +250,7 @@ export class GridStackEngine {
252250
this.addNode(node, false); // 'false' for add event trigger
253251
node._dirty = true; // will force attr update
254252
});
255-
return this.commit();
253+
return this.batchUpdate(false);
256254
}
257255

258256
/** enable/disable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html) */
@@ -829,7 +827,7 @@ export class GridStackEngine {
829827
this.addNode(node, false); // 'false' for add event trigger
830828
delete node._orig; // make sure the commit doesn't try to restore things back to original
831829
});
832-
this.commit();
830+
this.batchUpdate(false);
833831
delete this._inColumnResize;
834832
return this;
835833
}

src/gridstack-h5.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/**
2-
* index-h5.ts 5.1.1 - everything you need for a Grid that uses HTML5 native drag&drop
3-
* Copyright (c) 2021 Alain Dumesny - see GridStack root license
1+
/*!
2+
* gridstack-h5.ts 5.1.1 - everything you need for a Grid that uses HTML5 native drag&drop
3+
* Copyright (c) 2021 Alain Dumesny - see root license https://github.com/gridstack/gridstack.js/tree/master/LICENSE
44
*/
55

66
export * from './types';

src/gridstack-jq.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* index-jq.ts 5.1.1 - everything you need for a Grid that uses Jquery-ui drag&drop (original, full feature)
1+
/*!
2+
* gridstack-jq.ts 5.1.1 - everything you need for a Grid that uses Jquery-ui drag&drop (original, full feature)
33
* Copyright (c) 2021 Alain Dumesny - see GridStack root license
44
*/
55

src/gridstack-static.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* index-static.ts 5.1.1 - much smaller, everything you need for a static Grid (non draggable, API driven)
1+
/*!
2+
* gridstack-static.ts 5.1.1 - much smaller, everything you need for a static Grid (non draggable, API driven)
33
* Copyright (c) 2021 Alain Dumesny - see GridStack root license
44
*/
55

0 commit comments

Comments
 (0)