UNPKG

a-chart

Version:

A-Chart is a ReactJS Library/Package to address all posibilities of Reporting Functionality

277 lines (225 loc) 8.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.exportTable = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; exports.removeInternalKeys = removeInternalKeys; exports._prepHeader = _prepHeader; exports.isFindInObject = isFindInObject; exports.colMatch = colMatch; exports.selectAllGrid = selectAllGrid; exports.getSum = getSum; exports.doSortObjs = doSortObjs; exports.getColumnProps = getColumnProps; exports.exportData = exportData; var _indexR = require("../reducer/indexR"); var _indexR2 = _interopRequireDefault(_indexR); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function removeInternalKeys(obj) { for (var key in obj) { if (key.startsWith('_')) { delete obj[key]; } } }; function _prepHeader(items, config) { if (!config) { return; } var i = 0, ho = {}, hf = config.headers.length, hObj = {}, to; if (items && items.length) { hObj = _extends({}, items[0]); } else { if (config.headers && config.headers.length) { config.headers.forEach(function (v) { to = v; to = "a_" + v.replace(/\s+/g, ''); hObj[to] = v; }); } } removeInternalKeys(hObj); for (var key in hObj) { if (hf) { ho[key] = config.headers[i] ? config.headers[i] : " "; } else { ho[key] = key; } i++; } config.headersCReated = true; config.headers = ho; }; function isFindInObject(st, obj, fkeys) { var t = st.toLowerCase(); // alert(AChartR.subscribe); var val, isFind = -1; for (var key in fkeys) { val = obj[key]; if (!val) { continue; } else if (isNaN(val)) { val = val.toLowerCase(); isFind = val.search(t); } else { val = val.toString(); isFind = val.search(t); } if (isFind >= 0) { break; } } return isFind >= 0 ? true : false; }; function colMatch() { var find = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "d"; var dest = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ""; var arr = dest.split(','), rf = false; for (var i = 0; i < arr.length; i++) { if (arr[i] == find) { rf = true; break; } } return rf; }; function selectAllGrid(s) { var f; this.items.map(function (obj, i) { f = s ? true : null; obj._selected = f; }); }; function getSum() { var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ""; var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var sum = 0, lobj = {}, wf = true; list.map(function (obj, i) { if (isNaN(obj[key])) { if (wf) { alert('"' + key + "\" is not a Numeric field.");wf = false; } //continue; } else { lobj = obj; sum += obj[key]; } }); if (config.formatters[key]) { try { sum = config.formatters[key].apply(lobj, [sum]); } catch (e) { alert("Error Returned in Custom formatter:-" + key); } } return sum; }; function doSortObjs(list, key) { var asc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var keyA, keyB; list.sort(function (a, b) { if (isNaN(a[key])) { keyA = a[key].toLowerCase(); keyB = b[key].toLowerCase(); if (asc) { if (keyA < keyB) //sort string ascending return -1; if (keyA > keyB) return 1; return 0; //default return value (no sorting) } else { if (keyB < keyA) //sort string ascending return -1; if (keyB > keyA) return 1; return 0; //default return value (no sorting) } } else { if (asc) return a[key] - b[key];else return b[key] - a[key]; } }); }; var exportTable = exports.exportTable = function () { function exportTable(table) { _classCallCheck(this, exportTable); this.uri = 'data:application/vnd.ms-excel;base64,'; this.template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'; this.base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))); };this.format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }); }; this.tableToExport = document.getElementById(table); } _createClass(exportTable, [{ key: "download", value: function download(f, h) { document.getElementById("achart-dlink").href = h; document.getElementById("achart-dlink").download = f; document.getElementById("achart-dlink").click(); } }, { key: "toExcel", value: function toExcel(f, sheet) { var href, ctx = { worksheet: sheet || 'Worksheet', table: this.tableToExport.innerHTML }; href = this.uri + this.base64(this.format(this.template, ctx)); this.download(f, href); } }, { key: "toCSV", value: function toCSV(f) { var href, csvFile; var csv = []; var rows = this.tableToExport.querySelectorAll("table tr"); for (var i = 0; i < rows.length; i++) { var row = [], cols = rows[i].querySelectorAll("td, th"); for (var j = 0; j < cols.length; j++) { row.push(cols[j].innerText); }csv.push(row.join(",")); } csv = csv.join("\n"); csvFile = new Blob([csv], { type: "text/csv" }); href = window.URL.createObjectURL(csvFile); this.download(f, href); } }]); return exportTable; }(); ; function getColumnProps(fkey) { var c = { width: "auto", align: "center" }, ret = void 0; if (this.props.config && this.props.config.headerConfig && this.props.config.headerConfig[fkey]) { ret = Object.assign(c, this.props.config.headerConfig[fkey]); } else { ret = c; } return ret; }; function exportData(t, config) { var fn = config.exportFileName || "gridData", tblObj; tblObj = new this.exportTable(config._tableId); if (t == 'xls') { tblObj.toExcel(fn + "." + t, "Worksheet"); } else { tblObj.toCSV(fn + "." + t); } //this.exportTable("achart-d-grid","my test","testash.xls"); };