UNPKG

6.63 kBJavaScriptView Raw
1/**
2 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
3 * @version v18.1.2
4 * @link http://www.ag-grid.com/
5 * @license MIT
6 */
7"use strict";
8var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
9 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
10 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
11 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
12 return c > 3 && r && Object.defineProperty(target, key, r), r;
13};
14var __metadata = (this && this.__metadata) || function (k, v) {
15 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
16};
17Object.defineProperty(exports, "__esModule", { value: true });
18var rowRenderer_1 = require("./rowRenderer");
19var context_1 = require("../context/context");
20var gridOptionsWrapper_1 = require("../gridOptionsWrapper");
21var headerWrapperComp_1 = require("../headerRendering/header/headerWrapperComp");
22var AutoWidthCalculator = (function () {
23 function AutoWidthCalculator() {
24 }
25 AutoWidthCalculator.prototype.registerGridComp = function (gridPanel) {
26 this.gridPanel = gridPanel;
27 };
28 AutoWidthCalculator.prototype.registerHeaderRootComp = function (headerRootComp) {
29 this.headerRootComp = headerRootComp;
30 };
31 // this is the trick: we create a dummy container and clone all the cells
32 // into the dummy, then check the dummy's width. then destroy the dummy
33 // as we don't need it any more.
34 // drawback: only the cells visible on the screen are considered
35 AutoWidthCalculator.prototype.getPreferredWidthForColumn = function (column) {
36 var eHeaderCell = this.getHeaderCellForColumn(column);
37 // cell isn't visible
38 if (!eHeaderCell) {
39 return -1;
40 }
41 var eDummyContainer = document.createElement('span');
42 // position fixed, so it isn't restricted to the boundaries of the parent
43 eDummyContainer.style.position = 'fixed';
44 // we put the dummy into the body container, so it will inherit all the
45 // css styles that the real cells are inheriting
46 var eBodyContainer = this.gridPanel.getBodyContainer();
47 eBodyContainer.appendChild(eDummyContainer);
48 // get all the cells that are currently displayed (this only brings back
49 // rendered cells, rows not rendered due to row visualisation will not be here)
50 this.putRowCellsIntoDummyContainer(column, eDummyContainer);
51 // also put header cell in
52 // we only consider the lowest level cell, not the group cell. in 99% of the time, this
53 // will be enough. if we consider groups, then it gets to complicated for what it's worth,
54 // as the groups can span columns and this class only considers one column at a time.
55 this.cloneItemIntoDummy(eHeaderCell, eDummyContainer);
56 // at this point, all the clones are lined up vertically with natural widths. the dummy
57 // container will have a width wide enough just to fit the largest.
58 var dummyContainerWidth = eDummyContainer.offsetWidth;
59 // we are finished with the dummy container, so get rid of it
60 eBodyContainer.removeChild(eDummyContainer);
61 // we add padding as I found sometimes the gui still put '...' after some of the texts. so the
62 // user can configure the grid to add a few more pixels after the calculated width
63 var autoSizePadding = this.gridOptionsWrapper.getAutoSizePadding();
64 return dummyContainerWidth + autoSizePadding;
65 };
66 AutoWidthCalculator.prototype.getHeaderCellForColumn = function (column) {
67 var comp = null;
68 // find the rendered header cell
69 this.headerRootComp.forEachHeaderElement(function (headerElement) {
70 if (headerElement instanceof headerWrapperComp_1.HeaderWrapperComp) {
71 var headerWrapperComp = headerElement;
72 if (headerWrapperComp.getColumn() === column) {
73 comp = headerWrapperComp;
74 }
75 }
76 });
77 return comp ? comp.getGui() : null;
78 };
79 AutoWidthCalculator.prototype.putRowCellsIntoDummyContainer = function (column, eDummyContainer) {
80 var _this = this;
81 var eCells = this.rowRenderer.getAllCellsForColumn(column);
82 eCells.forEach(function (eCell) { return _this.cloneItemIntoDummy(eCell, eDummyContainer); });
83 };
84 AutoWidthCalculator.prototype.cloneItemIntoDummy = function (eCell, eDummyContainer) {
85 // make a deep clone of the cell
86 var eCellClone = eCell.cloneNode(true);
87 // the original has a fixed width, we remove this to allow the natural width based on content
88 eCellClone.style.width = '';
89 // the original has position = absolute, we need to remove this so it's positioned normally
90 eCellClone.style.position = 'static';
91 eCellClone.style.left = '';
92 // we put the cell into a containing div, as otherwise the cells would just line up
93 // on the same line, standard flow layout, by putting them into divs, they are laid
94 // out one per line
95 var eCloneParent = document.createElement('div');
96 // table-row, so that each cell is on a row. i also tried display='block', but this
97 // didn't work in IE
98 eCloneParent.style.display = 'table-row';
99 // the twig on the branch, the branch on the tree, the tree in the hole,
100 // the hole in the bog, the bog in the clone, the clone in the parent,
101 // the parent in the dummy, and the dummy down in the vall-e-ooo, OOOOOOOOO! Oh row the rattling bog....
102 eCloneParent.appendChild(eCellClone);
103 eDummyContainer.appendChild(eCloneParent);
104 };
105 __decorate([
106 context_1.Autowired('rowRenderer'),
107 __metadata("design:type", rowRenderer_1.RowRenderer)
108 ], AutoWidthCalculator.prototype, "rowRenderer", void 0);
109 __decorate([
110 context_1.Autowired('gridOptionsWrapper'),
111 __metadata("design:type", gridOptionsWrapper_1.GridOptionsWrapper)
112 ], AutoWidthCalculator.prototype, "gridOptionsWrapper", void 0);
113 AutoWidthCalculator = __decorate([
114 context_1.Bean('autoWidthCalculator')
115 ], AutoWidthCalculator);
116 return AutoWidthCalculator;
117}());
118exports.AutoWidthCalculator = AutoWidthCalculator;