Thanks to visit codestin.com
Credit goes to github.com

Skip to content

this.createWidgetDivs() instead of static #2969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/sizeToContent.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ <h1>sizeToContent options demo</h1>
grid.addWidget({content: `<div>New: ${text}</div>`});
}
function makeWidget() {
let el = GridStack.createWidgetDivs(undefined, {content: `<div>New Make: ${text}</div>`}, grid.el)
let el = grid.createWidgetDivs({content: `<div>New Make: ${text}</div>`}, grid.el)
grid.makeWidget(el, {w:2});
}
function more() {
Expand Down
2 changes: 1 addition & 1 deletion demo/two.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ <h1>Two grids demo</h1>
// clone the sidepanel item so we drag a copy, and in some case ('manual') create the final widget, else sidebarContent will be used.
function myClone(el) {
if (el.getAttribute('gs-id') === 'manual') {
return GridStack.createWidgetDivs(undefined, {w:2, content:'manual'}); // RenderCB() will be called
return grids[0].createWidgetDivs({w:2, content:'manual'}); // RenderCB() will be called
}
el = el.cloneNode(true);
// el.setAttribute('gs-id', 'foo'); // help debug #2231
Expand Down
42 changes: 21 additions & 21 deletions src/gridstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,26 +173,6 @@ export class GridStack {
return grid;
}

/** create the default grid item divs, and content possibly lazy loaded calling GridStack.renderCB */
static createWidgetDivs(itemClass: string, n: GridStackNode): HTMLElement {
const el = Utils.createDiv(['grid-stack-item', itemClass]);
const cont = Utils.createDiv(['grid-stack-item-content'], el);

if (Utils.lazyLoad(n)) {
if (!n.visibleObservable) {
n.visibleObservable = new IntersectionObserver(([entry]) => { if (entry.isIntersecting) {
n.visibleObservable?.disconnect();
delete n.visibleObservable;
GridStack.renderCB(cont, n);
n.grid?.prepareDragDrop(n.el);
}});
window.setTimeout(() => n.visibleObservable?.observe(el)); // wait until callee sets position attributes
}
} else GridStack.renderCB(cont, n);

return el;
}

/** call this method to register your engine instead of the default one.
* See instead `GridStackOptions.engineClass` if you only need to
* replace just one instance.
Expand Down Expand Up @@ -487,7 +467,7 @@ export class GridStack {
} else if (GridStack.addRemoveCB) {
el = GridStack.addRemoveCB(this.el, w, true, false);
} else {
el = GridStack.createWidgetDivs(this.opts.itemClass, node);
el = this.createWidgetDivs(node);
}

if (!el) return;
Expand All @@ -511,6 +491,26 @@ export class GridStack {
return el;
}

/** create the default grid item divs, and content (possibly lazy loaded) by using GridStack.renderCB() */
public createWidgetDivs(n: GridStackNode): HTMLElement {
const el = Utils.createDiv(['grid-stack-item', this.opts.itemClass]);
const cont = Utils.createDiv(['grid-stack-item-content'], el);

if (Utils.lazyLoad(n)) {
if (!n.visibleObservable) {
n.visibleObservable = new IntersectionObserver(([entry]) => { if (entry.isIntersecting) {
n.visibleObservable?.disconnect();
delete n.visibleObservable;
GridStack.renderCB(cont, n);
n.grid?.prepareDragDrop(n.el);
}});
window.setTimeout(() => n.visibleObservable?.observe(el)); // wait until callee sets position attributes
}
} else GridStack.renderCB(cont, n);

return el;
}

/**
* Convert an existing gridItem element into a sub-grid with the given (optional) options, else inherit them
* from the parent's subGrid options.
Expand Down