Skip to content

Commit 6decc5d

Browse files
committed
TS: support callback for load()
* more fix for #1286 changed method to load() (rather than restore()) * added a callback function in additon to boolean which default to true (will add/remove widgets by default)
1 parent abbe1fa commit 6decc5d

File tree

5 files changed

+50
-25
lines changed

5 files changed

+50
-25
lines changed

demo/serialization.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h1>Serialization demo</h1>
4242

4343
// NEW 2.x method
4444
loadGrid = function() {
45-
grid.restore(serializedData, true);
45+
grid.load(serializedData, true);
4646
}
4747

4848
// NEW 2.x method

doc/CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Change log
4040
- re-write to native Typescript, removing all JQuery from main code and API (drag&drop plugin still using for now which use latest v3.5.1)
4141
- add `getGridItems()` to return list of HTML grid items
4242
- add `{dragIn | dragInOptions}` grid attributes to handle external drag&drop items
43-
- add `save()` and `restore()` to serialize grids from JSON, saving all attributes (not just w,h,x,y) [1286](https://github.com/gridstack/gridstack.js/issues/1286)
43+
- add `save()` and `load()` to serialize grids from JSON, saving all attributes (not just w,h,x,y) [1286](https://github.com/gridstack/gridstack.js/issues/1286)
4444
- add `margin` to replace `verticalMargin` which affects both dimensions in code, rather than one in code the other in CSS.
4545
You can now have perfect square cells (default) [723](https://github.com/gridstack/gridstack.js/issues/723)
4646
- fix [1299](https://github.com/gridstack/gridstack.js/pull/1299) many columns round-off error

doc/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ gridstack.js API
3939
- [getGridItems(): GridItemHTMLElement[]](#getgriditems-griditemhtmlelement)
4040
- [getMargin()](#getmargin)
4141
- [isAreaEmpty(x, y, width, height)](#isareaemptyx-y-width-height)
42+
- [load(layout: GridStackWidget[], boolean | ((w: GridStackWidget, add: boolean) => void) = true)](#loadlayout-gridstackwidget-boolean--w-gridstackwidget-add-boolean--void---true)
4243
- [locked(el, val)](#lockedel-val)
4344
- [makeWidget(el)](#makewidgetel)
4445
- [margin(value: numberOrString)](#marginvalue-numberorstring)
@@ -52,7 +53,6 @@ gridstack.js API
5253
- [removeAll([removeDOM])](#removeallremovedom)
5354
- [resize(el, width, height)](#resizeel-width-height)
5455
- [resizable(el, val)](#resizableel-val)
55-
- [restore(layout: GridStackWidget[], addAndRemove?: boolean)](#restorelayout-gridstackwidget-addandremove-boolean)
5656
- [save(): GridStackWidget[]](#save-gridstackwidget)
5757
- [setAnimation(doAnimate)](#setanimationdoanimate)
5858
- [setStatic(staticValue)](#setstaticstaticvalue)
@@ -383,6 +383,15 @@ returns current margin value.
383383

384384
Checks if specified area is empty.
385385

386+
### load(layout: GridStackWidget[], boolean | ((w: GridStackWidget, add: boolean) => void) = true)
387+
388+
- load the widgets from a list (see `save()`). This will call `update()` on each (matching by id) or add/remove widgets that are not there.
389+
- Optional `addAndRemove` boolean (default true) or callback method can be passed to control if and how missing widgets can be added/removed, giving the user control of insertion.
390+
391+
- used to restore a grid layout for a saved layout list (see `save()`).
392+
- `addAndRemove` boolean (default true) or callback method can be passed to control if and how missing widgets can be added/removed, giving the user control of insertion.
393+
- see [example](http://gridstackjs.com/demo/serialization.html)
394+
386395
### locked(el, val)
387396

388397
Locks/unlocks widget.
@@ -488,15 +497,9 @@ Enables/Disables resizing.
488497
- `el` - widget to modify
489498
- `val` - if `true` widget will be resizable.
490499

491-
### restore(layout: GridStackWidget[], addAndRemove?: boolean)
492-
493-
- used to restore a grid layout for a saved layout list (see `save()`).
494-
- Optional `addAndRemove` can be passed if new widgets should be added or removed if the are not present (`id` is used to look items up)
495-
- see [example](http://gridstackjs.com/demo/serialization.html)
496-
497500
### save(): GridStackWidget[]
498501

499-
- returns the layout of the grid that can be serialized (list of item non default attributes, not just w,y,x,y but also min/max and id). See `restore()`
502+
- returns the layout of the grid that can be serialized (list of item non default attributes, not just w,y,x,y but also min/max and id). See `load()`
500503
- see [example](http://gridstackjs.com/demo/serialization.html)
501504

502505
### setAnimation(doAnimate)

spec/gridstack-spec.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ describe('gridstack', function() {
15041504

15051505
});
15061506

1507-
describe('save & restore', function() {
1507+
describe('save & load', function() {
15081508
beforeEach(function() {
15091509
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
15101510
});
@@ -1516,23 +1516,29 @@ describe('gridstack', function() {
15161516
let layout = grid.save();
15171517
expect(layout).toEqual([{x:0, y:0, width:4, height:2, id:'item1'}, {x:4, y:0, width:4, height:4, id:'item2'}]);
15181518
});
1519-
it('restore size 1 item', function() {
1519+
it('load move 1 item, delete others', function() {
15201520
let grid = GridStack.init();
1521-
grid.restore([{height:3, id:'item1'}]);
1521+
grid.load([{x:2, height:1, id:'item2'}]);
15221522
let layout = grid.save();
1523-
expect(layout).toEqual([{x:0, y:0, width:4, height:3, id:'item1'}, {x:4, y:0, width:4, height:4, id:'item2'}]);
1523+
expect(layout).toEqual([{x:2, y:0, width:4, height:1, id:'item2'}]);
15241524
});
1525-
it('restore move 1 item, delete others', function() {
1525+
it('load add new, delete others', function() {
15261526
let grid = GridStack.init();
1527-
grid.restore([{x:2, height:1, id:'item2'}], true);
1527+
grid.load([{width:2, height:1, id:'item3'}], true);
15281528
let layout = grid.save();
1529-
expect(layout).toEqual([{x:2, y:0, width:4, height:1, id:'item2'}]);
1529+
expect(layout).toEqual([{x:0, y:0, width:2, height:1, id:'item3'}]);
15301530
});
1531-
it('restore add new, delete others', function() {
1531+
it('load size 1 item only', function() {
15321532
let grid = GridStack.init();
1533-
grid.restore([{width:2, height:1, id:'item3'}], true);
1533+
grid.load([{height:3, id:'item1'}], false);
15341534
let layout = grid.save();
1535-
expect(layout).toEqual([{x:0, y:0, width:2, height:1, id:'item3'}]);
1535+
expect(layout).toEqual([{x:0, y:0, width:4, height:3, id:'item1'}, {x:4, y:0, width:4, height:4, id:'item2'}]);
1536+
});
1537+
it('load size 1 item only with callback', function() {
1538+
let grid = GridStack.init();
1539+
grid.load([{height:3, id:'item1'}], () => {});
1540+
let layout = grid.save();
1541+
expect(layout).toEqual([{x:0, y:0, width:4, height:3, id:'item1'}, {x:4, y:0, width:4, height:4, id:'item2'}]);
15361542
});
15371543
});
15381544

src/gridstack.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,29 @@ export class GridStack {
349349
/** saves the current layout returning a list of widgets for serialization */
350350
public save(): GridStackWidget[] { return this.engine.save(); }
351351

352-
/** restore the widgets from a list. This will call update() on each (matching by id),
353-
* or optionally add/remove widgets that are not there (either a boolean or a callback method) */
354-
public restore(layout: GridStackWidget[], addAndRemove?: boolean) {
352+
/**
353+
* load the widgets from a list. This will call update() on each (matching by id) or add/remove widgets that are not there.
354+
*
355+
* @param layout list of widgets definition to update/create
356+
* @param addAndRemove boolean (default true) or callback method can be passed to control if and how missing widgets can be added/removed, giving
357+
* the user control of insertion.
358+
*
359+
* @example
360+
* see http://gridstackjs.com/demo/serialization.html
361+
**/
362+
public load(layout: GridStackWidget[], addAndRemove: boolean | ((w: GridStackWidget, add: boolean) => void) = true) {
355363
let items = GridStack.Utils.sort(layout);
356364
this.batchUpdate();
357365
// see if any items are missing from new layout and need to be removed first
358366
if (addAndRemove) {
359367
this.engine.nodes.forEach(n => {
360368
let item = items.find(w => n.id === w.id);
361369
if (!item) {
362-
this.removeWidget(n.el);
370+
if (typeof(addAndRemove) === 'function') {
371+
addAndRemove(n, false);
372+
} else {
373+
this.removeWidget(n.el);
374+
}
363375
}
364376
});
365377
}
@@ -369,7 +381,11 @@ export class GridStack {
369381
if (item) {
370382
this.update(item.el, w.x, w.y, w.width, w.height); // TODO: full update
371383
} else if (addAndRemove) {
372-
this.addWidget('<div><div class="grid-stack-item-content"></div></div>', w);
384+
if (typeof(addAndRemove) === 'function') {
385+
addAndRemove(w, true);
386+
} else {
387+
this.addWidget('<div><div class="grid-stack-item-content"></div></div>', w);
388+
}
373389
}
374390
});
375391
this.commit();

0 commit comments

Comments
 (0)