Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default columns arg for non-12 columns scenarios #2851

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions spec/gridstack-engine-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,53 @@ describe('gridstack engine', function() {
});
});

describe('test columnChanged and save', function() {
beforeAll(function() {
});
it('wont\'t break layouts with 12 columns', function() {
engine = new GridStackEngine({ column: 12 });
// Add two side-by-side components 6+6 = 12 columns
engine.addNode({ x: 0, y: 0, w: 6, h: 1, id: "left" });
engine.addNode({ x: 6, y: 0, w: 6, h: 1, id: "right" });
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 6, h: 1}));
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 6, y: 0, w: 6, h: 1}));
// Resize to 1 column
engine.column = 1;
engine.columnChanged(12, 1);
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 1, h: 1}));
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 1, h: 1}));
// Resize back to 12 column
engine.column = 12;
engine.columnChanged(1, 12);
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 6, h: 1}));
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 6, y: 0, w: 6, h: 1}));
});
it('wont\'t break layouts with more than 12 columns', function() {
engine = new GridStackEngine({ column: 24 });
// Add two side-by-side components 12+12 = 24 columns
engine.addNode({ x: 0, y: 0, w: 12, h: 1, id: "left" });
engine.addNode({ x: 12, y: 0, w: 12, h: 1, id: "right" });
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 12, h: 1}));
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 12, y: 0, w: 12, h: 1}));
// Resize to 1 column
engine.column = 1;
engine.columnChanged(24, 1);
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 1, h: 1}));
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 0, y: 1, w: 1, h: 1}));
// Resize back to 24 column
engine.column = 24;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this would work BEFORE your changes too as you are calling engine.columnChanged() directly with 24 and defaultColumn is not updated to match

engine.columnChanged(1, 24);
engine.save().forEach(node => engine.nodeBoundFix(findNode(engine, node.id!)!));
expect(findNode(engine, "left")).toEqual(jasmine.objectContaining({x: 0, y: 0, w: 12, h: 1}));
expect(findNode(engine, "right")).toEqual(jasmine.objectContaining({x: 12, y: 0, w: 12, h: 1}));
});
});

describe('test compact', function() {
beforeAll(function() {
engine = new GridStackEngine();
Expand Down
11 changes: 6 additions & 5 deletions src/gridstack-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class GridStackEngine {
public addedNodes: GridStackNode[] = [];
public removedNodes: GridStackNode[] = [];
public batchMode: boolean;
public defaultColumn = 12;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of having a 'hard coded' max layout of 12, we can just use the largest size in the layout as the current max. will have to check code again...

/** @internal callback to update the DOM attributes */
protected onChange: OnChangeCB;
/** @internal */
Expand All @@ -49,7 +50,7 @@ export class GridStackEngine {
public static _idSeq = 0;

public constructor(opts: GridStackEngineOptions = {}) {
this.column = opts.column || 12;
this.column = opts.column || this.defaultColumn;
this.maxRow = opts.maxRow;
this._float = opts.float;
this.nodes = opts.nodes || [];
Expand Down Expand Up @@ -403,12 +404,12 @@ export class GridStackEngine {
// remember it's position & width so we can restore back (1 -> 12 column) #1655 #1985
// IFF we're not in the middle of column resizing!
const saveOrig = (node.x || 0) + (node.w || 1) > this.column;
if (saveOrig && this.column < 12 && !this._inColumnResize && node._id && this.findCacheLayout(node, 12) === -1) {
if (saveOrig && this.column < this.defaultColumn && !this._inColumnResize && node._id && this.findCacheLayout(node, this.defaultColumn) === -1) {
const copy = {...node}; // need _id + positions
if (copy.autoPosition || copy.x === undefined) { delete copy.x; delete copy.y; }
else copy.x = Math.min(11, copy.x);
copy.w = Math.min(12, copy.w || 1);
this.cacheOneLayout(copy, 12);
else copy.x = Math.min(this.defaultColumn - 1, copy.x);
copy.w = Math.min(this.defaultColumn, copy.w || 1);
this.cacheOneLayout(copy, this.defaultColumn);
}

if (node.w > this.column) {
Expand Down