Skip to content

Commit 80a1182

Browse files
committed
Util to GridStack.createWidgetDivs() move
* fix #2959 * `Util.createWidgetDivs()` has moved to `GridStack.createWidgetDivs()` to remove circular dependencies
1 parent a2e65b0 commit 80a1182

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ GridStack.renderCB = function(el: HTMLElement, w: GridStackNode) {
480480
};
481481
```
482482
* V11 add new `GridStack.renderCB` that is called for you to create the widget content (entire GridStackWidget is passed so you can use id or some other field as logic) while GS creates the 2 needed parent divs + classes, unlike `GridStack.addRemoveCB` which doesn't create anything for you. Both can be handy for Angular/React/Vue frameworks.
483-
* `addWidget(w: GridStackWidget)` is now the only supported format, no more string content passing. You will need to create content yourself (`Util.createWidgetDivs()` can be used to create parent divs) then call `makeWidget(el)` instead.
483+
* `addWidget(w: GridStackWidget)` is now the only supported format, no more string content passing. You will need to create content yourself (`GridStack.createWidgetDivs()` can be used to create parent divs) then call `makeWidget(el)` instead.
484484
485485
**Potential breaking change:**
486486

demo/sizeToContent.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ <h1>sizeToContent options demo</h1>
109109
grid.addWidget({content: `<div>New: ${text}</div>`});
110110
}
111111
function makeWidget() {
112-
let el = GridStack.Utils.createWidgetDivs(undefined, {content: `<div>New Make: ${text}</div>`}, grid.el)
112+
let el = GridStack.createWidgetDivs(undefined, {content: `<div>New Make: ${text}</div>`}, grid.el)
113113
grid.makeWidget(el, {w:2});
114114
}
115115
function more() {

demo/two.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ <h1>Two grids demo</h1>
107107
// clone the sidepanel item so we drag a copy, and in some case ('manual') create the final widget, else sidebarContent will be used.
108108
function myClone(el) {
109109
if (el.getAttribute('gs-id') === 'manual') {
110-
return GridStack.Utils.createWidgetDivs(undefined, {w:2, content:'manual'}); // RenderCB() will be called
110+
return GridStack.createWidgetDivs(undefined, {w:2, content:'manual'}); // RenderCB() will be called
111111
}
112112
el = el.cloneNode(true);
113113
// el.setAttribute('gs-id', 'foo'); // help debug #2231

doc/CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ Change log
127127
* fix: [#2955](https://github.com/gridstack/gridstack.js/issues/2955) angular circular dependency
128128
* fix: [#2951](https://github.com/gridstack/gridstack.js/issues/2951) shadow DOM dragging re-appending fix
129129
* fix: [#2964](https://github.com/gridstack/gridstack.js/pull/2964) minW larger than column fix
130-
* feat: [#2965](https://github.com/gridstack/gridstack.js/pull/2965) internal `_prepareDragDropByNode(n)` is now public as `prepareDragDrop(el)` so Angular, React, and others can call once the DOM content elements have been added (the outside griditem divs are always created for content)
130+
* feat: [#2965](https://github.com/gridstack/gridstack.js/pull/2965) internal `_prepareDragDropByNode(n)` is now public as `prepareDragDrop(el)` so Angular, React, and others can call once the DOM content elements have been added (the outside grid item divs are always created before content)
131+
* break: [#2959](https://github.com/gridstack/gridstack.js/issues/2959) `Util.createWidgetDivs()` has moved to `GridStack.createWidgetDivs()` to remove circular dependencies
131132

132133
## 11.3.0 (2025-01-26)
133134
* feat: added `isIgnoreChangeCB()` if changeCB should be ignored due to column change, sizeToContent, loading, etc...

src/gridstack.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ export class GridStack {
173173
return grid;
174174
}
175175

176+
/** create the default grid item divs, and content possibly lazy loaded calling GridStack.renderCB */
177+
static createWidgetDivs(itemClass: string, n: GridStackNode): HTMLElement {
178+
const el = Utils.createDiv(['grid-stack-item', itemClass]);
179+
const cont = Utils.createDiv(['grid-stack-item-content'], el);
180+
181+
if (Utils.lazyLoad(n)) {
182+
if (!n.visibleObservable) {
183+
n.visibleObservable = new IntersectionObserver(([entry]) => { if (entry.isIntersecting) {
184+
n.visibleObservable?.disconnect();
185+
delete n.visibleObservable;
186+
GridStack.renderCB(cont, n);
187+
n.grid?.prepareDragDrop(n.el);
188+
}});
189+
window.setTimeout(() => n.visibleObservable?.observe(el)); // wait until callee sets position attributes
190+
}
191+
} else GridStack.renderCB(cont, n);
192+
193+
return el;
194+
}
195+
176196
/** call this method to register your engine instead of the default one.
177197
* See instead `GridStackOptions.engineClass` if you only need to
178198
* replace just one instance.
@@ -467,7 +487,7 @@ export class GridStack {
467487
} else if (GridStack.addRemoveCB) {
468488
el = GridStack.addRemoveCB(this.el, w, true, false);
469489
} else {
470-
el = Utils.createWidgetDivs(this.opts.itemClass, node);
490+
el = GridStack.createWidgetDivs(this.opts.itemClass, node);
471491
}
472492

473493
if (!el) return;

src/utils.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright (c) 2021-2024 Alain Dumesny - see GridStack root license
44
*/
55

6-
import { GridStack } from './gridstack';
76
import { GridStackElement, GridStackNode, GridStackOptions, numberOrString, GridStackPosition, GridStackWidget } from './types';
87

98
export interface HeightData {
@@ -115,26 +114,6 @@ export class Utils {
115114
return n.lazyLoad || n.grid?.opts?.lazyLoad && n.lazyLoad !== false;
116115
}
117116

118-
/** create the default grid item divs, and content possibly lazy loaded calling GridStack.renderCB */
119-
static createWidgetDivs(itemClass: string, n: GridStackNode): HTMLElement {
120-
const el = Utils.createDiv(['grid-stack-item', itemClass]);
121-
const cont = Utils.createDiv(['grid-stack-item-content'], el);
122-
123-
if (Utils.lazyLoad(n)) {
124-
if (!n.visibleObservable) {
125-
n.visibleObservable = new IntersectionObserver(([entry]) => { if (entry.isIntersecting) {
126-
n.visibleObservable?.disconnect();
127-
delete n.visibleObservable;
128-
GridStack.renderCB(cont, n);
129-
n.grid?.prepareDragDrop(n.el);
130-
}});
131-
window.setTimeout(() => n.visibleObservable?.observe(el)); // wait until callee sets position attributes
132-
}
133-
} else GridStack.renderCB(cont, n);
134-
135-
return el;
136-
}
137-
138117
/** create a div with the given classes */
139118
static createDiv(classes: string[], parent?: HTMLElement): HTMLElement {
140119
const el = document.createElement('div');

0 commit comments

Comments
 (0)