Skip to content

Commit ec1d40f

Browse files
committed
feat(main): adaption of auto-width to general HTML content.
The plugin handles now rows including HTML elements and not only text. Lodash is no more needed. Fixed build errors, updated dependancies to lastest versions, generated files are all under /dist minWidth and maxWidth are now respected
1 parent 26ad8e4 commit ec1d40f

File tree

5 files changed

+58
-43
lines changed

5 files changed

+58
-43
lines changed

package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
"description": "The plugin ensures that the column width will be wide enough for showing the longest data of that column (or column name).",
77
"dependencies": {},
88
"devDependencies": {
9-
"angular": "~1.5.6",
10-
"angular-ui-grid": "~3.1.1",
11-
"awesome-typescript-loader": "~0.17.0",
12-
"commitizen": "~2.8.1",
13-
"cross-env": "~1.0.8",
14-
"cz-conventional-changelog": "~1.1.6",
15-
"ghooks": "~1.2.1",
16-
"jquery": "~2.2.4",
17-
"lodash": "~3.10.1",
18-
"ng-annotate-webpack-plugin": "~0.1.2",
19-
"rimraf": "~2.5.2",
20-
"semantic-release": "~4.3.5",
21-
"typescript": "~1.8.10",
22-
"typings": "~1.0.4",
23-
"validate-commit-msg": "~2.6.1",
24-
"webpack": "~1.13.1"
9+
"angular": "~1.6.6",
10+
"angular-ui-grid": "~4.0.6",
11+
"awesome-typescript-loader": "~3.2.3",
12+
"commitizen": "~2.9.6",
13+
"cross-env": "~5.0.5",
14+
"cz-conventional-changelog": "~2.0.0",
15+
"ghooks": "~2.0.0",
16+
"jquery": "~3.2.1",
17+
"ng-annotate-webpack-plugin": "~0.2.1-pre",
18+
"rimraf": "~2.6.2",
19+
"semantic-release": "~8.0.3",
20+
"ts-loader": "^2.3.7",
21+
"typescript": "~2.5.3",
22+
"typings": "~2.1.1",
23+
"validate-commit-msg": "~2.14.0",
24+
"webpack": "~3.6.0"
2525
},
2626
"scripts": {
2727
"prebuild": "typings install & rimraf dist build",

src/UiGridAutoFitColumnsService.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
import * as get from 'lodash/object/get';
1+
22
import Measurer from './Measurer';
33
import UiGridMetrics from './UiGridMetrics';
44

5-
interface IExtendedColumnDef extends uiGrid.IColumnDef {
5+
export interface IExtendedColumnDef extends uiGrid.IColumnDef {
66
enableColumnAutoFit: boolean;
77
}
88

9-
interface IExtendedGridColumn extends uiGrid.IGridColumn {
9+
export interface IExtendedGridColumn extends uiGrid.IGridColumn {
1010
colDef: IExtendedColumnDef;
11+
hasCustomWidth: boolean;
12+
minWidth: number;
13+
maxWidth: number;
14+
grid: IExtendedGridInstance;
1115
}
1216

13-
interface IExtendedGridInstance extends uiGrid.IGridInstance {
17+
export interface IExtendedGridInstance extends uiGrid.IGridInstance {
1418
options: IExtendedGridOptions;
19+
id: number;
1520
}
1621

17-
interface IExtendedGridOptions extends uiGrid.IGridOptions {
22+
export interface IExtendedGridOptions extends uiGrid.IGridOptions {
1823
enableColumnAutoFit: boolean;
1924
}
2025

21-
interface IAnyFilterPredicateFunc {
26+
export interface IAnyFilterPredicateFunc {
2227
(value: any, firstFlag?: any, secondFlag?: any): string;
2328
}
2429

@@ -87,33 +92,40 @@ export class UiGridAutoFitColumnsService {
8792
}
8893

8994
columnsProcessor(renderedColumnsToProcess?: Array<IExtendedGridColumn>, rows?: Array<uiGrid.IGridRow>) {
95+
9096
if (!rows.length) {
9197
return renderedColumnsToProcess;
9298
}
93-
// TODO: respect existing colDef options
94-
// if (col.colDef.enableColumnAutoFitting === false) return;
9599

96100
let optimalWidths: {
97101
[name: string]: number
98102
} = {};
99103

104+
/* to be able to calculate the width of a column in any browser, we need to temporarily deactivate the min-width and max-width attributes set before */
105+
let tempCSS = '';
106+
renderedColumnsToProcess.forEach(function (column) {
107+
//30px and 9000px are the default values for minWidth and maxWidth in UI Grid.
108+
tempCSS += ' .grid' + column.grid.id + ' ' + column.getColClass(true) + ' { min-width: 30px !important; max-width: 9000px !important; }';
109+
});
110+
let tempStyle = $('<style>').text(tempCSS).appendTo('body');
100111

101112
renderedColumnsToProcess.forEach(column => {
102113

103-
if (column.colDef.enableColumnAutoFit) {
114+
if (column.colDef.enableColumnAutoFit && !column.hasCustomWidth) {
104115
const columnKey = column.field || column.name;
105-
optimalWidths[columnKey] = Measurer.measureRoundedTextWidth(column.displayName, this.gridMetrics.getHeaderFont()) + this.gridMetrics.getHeaderButtonsWidth();
106-
107-
rows.forEach((row) => {
108-
let cellText = get(row.entity, columnKey);
109-
110-
if (!!column.colDef.cellFilter) {
111-
cellText = this.getFilteredValue(cellText, column.colDef.cellFilter);
112-
}
113-
114-
const currentCellWidth = Measurer.measureRoundedTextWidth(cellText, this.gridMetrics.getCellFont());
115-
const optimalCellWidth = currentCellWidth > 300 ? 300 : currentCellWidth;
116-
116+
117+
let currentHeaderWidth = 0;
118+
if($('.ui-grid-header-cell' + column.getColClass(true))[0] !== undefined) {
119+
currentHeaderWidth = $('.ui-grid-header-cell' + column.getColClass(true))[0].scrollWidth;
120+
}
121+
let optimalHeaderWidth = currentHeaderWidth < column.minWidth ? column.minWidth : currentHeaderWidth;
122+
optimalHeaderWidth = optimalHeaderWidth > column.maxWidth ? column.maxWidth : optimalHeaderWidth;
123+
optimalWidths[columnKey] = optimalHeaderWidth;
124+
125+
$(column.getColClass(true)).each(function () {
126+
let currentCellWidth = this.scrollWidth;
127+
let optimalCellWidth = currentCellWidth < column.minWidth ? column.minWidth : currentCellWidth;
128+
optimalCellWidth = optimalCellWidth > column.maxWidth ? column.maxWidth : optimalCellWidth;
117129
if (optimalCellWidth > optimalWidths[columnKey]) {
118130
optimalWidths[columnKey] = optimalCellWidth;
119131
}
@@ -123,6 +135,9 @@ export class UiGridAutoFitColumnsService {
123135
column.updateColumnDef(column.colDef, false);
124136
}
125137
});
138+
139+
tempStyle.remove();
140+
126141
return renderedColumnsToProcess;
127142
}
128143

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"module": "commonjs",
44
"target": "es5",
55
"sourceMap": true,
6-
"outDir": "./",
6+
"outDir": "./dist/compiler/",
77
"removeComments": false,
88
"noImplicitAny": false,
99
"noEmitHelpers": false,

typings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"devDependencies": {},
55
"globalDependencies": {
66
"angular": "registry:dt/angular#1.5.0+20160517064839",
7-
"jquery": "registry:dt/jquery#1.10.0+20160417213236",
7+
"jquery": "registry:dt/jquery#1.10.0+20170310222111",
88
"ui-grid": "registry:dt/ui-grid#0.0.0+20160423061350"
99
}
1010
}

webpack.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
entry: './src/index.ts',
1212

1313
output: {
14-
path: './dist',
14+
path: __dirname + '/dist',
1515
filename: isProduction() ? 'autoFitColumns.min.js' : 'autoFitColumns.js',
1616
libraryTarget: 'umd'
1717
},
@@ -21,14 +21,14 @@ module.exports = {
2121
},
2222

2323
resolve: {
24-
extensions: ['', '.ts', '.webpack.js', '.web.js', '.js']
24+
extensions: ['.ts', '.webpack.js', '.web.js', '.js']
2525
},
2626

2727
devtool: 'source-map',
2828

2929
module: {
3030
loaders: [
31-
{ test: /\.ts$/, loader: 'awesome-typescript', exclude: /(node_modules|bower_components)/ }
31+
{ test: /\.ts$/, loader: 'awesome-typescript-loader', exclude: /(node_modules|bower_components)/ }
3232
]
3333
},
3434

@@ -46,4 +46,4 @@ if(isProduction()) {
4646
drop_console: true
4747
}})
4848
);
49-
}
49+
}

0 commit comments

Comments
 (0)