Skip to content

Commit

Permalink
Merge pull request #335 from ghiscoding/feat/resize-by-content-events
Browse files Browse the repository at this point in the history
feat(services): add onBeforeResizeByContent (onAfter)
  • Loading branch information
ghiscoding authored May 7, 2021
2 parents fecaaa1 + 3e99fab commit ec2ad6c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
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

0 comments on commit ec2ad6c

Please sign in to comment.