otsdaq_utilities  v2_05_02_indev
html_table_export.js
1 var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
2 
3 /* Tabulator v4.5.3 (c) Oliver Folkerd */
4 
5 var HtmlTableExport = function HtmlTableExport(table) {
6  this.table = table; //hold Tabulator object
7  this.config = {};
8  this.cloneTableStyle = true;
9  this.colVisProp = "";
10 };
11 
12 HtmlTableExport.prototype.genereateTable = function (config, style, visible, colVisProp) {
13  this.cloneTableStyle = style;
14  this.config = config || {};
15  this.colVisProp = colVisProp;
16 
17  var headers = this.generateHeaderElements();
18  var body = this.generateBodyElements(visible);
19 
20  var table = document.createElement("table");
21  table.classList.add("tabulator-print-table");
22  table.appendChild(headers);
23  table.appendChild(body);
24 
25  this.mapElementStyles(this.table.element, table, ["border-top", "border-left", "border-right", "border-bottom"]);
26 
27  return table;
28 };
29 
30 HtmlTableExport.prototype.generateColumnGroupHeaders = function () {
31  var _this = this;
32 
33  var output = [];
34 
35  var columns = this.config.columnGroups !== false ? this.table.columnManager.columns : this.table.columnManager.columnsByIndex;
36 
37  columns.forEach(function (column) {
38  var colData = _this.processColumnGroup(column);
39 
40  if (colData) {
41  output.push(colData);
42  }
43  });
44 
45  return output;
46 };
47 
48 HtmlTableExport.prototype.processColumnGroup = function (column) {
49  var _this2 = this;
50 
51  var subGroups = column.columns,
52  maxDepth = 0;
53 
54  var groupData = {
55  title: column.definition.title,
56  column: column,
57  depth: 1
58  };
59 
60  if (subGroups.length) {
61  groupData.subGroups = [];
62  groupData.width = 0;
63 
64  subGroups.forEach(function (subGroup) {
65  var subGroupData = _this2.processColumnGroup(subGroup);
66 
67  if (subGroupData) {
68  groupData.width += subGroupData.width;
69  groupData.subGroups.push(subGroupData);
70 
71  if (subGroupData.depth > maxDepth) {
72  maxDepth = subGroupData.depth;
73  }
74  }
75  });
76 
77  groupData.depth += maxDepth;
78 
79  if (!groupData.width) {
80  return false;
81  }
82  } else {
83  if (this.columnVisCheck(column)) {
84  groupData.width = 1;
85  } else {
86  return false;
87  }
88  }
89 
90  return groupData;
91 };
92 
93 HtmlTableExport.prototype.groupHeadersToRows = function (columns) {
94 
95  var headers = [],
96  headerDepth = 0;
97 
98  function parseColumnGroup(column, level) {
99 
100  var depth = headerDepth - level;
101 
102  if (typeof headers[level] === "undefined") {
103  headers[level] = [];
104  }
105 
106  column.height = column.subGroups ? 1 : depth - column.depth + 1;
107 
108  headers[level].push(column);
109 
110  if (column.subGroups) {
111  column.subGroups.forEach(function (subGroup) {
112  parseColumnGroup(subGroup, level + 1);
113  });
114  }
115  }
116 
117  //calculate maximum header debth
118  columns.forEach(function (column) {
119  if (column.depth > headerDepth) {
120  headerDepth = column.depth;
121  }
122  });
123 
124  columns.forEach(function (column) {
125  parseColumnGroup(column, 0);
126  });
127 
128  return headers;
129 };
130 
131 HtmlTableExport.prototype.generateHeaderElements = function () {
132  var _this3 = this;
133 
134  var headerEl = document.createElement("thead");
135 
136  var rows = this.groupHeadersToRows(this.generateColumnGroupHeaders());
137 
138  rows.forEach(function (row) {
139  var rowEl = document.createElement("tr");
140 
141  _this3.mapElementStyles(_this3.table.columnManager.getHeadersElement(), headerEl, ["border-top", "border-left", "border-right", "border-bottom", "background-color", "color", "font-weight", "font-family", "font-size"]);
142 
143  row.forEach(function (column) {
144  var cellEl = document.createElement("th");
145  var classNames = column.column.definition.cssClass ? column.column.definition.cssClass.split(" ") : [];
146 
147  cellEl.colSpan = column.width;
148  cellEl.rowSpan = column.height;
149 
150  cellEl.innerHTML = column.column.definition.title;
151 
152  if (_this3.cloneTableStyle) {
153  cellEl.style.boxSizing = "border-box";
154  }
155 
156  classNames.forEach(function (className) {
157  cellEl.classList.add(className);
158  });
159 
160  _this3.mapElementStyles(column.column.getElement(), cellEl, ["text-align", "border-top", "border-left", "border-right", "border-bottom", "background-color", "color", "font-weight", "font-family", "font-size"]);
161  _this3.mapElementStyles(column.column.contentElement, cellEl, ["padding-top", "padding-left", "padding-right", "padding-bottom"]);
162 
163  if (column.column.visible) {
164  _this3.mapElementStyles(column.column.getElement(), cellEl, ["width"]);
165  } else {
166  if (column.column.definition.width) {
167  cellEl.style.width = column.column.definition.width + "px";
168  }
169  }
170 
171  if (column.column.parent) {
172  _this3.mapElementStyles(column.column.parent.groupElement, cellEl, ["border-top"]);
173  }
174 
175  rowEl.appendChild(cellEl);
176  });
177 
178  headerEl.appendChild(rowEl);
179  });
180 
181  return headerEl;
182 };
183 
184 HtmlTableExport.prototype.generateBodyElements = function (visible) {
185  var _this4 = this;
186 
187  var oddRow, evenRow, calcRow, firstRow, firstCell, firstGroup, lastCell, styleCells, styleRow;
188 
189  //lookup row styles
190  if (this.cloneTableStyle && window.getComputedStyle) {
191  oddRow = this.table.element.querySelector(".tabulator-row-odd:not(.tabulator-group):not(.tabulator-calcs)");
192  evenRow = this.table.element.querySelector(".tabulator-row-even:not(.tabulator-group):not(.tabulator-calcs)");
193  calcRow = this.table.element.querySelector(".tabulator-row.tabulator-calcs");
194  firstRow = this.table.element.querySelector(".tabulator-row:not(.tabulator-group):not(.tabulator-calcs)");
195  firstGroup = this.table.element.getElementsByClassName("tabulator-group")[0];
196 
197  if (firstRow) {
198  styleCells = firstRow.getElementsByClassName("tabulator-cell");
199  firstCell = styleCells[0];
200  lastCell = styleCells[styleCells.length - 1];
201  }
202  }
203 
204  var bodyEl = document.createElement("tbody");
205 
206  var rows = visible ? this.table.rowManager.getVisibleRows(true) : this.table.rowManager.getDisplayRows();
207  var columns = [];
208 
209  if (this.config.columnCalcs !== false && this.table.modExists("columnCalcs")) {
210  if (this.table.modules.columnCalcs.topInitialized) {
211  rows.unshift(this.table.modules.columnCalcs.topRow);
212  }
213 
214  if (this.table.modules.columnCalcs.botInitialized) {
215  rows.push(this.table.modules.columnCalcs.botRow);
216  }
217  }
218 
219  this.table.columnManager.columnsByIndex.forEach(function (column) {
220  if (_this4.columnVisCheck(column)) {
221  columns.push(column);
222  }
223  });
224 
225  rows = rows.filter(function (row) {
226  switch (row.type) {
227  case "group":
228  return _this4.config.rowGroups !== false;
229  break;
230 
231  case "calc":
232  return _this4.config.columnCalcs !== false;
233  break;
234  }
235 
236  return true;
237  });
238 
239  if (rows.length > 1000) {
240  console.warn("It may take a long time to render an HTML table with more than 1000 rows");
241  }
242 
243  rows.forEach(function (row, i) {
244  var rowData = row.getData();
245 
246  var rowEl = document.createElement("tr");
247  rowEl.classList.add("tabulator-print-table-row");
248 
249  switch (row.type) {
250  case "group":
251  var cellEl = document.createElement("td");
252  cellEl.colSpan = columns.length;
253  cellEl.innerHTML = row.key;
254 
255  rowEl.classList.add("tabulator-print-table-group");
256 
257  _this4.mapElementStyles(firstGroup, rowEl, ["border-top", "border-left", "border-right", "border-bottom", "color", "font-weight", "font-family", "font-size", "background-color"]);
258  _this4.mapElementStyles(firstGroup, cellEl, ["padding-top", "padding-left", "padding-right", "padding-bottom"]);
259  rowEl.appendChild(cellEl);
260  break;
261 
262  case "calc":
263  rowEl.classList.add("tabulator-print-table-calcs");
264 
265  case "row":
266  columns.forEach(function (column) {
267  var cellEl = document.createElement("td");
268 
269  var value = column.getFieldValue(rowData);
270 
271  var cellWrapper = {
272  modules: {},
273  getValue: function getValue() {
274  return value;
275  },
276  getField: function getField() {
277  return column.definition.field;
278  },
279  getElement: function getElement() {
280  return cellEl;
281  },
282  getColumn: function getColumn() {
283  return column.getComponent();
284  },
285  getData: function getData() {
286  return rowData;
287  },
288  getRow: function getRow() {
289  return row.getComponent();
290  },
291  getComponent: function getComponent() {
292  return cellWrapper;
293  },
294  column: column
295  };
296 
297  var classNames = column.definition.cssClass ? column.definition.cssClass.split(" ") : [];
298 
299  classNames.forEach(function (className) {
300  cellEl.classList.add(className);
301  });
302 
303  if (_this4.table.modExists("format")) {
304  value = _this4.table.modules.format.formatValue(cellWrapper);
305  } else {
306  switch (typeof value === "undefined" ? "undefined" : _typeof(value)) {
307  case "object":
308  value = JSON.stringify(value);
309  break;
310 
311  case "undefined":
312  case "null":
313  value = "";
314  break;
315 
316  default:
317  value = value;
318  }
319  }
320 
321  if (value instanceof Node) {
322  cellEl.appendChild(value);
323  } else {
324  cellEl.innerHTML = value;
325  }
326 
327  if (firstCell) {
328  _this4.mapElementStyles(firstCell, cellEl, ["padding-top", "padding-left", "padding-right", "padding-bottom", "border-top", "border-left", "border-right", "border-bottom", "color", "font-weight", "font-family", "font-size", "text-align"]);
329  }
330 
331  rowEl.appendChild(cellEl);
332  });
333 
334  styleRow = row.type == "calc" ? calcRow : i % 2 && evenRow ? evenRow : oddRow;
335 
336  _this4.mapElementStyles(styleRow, rowEl, ["border-top", "border-left", "border-right", "border-bottom", "color", "font-weight", "font-family", "font-size", "background-color"]);
337  break;
338  }
339 
340  bodyEl.appendChild(rowEl);
341  });
342 
343  return bodyEl;
344 };
345 
346 HtmlTableExport.prototype.columnVisCheck = function (column) {
347  return column.definition[this.colVisProp] !== false && (column.visible || !column.visible && column.definition[this.colVisProp]);
348 };
349 
350 HtmlTableExport.prototype.getHtml = function (visible, style, config) {
351  var holder = document.createElement("div");
352 
353  holder.appendChild(this.genereateTable(config || this.table.options.htmlOutputConfig, style, visible, "htmlOutput"));
354 
355  return holder.innerHTML;
356 };
357 
358 HtmlTableExport.prototype.mapElementStyles = function (from, to, props) {
359  if (this.cloneTableStyle && from && to) {
360 
361  var lookup = {
362  "background-color": "backgroundColor",
363  "color": "fontColor",
364  "width": "width",
365  "font-weight": "fontWeight",
366  "font-family": "fontFamily",
367  "font-size": "fontSize",
368  "text-align": "textAlign",
369  "border-top": "borderTop",
370  "border-left": "borderLeft",
371  "border-right": "borderRight",
372  "border-bottom": "borderBottom",
373  "padding-top": "paddingTop",
374  "padding-left": "paddingLeft",
375  "padding-right": "paddingRight",
376  "padding-bottom": "paddingBottom"
377  };
378 
379  if (window.getComputedStyle) {
380  var fromStyle = window.getComputedStyle(from);
381 
382  props.forEach(function (prop) {
383  to.style[lookup[prop]] = fromStyle.getPropertyValue(prop);
384  });
385  }
386  }
387 };
388 
389 Tabulator.prototype.registerModule("htmlTableExport", HtmlTableExport);