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

feat(services): add onBeforeResizeByContent (onAfter) #335

Merged
merged 1 commit into from
May 7, 2021
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
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ describe('Resizer Service', () => {
it('should call the resize and expect first column have a fixed width while other will have a calculated width when resizing by their content', () => {
const setColumnsSpy = jest.spyOn(gridStub, 'setColumns');
const reRenderColumnsSpy = jest.spyOn(gridStub, 'reRenderColumns');
const pubSubSpy = jest.spyOn(pubSubServiceStub, 'publish');

service.init(gridStub, divContainer);
service.resizeColumnsByCellContent(true);
Expand All @@ -324,11 +325,17 @@ describe('Resizer Service', () => {
expect.objectContaining({ id: 'country', width: 14 }), // longest number "United States of America" goes over resizeMaxWidthThreshold so we fallback to it
]));
expect(reRenderColumnsSpy).toHaveBeenCalledWith(true);
expect(pubSubSpy).toBeCalledWith('onBeforeResizeByContent');
expect(pubSubSpy).toBeCalledWith('onAfterResizeByContent', {
calculateColumnWidths: { userId: 30, firstName: 56, lastName: 68, gender: 57, age: 29, street: 15, country: 14, },
readItemCount: 5
});
});

it('should call the resize and expect first column have a fixed width while other will have a calculated width when resizing by their content and grid is editable', () => {
const setColumnsSpy = jest.spyOn(gridStub, 'setColumns');
const reRenderColumnsSpy = jest.spyOn(gridStub, 'reRenderColumns');
const pubSubSpy = jest.spyOn(pubSubServiceStub, 'publish');

mockGridOptions.editable = true;
service.init(gridStub, divContainer);
Expand All @@ -346,6 +353,11 @@ describe('Resizer Service', () => {
expect.objectContaining({ id: 'country', width: 14 }), // longest number "United States of America" goes over resizeMaxWidthThreshold so we fallback to it
]));
expect(reRenderColumnsSpy).toHaveBeenCalledWith(true);
expect(pubSubSpy).toBeCalledWith('onBeforeResizeByContent');
expect(pubSubSpy).toBeCalledWith('onAfterResizeByContent', {
calculateColumnWidths: { userId: 30, firstName: 61, lastName: 73, gender: 57, age: 29, street: 15, country: 14, },
readItemCount: 5
});
});
});
});
Expand Down
9 changes: 9 additions & 0 deletions packages/vanilla-bundle/src/services/resizer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export class ResizerService {
if (!Array.isArray(dataset) || dataset.length === 0) {
return;
}
this.eventPubSubService.publish('onBeforeResizeByContent');
let readItemCount = 0;

// read a few optional resize by content grid options
const resizeCellCharWidthInPx = this.gridOptions.resizeCellCharWidthInPx ?? 7; // width in pixels of a string character, this can vary depending on which font family/size is used & cell padding
Expand Down Expand Up @@ -196,6 +198,7 @@ export class ResizerService {
}
}
});
readItemCount = rowIdx + 1;
}

// finally loop through all column definitions one last time to apply new calculated `width` on each elligible column
Expand Down Expand Up @@ -254,10 +257,16 @@ export class ResizerService {
// send updated column definitions widths to SlickGrid
this._grid.setColumns(columnDefinitions);

const calculateColumnWidths: { [columnId in string | number]: number | undefined; } = {};
for (const columnDef of columnDefinitions) {
calculateColumnWidths[columnDef.id] = columnDef.width;
}

// get the grid container viewport width and if our viewport calculated total columns is greater than the viewport width
// then we'll call reRenderColumns() when getting wider than viewport or else the default autosizeColumns() when we know we have plenty of space to shrink the columns
const viewportWidth = this._gridParentContainerElm?.offsetWidth ?? 0;
this._totalColumnsWidthByContent > viewportWidth ? this._grid.reRenderColumns(reRender) : this._grid.autosizeColumns();
this.eventPubSubService.publish('onAfterResizeByContent', { readItemCount, calculateColumnWidths });
}

/**
Expand Down