Built files from Bizgaze WebServer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

export.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. /* Tabulator v4.8.2 (c) Oliver Folkerd */
  3. var ExportRow = function ExportRow(type, columns, component, indent) {
  4. this.type = type;
  5. this.columns = columns;
  6. this.component = component || false;
  7. this.indent = indent || 0;
  8. };
  9. var ExportColumn = function ExportColumn(value, component, width, height, depth) {
  10. this.value = value;
  11. this.component = component || false;
  12. this.width = width;
  13. this.height = height;
  14. this.depth = depth;
  15. };
  16. var Export = function Export(table) {
  17. this.table = table; //hold Tabulator object
  18. this.config = {};
  19. this.cloneTableStyle = true;
  20. this.colVisProp = "";
  21. };
  22. Export.prototype.generateExportList = function (config, style, range, colVisProp) {
  23. this.cloneTableStyle = style;
  24. this.config = config || {};
  25. this.colVisProp = colVisProp;
  26. var headers = this.config.columnHeaders !== false ? this.headersToExportRows(this.generateColumnGroupHeaders()) : [];
  27. var body = this.bodyToExportRows(this.rowLookup(range));
  28. return headers.concat(body);
  29. };
  30. Export.prototype.genereateTable = function (config, style, range, colVisProp) {
  31. var list = this.generateExportList(config, style, range, colVisProp);
  32. return this.genereateTableElement(list);
  33. };
  34. Export.prototype.rowLookup = function (range) {
  35. var _this = this;
  36. var rows = [];
  37. if (typeof range == "function") {
  38. range.call(this.table).forEach(function (row) {
  39. row = _this.table.rowManager.findRow(row);
  40. if (row) {
  41. rows.push(row);
  42. }
  43. });
  44. } else {
  45. switch (range) {
  46. case true:
  47. case "visible":
  48. rows = this.table.rowManager.getVisibleRows(true);
  49. break;
  50. case "all":
  51. rows = this.table.rowManager.rows;
  52. break;
  53. case "selected":
  54. rows = this.table.modules.selectRow.selectedRows;
  55. break;
  56. case "active":
  57. default:
  58. if (this.table.options.pagination) {
  59. rows = this.table.rowManager.getDisplayRows(this.table.rowManager.displayRows.length - 2);
  60. } else {
  61. rows = this.table.rowManager.getDisplayRows();
  62. }
  63. }
  64. }
  65. return Object.assign([], rows);
  66. };
  67. Export.prototype.generateColumnGroupHeaders = function () {
  68. var _this2 = this;
  69. var output = [];
  70. var columns = this.config.columnGroups !== false ? this.table.columnManager.columns : this.table.columnManager.columnsByIndex;
  71. columns.forEach(function (column) {
  72. var colData = _this2.processColumnGroup(column);
  73. if (colData) {
  74. output.push(colData);
  75. }
  76. });
  77. return output;
  78. };
  79. Export.prototype.processColumnGroup = function (column) {
  80. var _this3 = this;
  81. var subGroups = column.columns,
  82. maxDepth = 0,
  83. title = column.definition["title" + (this.colVisProp.charAt(0).toUpperCase() + this.colVisProp.slice(1))] || column.definition.title;
  84. var groupData = {
  85. title: title,
  86. column: column,
  87. depth: 1
  88. };
  89. if (subGroups.length) {
  90. groupData.subGroups = [];
  91. groupData.width = 0;
  92. subGroups.forEach(function (subGroup) {
  93. var subGroupData = _this3.processColumnGroup(subGroup);
  94. if (subGroupData) {
  95. groupData.width += subGroupData.width;
  96. groupData.subGroups.push(subGroupData);
  97. if (subGroupData.depth > maxDepth) {
  98. maxDepth = subGroupData.depth;
  99. }
  100. }
  101. });
  102. groupData.depth += maxDepth;
  103. if (!groupData.width) {
  104. return false;
  105. }
  106. } else {
  107. if (this.columnVisCheck(column)) {
  108. groupData.width = 1;
  109. } else {
  110. return false;
  111. }
  112. }
  113. return groupData;
  114. };
  115. Export.prototype.columnVisCheck = function (column) {
  116. return column.definition[this.colVisProp] !== false && (column.visible || !column.visible && column.definition[this.colVisProp]);
  117. };
  118. Export.prototype.headersToExportRows = function (columns) {
  119. var headers = [],
  120. headerDepth = 0,
  121. exportRows = [];
  122. function parseColumnGroup(column, level) {
  123. var depth = headerDepth - level;
  124. if (typeof headers[level] === "undefined") {
  125. headers[level] = [];
  126. }
  127. column.height = column.subGroups ? 1 : depth - column.depth + 1;
  128. headers[level].push(column);
  129. if (column.height > 1) {
  130. for (var _i = 1; _i < column.height; _i++) {
  131. if (typeof headers[level + _i] === "undefined") {
  132. headers[level + _i] = [];
  133. }
  134. headers[level + _i].push(false);
  135. }
  136. }
  137. if (column.width > 1) {
  138. for (var _i2 = 1; _i2 < column.width; _i2++) {
  139. headers[level].push(false);
  140. }
  141. }
  142. if (column.subGroups) {
  143. column.subGroups.forEach(function (subGroup) {
  144. parseColumnGroup(subGroup, level + 1);
  145. });
  146. }
  147. }
  148. //calculate maximum header debth
  149. columns.forEach(function (column) {
  150. if (column.depth > headerDepth) {
  151. headerDepth = column.depth;
  152. }
  153. });
  154. columns.forEach(function (column) {
  155. parseColumnGroup(column, 0);
  156. });
  157. headers.forEach(function (header) {
  158. var columns = [];
  159. header.forEach(function (col) {
  160. if (col) {
  161. columns.push(new ExportColumn(col.title, col.column.getComponent(), col.width, col.height, col.depth));
  162. } else {
  163. columns.push(null);
  164. }
  165. });
  166. exportRows.push(new ExportRow("header", columns));
  167. });
  168. return exportRows;
  169. };
  170. Export.prototype.bodyToExportRows = function (rows) {
  171. var _this4 = this;
  172. var columns = [];
  173. var exportRows = [];
  174. this.table.columnManager.columnsByIndex.forEach(function (column) {
  175. if (_this4.columnVisCheck(column)) {
  176. columns.push(column.getComponent());
  177. }
  178. });
  179. if (this.config.columnCalcs !== false && this.table.modExists("columnCalcs")) {
  180. if (this.table.modules.columnCalcs.topInitialized) {
  181. rows.unshift(this.table.modules.columnCalcs.topRow);
  182. }
  183. if (this.table.modules.columnCalcs.botInitialized) {
  184. rows.push(this.table.modules.columnCalcs.botRow);
  185. }
  186. }
  187. rows = rows.filter(function (row) {
  188. switch (row.type) {
  189. case "group":
  190. return _this4.config.rowGroups !== false;
  191. break;
  192. case "calc":
  193. return _this4.config.columnCalcs !== false;
  194. break;
  195. case "row":
  196. return !(_this4.table.options.dataTree && _this4.config.dataTree === false && row.modules.dataTree.parent);
  197. break;
  198. }
  199. return true;
  200. });
  201. rows.forEach(function (row, i) {
  202. var rowData = row.getData(_this4.colVisProp);
  203. var exportCols = [];
  204. var indent = 0;
  205. switch (row.type) {
  206. case "group":
  207. indent = row.level;
  208. exportCols.push(new ExportColumn(row.key, row.getComponent(), columns.length, 1));
  209. break;
  210. case "calc":
  211. case "row":
  212. columns.forEach(function (col) {
  213. exportCols.push(new ExportColumn(col._column.getFieldValue(rowData), col, 1, 1));
  214. });
  215. if (_this4.table.options.dataTree && _this4.config.dataTree !== false) {
  216. indent = row.modules.dataTree.index;
  217. }
  218. break;
  219. }
  220. exportRows.push(new ExportRow(row.type, exportCols, row.getComponent(), indent));
  221. });
  222. return exportRows;
  223. };
  224. Export.prototype.genereateTableElement = function (list) {
  225. var _this5 = this;
  226. var table = document.createElement("table"),
  227. headerEl = document.createElement("thead"),
  228. bodyEl = document.createElement("tbody"),
  229. styles = this.lookupTableStyles(),
  230. rowFormatter = this.table.options["rowFormatter" + (this.colVisProp.charAt(0).toUpperCase() + this.colVisProp.slice(1))],
  231. setup = {};
  232. setup.rowFormatter = rowFormatter !== null ? rowFormatter : this.table.options.rowFormatter;
  233. if (this.table.options.dataTree && this.config.dataTree !== false && this.table.modExists("columnCalcs")) {
  234. setup.treeElementField = this.table.modules.dataTree.elementField;
  235. }
  236. //assign group header formatter
  237. setup.groupHeader = this.table.options["groupHeader" + (this.colVisProp.charAt(0).toUpperCase() + this.colVisProp.slice(1))];
  238. if (setup.groupHeader && !Array.isArray(setup.groupHeader)) {
  239. setup.groupHeader = [setup.groupHeader];
  240. }
  241. table.classList.add("tabulator-print-table");
  242. this.mapElementStyles(this.table.columnManager.getHeadersElement(), headerEl, ["border-top", "border-left", "border-right", "border-bottom", "background-color", "color", "font-weight", "font-family", "font-size"]);
  243. if (list.length > 1000) {
  244. console.warn("It may take a long time to render an HTML table with more than 1000 rows");
  245. }
  246. list.forEach(function (row, i) {
  247. switch (row.type) {
  248. case "header":
  249. headerEl.appendChild(_this5.genereateHeaderElement(row, setup, styles));
  250. break;
  251. case "group":
  252. bodyEl.appendChild(_this5.genereateGroupElement(row, setup, styles));
  253. break;
  254. case "calc":
  255. bodyEl.appendChild(_this5.genereateCalcElement(row, setup, styles));
  256. break;
  257. case "row":
  258. var rowEl = _this5.genereateRowElement(row, setup, styles);
  259. _this5.mapElementStyles(i % 2 && styles.evenRow ? styles.evenRow : styles.oddRow, rowEl, ["border-top", "border-left", "border-right", "border-bottom", "color", "font-weight", "font-family", "font-size", "background-color"]);
  260. bodyEl.appendChild(rowEl);
  261. break;
  262. }
  263. });
  264. if (headerEl.innerHTML) {
  265. table.appendChild(headerEl);
  266. }
  267. table.appendChild(bodyEl);
  268. this.mapElementStyles(this.table.element, table, ["border-top", "border-left", "border-right", "border-bottom"]);
  269. return table;
  270. };
  271. Export.prototype.lookupTableStyles = function () {
  272. var styles = {};
  273. //lookup row styles
  274. if (this.cloneTableStyle && window.getComputedStyle) {
  275. styles.oddRow = this.table.element.querySelector(".tabulator-row-odd:not(.tabulator-group):not(.tabulator-calcs)");
  276. styles.evenRow = this.table.element.querySelector(".tabulator-row-even:not(.tabulator-group):not(.tabulator-calcs)");
  277. styles.calcRow = this.table.element.querySelector(".tabulator-row.tabulator-calcs");
  278. styles.firstRow = this.table.element.querySelector(".tabulator-row:not(.tabulator-group):not(.tabulator-calcs)");
  279. styles.firstGroup = this.table.element.getElementsByClassName("tabulator-group")[0];
  280. if (styles.firstRow) {
  281. styles.styleCells = styles.firstRow.getElementsByClassName("tabulator-cell");
  282. styles.firstCell = styles.styleCells[0];
  283. styles.lastCell = styles.styleCells[styles.styleCells.length - 1];
  284. }
  285. }
  286. return styles;
  287. };
  288. Export.prototype.genereateHeaderElement = function (row, setup, styles) {
  289. var _this6 = this;
  290. var rowEl = document.createElement("tr");
  291. row.columns.forEach(function (column) {
  292. if (column) {
  293. var cellEl = document.createElement("th");
  294. var classNames = column.component._column.definition.cssClass ? column.component._column.definition.cssClass.split(" ") : [];
  295. cellEl.colSpan = column.width;
  296. cellEl.rowSpan = column.height;
  297. cellEl.innerHTML = column.value;
  298. if (_this6.cloneTableStyle) {
  299. cellEl.style.boxSizing = "border-box";
  300. }
  301. classNames.forEach(function (className) {
  302. cellEl.classList.add(className);
  303. });
  304. _this6.mapElementStyles(column.component.getElement(), cellEl, ["text-align", "border-top", "border-left", "border-right", "border-bottom", "background-color", "color", "font-weight", "font-family", "font-size"]);
  305. _this6.mapElementStyles(column.component._column.contentElement, cellEl, ["padding-top", "padding-left", "padding-right", "padding-bottom"]);
  306. if (column.component._column.visible) {
  307. _this6.mapElementStyles(column.component.getElement(), cellEl, ["width"]);
  308. } else {
  309. if (column.component._column.definition.width) {
  310. cellEl.style.width = column.component._column.definition.width + "px";
  311. }
  312. }
  313. if (column.component._column.parent) {
  314. _this6.mapElementStyles(column.component._column.parent.groupElement, cellEl, ["border-top"]);
  315. }
  316. rowEl.appendChild(cellEl);
  317. }
  318. });
  319. return rowEl;
  320. };
  321. Export.prototype.genereateGroupElement = function (row, setup, styles) {
  322. var rowEl = document.createElement("tr"),
  323. cellEl = document.createElement("td"),
  324. group = row.columns[0];
  325. rowEl.classList.add("tabulator-print-table-row");
  326. if (setup.groupHeader && setup.groupHeader[row.indent]) {
  327. group.value = setup.groupHeader[row.indent](group.value, row.component._group.getRowCount(), row.component._group.getData(), row.component);
  328. } else {
  329. if (setup.groupHeader === false) {
  330. group.value = group.value;
  331. } else {
  332. group.value = row.component._group.generator(group.value, row.component._group.getRowCount(), row.component._group.getData(), row.component);
  333. }
  334. }
  335. cellEl.colSpan = group.width;
  336. cellEl.innerHTML = group.value;
  337. rowEl.classList.add("tabulator-print-table-group");
  338. rowEl.classList.add("tabulator-group-level-" + row.indent);
  339. if (group.component.isVisible()) {
  340. rowEl.classList.add("tabulator-group-visible");
  341. }
  342. this.mapElementStyles(styles.firstGroup, rowEl, ["border-top", "border-left", "border-right", "border-bottom", "color", "font-weight", "font-family", "font-size", "background-color"]);
  343. this.mapElementStyles(styles.firstGroup, cellEl, ["padding-top", "padding-left", "padding-right", "padding-bottom"]);
  344. rowEl.appendChild(cellEl);
  345. return rowEl;
  346. };
  347. Export.prototype.genereateCalcElement = function (row, setup, styles) {
  348. var rowEl = this.genereateRowElement(row, setup, styles);
  349. rowEl.classList.add("tabulator-print-table-calcs");
  350. this.mapElementStyles(styles.calcRow, rowEl, ["border-top", "border-left", "border-right", "border-bottom", "color", "font-weight", "font-family", "font-size", "background-color"]);
  351. return rowEl;
  352. };
  353. Export.prototype.genereateRowElement = function (row, setup, styles) {
  354. var _this7 = this;
  355. var rowEl = document.createElement("tr");
  356. rowEl.classList.add("tabulator-print-table-row");
  357. row.columns.forEach(function (col) {
  358. if (col) {
  359. var cellEl = document.createElement("td"),
  360. column = col.component._column,
  361. value = col.value;
  362. var cellWrapper = {
  363. modules: {},
  364. getValue: function getValue() {
  365. return value;
  366. },
  367. getField: function getField() {
  368. return column.definition.field;
  369. },
  370. getElement: function getElement() {
  371. return cellEl;
  372. },
  373. getColumn: function getColumn() {
  374. return column.getComponent();
  375. },
  376. getData: function getData() {
  377. return row.component.getData();
  378. },
  379. getRow: function getRow() {
  380. return row.component;
  381. },
  382. getComponent: function getComponent() {
  383. return cellWrapper;
  384. },
  385. column: column
  386. };
  387. var classNames = column.definition.cssClass ? column.definition.cssClass.split(" ") : [];
  388. classNames.forEach(function (className) {
  389. cellEl.classList.add(className);
  390. });
  391. if (_this7.table.modExists("format") && _this7.config.formatCells !== false) {
  392. value = _this7.table.modules.format.formatExportValue(cellWrapper, _this7.colVisProp);
  393. } else {
  394. switch (typeof value === "undefined" ? "undefined" : _typeof(value)) {
  395. case "object":
  396. value = JSON.stringify(value);
  397. break;
  398. case "undefined":
  399. case "null":
  400. value = "";
  401. break;
  402. default:
  403. value = value;
  404. }
  405. }
  406. if (value instanceof Node) {
  407. cellEl.appendChild(value);
  408. } else {
  409. cellEl.innerHTML = value;
  410. }
  411. if (styles.firstCell) {
  412. _this7.mapElementStyles(styles.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"]);
  413. if (column.definition.align) {
  414. cellEl.style.textAlign = column.definition.align;
  415. }
  416. }
  417. if (_this7.table.options.dataTree && _this7.config.dataTree !== false) {
  418. if (setup.treeElementField && setup.treeElementField == column.field || !setup.treeElementField && i == 0) {
  419. if (row.component._row.modules.dataTree.controlEl) {
  420. cellEl.insertBefore(row.component._row.modules.dataTree.controlEl.cloneNode(true), cellEl.firstChild);
  421. }
  422. if (row.component._row.modules.dataTree.branchEl) {
  423. cellEl.insertBefore(row.component._row.modules.dataTree.branchEl.cloneNode(true), cellEl.firstChild);
  424. }
  425. }
  426. }
  427. rowEl.appendChild(cellEl);
  428. if (cellWrapper.modules.format && cellWrapper.modules.format.renderedCallback) {
  429. cellWrapper.modules.format.renderedCallback();
  430. }
  431. if (setup.rowFormatter && _this7.config.formatCells !== false) {
  432. setup.rowFormatter(row.component);
  433. }
  434. }
  435. });
  436. return rowEl;
  437. };
  438. Export.prototype.genereateHTMLTable = function (list) {
  439. var holder = document.createElement("div");
  440. holder.appendChild(this.genereateTableElement(list));
  441. return holder.innerHTML;
  442. };
  443. Export.prototype.getHtml = function (visible, style, config, colVisProp) {
  444. var list = this.generateExportList(config || this.table.options.htmlOutputConfig, style, visible, colVisProp || "htmlOutput");
  445. return this.genereateHTMLTable(list);
  446. };
  447. Export.prototype.mapElementStyles = function (from, to, props) {
  448. if (this.cloneTableStyle && from && to) {
  449. var lookup = {
  450. "background-color": "backgroundColor",
  451. "color": "fontColor",
  452. "width": "width",
  453. "font-weight": "fontWeight",
  454. "font-family": "fontFamily",
  455. "font-size": "fontSize",
  456. "text-align": "textAlign",
  457. "border-top": "borderTop",
  458. "border-left": "borderLeft",
  459. "border-right": "borderRight",
  460. "border-bottom": "borderBottom",
  461. "padding-top": "paddingTop",
  462. "padding-left": "paddingLeft",
  463. "padding-right": "paddingRight",
  464. "padding-bottom": "paddingBottom"
  465. };
  466. if (window.getComputedStyle) {
  467. var fromStyle = window.getComputedStyle(from);
  468. props.forEach(function (prop) {
  469. to.style[lookup[prop]] = fromStyle.getPropertyValue(prop);
  470. });
  471. }
  472. }
  473. };
  474. Tabulator.prototype.registerModule("export", Export);