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

Method _setupAcceptWidget

src/gridstack.ts:2407–2680  ·  view source on GitHub ↗

@internal called to add drag over to support widgets being added externally

()

Source from the content-addressed store, hash-verified

2405
2406 /** @internal called to add drag over to support widgets being added externally */
2407 protected _setupAcceptWidget(): GridStack {
2408
2409 // check if we need to disable things
2410 if (this.opts.staticGrid || (!this.opts.acceptWidgets && !this.opts.removable)) {
2411 dd.droppable(this.el, 'destroy');
2412 return this;
2413 }
2414
2415 // vars shared across all methods
2416 let cellHeight: number, cellWidth: number;
2417
2418 const onDrag = (event: DragEvent, el: GridItemHTMLElement, helper: GridItemHTMLElement) => {
2419 helper = helper || el;
2420 const node = helper.gridstackNode;
2421 if (!node) return;
2422
2423 // if the element is being dragged from outside, scale it down to match the grid's scale
2424 // and slightly adjust its position relative to the mouse
2425 if (!node.grid?.el) {
2426 // this scales the helper down
2427 helper.style.transform = `scale(${1 / this.dragTransform.xScale},${1 / this.dragTransform.yScale})`;
2428 // this makes it so that the helper is well positioned relative to the mouse after scaling
2429 const helperRect = helper.getBoundingClientRect();
2430 helper.style.left = helperRect.x + (this.dragTransform.xScale - 1) * (event.clientX - helperRect.x) / this.dragTransform.xScale + 'px';
2431 helper.style.top = helperRect.y + (this.dragTransform.yScale - 1) * (event.clientY - helperRect.y) / this.dragTransform.yScale + 'px';
2432 helper.style.transformOrigin = `0px 0px`
2433 }
2434
2435 let { top, left } = helper.getBoundingClientRect();
2436 const rect = this.el.getBoundingClientRect();
2437 left -= rect.left;
2438 top -= rect.top;
2439 const ui: DDUIData = {
2440 position: {
2441 top: top * this.dragTransform.xScale,
2442 left: left * this.dragTransform.yScale
2443 }
2444 };
2445
2446 if (node._temporaryRemoved) {
2447 node.x = Math.max(0, Math.round(left / cellWidth));
2448 node.y = Math.max(0, Math.round(top / cellHeight));
2449 delete node.autoPosition;
2450 this.engine.nodeBoundFix(node);
2451
2452 // don't accept *initial* location if doesn't fit #1419 (locked drop region, or can't grow), but maybe try if it will go somewhere
2453 if (!this.engine.willItFit(node)) {
2454 node.autoPosition = true; // ignore x,y and try for any slot...
2455 if (!this.engine.willItFit(node)) {
2456 dd.off(el, 'drag'); // stop calling us
2457 return; // full grid or can't grow
2458 }
2459 if (node._willFitPos) {
2460 // use the auto position instead #1687
2461 Utils.copyPos(node, node._willFitPos);
2462 delete node._willFitPos;
2463 }
2464 }

Callers 3

constructorMethod · 0.95
setStaticMethod · 0.95
updateOptionsMethod · 0.95

Calls 15

_readAttrMethod · 0.95
_leaveMethod · 0.95
cellWidthMethod · 0.95
getCellHeightMethod · 0.95
removeAsSubGridMethod · 0.95
setAnimationMethod · 0.95
_triggerChangeEventMethod · 0.95
_removeDDMethod · 0.95
addWidgetMethod · 0.95
_prepareElementMethod · 0.95
resizeToContentCheckMethod · 0.95

Tested by

no test coverage detected