Skip to content

Commit

Permalink
feat(enableColumnAutoFit): add property to control auto-fit behaviour
Browse files Browse the repository at this point in the history
gridOptions.enableColumnAutoFit controls the column' defaults; colDef.enableColumnAutoFit overrides

them; skip when width already set
  • Loading branch information
Den-dp committed Jun 3, 2016
1 parent dd78c26 commit 49dc4f7
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/UiGridAutoFitColumnsService.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import * as get from 'lodash/object/get';
import Measurer from './Measurer';
import IColumnDef = uiGrid.IColumnDef;
import IGridColumn = uiGrid.IGridColumn;
import IGridOptions = uiGrid.IGridOptions;
import IGridRow = uiGrid.IGridRow;
import IGridInstance = uiGrid.IGridInstance;

interface IExtendedGridInstance extends IGridInstance {
interface IExtendedColumnDef extends uiGrid.IColumnDef {
enableColumnAutoFit: boolean;
}

interface IExtendedGridColumn extends uiGrid.IGridColumn {
colDef: IExtendedColumnDef;
}

interface IExtendedGridInstance extends uiGrid.IGridInstance {
options: IExtendedGridOptions;
}

interface IExtendedGridOptions extends IGridOptions {
interface IExtendedGridOptions extends uiGrid.IGridOptions {
enableColumnAutoFit: boolean;
}

Expand All @@ -34,6 +37,7 @@ export class UiGridAutoFitColumnsService {
}

static defaultGridOptions(gridOptions: IExtendedGridOptions){
// true by default
gridOptions.enableColumnAutoFit = gridOptions.enableColumnAutoFit !== false;
}

Expand Down Expand Up @@ -62,19 +66,26 @@ export class UiGridAutoFitColumnsService {
return value;
}

colAutoFitColumnBuilder(colDef: IColumnDef, col: IGridColumn, gridOptions: IExtendedGridOptions){
colAutoFitColumnBuilder(colDef: IExtendedColumnDef, col: IExtendedGridColumn, gridOptions: IExtendedGridOptions){
var promises = [];

//colDef.enableColumnAutoFit = colDef.enableColumnAutoFit === undefined ? gridOptions.enableColumnAutoFit : colDef.enableColumnAutoFit;

if(colDef.enableColumnAutoFit === undefined) {
//TODO: make it as col.isResizable()
if(UiGridAutoFitColumnsService.isResizable(colDef)) {
colDef.enableColumnAutoFit = gridOptions.enableColumnAutoFit;
} else {
colDef.enableColumnAutoFit = false;
}
}

return this.$q.all(promises);
}

static isResizable(col: IGridColumn): boolean{
return !col.colDef.hasOwnProperty('width');
static isResizable(colDef: IExtendedColumnDef): boolean{
return !colDef.hasOwnProperty('width');
}

columnsProcessor(renderedColumnsToProcess?: Array<IGridColumn>, rows?: Array<IGridRow>){
columnsProcessor(renderedColumnsToProcess?: Array<IExtendedGridColumn>, rows?: Array<uiGrid.IGridRow>){
if(!rows.length){
return renderedColumnsToProcess;
}
Expand All @@ -95,8 +106,8 @@ export class UiGridAutoFitColumnsService {
} = {};

renderedColumnsToProcess.forEach(column => {
//TODO: make it as col.isResizable()
if(UiGridAutoFitColumnsService.isResizable(column)) {

if(column.colDef.enableColumnAutoFit) {
const columnKey = column.name;
optimalWidths[columnKey] = Measurer.measureTextWidth(column.displayName, font) + HEADER_BUTTONS_WIDTH;
//this.$log.info(`${optimalWidths[pair.key]} ${pair.displayName}`);
Expand Down

0 comments on commit 49dc4f7

Please sign in to comment.