Skip to content

Commit fdbce47

Browse files
committed
locked item can be moved/resized
* fix #1767 * `locked` item can be user moved/resized just not pushed by other nodes (broke in 1.1.1) * had incorrectly assume locked = noMove + noSize (user driven) but it's for layout only, so now you have complete choice.
1 parent 3e430d0 commit fdbce47

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

demo/float.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ <h1>Float grid demo</h1>
2929
addEvents(grid);
3030

3131
let items = [
32-
{x: 1, y: 1},
32+
{x: 1, y: 1}, //, locked:true, content:"locked"},
3333
{x: 2, y: 2, w: 3},
3434
{x: 4, y: 2},
3535
{x: 3, y: 1, h: 2},
@@ -44,8 +44,9 @@ <h1>Float grid demo</h1>
4444
w: Math.round(1 + 3 * Math.random()),
4545
h: Math.round(1 + 3 * Math.random())
4646
};
47-
n.content = String(count++);
47+
n.content = n.content || String(count);
4848
grid.addWidget(n);
49+
count++;
4950
};
5051

5152
toggleFloat = function() {

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Change log
6161

6262
* fix [#1760](https://github.com/gridstack/gridstack.js/issues/1760) `removable:true` working again (broke in 4.x)
6363
* fix [#1761](https://github.com/gridstack/gridstack.js/issues/1761) `staticGrid(false)` will now enable drag in behavior (if set)
64+
* fix [#1767](https://github.com/gridstack/gridstack.js/issues/1767) `locked` item can be user moved/resized again, just not pushed by other nodes (broke in 1.1.1)
6465

6566
## 4.2.3 (2021-5-8)
6667

spec/gridstack-engine-spec.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -359,27 +359,32 @@ describe('gridstack engine', function() {
359359
});
360360
it('should add widgets around locked one', function() {
361361
let nodes: GridStackNode[] = [
362-
{x: 0, y: 1, w: 12, h: 1, locked: true, noMove: true, noResize: true, id: 1},
363-
{x: 1, y: 0, w: 2, h: 3, id: 2}
362+
{x: 0, y: 1, w: 12, h: 1, locked: true, noMove: true, noResize: true, id: 0},
363+
{x: 1, y: 0, w: 2, h: 3, id: 1}
364364
];
365365
// add locked item
366366
engine.addNode(nodes[0])
367-
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
368-
engine.addNode(nodes[1])
367+
expect(findNode(engine, 0)).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
369368
// add item that moves past locked one
370-
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
371-
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 1, y: 2, h: 3, id: 2}));
372-
// prevents moving locked item
369+
engine.addNode(nodes[1])
370+
expect(findNode(engine, 0)).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 12, h: 1, locked: true}));
371+
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 1, y: 2, h: 3}));
372+
// locked item can still be moved directly (what user does)
373+
let node0 = findNode(engine, 0);
374+
expect(engine.moveNode(node0, {y:6})).toEqual(true);
375+
expect(findNode(engine, 0)).toEqual(jasmine.objectContaining({x: 0, y: 6, h: 1, locked: true}));
376+
// but moves regular one past it
373377
let node1 = findNode(engine, 1);
374-
expect(engine.moveNode(node1, {x:6, y:6})).toEqual(false);
375-
// but moves regular one (gravity ON)
376-
let node2 = findNode(engine, 2);
377-
expect(engine.moveNode(node2, {x:6, y:6})).toEqual(true);
378-
expect(node2).toEqual(jasmine.objectContaining({x: 6, y: 2, w: 2, h: 3}));
379-
// but moves regular one (gravity OFF)
378+
expect(engine.moveNode(node1, {x:6, y:6})).toEqual(true);
379+
expect(node1).toEqual(jasmine.objectContaining({x: 6, y: 7, w: 2, h: 3}));
380+
// but moves regular one before (gravity ON)
381+
engine.float = false;
382+
expect(engine.moveNode(node1, {x:7, y:3})).toEqual(true);
383+
expect(node1).toEqual(jasmine.objectContaining({x: 7, y: 0, w: 2, h: 3}));
384+
// but moves regular one before (gravity OFF)
380385
engine.float = true;
381-
expect(engine.moveNode(node2, {x:7, y:6})).toEqual(true);
382-
expect(node2).toEqual(jasmine.objectContaining({x: 7, y: 6, w: 2, h: 3}));
386+
expect(engine.moveNode(node1, {x:7, y:3})).toEqual(true);
387+
expect(node1).toEqual(jasmine.objectContaining({x: 7, y: 3, w: 2, h: 3}));
383388
});
384389
});
385390

src/gridstack-dd.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ GridStack.prototype._prepareDragDropByNode = function(node: GridStackNode): Grid
353353
let dd = GridStackDD.get();
354354

355355
// check for disabled grid first
356-
if (this.opts.staticGrid || node.locked ||
357-
((node.noMove || this.opts.disableDrag) && (node.noResize || this.opts.disableResize))) {
356+
if (this.opts.staticGrid || ((node.noMove || this.opts.disableDrag) && (node.noResize || this.opts.disableResize))) {
358357
if (node._initDD) {
359358
dd.remove(el); // nukes everything instead of just disable, will add some styles back next
360359
delete node._initDD;
@@ -616,7 +615,7 @@ GridStack.prototype.movable = function(els: GridStackElement, val: boolean): Gri
616615
if (this.opts.staticGrid) return this; // can't move a static grid!
617616
GridStack.getElements(els).forEach(el => {
618617
let node = el.gridstackNode;
619-
if (!node || node.locked) return;
618+
if (!node) return;
620619
if (val) delete node.noMove; else node.noMove = true;
621620
this._prepareDragDropByNode(node); // init DD if need be, and adjust
622621
});
@@ -632,7 +631,7 @@ GridStack.prototype.resizable = function(els: GridStackElement, val: boolean): G
632631
if (this.opts.staticGrid) return this; // can't resize a static grid!
633632
GridStack.getElements(els).forEach(el => {
634633
let node = el.gridstackNode;
635-
if (!node || node.locked) return;
634+
if (!node) return;
636635
if (val) delete node.noResize; else node.noResize = true;
637636
this._prepareDragDropByNode(node); // init DD if need be, and adjust
638637
});

src/gridstack-engine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ export class GridStackEngine {
506506
* In more complicated cases (maxRow) it will attempt at moving the item and fixing
507507
* others in a clone first, then apply those changes if still within specs. */
508508
public moveNodeCheck(node: GridStackNode, o: GridStackMoveOpts): boolean {
509-
if (node.locked) return false;
509+
// if (node.locked) return false;
510510
if (!this.changedPosConstrain(node, o)) return false;
511511
o.pack = true;
512512

@@ -594,7 +594,7 @@ export class GridStackEngine {
594594

595595
/** return true if the passed in node was actually moved (checks for no-op and locked) */
596596
public moveNode(node: GridStackNode, o: GridStackMoveOpts): boolean {
597-
if (!node || node.locked || !o) return false;
597+
if (!node || /*node.locked ||*/ !o) return false;
598598
if (o.pack === undefined) o.pack = true;
599599

600600
// constrain the passed in values and check if we're still changing our node

0 commit comments

Comments
 (0)