import { Datatable } from 'src/plugins/expressions/public' ; import { CoreStart } from '../../../../../core/public' ; import { FormatFactory } from '../../../../field_formats/common' ; import { DataPublicPluginStart , exporters } from '../../services/data' ; import { downloadMultipleAs } from '../../services/share' ; import { Adapters , IEmbeddable } from '../../services/embeddable' ; import { Action } from '../../services/ui_actions' ; import { dashboardExportCsvAction } from '../../dashboard_strings' ; export const ACTION_EXPORT_CSV = 'ACTION_EXPORT_CSV' ; export interface Params { core : CoreStart ; data : DataPublicPluginStart ; } export interface ExportContext { embeddable ? : IEmbeddable ; asString ? : boolean ; } /**
 * This is "Export CSV" action which appears in the context
 * menu of a dashboard panel.
 */ export class ExportCSVAction implements Action < ExportContext > { public readonly id = ACTION_EXPORT_CSV ; public readonly type = ACTION_EXPORT_CSV ; public readonly order = 5 ; constructor ( protected readonly params ) { } public getIconType ( ) { return 'exportAction' ; } public readonly getDisplayName = ( context ) : string => dashboardExportCsvAction . getDisplayName ( ) ; public async isCompatible ( context ) { return ! ! this . hasDatatableContent ( context . embeddable ?. getInspectorAdapters ?. ( ) ) ; } private hasDatatableContent = ( adapters ) => { return Object . keys ( adapters ?. tables || { } ) . length > 0 && adapters ! . tables . allowCsvExport ; } ; private getFormatter = ( ) : FormatFactory | undefined => { if ( this . params . data ) { return this . params . data . fieldFormats . deserialize ; } } ; private getDataTableContent = ( adapters ) => { if ( this . hasDatatableContent ( adapters ) ) { return adapters ?. tables . tables ; } return ; } ; private exportCSV = async ( context ) => { const formatFactory = this . getFormatter ( ) ; if ( ! formatFactory ) { return ; } const tableAdapters = this . getDataTableContent ( context ?. embeddable ?. getInspectorAdapters ( ) ) as Record < string , Datatable > ; if ( tableAdapters ) { const datatables = Object . values ( tableAdapters ) ; const content = datatables . reduce < Record < string , { content : string ; type : string ; } > > ( ( memo , datatable , i ) => { if ( datatable ) { const postFix = datatables . length > 1 ? `-${ i + 1 }` : '' ; const untitledFilename = dashboardExportCsvAction . getUntitledFilename ( ) ; memo [ `${ context ! . embeddable ! . getTitle ( ) || untitledFilename }${ postFix }.csv` ] = { content : exporters . datatableToCSV ( datatable , { csvSeparator : this . params . core . uiSettings . get ( 'csv:separator' , ',' ) , quoteValues : this . params . core . uiSettings . get ( 'csv:quoteValues' , true ) , formatFactory , escapeFormulaValues : false , } ) , type : exporters . CSV_MIME_TYPE , } ; } return memo ; } , { } ) ; if ( context . asString ) { return content as unknown as Promise < void > ; } if ( content ) { return downloadMultipleAs ( content ) ; } } } ; public async execute ( context ) { return await this . exportCSV ( context ) ; } }