MCPcopy Index your code
hub / github.com/gridstack/gridstack.js / addWidget

Method addWidget

src/gridstack.ts:432–467  ·  view source on GitHub ↗

* add a new widget and returns it. * * Widget will be always placed even if result height is more than actual grid height. * You need to use `willItFit()` before calling addWidget for additional check. * See also `makeWidget(el)` for DOM element. * * @example * const grid = Grid

(w: GridStackWidget)

Source from the content-addressed store, hash-verified

430 * @param w GridStackWidget definition. used MakeWidget(el) if you have dom element instead.
431 */
432 public addWidget(w: GridStackWidget): GridItemHTMLElement {
433 if (!w) return;
434 if (typeof w === 'string') { console.error('V11: GridStack.addWidget() does not support string anymore. see #2736'); return; }
435 if ((w as HTMLElement).ELEMENT_NODE) { console.error('V11: GridStack.addWidget() does not support HTMLElement anymore. use makeWidget()'); return this.makeWidget(w as HTMLElement); }
436
437 let el: GridItemHTMLElement;
438 let node: GridStackNode = w;
439 node.grid = this;
440 if (node.el) {
441 el = node.el; // re-use element stored in the node
442 } else if (GridStack.addRemoveCB) {
443 el = GridStack.addRemoveCB(this.el, w, true, false);
444 } else {
445 el = this.createWidgetDivs(node);
446 }
447
448 if (!el) return;
449
450 // if the caller ended up initializing the widget in addRemoveCB, or we stared with one already, skip the rest
451 node = el.gridstackNode;
452 if (node && el.parentElement === this.el && this.engine.nodes.find(n => n._id === node._id)) return el;
453
454 // Tempting to initialize the passed in opt with default and valid values, but this break knockout demos
455 // as the actual value are filled in when _prepareElement() calls el.getAttribute('gs-xyz') before adding the node.
456 // So make sure we load any DOM attributes that are not specified in passed in options (which override)
457 const domAttr = this._readAttr(el);
458 Utils.defaults(w, domAttr);
459 this.engine.prepareNode(w);
460 // this._writeAttr(el, w); why write possibly incorrect values back when makeWidget() will ?
461
462 this.el.appendChild(el);
463
464 this.makeWidget(el, w);
465
466 return el;
467 }
468
469 /**
470 * Create the default grid item divs and content (possibly lazy loaded) by using GridStack.renderCB().

Callers 7

loadMethod · 0.95
_setupAcceptWidgetMethod · 0.95
GridStackProviderFunction · 0.80
addMethod · 0.80
addMethod · 0.80
gridstack-spec.tsFile · 0.80

Calls 6

makeWidgetMethod · 0.95
createWidgetDivsMethod · 0.95
_readAttrMethod · 0.95
findMethod · 0.80
defaultsMethod · 0.80
prepareNodeMethod · 0.80

Tested by

no test coverage detected