Skip to content

Commit 715b376

Browse files
authored
Merge pull request #2851 from iongroup/fix/columns
Default columns arg for non-12 columns scenarios
2 parents 9041dc3 + ecef223 commit 715b376

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

spec/gridstack-engine-spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,53 @@ describe('gridstack engine', function() {
371371
});
372372
});
373373

374+
describe('test columnChanged and save', function() {
375+
beforeAll(function() {
376+
});
377+
it('wont\'t break layouts with 12 columns', function() {
378+
engine = new GridStackEngine({ column: 12 });
379+
// Add two side-by-side components 6+6 = 12 columns
380+
engine.addNode({ x: 0, y: 0, w: 6, h: 1, id: "left" });
381+
engine.addNode({ x: 6, y: 0, w: 6, h: 1, id: "right" });
382+
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
383+
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 6, h: 1}));
384+
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 6, y: 0, w: 6, h: 1}));
385+
// Resize to 1 column
386+
engine.column = 1;
387+
engine.columnChanged(12, 1);
388+
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
389+
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 1, h: 1}));
390+
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 1, h: 1}));
391+
// Resize back to 12 column
392+
engine.column = 12;
393+
engine.columnChanged(1, 12);
394+
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
395+
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 6, h: 1}));
396+
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 6, y: 0, w: 6, h: 1}));
397+
});
398+
it('wont\'t break layouts with more than 12 columns', function() {
399+
engine = new GridStackEngine({ column: 24 });
400+
// Add two side-by-side components 12+12 = 24 columns
401+
engine.addNode({ x: 0, y: 0, w: 12, h: 1, id: "left" });
402+
engine.addNode({ x: 12, y: 0, w: 12, h: 1, id: "right" });
403+
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
404+
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 12, h: 1}));
405+
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 12, y: 0, w: 12, h: 1}));
406+
// Resize to 1 column
407+
engine.column = 1;
408+
engine.columnChanged(24, 1);
409+
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
410+
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 1, h: 1}));
411+
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 1, h: 1}));
412+
// Resize back to 24 column
413+
engine.column = 24;
414+
engine.columnChanged(1, 24);
415+
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
416+
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 12, h: 1}));
417+
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 12, y: 0, w: 12, h: 1}));
418+
});
419+
});
420+
374421
describe('test compact', function() {
375422
beforeAll(function() {
376423
engine = new GridStackEngine();

src/gridstack-engine.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class GridStackEngine {
3131
public addedNodes: GridStackNode[] = [];
3232
public removedNodes: GridStackNode[] = [];
3333
public batchMode: boolean;
34+
public defaultColumn = 12;
3435
/** @internal callback to update the DOM attributes */
3536
protected onChange: OnChangeCB;
3637
/** @internal */
@@ -49,7 +50,7 @@ export class GridStackEngine {
4950
public static _idSeq = 0;
5051

5152
public constructor(opts: GridStackEngineOptions = {}) {
52-
this.column = opts.column || 12;
53+
this.column = opts.column || this.defaultColumn;
5354
this.maxRow = opts.maxRow;
5455
this._float = opts.float;
5556
this.nodes = opts.nodes || [];
@@ -407,12 +408,12 @@ export class GridStackEngine {
407408
// remember it's position & width so we can restore back (1 -> 12 column) #1655 #1985
408409
// IFF we're not in the middle of column resizing!
409410
const saveOrig = (node.x || 0) + (node.w || 1) > this.column;
410-
if (saveOrig && this.column < 12 && !this._inColumnResize && node._id && this.findCacheLayout(node, 12) === -1) {
411+
if (saveOrig && this.column < this.defaultColumn && !this._inColumnResize && node._id && this.findCacheLayout(node, this.defaultColumn) === -1) {
411412
const copy = {...node}; // need _id + positions
412413
if (copy.autoPosition || copy.x === undefined) { delete copy.x; delete copy.y; }
413-
else copy.x = Math.min(11, copy.x);
414-
copy.w = Math.min(12, copy.w || 1);
415-
this.cacheOneLayout(copy, 12);
414+
else copy.x = Math.min(this.defaultColumn - 1, copy.x);
415+
copy.w = Math.min(this.defaultColumn, copy.w || 1);
416+
this.cacheOneLayout(copy, this.defaultColumn);
416417
}
417418

418419
if (node.w > this.column) {

0 commit comments

Comments
 (0)