import * as React from 'react'; import FadeInteraction from '../fadeInteraction/FadeInteraction'; import { observer } from 'mobx-react'; import { action, computed, makeObservable, observable } from 'mobx'; import autobind from 'autobind-decorator'; import fileDownload from 'react-file-download'; import DefaultTooltip from '../defaultTooltip/DefaultTooltip'; import classnames from 'classnames'; import styles from './DownloadControls.module.scss'; import { saveSvg, saveSvgAsPng } from 'save-svg-as-png'; import svgToPdfDownload from '../../lib/svgToPdfDownload'; import { CSSProperties } from 'react'; import { isPromiseLike } from 'cbioportal-utils'; import { AppContext } from '../appContext/AppContext'; type ButtonSpec = { key: string; content: JSX.Element; onClick: () => void; disabled?: boolean; }; export type DownloadControlsButton = | 'PDF' | 'PNG' | 'SVG' | 'Data' | 'Summary Data' | 'Full Data'; export type DataType = 'summary' | 'full'; interface IDownloadControlsProps { getSvg?: () => SVGElement | null | PromiseLike; getData?: ( dataType?: DataType ) => string | null | PromiseLike; filename: string; buttons?: DownloadControlsButton[]; additionalLeftButtons?: ButtonSpec[]; additionalRightButtons?: ButtonSpec[]; dontFade?: boolean; type?: 'button' | 'buttonGroup' | 'dropdown'; style?: CSSProperties; className?: any; dataExtension?: string; } function makeButton(spec: ButtonSpec) { return ( ); } function makeMenuItem(spec: ButtonSpec) { return (
{} : spec.onClick} className={classnames( { [styles.menuItemEnabled]: !spec.disabled, [styles.menuItemDisabled]: !!spec.disabled, }, styles.menuItem )} > {spec.key}
); } function makeDropdownItem(spec: ButtonSpec) { return (
  • {} : spec.onClick} > {spec.key}
  • ); } @observer export default class DownloadControls extends React.Component< IDownloadControlsProps, {} > { constructor(props: any) { super(props); makeObservable(this); } @observable private collapsed = true; @autobind private download( saveMethod: (svg: SVGElement, fileName: string) => void, fileExtension: string ) { if (this.props.getSvg) { const result = this.props.getSvg(); if (result) { if (isPromiseLike(result)) { result.then(svg => { if (svg) { saveMethod( svg, `${this.props.filename}.${fileExtension}` ); } }); } else { saveMethod( result, `${this.props.filename}.${fileExtension}` ); } } } } @autobind private downloadSvg() { this.download(saveSvg, 'svg'); } @autobind private downloadPng() { this.download( (svg, fileName) => saveSvgAsPng(svg, fileName, { backgroundColor: '#ffffff' }), 'png' ); } @autobind private downloadPdf() { this.download( (svg, fileName) => svgToPdfDownload(fileName, svg), 'pdf' ); } @autobind private downloadData(dataType?: DataType) { if (this.props.getData) { const result = this.props.getData(dataType); if (result !== null) { if (isPromiseLike(result)) { result.then(data => { if (data) { fileDownload( data, `${this.props.filename}.` + `${ this.props.dataExtension ? this.props.dataExtension : 'txt' }` ); } }); } else { fileDownload( result, `${this.props.filename}.` + `${ this.props.dataExtension ? this.props.dataExtension : 'txt' }` ); } } } } @computed get downloadControlsButtons(): { [button in DownloadControlsButton]: ButtonSpec; } { return { SVG: { key: 'SVG', content: ( SVG{' '}