123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 'use strict';
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| IBM Cognos Products: BI Dashboard
- *| (C) Copyright IBM Corp. 2020
- *|
- *| US Government Users Restricted Rights - Use, duplication or disclosure
- *| restricted by GSA ADP Schedule Contract with IBM Corp.
- *+------------------------------------------------------------------------+
- */
- /**
- * @class CSVExport
- * @hideconstructor
- * @classdesc CSV Export Feature
- */
- define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', './api/CsvExportAPI'], function (APIFactory, CsvExportAPI) {
- var CsvExporter = function () {
- function CsvExporter(dashboard) {
- _classCallCheck(this, CsvExporter);
- this.dashboard = dashboard;
- this.api = APIFactory.createAPI(this, [CsvExportAPI]);
- }
- CsvExporter.prototype.destroy = function destroy() {
- this.dashboard = null;
- };
- CsvExporter.prototype.getAPI = function getAPI() {
- return this.api;
- };
- CsvExporter.prototype._toCSV = function _toCSV(data) {
- if (Array.isArray(data)) {
- var parseCellValue = function parseCellValue(cellData) {
- var value = cellData;
- if (isNaN(value)) {
- value = value.toString().trim();
- if (value.indexOf(',') !== -1) {
- value = '"' + value.replace(/"/g, '""') + '"';
- }
- }
- return value;
- };
- return data.map(function (row) {
- return row.map ? row.map(parseCellValue).join(',') : parseCellValue(row);
- }).join('\n') + '\n';
- } else {
- throw new Error('The export data must be an array');
- }
- };
- /**
- * @function CsvExportAPII#export
- * @description Export a CSV file for supplied name & data
- * @param {String} fileName The name of the CSV file
- * @param {object[][]} data An array of data arrays to aggregate and export
- * @return {object} returns a Blob containing the exported CSV data
- */
- CsvExporter.prototype.export = function _export(filename) {
- if (typeof filename !== 'string') {
- throw new Error('The filename must be a string');
- }
- var _window = this._getWindow();
- for (var _len = arguments.length, data = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- data[_key - 1] = arguments[_key];
- }
- var csvDataArrays = data.map(this._toCSV.bind(this));
- var fullFileName = filename + '.csv';
- var csvBlob = new Blob(csvDataArrays, { type: 'text/csv' });
- if (!filename) throw new Error('Invalid filename');
- var anchor = this._createAnchor(_window);
- if (typeof anchor.download !== 'undefined') {
- var blobUrl = _window.URL.createObjectURL(csvBlob);
- anchor.setAttribute('href', blobUrl);
- anchor.setAttribute('download', fullFileName);
- anchor.style.display = 'none';
- _window.document.body.appendChild(anchor);
- anchor.click();
- _window.document.body.removeChild(anchor);
- setTimeout(_window.URL.revokeObjectURL.bind(this, blobUrl), 5000);
- return csvBlob;
- } else if (_window.navigator && _window.navigator.msSaveBlob) {
- //IE11 support
- _window.navigator.msSaveBlob(csvBlob, fullFileName);
- return csvBlob;
- } else {
- throw new Error('This browser does not support the ability to export');
- }
- };
- CsvExporter.prototype._getWindow = function _getWindow() {
- return window;
- };
- CsvExporter.prototype._createAnchor = function _createAnchor(_window) {
- return _window.document.createElement('a');
- };
- return CsvExporter;
- }();
- return CsvExporter;
- });
- //# sourceMappingURL=CsvExport.js.map
|