Skip to content

Commit 1925cef

Browse files
authored
Merge pull request #2991 from adumesny/master
new updateOptions()
2 parents 73dd55a + c18fd26 commit 1925cef

File tree

5 files changed

+79
-20
lines changed

5 files changed

+79
-20
lines changed

angular/projects/lib/src/lib/gridstack-item.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ export interface GridItemCompHTMLElement extends GridItemHTMLElement {
1919
selector: 'gridstack-item',
2020
template: `
2121
<div class="grid-stack-item-content">
22-
<!-- where dynamic items go based on component types, or sub-grids, etc...) -->
22+
<!-- where dynamic items go based on component selector (recommended way), or sub-grids, etc...) -->
2323
<ng-template #container></ng-template>
24-
<!-- any static (defined in dom) content goes here -->
24+
<!-- any static (defined in DOM - not recommended) content goes here -->
2525
<ng-content></ng-content>
26-
<!-- fallback HTML content from GridStackWidget content field if used instead -->
26+
<!-- fallback HTML content from GridStackWidget.content if used instead (not recommended) -->
2727
{{options.content}}
2828
</div>`,
2929
styles: [`

angular/projects/lib/src/lib/gridstack.component.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@ export type SelectorToType = {[key: string]: Type<Object>};
4949
})
5050
export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
5151

52-
/** track list of TEMPLATE grid items so we can sync between DOM and GS internals */
52+
/** track list of TEMPLATE (not recommended) grid items so we can sync between DOM and GS internals */
5353
@ContentChildren(GridstackItemComponent) public gridstackItems?: QueryList<GridstackItemComponent>;
54-
/** container to append items dynamically */
54+
/** container to append items dynamically (recommended way) */
5555
@ViewChild('container', { read: ViewContainerRef, static: true}) public container?: ViewContainerRef;
5656

5757
/** initial options for creation of the grid */
58-
@Input() public set options(val: GridStackOptions) { this._options = val; }
58+
@Input() public set options(o: GridStackOptions) {
59+
if (this._grid) {
60+
// this._grid.updateOptions(o); // new API
61+
} else {
62+
this._options = o;
63+
}
64+
}
5965
/** return the current running options */
6066
public get options(): GridStackOptions { return this._grid?.opts || this._options || {}; }
6167

@@ -110,11 +116,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
110116
protected _sub: Subscription | undefined;
111117
protected loaded?: boolean;
112118

113-
constructor(
114-
// protected readonly zone: NgZone,
115-
// protected readonly cd: ChangeDetectorRef,
116-
protected readonly elementRef: ElementRef<GridCompHTMLElement>,
117-
) {
119+
constructor(protected readonly elementRef: ElementRef<GridCompHTMLElement>) {
118120
// set globally our method to create the right widget type
119121
if (!GridStack.addRemoveCB) {
120122
GridStack.addRemoveCB = gsCreateNgComponents;
@@ -154,7 +156,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
154156
}
155157

156158
/**
157-
* called when the TEMPLATE list of items changes - get a list of nodes and
159+
* called when the TEMPLATE (not recommended) list of items changes - get a list of nodes and
158160
* update the layout accordingly (which will take care of adding/removing items changed by Angular)
159161
*/
160162
public updateAll() {
@@ -170,10 +172,7 @@ export class GridstackComponent implements OnInit, AfterContentInit, OnDestroy {
170172
/** check if the grid is empty, if so show alternative content */
171173
public checkEmpty() {
172174
if (!this.grid) return;
173-
const isEmpty = !this.grid.engine.nodes.length;
174-
if (isEmpty === this.isEmpty) return;
175-
this.isEmpty = isEmpty;
176-
// this.cd.detectChanges();
175+
this.isEmpty = !this.grid.engine.nodes.length;
177176
}
178177

179178
/** get all known events as easy to use Outputs for convenience */

doc/CHANGES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ Change log
123123
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
124124

125125
## 11.4.0-dev (TBD)
126-
* feat: [#2960](https://github.com/gridstack/gridstack.js/pull/2960) `prepareDragDrop(el, force)` option to force re-creation of the drag&drop event binding
126+
* feat: [#2975](https://github.com/gridstack/gridstack.js/pull/2975) `prepareDragDrop(el, force)` option to force re-creation of the drag&drop event binding
127+
* feat: [#2989](https://github.com/gridstack/gridstack.js/pull/2989) new `updateOptions(o: GridStackOptions)` to update PARTIAL list of options after grid as been created
128+
* fix: [#2980](https://github.com/gridstack/gridstack.js/issues/2980) dd-touch circular dependency
129+
* fix: [#2667](https://github.com/gridstack/gridstack.js/issues/2667) sidebar items not honoring gs-w (enter-leave-re-enter)
127130

128131
## 11.4.0 (2025-02-27)
129132
* fix: [#2921](https://github.com/gridstack/gridstack.js/pull/2921) replace initMouseEvent with MouseEvent constructor and added composed: true

spec/gridstack-spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,4 +2002,34 @@ describe('gridstack >', function() {
20022002
expect(window.getComputedStyle(grid.el.querySelector("#item1")!).height).toBe("60px");
20032003
});
20042004
});
2005+
2006+
2007+
describe('updateOptions()', function() {
2008+
let grid: GridStack;
2009+
beforeEach(function() {
2010+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
2011+
grid = GridStack.init({ cellHeight: 30 });
2012+
});
2013+
afterEach(function() {
2014+
document.body.removeChild(document.getElementById('gs-cont'));
2015+
});
2016+
it('update all values supported', function() {
2017+
grid.updateOptions({
2018+
cellHeight: '40px',
2019+
margin: 8,
2020+
column: 11,
2021+
float: true,
2022+
row: 10,
2023+
});
2024+
expect(grid.getCellHeight(true)).toBe(40);
2025+
expect(grid.getMargin()).toBe(8);
2026+
expect(grid.opts.marginTop).toBe(8);
2027+
expect(grid.getColumn()).toBe(11);
2028+
expect(grid.getFloat()).toBe(true);
2029+
expect(grid.opts.row).toBe(10);
2030+
expect(grid.opts.minRow).toBe(10);
2031+
expect(grid.opts.maxRow).toBe(10);
2032+
});
2033+
});
2034+
20052035
});

src/gridstack.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,32 @@ export class GridStack {
13051305
return this;
13061306
}
13071307

1308+
/**
1309+
* Updates the passed in options on the grid (similar to update(widget) for for the grid options).
1310+
* @param options PARTIAL grid options to update - only items specified will be updated.
1311+
* NOTE: not all options updating are currently supported (lot of code, unlikely to change)
1312+
*/
1313+
public updateOptions(o: GridStackOptions): GridStack {
1314+
const opts = this.opts;
1315+
if (o.acceptWidgets !== undefined) this._setupAcceptWidget();
1316+
if (o.animate !== undefined) this.setAnimation();
1317+
if (o.cellHeight) { this.cellHeight(o.cellHeight, true); delete o.cellHeight; }
1318+
if (o.class && o.class !== opts.class) { if (opts.class) this.el.classList.remove(opts.class); this.el.classList.add(o.class); }
1319+
if (typeof(o.column) === 'number' && !o.columnOpts) { this.column(o.column); delete o.column; }// responsive column take over actual count
1320+
if (o.margin !== undefined) this.margin(o.margin);
1321+
if (o.staticGrid !== undefined) this.setStatic(o.staticGrid);
1322+
if (o.disableDrag !== undefined && !o.staticGrid) this.enableMove(!o.disableDrag);
1323+
if (o.disableResize !== undefined && !o.staticGrid) this.enableResize(!o.disableResize);
1324+
if (o.float !== undefined) this.float(o.float);
1325+
if (o.row !== undefined) { opts.minRow = opts.maxRow = o.row; }
1326+
if (o.children?.length) { this.load(o.children); delete o.children; }
1327+
// TBD if we have a real need for these (more complex code)
1328+
// alwaysShowResizeHandle, draggable, handle, handleClass, itemClass, layout, placeholderClass, placeholderText, resizable, removable, row,...
1329+
// rest are just copied over...
1330+
this.opts = {...this.opts, ...o};
1331+
return this;
1332+
}
1333+
13081334
/**
13091335
* Updates widget position/size and other info. Note: if you need to call this on all nodes, use load() instead which will update what changed.
13101336
* @param els widget or selector of objects to modify (note: setting the same x,y for multiple items will be indeterministic and likely unwanted)
@@ -2418,12 +2444,13 @@ export class GridStack {
24182444
}
24192445

24202446
/**
2421-
* prepares the element for drag&drop - this is normally called by makeWiget() unless are are delay loading
2447+
* prepares the element for drag&drop - this is normally called by makeWidget() unless are are delay loading
24222448
* @param el GridItemHTMLElement of the widget
2423-
* @param [force=false]
2449+
* @param [force=false]
24242450
* */
24252451
public prepareDragDrop(el: GridItemHTMLElement, force = false): GridStack {
2426-
const node = el.gridstackNode;
2452+
const node = el?.gridstackNode;
2453+
if (!node) return;
24272454
const noMove = node.noMove || this.opts.disableDrag;
24282455
const noResize = node.noResize || this.opts.disableResize;
24292456

0 commit comments

Comments
 (0)