Skip to content

Commit 6f892c3

Browse files
MKirovaMKirova
authored andcommitted
chore(*): Refactor to re-use common code for header sizing from grid base.
1 parent c91283b commit 6f892c3

File tree

4 files changed

+29
-37
lines changed

4 files changed

+29
-37
lines changed

projects/igniteui-angular/src/lib/grids/columns/column.component.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,23 +2312,7 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
23122312
* Returns the width and padding of a header cell.
23132313
*/
23142314
public getHeaderCellWidths() {
2315-
const range = this.grid.document.createRange();
2316-
2317-
// We do not cover cases where there are children with width 100% and etc,
2318-
// because then we try to get new column size, based on header content, which is sized based on column size...
2319-
const headerWidth = this.platform.getNodeSizeViaRange(range,
2320-
this.headerCell.nativeElement,
2321-
this.headerGroup.nativeElement);
2322-
2323-
const headerStyle = this.grid.document.defaultView.getComputedStyle(this.headerCell.nativeElement);
2324-
const headerPadding = parseFloat(headerStyle.paddingLeft) + parseFloat(headerStyle.paddingRight) +
2325-
parseFloat(headerStyle.borderRightWidth);
2326-
2327-
// Take into consideration the header group element, since column pinning applies borders to it if its not a columnGroup.
2328-
const headerGroupStyle = this.grid.document.defaultView.getComputedStyle(this.headerGroup.nativeElement);
2329-
const borderSize = !this.parent ? parseFloat(headerGroupStyle.borderRightWidth) + parseFloat(headerGroupStyle.borderLeftWidth) : 0;
2330-
2331-
return { width: Math.ceil(headerWidth), padding: Math.ceil(headerPadding + borderSize) };
2315+
return this.grid.getHeaderCellWidth(this.headerCell.nativeElement);
23322316
}
23332317

23342318
/**

projects/igniteui-angular/src/lib/grids/common/grid.interface.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ export interface GridType extends IGridDataBindable {
404404
hasColumnGroups: boolean;
405405
hasEditableColumns: boolean;
406406
uniqueColumnValuesStrategy: (column: ColumnType, tree: FilteringExpressionsTree, done: (values: any[]) => void) => void;
407+
getHeaderCellWidth: (element: HTMLElement) => ISizeInfo;
407408

408409
cdr: ChangeDetectorRef;
409410
document: Document;
@@ -643,3 +644,8 @@ export interface GridSVGIcon {
643644
name: string;
644645
value: string;
645646
}
647+
648+
export interface ISizeInfo {
649+
width: number,
650+
padding: number
651+
}

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ import {
123123
IPinColumnCancellableEventArgs
124124
} from './common/events';
125125
import { IgxAdvancedFilteringDialogComponent } from './filtering/advanced-filtering/advanced-filtering-dialog.component';
126-
import { ColumnType, GridServiceType, GridType, IGX_GRID_SERVICE_BASE, RowType } from './common/grid.interface';
126+
import { ColumnType, GridServiceType, GridType, IGX_GRID_SERVICE_BASE, ISizeInfo, RowType } from './common/grid.interface';
127127
import { DropPosition } from './moving/moving.service';
128128
import { IgxHeadSelectorDirective, IgxRowSelectorDirective } from './selection/row-selectors';
129129
import { IgxColumnComponent } from './columns/column.component';
@@ -3883,6 +3883,26 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
38833883
return this.width === null || diff >= 0;
38843884
}
38853885

3886+
/**
3887+
* @hidden @internal
3888+
* Gets the header cell inner width for auto-sizing.
3889+
*/
3890+
public getHeaderCellWidth(element: HTMLElement): ISizeInfo {
3891+
const range = this.document.createRange();
3892+
const headerWidth = this.platform.getNodeSizeViaRange(range,
3893+
element,
3894+
element.parentElement);
3895+
3896+
const headerStyle = this.document.defaultView.getComputedStyle(element);
3897+
const headerPadding = parseFloat(headerStyle.paddingLeft) + parseFloat(headerStyle.paddingRight) +
3898+
parseFloat(headerStyle.borderRightWidth);
3899+
3900+
// Take into consideration the header group element, since column pinning applies borders to it if its not a columnGroup.
3901+
const headerGroupStyle = this.document.defaultView.getComputedStyle(element.parentElement);
3902+
const borderSize = parseFloat(headerGroupStyle.borderRightWidth) + parseFloat(headerGroupStyle.borderLeftWidth);
3903+
return { width: Math.ceil(headerWidth), padding: Math.ceil(headerPadding + borderSize) };
3904+
}
3905+
38863906
/**
38873907
* @hidden @internal
38883908
* Gets the combined width of the columns that are specific to the enabled grid features. They are fixed.

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.component.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,40 +1021,22 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
10211021
@ViewChildren(IgxPivotRowDimensionContentComponent)
10221022
protected rowDimensionContentCollection: QueryList<IgxPivotRowDimensionContentComponent>;
10231023

1024-
protected getElementContentWidth(element: HTMLElement): any {
1025-
const range = this.document.createRange();
1026-
const headerWidth = this.platform.getNodeSizeViaRange(range,
1027-
element,
1028-
element.parentElement);
1029-
1030-
const headerStyle = this.document.defaultView.getComputedStyle(element);
1031-
const headerPadding = parseFloat(headerStyle.paddingLeft) + parseFloat(headerStyle.paddingRight) +
1032-
parseFloat(headerStyle.borderRightWidth);
1033-
1034-
return { width: Math.ceil(headerWidth), padding: Math.ceil(headerPadding) };
1035-
}
1036-
10371024
protected getDimensionType(dimension: IPivotDimension): PivotDimensionType {
10381025
return PivotUtil.flatten(this.rowDimensions).indexOf(dimension) !== -1 ? PivotDimensionType.Row :
10391026
PivotUtil.flatten(this.columnDimensions).indexOf(dimension) !== -1 ? PivotDimensionType.Column : PivotDimensionType.Filter;
10401027
}
10411028

10421029
protected getLargesContentWidth(contents: ElementRef[]): string {
1043-
const range = this.document.createRange();
10441030
const largest = new Map<number, number>();
1045-
10461031
if (contents.length > 0) {
10471032
const cellsContentWidths = [];
1048-
contents.forEach((elem) => cellsContentWidths.push(this.getElementContentWidth(elem.nativeElement).width));
1049-
1033+
contents.forEach((elem) => cellsContentWidths.push(this.getHeaderCellWidth(elem.nativeElement).width));
10501034
const index = cellsContentWidths.indexOf(Math.max(...cellsContentWidths));
10511035
const cellStyle = this.document.defaultView.getComputedStyle(contents[index].nativeElement);
10521036
const cellPadding = parseFloat(cellStyle.paddingLeft) + parseFloat(cellStyle.paddingRight) +
10531037
parseFloat(cellStyle.borderLeftWidth) + parseFloat(cellStyle.borderRightWidth);
1054-
10551038
largest.set(Math.max(...cellsContentWidths), cellPadding);
10561039
}
1057-
10581040
const largestCell = Math.max(...Array.from(largest.keys()));
10591041
const width = Math.ceil(largestCell + largest.get(largestCell));
10601042

0 commit comments

Comments
 (0)