/*! * ui-grid - v4.4.6 - 2018-04-06 * Copyright (c) 2018 ; License: MIT */ /* global ExcelBuilder */ /* global console */ (function () { 'use strict'; /** * @ngdoc overview * @name ui.grid.exporter * @description * * # ui.grid.exporter * *
* gridOptions.exporterSuppressColumns = [ 'buttons' ];
*
*/
gridOptions.exporterSuppressColumns = gridOptions.exporterSuppressColumns ? gridOptions.exporterSuppressColumns : [];
/**
* @ngdoc object
* @name exporterCsvColumnSeparator
* @propertyOf ui.grid.exporter.api:GridOptions
* @description The character to use as column separator
* link
*
* {
* fontSize: 11
* }
*
*/
gridOptions.exporterPdfDefaultStyle = gridOptions.exporterPdfDefaultStyle ? gridOptions.exporterPdfDefaultStyle : { fontSize: 11 };
/**
* @ngdoc object
* @name exporterPdfTableStyle
* @propertyOf ui.grid.exporter.api:GridOptions
* @description The table style in pdfMake format
*
* {
* margin: [0, 5, 0, 15]
* }
*
*/
gridOptions.exporterPdfTableStyle = gridOptions.exporterPdfTableStyle ? gridOptions.exporterPdfTableStyle : { margin: [0, 5, 0, 15] };
/**
* @ngdoc object
* @name exporterPdfTableHeaderStyle
* @propertyOf ui.grid.exporter.api:GridOptions
* @description The tableHeader style in pdfMake format
*
* {
* bold: true,
* fontSize: 12,
* color: 'black'
* }
*
*/
gridOptions.exporterPdfTableHeaderStyle = gridOptions.exporterPdfTableHeaderStyle ? gridOptions.exporterPdfTableHeaderStyle : { bold: true, fontSize: 12, color: 'black' };
/**
* @ngdoc object
* @name exporterPdfHeader
* @propertyOf ui.grid.exporter.api:GridOptions
* @description The header section for pdf exports. Can be
* simple text:
*
* gridOptions.exporterPdfHeader = 'My Header';
*
* Can be a more complex object in pdfMake format:
*
* gridOptions.exporterPdfHeader = {
* columns: [
* 'Left part',
* { text: 'Right part', alignment: 'right' }
* ]
* };
*
* Or can be a function, allowing page numbers and the like
*
* gridOptions.exporterPdfHeader: function(currentPage, pageCount) { return currentPage.toString() + ' of ' + pageCount; };
*
*/
gridOptions.exporterPdfHeader = gridOptions.exporterPdfHeader ? gridOptions.exporterPdfHeader : null;
/**
* @ngdoc object
* @name exporterPdfFooter
* @propertyOf ui.grid.exporter.api:GridOptions
* @description The header section for pdf exports. Can be
* simple text:
*
* gridOptions.exporterPdfFooter = 'My Footer';
*
* Can be a more complex object in pdfMake format:
*
* gridOptions.exporterPdfFooter = {
* columns: [
* 'Left part',
* { text: 'Right part', alignment: 'right' }
* ]
* };
*
* Or can be a function, allowing page numbers and the like
*
* gridOptions.exporterPdfFooter: function(currentPage, pageCount) { return currentPage.toString() + ' of ' + pageCount; };
*
*/
gridOptions.exporterPdfFooter = gridOptions.exporterPdfFooter ? gridOptions.exporterPdfFooter : null;
/**
* @ngdoc object
* @name exporterPdfOrientation
* @propertyOf ui.grid.exporter.api:GridOptions
* @description The orientation, should be a valid pdfMake value,
* 'landscape' or 'portrait'
*
* gridOptions.exporterPdfCustomFormatter = function ( docDefinition ) {
* docDefinition.styles.footerStyle = { bold: true, fontSize: 10 };
* return docDefinition;
* }
*
* gridOptions.exporterPdfFooter = { text: 'My footer', style: 'footerStyle' }
*
*/
gridOptions.exporterPdfCustomFormatter = ( gridOptions.exporterPdfCustomFormatter && typeof( gridOptions.exporterPdfCustomFormatter ) === 'function' ) ? gridOptions.exporterPdfCustomFormatter : function ( docDef ) { return docDef; };
/**
* @ngdoc object
* @name exporterHeaderFilterUseName
* @propertyOf ui.grid.exporter.api:GridOptions
* @description Defaults to false, which leads to `displayName` being passed into the headerFilter.
* If set to true, then will pass `name` instead.
*
*
* @example
*
* gridOptions.exporterHeaderFilterUseName = true;
*
*/
gridOptions.exporterHeaderFilterUseName = gridOptions.exporterHeaderFilterUseName === true;
/**
* @ngdoc object
* @name exporterHeaderFilter
* @propertyOf ui.grid.exporter.api:GridOptions
* @description A function to apply to the header displayNames before exporting. Useful for internationalisation,
* for example if you were using angular-translate you'd set this to `$translate.instant`. Note that this
* call must be synchronous, it cannot be a call that returns a promise.
*
* Behaviour can be changed to pass in `name` instead of `displayName` through use of `exporterHeaderFilterUseName: true`.
*
* @example
*
* gridOptions.exporterHeaderFilter = function( displayName ){ return 'col: ' + name; };
*
* OR
*
* gridOptions.exporterHeaderFilter = $translate.instant;
*
*/
/**
* @ngdoc function
* @name exporterFieldCallback
* @propertyOf ui.grid.exporter.api:GridOptions
* @description A function to call for each field before exporting it. Allows
* massaging of raw data into a display format, for example if you have applied
* filters to convert codes into decodes, or you require
* a specific date format in the exported content.
*
* The method is called once for each field exported, and provides the grid, the
* gridCol and the GridRow for you to use as context in massaging the data.
*
* @param {Grid} grid provides the grid in case you have need of it
* @param {GridRow} row the row from which the data comes
* @param {GridColumn} col the column from which the data comes
* @param {object} value the value for your massaging
* @returns {object} you must return the massaged value ready for exporting
*
* @example
*
* gridOptions.exporterFieldCallback = function ( grid, row, col, value ){
* if ( col.name === 'status' ){
* value = decodeStatus( value );
* }
* return value;
* }
*
*/
gridOptions.exporterFieldCallback = gridOptions.exporterFieldCallback ? gridOptions.exporterFieldCallback : defaultExporterFieldCallback;
/**
* @ngdoc function
* @name exporterFieldFormatCallback
* @propertyOf ui.grid.exporter.api:GridOptions
* @description A function to call for each field before exporting it. Allows
* general object to be return to modify the format of a cell in the case of
* excel exports
*
* The method is called once for each field exported, and provides the grid, the
* gridCol and the GridRow for you to use as context in massaging the data.
*
* @param {Grid} grid provides the grid in case you have need of it
* @param {GridRow} row the row from which the data comes
* @param {GridColumn} col the column from which the data comes
* @param {object} value the value for your massaging
* @returns {object} you must return the massaged value ready for exporting
*
* @example
*
* gridOptions.exporterFieldCallback = function ( grid, row, col, value ){
* if ( col.name === 'status' ){
* value = decodeStatus( value );
* }
* return value;
* }
*
*/
gridOptions.exporterFieldFormatCallback = gridOptions.exporterFieldFormatCallback ? gridOptions.exporterFieldFormatCallback : function( grid, row, col, value ) { return null; };
/**
* @ngdoc object
* @name exporterFieldApplyFilters
* @propertyOf ui.grid.exporter.api:GridOptions
* @description Defaults to false, which leads to filters being evaluated on export *
*
* @example
*
* gridOptions.exporterFieldApplyFilters = true;
*
*/
gridOptions.exporterFieldApplyFilters = gridOptions.exporterFieldApplyFilters === true;
/**
* @ngdoc function
* @name exporterAllDataFn
* @propertyOf ui.grid.exporter.api:GridOptions
* @description This promise is needed when exporting all rows,
* and the data need to be provided by server side. Default is null.
* @returns {Promise} a promise to load all data from server
*
* @example
*
* gridOptions.exporterAllDataFn = function () {
* return $http.get('/data/100.json')
* }
*
*/
gridOptions.exporterAllDataFn = gridOptions.exporterAllDataFn ? gridOptions.exporterAllDataFn : null;
/**
* @ngdoc function
* @name exporterAllDataPromise
* @propertyOf ui.grid.exporter.api:GridOptions
* @description DEPRECATED - exporterAllDataFn used to be
* called this, but it wasn't a promise, it was a function that returned
* a promise. Deprecated, but supported for backward compatibility, use
* exporterAllDataFn instead.
* @returns {Promise} a promise to load all data from server
*
* @example
*
* gridOptions.exporterAllDataFn = function () {
* return $http.get('/data/100.json')
* }
*
*/
if ( gridOptions.exporterAllDataFn === null && gridOptions.exporterAllDataPromise ) {
gridOptions.exporterAllDataFn = gridOptions.exporterAllDataPromise;
}
},
/**
* @ngdoc function
* @name addToMenu
* @methodOf ui.grid.exporter.service:uiGridExporterService
* @description Adds export items to the grid menu,
* allowing the user to select export options
* @param {Grid} grid the grid from which data should be exported
*/
addToMenu: function ( grid ) {
grid.api.core.addToGridMenu( grid, [
{
title: i18nService.getSafeText('gridMenu.exporterAllAsCsv'),
action: function ($event) {
grid.api.exporter.csvExport( uiGridExporterConstants.ALL, uiGridExporterConstants.ALL );
},
shown: function() {
return grid.options.exporterMenuCsv && grid.options.exporterMenuAllData;
},
order: grid.options.exporterMenuItemOrder
},
{
title: i18nService.getSafeText('gridMenu.exporterVisibleAsCsv'),
action: function ($event) {
grid.api.exporter.csvExport( uiGridExporterConstants.VISIBLE, uiGridExporterConstants.VISIBLE );
},
shown: function() {
return grid.options.exporterMenuCsv && grid.options.exporterMenuVisibleData;
},
order: grid.options.exporterMenuItemOrder + 1
},
{
title: i18nService.getSafeText('gridMenu.exporterSelectedAsCsv'),
action: function ($event) {
grid.api.exporter.csvExport( uiGridExporterConstants.SELECTED, uiGridExporterConstants.VISIBLE );
},
shown: function() {
return grid.options.exporterMenuCsv && grid.options.exporterMenuSelectedData &&
( grid.api.selection && grid.api.selection.getSelectedRows().length > 0 );
},
order: grid.options.exporterMenuItemOrder + 2
},
{
title: i18nService.getSafeText('gridMenu.exporterAllAsPdf'),
action: function ($event) {
grid.api.exporter.pdfExport( uiGridExporterConstants.ALL, uiGridExporterConstants.ALL );
},
shown: function() {
return grid.options.exporterMenuPdf && grid.options.exporterMenuAllData;
},
order: grid.options.exporterMenuItemOrder + 3
},
{
title: i18nService.getSafeText('gridMenu.exporterVisibleAsPdf'),
action: function ($event) {
grid.api.exporter.pdfExport( uiGridExporterConstants.VISIBLE, uiGridExporterConstants.VISIBLE );
},
shown: function() {
return grid.options.exporterMenuPdf && grid.options.exporterMenuVisibleData;
},
order: grid.options.exporterMenuItemOrder + 4
},
{
title: i18nService.getSafeText('gridMenu.exporterSelectedAsPdf'),
action: function ($event) {
grid.api.exporter.pdfExport( uiGridExporterConstants.SELECTED, uiGridExporterConstants.VISIBLE );
},
shown: function() {
return grid.options.exporterMenuPdf && grid.options.exporterMenuSelectedData &&
( grid.api.selection && grid.api.selection.getSelectedRows().length > 0 );
},
order: grid.options.exporterMenuItemOrder + 5
},
{
title: i18nService.getSafeText('gridMenu.exporterAllAsExcel'),
action: function ($event) {
grid.api.exporter.excelExport( uiGridExporterConstants.ALL, uiGridExporterConstants.ALL );
},
shown: function() {
return grid.options.exporterMenuExcel && grid.options.exporterMenuAllData;
},
order: grid.options.exporterMenuItemOrder + 6
},
{
title: i18nService.getSafeText('gridMenu.exporterVisibleAsExcel'),
action: function ($event) {
grid.api.exporter.excelExport( uiGridExporterConstants.VISIBLE, uiGridExporterConstants.VISIBLE );
},
shown: function() {
return grid.options.exporterMenuExcel && grid.options.exporterMenuVisibleData;
},
order: grid.options.exporterMenuItemOrder + 7
},
{
title: i18nService.getSafeText('gridMenu.exporterSelectedAsExcel'),
action: function ($event) {
grid.api.exporter.excelExport( uiGridExporterConstants.SELECTED, uiGridExporterConstants.VISIBLE );
},
shown: function() {
return grid.options.exporterMenuExcel && grid.options.exporterMenuSelectedData &&
( grid.api.selection && grid.api.selection.getSelectedRows().length > 0 );
},
order: grid.options.exporterMenuItemOrder + 8
}
]);
},
/**
* @ngdoc function
* @name csvExport
* @methodOf ui.grid.exporter.service:uiGridExporterService
* @description Exports rows from the grid in csv format,
* the data exported is selected based on the provided options
* @param {Grid} grid the grid from which data should be exported
* @param {string} rowTypes which rows to export, valid values are
* uiGridExporterConstants.ALL, uiGridExporterConstants.VISIBLE,
* uiGridExporterConstants.SELECTED
* @param {string} colTypes which columns to export, valid values are
* uiGridExporterConstants.ALL, uiGridExporterConstants.VISIBLE,
* uiGridExporterConstants.SELECTED
*/
csvExport: function (grid, rowTypes, colTypes) {
var self = this;
this.loadAllDataIfNeeded(grid, rowTypes, colTypes).then(function() {
var exportColumnHeaders = grid.options.showHeader ? self.getColumnHeaders(grid, colTypes) : [];
var exportData = self.getData(grid, rowTypes, colTypes);
var csvContent = self.formatAsCsv(exportColumnHeaders, exportData, grid.options.exporterCsvColumnSeparator);
self.downloadFile (grid.options.exporterCsvFilename, csvContent, grid.options.exporterCsvColumnSeparator, grid.options.exporterOlderExcelCompatibility, grid.options.exporterIsExcelCompatible);
});
},
/**
* @ngdoc function
* @name loadAllDataIfNeeded
* @methodOf ui.grid.exporter.service:uiGridExporterService
* @description When using server side pagination, use exporterAllDataFn to
* load all data before continuing processing.
* When using client side pagination, return a resolved promise so processing
* continues immediately
* @param {Grid} grid the grid from which data should be exported
* @param {string} rowTypes which rows to export, valid values are
* uiGridExporterConstants.ALL, uiGridExporterConstants.VISIBLE,
* uiGridExporterConstants.SELECTED
* @param {string} colTypes which columns to export, valid values are
* uiGridExporterConstants.ALL, uiGridExporterConstants.VISIBLE,
* uiGridExporterConstants.SELECTED
*/
loadAllDataIfNeeded: function (grid, rowTypes, colTypes) {
if ( rowTypes === uiGridExporterConstants.ALL && grid.rows.length !== grid.options.totalItems && grid.options.exporterAllDataFn) {
return grid.options.exporterAllDataFn()
.then(function(allData) {
grid.modifyRows(allData);
});
} else {
var deferred = $q.defer();
deferred.resolve();
return deferred.promise;
}
},
/**
* @ngdoc property
* @propertyOf ui.grid.exporter.api:ColumnDef
* @name exporterSuppressExport
* @description Suppresses export for this column. Used by selection and expandable.
*/
/**
* @ngdoc function
* @name getColumnHeaders
* @methodOf ui.grid.exporter.service:uiGridExporterService
* @description Gets the column headers from the grid to use
* as a title row for the exported file, all headers have
* headerCellFilters applied as appropriate.
*
* Column headers are an array of objects, each object has
* name, displayName, width and align attributes. Only name is
* used for csv, all attributes are used for pdf.
*
* @param {Grid} grid the grid from which data should be exported
* @param {string} colTypes which columns to export, valid values are
* uiGridExporterConstants.ALL, uiGridExporterConstants.VISIBLE,
* uiGridExporterConstants.SELECTED
*/
getColumnHeaders: function (grid, colTypes) {
var headers = [];
var columns;
if ( colTypes === uiGridExporterConstants.ALL ){
columns = grid.columns;
} else {
var leftColumns = grid.renderContainers.left ? grid.renderContainers.left.visibleColumnCache.filter( function( column ){ return column.visible; } ) : [];
var bodyColumns = grid.renderContainers.body ? grid.renderContainers.body.visibleColumnCache.filter( function( column ){ return column.visible; } ) : [];
var rightColumns = grid.renderContainers.right ? grid.renderContainers.right.visibleColumnCache.filter( function( column ){ return column.visible; } ) : [];
columns = leftColumns.concat(bodyColumns,rightColumns);
}
columns.forEach( function( gridCol ) {
// $$hashKey check since when grouping and sorting pragmatically this ends up in export. Filtering it out
if ( gridCol.colDef.exporterSuppressExport !== true && gridCol.field !== '$$hashKey' &&
grid.options.exporterSuppressColumns.indexOf( gridCol.name ) === -1 ){
var headerEntry = {
name: gridCol.field,
displayName: getDisplayName(grid, gridCol),
width: gridCol.drawnWidth ? gridCol.drawnWidth : gridCol.width,
align: gridCol.colDef.align ? gridCol.colDef.align : (gridCol.colDef.type === 'number' ? 'right' : 'left')
};
headers.push(headerEntry);
}
});
return headers;
},
/**
* @ngdoc property
* @propertyOf ui.grid.exporter.api:ColumnDef
* @name exporterPdfAlign
* @description the alignment you'd like for this specific column when
* exported into a pdf. Can be 'left', 'right', 'center' or any other
* valid pdfMake alignment option.
*/
/**
* @ngdoc object
* @name ui.grid.exporter.api:GridRow
* @description GridRow settings for exporter
*/
/**
* @ngdoc object
* @name exporterEnableExporting
* @propertyOf ui.grid.exporter.api:GridRow
* @description If set to false, then don't export this row, notwithstanding visible or
* other settings
*