CsvExport.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /*
  4. *+------------------------------------------------------------------------+
  5. *| Licensed Materials - Property of IBM
  6. *| IBM Cognos Products: BI Dashboard
  7. *| (C) Copyright IBM Corp. 2020
  8. *|
  9. *| US Government Users Restricted Rights - Use, duplication or disclosure
  10. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  11. *+------------------------------------------------------------------------+
  12. */
  13. /**
  14. * @class CSVExport
  15. * @hideconstructor
  16. * @classdesc CSV Export Feature
  17. */
  18. define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', './api/CsvExportAPI'], function (APIFactory, CsvExportAPI) {
  19. var CsvExporter = function () {
  20. function CsvExporter(dashboard) {
  21. _classCallCheck(this, CsvExporter);
  22. this.dashboard = dashboard;
  23. this.api = APIFactory.createAPI(this, [CsvExportAPI]);
  24. }
  25. CsvExporter.prototype.destroy = function destroy() {
  26. this.dashboard = null;
  27. };
  28. CsvExporter.prototype.getAPI = function getAPI() {
  29. return this.api;
  30. };
  31. CsvExporter.prototype._toCSV = function _toCSV(data) {
  32. if (Array.isArray(data)) {
  33. var parseCellValue = function parseCellValue(cellData) {
  34. var value = cellData;
  35. if (isNaN(value)) {
  36. value = value.toString().trim();
  37. if (value.indexOf(',') !== -1) {
  38. value = '"' + value.replace(/"/g, '""') + '"';
  39. }
  40. }
  41. return value;
  42. };
  43. return data.map(function (row) {
  44. return row.map ? row.map(parseCellValue).join(',') : parseCellValue(row);
  45. }).join('\n') + '\n';
  46. } else {
  47. throw new Error('The export data must be an array');
  48. }
  49. };
  50. /**
  51. * @function CsvExportAPII#export
  52. * @description Export a CSV file for supplied name & data
  53. * @param {String} fileName The name of the CSV file
  54. * @param {object[][]} data An array of data arrays to aggregate and export
  55. * @return {object} returns a Blob containing the exported CSV data
  56. */
  57. CsvExporter.prototype.export = function _export(filename) {
  58. if (typeof filename !== 'string') {
  59. throw new Error('The filename must be a string');
  60. }
  61. var _window = this._getWindow();
  62. for (var _len = arguments.length, data = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  63. data[_key - 1] = arguments[_key];
  64. }
  65. var csvDataArrays = data.map(this._toCSV.bind(this));
  66. var fullFileName = filename + '.csv';
  67. var csvBlob = new Blob(csvDataArrays, { type: 'text/csv' });
  68. if (!filename) throw new Error('Invalid filename');
  69. var anchor = this._createAnchor(_window);
  70. if (typeof anchor.download !== 'undefined') {
  71. var blobUrl = _window.URL.createObjectURL(csvBlob);
  72. anchor.setAttribute('href', blobUrl);
  73. anchor.setAttribute('download', fullFileName);
  74. anchor.style.display = 'none';
  75. _window.document.body.appendChild(anchor);
  76. anchor.click();
  77. _window.document.body.removeChild(anchor);
  78. setTimeout(_window.URL.revokeObjectURL.bind(this, blobUrl), 5000);
  79. return csvBlob;
  80. } else if (_window.navigator && _window.navigator.msSaveBlob) {
  81. //IE11 support
  82. _window.navigator.msSaveBlob(csvBlob, fullFileName);
  83. return csvBlob;
  84. } else {
  85. throw new Error('This browser does not support the ability to export');
  86. }
  87. };
  88. CsvExporter.prototype._getWindow = function _getWindow() {
  89. return window;
  90. };
  91. CsvExporter.prototype._createAnchor = function _createAnchor(_window) {
  92. return _window.document.createElement('a');
  93. };
  94. return CsvExporter;
  95. }();
  96. return CsvExporter;
  97. });
  98. //# sourceMappingURL=CsvExport.js.map