otsdaq_utilities  v2_05_02_indev
frozen_columns.js
1 /* Tabulator v4.5.3 (c) Oliver Folkerd */
2 
3 var FrozenColumns = function FrozenColumns(table) {
4  this.table = table; //hold Tabulator object
5  this.leftColumns = [];
6  this.rightColumns = [];
7  this.leftMargin = 0;
8  this.rightMargin = 0;
9  this.rightPadding = 0;
10  this.initializationMode = "left";
11  this.active = false;
12  this.scrollEndTimer = false;
13 };
14 
15 //reset initial state
16 FrozenColumns.prototype.reset = function () {
17  this.initializationMode = "left";
18  this.leftColumns = [];
19  this.rightColumns = [];
20  this.leftMargin = 0;
21  this.rightMargin = 0;
22  this.rightMargin = 0;
23  this.active = false;
24 
25  this.table.columnManager.headersElement.style.marginLeft = 0;
26  this.table.columnManager.element.style.paddingRight = 0;
27 };
28 
29 //initialize specific column
30 FrozenColumns.prototype.initializeColumn = function (column) {
31  var config = { margin: 0, edge: false };
32 
33  if (column.definition.frozen) {
34 
35  if (!column.parent.isGroup) {
36 
37  if (!column.isGroup) {
38  config.position = this.initializationMode;
39 
40  if (this.initializationMode == "left") {
41  this.leftColumns.push(column);
42  } else {
43  this.rightColumns.unshift(column);
44  }
45 
46  this.active = true;
47 
48  column.modules.frozen = config;
49  } else {
50  console.warn("Frozen Column Error - Column Groups cannot be frozen");
51  }
52  } else {
53  console.warn("Frozen Column Error - Grouped columns cannot be frozen");
54  }
55  } else {
56  this.initializationMode = "right";
57  }
58 };
59 
60 //quick layout to smooth horizontal scrolling
61 FrozenColumns.prototype.scrollHorizontal = function () {
62  var _this = this;
63 
64  var rows;
65 
66  if (this.active) {
67  clearTimeout(this.scrollEndTimer);
68 
69  //layout all rows after scroll is complete
70  this.scrollEndTimer = setTimeout(function () {
71  _this.layout();
72  }, 100);
73 
74  rows = this.table.rowManager.getVisibleRows();
75 
76  this.calcMargins();
77 
78  this.layoutColumnPosition();
79 
80  this.layoutCalcRows();
81 
82  rows.forEach(function (row) {
83  if (row.type === "row") {
84  _this.layoutRow(row);
85  }
86  });
87 
88  this.table.rowManager.tableElement.style.marginRight = this.rightMargin;
89  }
90 };
91 
92 //calculate margins for rows
93 FrozenColumns.prototype.calcMargins = function () {
94  this.leftMargin = this._calcSpace(this.leftColumns, this.leftColumns.length) + "px";
95  this.table.columnManager.headersElement.style.marginLeft = this.leftMargin;
96 
97  this.rightMargin = this._calcSpace(this.rightColumns, this.rightColumns.length) + "px";
98  this.table.columnManager.element.style.paddingRight = this.rightMargin;
99 
100  //calculate right frozen columns
101  this.rightPadding = this.table.rowManager.element.clientWidth + this.table.columnManager.scrollLeft;
102 };
103 
104 //layout calculation rows
105 FrozenColumns.prototype.layoutCalcRows = function () {
106  if (this.table.modExists("columnCalcs")) {
107  if (this.table.modules.columnCalcs.topInitialized && this.table.modules.columnCalcs.topRow) {
108  this.layoutRow(this.table.modules.columnCalcs.topRow);
109  }
110  if (this.table.modules.columnCalcs.botInitialized && this.table.modules.columnCalcs.botRow) {
111  this.layoutRow(this.table.modules.columnCalcs.botRow);
112  }
113  }
114 };
115 
116 //calculate column positions and layout headers
117 FrozenColumns.prototype.layoutColumnPosition = function (allCells) {
118  var _this2 = this;
119 
120  this.leftColumns.forEach(function (column, i) {
121  column.modules.frozen.margin = _this2._calcSpace(_this2.leftColumns, i) + _this2.table.columnManager.scrollLeft + "px";
122 
123  if (i == _this2.leftColumns.length - 1) {
124  column.modules.frozen.edge = true;
125  } else {
126  column.modules.frozen.edge = false;
127  }
128 
129  _this2.layoutElement(column.getElement(), column);
130 
131  if (allCells) {
132  column.cells.forEach(function (cell) {
133  _this2.layoutElement(cell.getElement(), column);
134  });
135  }
136  });
137 
138  this.rightColumns.forEach(function (column, i) {
139  column.modules.frozen.margin = _this2.rightPadding - _this2._calcSpace(_this2.rightColumns, i + 1) + "px";
140 
141  if (i == _this2.rightColumns.length - 1) {
142  column.modules.frozen.edge = true;
143  } else {
144  column.modules.frozen.edge = false;
145  }
146 
147  _this2.layoutElement(column.getElement(), column);
148 
149  if (allCells) {
150  column.cells.forEach(function (cell) {
151  _this2.layoutElement(cell.getElement(), column);
152  });
153  }
154  });
155 };
156 
157 //layout columns appropropriatly
158 FrozenColumns.prototype.layout = function () {
159  var self = this,
160  rightMargin = 0;
161 
162  if (self.active) {
163 
164  //calculate row padding
165  this.calcMargins();
166 
167  // self.table.rowManager.activeRows.forEach(function(row){
168  // self.layoutRow(row);
169  // });
170 
171  // if(self.table.options.dataTree){
172  self.table.rowManager.getDisplayRows().forEach(function (row) {
173  if (row.type === "row") {
174  self.layoutRow(row);
175  }
176  });
177  // }
178 
179  this.layoutCalcRows();
180 
181  //calculate left columns
182  this.layoutColumnPosition(true);
183 
184  // if(tableHolder.scrollHeight > tableHolder.clientHeight){
185  // rightMargin -= tableHolder.offsetWidth - tableHolder.clientWidth;
186  // }
187 
188  this.table.rowManager.tableElement.style.marginRight = this.rightMargin;
189  }
190 };
191 
192 FrozenColumns.prototype.layoutRow = function (row) {
193  var _this3 = this;
194 
195  var rowEl = row.getElement();
196 
197  rowEl.style.paddingLeft = this.leftMargin;
198  // rowEl.style.paddingRight = this.rightMargin + "px";
199 
200  this.leftColumns.forEach(function (column) {
201  var cell = row.getCell(column);
202 
203  if (cell) {
204  _this3.layoutElement(cell.getElement(), column);
205  }
206  });
207 
208  this.rightColumns.forEach(function (column) {
209  var cell = row.getCell(column);
210 
211  if (cell) {
212  _this3.layoutElement(cell.getElement(), column);
213  }
214  });
215 };
216 
217 FrozenColumns.prototype.layoutElement = function (element, column) {
218 
219  if (column.modules.frozen) {
220  element.style.position = "absolute";
221  element.style.left = column.modules.frozen.margin;
222 
223  element.classList.add("tabulator-frozen");
224 
225  if (column.modules.frozen.edge) {
226  element.classList.add("tabulator-frozen-" + column.modules.frozen.position);
227  }
228  }
229 };
230 
231 FrozenColumns.prototype._calcSpace = function (columns, index) {
232  var width = 0;
233 
234  for (var i = 0; i < index; i++) {
235  if (columns[i].visible) {
236  width += columns[i].getWidth();
237  }
238  }
239 
240  return width;
241 };
242 
243 Tabulator.prototype.registerModule("frozenColumns", FrozenColumns);