ui.pivot_grid.export.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * DevExtreme (ui/pivot_grid/ui.pivot_grid.export.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var _class = require("../../core/class");
  11. var _class2 = _interopRequireDefault(_class);
  12. var _type = require("../../core/utils/type");
  13. var _extend = require("../../core/utils/extend");
  14. var _iterator = require("../../core/utils/iterator");
  15. var _format_helper = require("../../format_helper");
  16. var _number = require("../../localization/number");
  17. var _exporter = require("../../exporter");
  18. var _exporter2 = _interopRequireDefault(_exporter);
  19. var _uiGrid_core = require("../grid_core/ui.grid_core.export_mixin");
  20. var _uiGrid_core2 = _interopRequireDefault(_uiGrid_core);
  21. var _deferred = require("../../core/utils/deferred");
  22. function _interopRequireDefault(obj) {
  23. return obj && obj.__esModule ? obj : {
  24. "default": obj
  25. }
  26. }
  27. var DEFAULT_DATA_TYPE = "string";
  28. var COLUMN_HEADER_STYLE_ID = 0;
  29. var ROW_HEADER_STYLE_ID = 1;
  30. var DATA_STYLE_OFFSET = 2;
  31. var DEFAUL_COLUMN_WIDTH = 100;
  32. exports.ExportMixin = (0, _extend.extend)({}, _uiGrid_core2.default, {
  33. exportToExcel: function() {
  34. var that = this;
  35. _exporter2.default.export(that.getDataProvider(), {
  36. fileName: that.option("export.fileName"),
  37. proxyUrl: that.option("export.proxyUrl"),
  38. format: "EXCEL",
  39. rtlEnabled: that.option("rtlEnabled"),
  40. ignoreErrors: that.option("export.ignoreExcelErrors"),
  41. exportingAction: that._actions.onExporting,
  42. exportedAction: that._actions.onExported,
  43. fileSavingAction: that._actions.onFileSaving
  44. }, _exporter.excel.getData)
  45. },
  46. _getLength: function(items) {
  47. var itemCount = items[0].length;
  48. var cellCount = 0;
  49. for (var i = 0; i < itemCount; i++) {
  50. cellCount += items[0][i].colspan || 1
  51. }
  52. return cellCount
  53. },
  54. _correctCellsInfoItemLengths: function(cellsInfo, expectedLength) {
  55. for (var i = 0; i < cellsInfo.length; i++) {
  56. while (cellsInfo[i].length < expectedLength) {
  57. cellsInfo[i].push({})
  58. }
  59. }
  60. return cellsInfo
  61. },
  62. _calculateCellInfoItemLength: function(columnsRow) {
  63. var result = 0;
  64. for (var columnIndex = 0; columnIndex < columnsRow.length; columnIndex++) {
  65. result += (0, _type.isDefined)(columnsRow[columnIndex].colspan) ? columnsRow[columnIndex].colspan : 1
  66. }
  67. return result
  68. },
  69. _getAllItems: function(columnsInfo, rowsInfoItems, cellsInfo) {
  70. var correctedCellsInfo = cellsInfo;
  71. var rowsLength = this._getLength(rowsInfoItems);
  72. var headerRowsCount = columnsInfo.length;
  73. if (columnsInfo.length > 0 && columnsInfo[0].length > 0 && cellsInfo.length > 0 && 0 === cellsInfo[0].length) {
  74. var cellInfoItemLength = this._calculateCellInfoItemLength(columnsInfo[0]);
  75. if (cellInfoItemLength > 0) {
  76. correctedCellsInfo = this._correctCellsInfoItemLengths(cellsInfo, cellInfoItemLength)
  77. }
  78. }
  79. var sourceItems = columnsInfo.concat(correctedCellsInfo);
  80. for (var rowIndex = 0; rowIndex < rowsInfoItems.length; rowIndex++) {
  81. for (var cellIndex = rowsInfoItems[rowIndex].length - 1; cellIndex >= 0; cellIndex--) {
  82. if (!(0, _type.isDefined)(sourceItems[rowIndex + headerRowsCount])) {
  83. sourceItems[rowIndex + headerRowsCount] = []
  84. }
  85. sourceItems[rowIndex + headerRowsCount].splice(0, 0, (0, _extend.extend)({}, rowsInfoItems[rowIndex][cellIndex]))
  86. }
  87. }
  88. sourceItems[0].splice(0, 0, (0, _extend.extend)({}, this._getEmptyCell(), {
  89. alignment: this._options.rtlEnabled ? "right" : "left",
  90. colspan: rowsLength,
  91. rowspan: headerRowsCount
  92. }));
  93. return this._prepareItems(sourceItems)
  94. },
  95. getDataProvider: function() {
  96. var that = this;
  97. var dataController = this._dataController;
  98. var items = new _deferred.Deferred;
  99. dataController.beginLoading();
  100. setTimeout(function() {
  101. var columnsInfo = (0, _extend.extend)(true, [], dataController.getColumnsInfo(true));
  102. var rowsInfoItems = (0, _extend.extend)(true, [], dataController.getRowsInfo(true));
  103. var cellsInfo = dataController.getCellsInfo(true);
  104. items.resolve(that._getAllItems(columnsInfo, rowsInfoItems, cellsInfo));
  105. dataController.endLoading()
  106. });
  107. return new exports.DataProvider({
  108. items: items,
  109. rtlEnabled: this.option("rtlEnabled"),
  110. dataFields: this.getDataSource().getAreaFields("data"),
  111. customizeExcelCell: this.option("export.customizeExcelCell")
  112. })
  113. }
  114. });
  115. function getCellDataType(field) {
  116. if (field && field.customizeText) {
  117. return "string"
  118. }
  119. if (field.dataType) {
  120. return field.dataType
  121. }
  122. if (field.format) {
  123. if (1 === (0, _number.parse)((0, _format_helper.format)(1, field.format))) {
  124. return "number"
  125. }
  126. if ((0, _format_helper.format)(new Date, field.format)) {
  127. return "date"
  128. }
  129. }
  130. return DEFAULT_DATA_TYPE
  131. }
  132. exports.DataProvider = _class2.default.inherit({
  133. ctor: function(options) {
  134. this._options = options;
  135. this._styles = []
  136. },
  137. ready: function() {
  138. var that = this;
  139. var options = that._options;
  140. var dataFields = options.dataFields;
  141. return (0, _deferred.when)(options.items).done(function(items) {
  142. var headerSize = items[0][0].rowspan;
  143. var columns = items[headerSize - 1];
  144. var dataItemStyle = {
  145. alignment: options.rtlEnabled ? "left" : "right"
  146. };
  147. that._styles = [{
  148. alignment: "center",
  149. dataType: "string"
  150. }, {
  151. alignment: options.rtlEnabled ? "right" : "left",
  152. dataType: "string"
  153. }];
  154. if (dataFields.length) {
  155. dataFields.forEach(function(dataField) {
  156. that._styles.push((0, _extend.extend)({}, dataItemStyle, {
  157. format: dataField.format,
  158. dataType: getCellDataType(dataField)
  159. }))
  160. })
  161. } else {
  162. that._styles.push(dataItemStyle)
  163. }(0, _iterator.each)(columns, function(columnIndex, column) {
  164. column.width = DEFAUL_COLUMN_WIDTH
  165. });
  166. options.columns = columns;
  167. options.items = items
  168. })
  169. },
  170. getColumns: function() {
  171. return this._options.columns
  172. },
  173. getRowsCount: function() {
  174. return this._options.items.length
  175. },
  176. getGroupLevel: function() {
  177. return 0
  178. },
  179. getCellMerging: function(rowIndex, cellIndex) {
  180. var items = this._options.items;
  181. var item = items[rowIndex] && items[rowIndex][cellIndex];
  182. return item ? {
  183. colspan: item.colspan - 1,
  184. rowspan: item.rowspan - 1
  185. } : {
  186. colspan: 0,
  187. rowspan: 0
  188. }
  189. },
  190. getFrozenArea: function() {
  191. var items = this._options.items;
  192. return {
  193. x: items[0][0].colspan,
  194. y: items[0][0].rowspan
  195. }
  196. },
  197. getCellType: function(rowIndex, cellIndex) {
  198. var style = this._styles[this.getStyleId(rowIndex, cellIndex)];
  199. return style && style.dataType || "string"
  200. },
  201. getCellData: function(rowIndex, cellIndex) {
  202. var result = {};
  203. var items = this._options.items;
  204. var item = items[rowIndex] && items[rowIndex][cellIndex] || {};
  205. if ("string" === this.getCellType(rowIndex, cellIndex)) {
  206. result.value = item.text
  207. } else {
  208. result.value = item.value
  209. }
  210. return result
  211. },
  212. getStyles: function() {
  213. return this._styles
  214. },
  215. getStyleId: function(rowIndex, cellIndex) {
  216. var items = this._options.items;
  217. var columnHeaderSize = items[0][0].rowspan;
  218. var rowHeaderSize = items[0][0].colspan;
  219. var item = items[rowIndex] && items[rowIndex][cellIndex] || {};
  220. if (0 === cellIndex && 0 === rowIndex) {
  221. return COLUMN_HEADER_STYLE_ID
  222. } else {
  223. if (cellIndex >= rowHeaderSize && rowIndex < columnHeaderSize) {
  224. return COLUMN_HEADER_STYLE_ID
  225. } else {
  226. if (rowIndex >= columnHeaderSize && cellIndex < rowHeaderSize) {
  227. return ROW_HEADER_STYLE_ID
  228. }
  229. }
  230. }
  231. return DATA_STYLE_OFFSET + (item.dataIndex || 0)
  232. },
  233. hasCustomizeExcelCell: function() {
  234. return (0, _type.isDefined)(this._options.customizeExcelCell)
  235. },
  236. customizeExcelCell: function(e) {
  237. if (this._options.customizeExcelCell) {
  238. this._options.customizeExcelCell(e)
  239. }
  240. }
  241. });