Skip to content

Commit d99a9f5

Browse files
authored
Merge pull request #2274 from adumesny/master
Break: GridStackWidget.subGridOpts rename
2 parents 5830739 + f19aa5e commit d99a9f5

File tree

9 files changed

+46
-41
lines changed

9 files changed

+46
-41
lines changed

demo/angular/src/app/app.component.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export class AppComponent implements OnInit {
5151
private sub2: NgGridStackWidget[] = [ {x:0, y:0}, {x:0, y:1, w:2}];
5252
private subChildren: NgGridStackWidget[] = [
5353
{x:0, y:0, content: 'regular item'},
54-
{x:1, y:0, w:4, h:4, subGrid: {children: this.sub1, id:'sub1_grid', class: 'sub1', ...this.subOptions}},
55-
{x:5, y:0, w:3, h:4, subGrid: {children: this.sub2, id:'sub2_grid', class: 'sub2', ...this.subOptions}},
54+
{x:1, y:0, w:4, h:4, subGridOpts: {children: this.sub1, id:'sub1_grid', class: 'sub1', ...this.subOptions}},
55+
{x:5, y:0, w:3, h:4, subGridOpts: {children: this.sub2, id:'sub2_grid', class: 'sub2', ...this.subOptions}},
5656
]
5757
public nestedGridOptions: GridStackOptions = { // main grid options
5858
cellHeight: 50,
@@ -68,7 +68,7 @@ export class AppComponent implements OnInit {
6868
constructor() {
6969
// give them content and unique id to make sure we track them during changes below...
7070
[...this.items, ...this.subChildren, ...this.sub1, ...this.sub2].forEach((w: NgGridStackWidget) => {
71-
if (!w.type && !w.subGrid) w.content = `item ${ids}`;
71+
if (!w.type && !w.subGridOpts) w.content = `item ${ids}`;
7272
w.id = String(ids++);
7373
});
7474
}
@@ -141,9 +141,10 @@ export class AppComponent implements OnInit {
141141
let grid = this.gridComp?.grid;
142142
if (!grid) return;
143143
let node = grid.engine.nodes[0];
144-
if (node?.subGrid) {
145-
grid = node.subGrid as GridStack;
146-
node = grid?.engine.nodes[0];
144+
// delete any children first before subGrid itself...
145+
if (node?.subGrid && node.subGrid.engine.nodes.length) {
146+
grid = node.subGrid;
147+
node = grid.engine.nodes[0];
147148
}
148149
if (node) grid.removeWidget(node.el!);
149150
}

demo/angular/src/app/gridstack.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export function gsCreateNgComponents(host: GridCompHTMLElement | HTMLElement, w:
216216

217217
// IFF we're not a subGrid, define what type of component to create as child, OR you can do it GridstackItemComponent template, but this is more generic
218218
const type = (w as NgGridStackWidget).type;
219-
if (!w.subGrid && type && GridstackComponent.selectorToType[type]) {
219+
if (!w.subGridOpts && type && GridstackComponent.selectorToType[type]) {
220220
gridItem?.container?.createComponent(GridstackComponent.selectorToType[type]);
221221
}
222222

demo/nested.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ <h1>Nested grids demo</h1>
5151
id: 'main',
5252
children: [
5353
{x:0, y:0, content: 'regular item'},
54-
{x:1, y:0, w:4, h:4, subGrid: {children: sub1, id:'sub1_grid', class: 'sub1', ...subOptions}},
55-
{x:5, y:0, w:3, h:4, subGrid: {children: sub2, id:'sub2_grid', class: 'sub2', ...subOptions}},
54+
{x:1, y:0, w:4, h:4, subGridOpts: {children: sub1, id:'sub1_grid', class: 'sub1', ...subOptions}},
55+
{x:5, y:0, w:3, h:4, subGridOpts: {children: sub2, id:'sub2_grid', class: 'sub2', ...subOptions}},
5656
]
5757
};
5858

demo/nested_advanced.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ <h1>Advanced Nested grids demo</h1>
4545
let main = [{x:0, y:0}, {x:0, y:1}, {x:1, y:0}]
4646
let sub1 = [{x:0, y:0}];
4747
let sub0 = [{x:0, y:0}, {x:1, y:0}];
48-
// let sub0 = [{x:0, y:0}, {x:1, y:0}, {x:1, y:1, h:2, subGrid: {children: sub1, ...subOptions}}];
48+
// let sub0 = [{x:0, y:0}, {x:1, y:0}, {x:1, y:1, h:2, subGridOpts: {children: sub1, ...subOptions}}];
4949
let options = { // main grid options
5050
cellHeight: 50,
5151
margin: 5,
5252
minRow: 2, // don't collapse when empty
5353
acceptWidgets: true,
54-
subGrid: subOptions,
54+
subGridOpts: subOptions,
5555
subGridDynamic: true, // v7 api to create sub-grids on the fly
5656
children: [
5757
...main,
58-
{x:2, y:0, w:2, h:3, id: 'sub0', subGrid: {children: sub0, ...subOptions}},
59-
{x:4, y:0, h:2, id: 'sub1', subGrid: {children: sub1, ...subOptions}},
60-
// {x:2, y:0, w:2, h:3, subGrid: {children: [...sub1, {x:0, y:1, subGrid: subOptions}], ...subOptions}/*,content: "<div>nested grid here</div>"*/},
58+
{x:2, y:0, w:2, h:3, id: 'sub0', subGridOpts: {children: sub0, ...subOptions}},
59+
{x:4, y:0, h:2, id: 'sub1', subGridOpts: {children: sub1, ...subOptions}},
60+
// {x:2, y:0, w:2, h:3, subGridOpts: {children: [...sub1, {x:0, y:1, subGridOpts: subOptions}], ...subOptions}/*,content: "<div>nested grid here</div>"*/},
6161
]
6262
};
6363
let count = 0;

demo/nested_constraint.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ <h1>Constraint Nested grids demo</h1>
5555
id: 'main',
5656
children: [
5757
{y:0, content: 'regular item'},
58-
{x:1, w:4, h:4, subGrid: {children: sub1, class: 'sub1', ...subOptions}},
59-
{x:5, w:4, h:4, subGrid: {children: sub2, class: 'sub2', ...subOptions}},
58+
{x:1, w:4, h:4, subGridOpts: {children: sub1, class: 'sub1', ...subOptions}},
59+
{x:5, w:4, h:4, subGridOpts: {children: sub2, class: 'sub2', ...subOptions}},
6060
]
6161
};
6262

demo/old_nested-jq.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ <h1>Nested JQuery grids demo</h1>
7676
id: 'main',
7777
children: [
7878
{x:0, y:0, content: 'regular item'},
79-
{x:1, w:4, h:4, subGrid: {children: sub1, dragOut: true, class: 'sub1', ...subOptions}},
80-
{x:5, w:3, h:4, subGrid: {children: sub2, dragOut: false, class: 'sub2', ...subOptions}},
79+
{x:1, w:4, h:4, subGridOpts: {children: sub1, dragOut: true, class: 'sub1', ...subOptions}},
80+
{x:5, w:3, h:4, subGridOpts: {children: sub2, dragOut: false, class: 'sub2', ...subOptions}},
8181
]
8282
};
8383

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Change log
9191
* add: `GridStack.saveCB` global callback for each item during save so app can insert any custom data before serializing it. `save()` can now be passed optional callback
9292
* move: `GridStack.addRemoveCB` is now global instead of grid option. `load()` can still be passed different optional callback
9393
* fix: addGrid() to handle passing an existing initialized grid already
94+
* break: `GridStackOptions.subGrid` -> `GridStackOptions.subGridOpts`. We now have `GridStackWidget.subGridOpts` vs `GridStackNode.subGrid` (subclass) rather than try to merge the two at runtime since very different types...
9495

9596
## 7.3.0 (2023-04-01)
9697
* feat [#2229](https://github.com/gridstack/gridstack.js/pull/2229) support nonce for CSP. Thank you [@jedwards1211](https://github.com/jedwards1211)

src/gridstack.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,8 @@ export class GridStack {
469469
this._updateContainerHeight();
470470

471471
// see if there is a sub-grid to create
472-
if (node.subGrid) {
473-
this.makeSubGrid(node.el, undefined, undefined, false); //node.subGrid will be used as option in method, no need to pass
472+
if (node.subGridOpts) {
473+
this.makeSubGrid(node.el, node.subGridOpts, undefined, false); // node.subGrid will be used as option in method, no need to pass
474474
}
475475

476476
// if we're adding an item into 1 column (_prevColumn is set only when going to 1) make sure
@@ -498,18 +498,18 @@ export class GridStack {
498498
if (!node) {
499499
node = this.makeWidget(el).gridstackNode;
500500
}
501-
if ((node.subGrid as GridStack)?.el) return node.subGrid as GridStack; // already done
501+
if (node.subGrid?.el) return node.subGrid; // already done
502502

503503
// find the template subGrid stored on a parent as fallback...
504504
let subGridTemplate: GridStackOptions; // eslint-disable-next-line @typescript-eslint/no-this-alias
505505
let grid: GridStack = this;
506506
while (grid && !subGridTemplate) {
507-
subGridTemplate = grid.opts?.subGrid;
507+
subGridTemplate = grid.opts?.subGridOpts;
508508
grid = grid.parentGridItem?.grid;
509509
}
510510
//... and set the create options
511-
ops = Utils.cloneDeep({...(subGridTemplate || {}), children: undefined, ...(ops || node.subGrid as GridStackOptions)});
512-
node.subGrid = ops;
511+
ops = Utils.cloneDeep({...(subGridTemplate || {}), children: undefined, ...(ops || node.subGridOpts)});
512+
node.subGridOpts = ops;
513513

514514
// if column special case it set, remember that flag and set default
515515
let autoColumn: boolean;
@@ -527,7 +527,7 @@ export class GridStack {
527527
this._removeDD(node.el); // remove D&D since it's set on content div
528528
newItemOpt = {...node, x:0, y:0};
529529
Utils.removeInternalForSave(newItemOpt);
530-
delete newItemOpt.subGrid;
530+
delete newItemOpt.subGridOpts;
531531
if (node.content) {
532532
newItemOpt.content = node.content;
533533
delete node.content;
@@ -625,9 +625,10 @@ export class GridStack {
625625
} else {
626626
if (!saveContent && !saveCB) { delete n.content; }
627627
// check for nested grid
628-
if ((n.subGrid as GridStack)?.el) {
629-
const listOrOpt = (n.subGrid as GridStack).save(saveContent, saveGridOpt, saveCB);
630-
n.subGrid = (saveGridOpt ? listOrOpt : {children: listOrOpt}) as GridStackOptions;
628+
if (n.subGrid?.el) {
629+
const listOrOpt = n.subGrid.save(saveContent, saveGridOpt, saveCB);
630+
n.subGridOpts = (saveGridOpt ? listOrOpt : {children: listOrOpt}) as GridStackOptions;
631+
delete n.subGrid;
631632
}
632633
}
633634
delete n.el;
@@ -711,10 +712,10 @@ export class GridStack {
711712
let item = (w.id || w.id === 0) ? this.engine.nodes.find(n => n.id === w.id) : undefined;
712713
if (item) {
713714
this.update(item.el, w);
714-
if (w.subGrid && (w.subGrid as GridStackOptions).children) { // update any sub grid as well
715+
if (w.subGridOpts?.children) { // update any sub grid as well
715716
let sub = item.el.querySelector('.grid-stack') as GridHTMLElement;
716717
if (sub && sub.gridstack) {
717-
sub.gridstack.load((w.subGrid as GridStackOptions).children); // TODO: support updating grid options ?
718+
sub.gridstack.load(w.subGridOpts.children); // TODO: support updating grid options ?
718719
this._insertNotAppend = true; // got reset by above call
719720
}
720721
}
@@ -1138,7 +1139,7 @@ export class GridStack {
11381139
this._setupAcceptWidget();
11391140
this.engine.nodes.forEach(n => {
11401141
this._prepareDragDropByNode(n); // either delete or init Drag&drop
1141-
if (n.subGrid && recurse) (n.subGrid as GridStack).setStatic(val, updateClass, recurse);
1142+
if (n.subGrid && recurse) n.subGrid.setStatic(val, updateClass, recurse);
11421143
});
11431144
if (updateClass) { this._setStaticClass(); }
11441145
return this;
@@ -1553,7 +1554,7 @@ export class GridStack {
15531554

15541555
// finally update any nested grids
15551556
this.engine.nodes.forEach(n => {
1556-
if (n.subGrid) {(n.subGrid as GridStack).onParentResize()}
1557+
if (n.subGrid) n.subGrid.onParentResize()
15571558
});
15581559

15591560
return this;
@@ -1757,7 +1758,7 @@ export class GridStack {
17571758
this.opts.disableDrag = !doEnable; // FIRST before we update children as grid overrides #1658
17581759
this.engine.nodes.forEach(n => {
17591760
this.movable(n.el, doEnable);
1760-
if (n.subGrid && recurse) (n.subGrid as GridStack).enableMove(doEnable, recurse);
1761+
if (n.subGrid && recurse) n.subGrid.enableMove(doEnable, recurse);
17611762
});
17621763
return this;
17631764
}
@@ -1771,7 +1772,7 @@ export class GridStack {
17711772
this.opts.disableResize = !doEnable; // FIRST before we update children as grid overrides #1658
17721773
this.engine.nodes.forEach(n => {
17731774
this.resizable(n.el, doEnable);
1774-
if (n.subGrid && recurse) (n.subGrid as GridStack).enableResize(doEnable, recurse);
1775+
if (n.subGrid && recurse) n.subGrid.enableResize(doEnable, recurse);
17751776
});
17761777
return this;
17771778
}
@@ -1987,7 +1988,7 @@ export class GridStack {
19871988
if (!wasAdded) return false;
19881989
el.gridstackNode = node;
19891990
node.el = el;
1990-
let subGrid = (node.subGrid as GridStack)?.el?.gridstack; // set when actual sub-grid present
1991+
let subGrid = node.subGrid?.el?.gridstack; // set when actual sub-grid present
19911992
// @ts-ignore
19921993
Utils.copyPos(node, this._readAttr(this.placeholder)); // placeholder values as moving VERY fast can throw things off #1578
19931994
Utils.removePositioningStyles(el);// @ts-ignore
@@ -2263,7 +2264,7 @@ export class GridStack {
22632264
node._lastUiPosition = ui.position;
22642265
this.engine.cacheRects(cellWidth, cellHeight, mTop, mRight, mBottom, mLeft);
22652266
delete node._skipDown;
2266-
if (resizing && node.subGrid) { (node.subGrid as GridStack).onParentResize(); }// @ts-ignore
2267+
if (resizing && node.subGrid) node.subGrid.onParentResize();
22672268
this._extraDragRow = 0;// @ts-ignore
22682269
this._updateContainerHeight();
22692270

src/types.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ export interface GridStackOptions {
252252
/** if `true` will add style element to `<head>` otherwise will add it to element's parent node (default `false`). */
253253
styleInHead?: boolean;
254254

255-
/** list of differences in options for automatically created sub-grids under us */
256-
subGrid?: GridStackOptions;
255+
/** list of differences in options for automatically created sub-grids under us (inside our grid-items) */
256+
subGridOpts?: GridStackOptions;
257257

258258
/** enable/disable the creation of sub-grids on the fly by dragging items completely
259259
* over others (nest) vs partially (push). Forces `DDDragOpt.pause=true` to accomplish that. */
@@ -318,8 +318,8 @@ export interface GridStackWidget extends GridStackPosition {
318318
id?: numberOrString;
319319
/** html to append inside as content */
320320
content?: string;
321-
/** optional nested grid options and list of children, which then turns into actual instance at runtime */
322-
subGrid?: GridStackOptions | GridStack;
321+
/** optional nested grid options and list of children, which then turns into actual instance at runtime to get options from */
322+
subGridOpts?: GridStackOptions;
323323
}
324324

325325
/** Drag&Drop resize options */
@@ -390,8 +390,10 @@ export interface DDUIData {
390390
export interface GridStackNode extends GridStackWidget {
391391
/** pointer back to HTML element */
392392
el?: GridItemHTMLElement;
393-
/** pointer back to Grid instance */
393+
/** pointer back to parent Grid instance */
394394
grid?: GridStack;
395+
/** actual sub-grid instance */
396+
subGrid?: GridStack;
395397
/** @internal internal id used to match when cloning engines or saving column layouts */
396398
_id?: number;
397399
/** @internal */

0 commit comments

Comments
 (0)