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

Skip to content

Commit 1b740cb

Browse files
authored
Merge pull request gridstack#2085 from adumesny/master
added GridStackEngine.findEmptyPosition()
2 parents a1b9470 + 987553c commit 1b740cb

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Change log
7777

7878
## 7.0.2 (TBD)
7979
* fixed [#2081](https://github.com/gridstack/gridstack.js/issues/2081) removeWidget() after it's gone from DOM
80-
80+
* add GridStackEngine.findEmptyPosition()
8181
## 7.0.1 (2022-10-14)
8282
* fixed [#2073](https://github.com/gridstack/gridstack.js/issues/2073) SSR (server side rendering) isTouch issue (introduced in v6)
8383
* fixed - removing last item delete sub-grid that are not auto-generated (nested.html vs nested_advanced.html)

src/gridstack-engine.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,26 @@ export class GridStackEngine {
470470
return this;
471471
}
472472

473+
/** find the first available empty spot for the given node width/height, updating the x,y attributes. return true if found */
474+
public findEmptyPosition(node: GridStackNode): boolean {
475+
this.sortNodes();
476+
let found = false;
477+
for (let i = 0; !found; ++i) {
478+
let x = i % this.column;
479+
let y = Math.floor(i / this.column);
480+
if (x + node.w > this.column) {
481+
continue;
482+
}
483+
let box = {x, y, w: node.w, h: node.h};
484+
if (!this.nodes.find(n => Utils.isIntercepted(box, n))) {
485+
node.x = x;
486+
node.y = y;
487+
found = true;
488+
}
489+
}
490+
return found;
491+
}
492+
473493
/** call to add the given node to our list, fixing collision and re-packing */
474494
public addNode(node: GridStackNode, triggerAddEvent = false): GridStackNode {
475495
let dup = this.nodes.find(n => n._id === node._id);
@@ -480,23 +500,8 @@ export class GridStackEngine {
480500
delete node._temporaryRemoved;
481501
delete node._removeDOM;
482502

483-
if (node.autoPosition) {
484-
this.sortNodes();
485-
486-
for (let i = 0;; ++i) {
487-
let x = i % this.column;
488-
let y = Math.floor(i / this.column);
489-
if (x + node.w > this.column) {
490-
continue;
491-
}
492-
let box = {x, y, w: node.w, h: node.h};
493-
if (!this.nodes.find(n => Utils.isIntercepted(box, n))) {
494-
node.x = x;
495-
node.y = y;
496-
delete node.autoPosition; // found our slot
497-
break;
498-
}
499-
}
503+
if (node.autoPosition && this.findEmptyPosition(node)) {
504+
delete node.autoPosition; // found our slot
500505
}
501506

502507
this.nodes.push(node);

src/gridstack.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,6 @@ export class GridStack {
378378
* @param options widget position/size options (optional, and ignore if first param is already option) - see GridStackWidget
379379
*/
380380
public addWidget(els?: GridStackWidget | GridStackElement, options?: GridStackWidget): GridItemHTMLElement {
381-
// support legacy call for now ?
382-
if (arguments.length > 2) {
383-
console.warn('gridstack.ts: `addWidget(el, x, y, width...)` is deprecated. Use `addWidget({x, y, w, content, ...})`. It will be removed soon');
384-
// eslint-disable-next-line prefer-rest-params
385-
let a = arguments, i = 1,
386-
opt: GridStackWidget = { x:a[i++], y:a[i++], w:a[i++], h:a[i++], autoPosition:a[i++],
387-
minW:a[i++], maxW:a[i++], minH:a[i++], maxH:a[i++], id:a[i++] };
388-
return this.addWidget(els, opt);
389-
}
390-
391381
function isGridStackWidget(w: GridStackWidget): w is GridStackWidget { // https://medium.com/ovrsea/checking-the-type-of-an-object-in-typescript-the-type-guards-24d98d9119b0
392382
return w.x !== undefined || w.y !== undefined || w.w !== undefined || w.h !== undefined || w.content !== undefined ? true : false;
393383
}

0 commit comments

Comments
 (0)