| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
import { Chart } from 'react-google-charts'; |
| 5 |
|
| 6 |
import DataTable from './DataTable.js'; |
| 7 |
|
| 8 |
import FileImport from './Import/FileImport.js'; |
| 9 |
|
| 10 |
import RemoteImport from './Import/RemoteImport.js'; |
| 11 |
|
| 12 |
import ChartImport from './Import/ChartImport.js'; |
| 13 |
|
| 14 |
import ManualData from './Import/ManualData.js'; |
| 15 |
|
| 16 |
import Sidebar from './Sidebar.js'; |
| 17 |
|
| 18 |
import ChartPermissions from './ChartPermissions.js'; |
| 19 |
|
| 20 |
import PanelButton from './PanelButton.js'; |
| 21 |
|
| 22 |
import merge from 'merge'; |
| 23 |
|
| 24 |
import { compact, formatDate, isValidJSON } from '../utils.js'; |
| 25 |
|
| 26 |
/** |
| 27 |
* WordPress dependencies |
| 28 |
*/ |
| 29 |
const { startCase } = lodash; |
| 30 |
|
| 31 |
const { __ } = wp.i18n; |
| 32 |
|
| 33 |
const { |
| 34 |
Component, |
| 35 |
Fragment |
| 36 |
} = wp.element; |
| 37 |
|
| 38 |
const { InspectorControls } = wp.editor; |
| 39 |
|
| 40 |
class ChartSelect extends Component { |
| 41 |
constructor() { |
| 42 |
super( ...arguments ); |
| 43 |
|
| 44 |
this.state = { |
| 45 |
|
| 46 |
/** |
| 47 |
* Sidebar Route Status |
| 48 |
* |
| 49 |
* home - Initial screen. |
| 50 |
* showAdvanced - Show Advanced Options. |
| 51 |
*/ |
| 52 |
route: 'home' |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
render() { |
| 57 |
|
| 58 |
let chart; |
| 59 |
|
| 60 |
let data = formatDate( JSON.parse( JSON.stringify( this.props.chart ) ) ); |
| 61 |
|
| 62 |
if ( 0 <= [ 'gauge', 'table', 'timeline', 'dataTable' ].indexOf( this.props.chart['visualizer-chart-type']) ) { |
| 63 |
if ( 'dataTable' === data['visualizer-chart-type']) { |
| 64 |
chart = data['visualizer-chart-type']; |
| 65 |
} else { |
| 66 |
chart = startCase( this.props.chart['visualizer-chart-type']); |
| 67 |
} |
| 68 |
} else { |
| 69 |
chart = `${ startCase( this.props.chart['visualizer-chart-type']) }Chart`; |
| 70 |
} |
| 71 |
|
| 72 |
return ( |
| 73 |
<Fragment> |
| 74 |
{ 'home' === this.state.route && |
| 75 |
<InspectorControls> |
| 76 |
|
| 77 |
<FileImport |
| 78 |
chart={ this.props.chart } |
| 79 |
readUploadedFile={ this.props.readUploadedFile } |
| 80 |
/> |
| 81 |
|
| 82 |
<RemoteImport |
| 83 |
chart={ this.props.chart } |
| 84 |
editURL={ this.props.editURL } |
| 85 |
isLoading={ this.props.isLoading } |
| 86 |
uploadData={ this.props.uploadData } |
| 87 |
editSchedule={ this.props.editSchedule } |
| 88 |
/> |
| 89 |
|
| 90 |
<ChartImport getChartData={ this.props.getChartData } isLoading={ this.props.isLoading } /> |
| 91 |
|
| 92 |
<ManualData chart={ this.props.chart } editChartData={ this.props.editChartData } /> |
| 93 |
|
| 94 |
<PanelButton |
| 95 |
label={ __( 'Advanced Options' ) } |
| 96 |
icon="admin-tools" |
| 97 |
onClick={ () => this.setState({ route: 'showAdvanced' }) } |
| 98 |
/> |
| 99 |
|
| 100 |
<PanelButton |
| 101 |
label={ __( 'Chart Permissions' ) } |
| 102 |
icon="admin-users" |
| 103 |
onClick={ () => this.setState({ route: 'showPermissions' }) } |
| 104 |
/> |
| 105 |
</InspectorControls> |
| 106 |
} |
| 107 |
|
| 108 |
{ ( 'showAdvanced' === this.state.route || 'showPermissions' === this.state.route ) && |
| 109 |
<InspectorControls> |
| 110 |
<PanelButton |
| 111 |
label={ __( 'Chart Settings' ) } |
| 112 |
onClick={ () => this.setState({ route: 'home' }) } |
| 113 |
isBack={ true } |
| 114 |
/> |
| 115 |
|
| 116 |
{ 'showAdvanced' === this.state.route && |
| 117 |
<Sidebar chart={ this.props.chart } edit={ this.props.editSettings } /> |
| 118 |
} |
| 119 |
|
| 120 |
{ 'showPermissions' === this.state.route && |
| 121 |
<ChartPermissions chart={ this.props.chart } edit={ this.props.editPermissions } /> |
| 122 |
} |
| 123 |
</InspectorControls> |
| 124 |
} |
| 125 |
|
| 126 |
<div className="visualizer-settings__chart"> |
| 127 |
|
| 128 |
{ ( null !== this.props.chart ) && |
| 129 |
|
| 130 |
( 'dataTable' === chart ) ? ( |
| 131 |
<DataTable |
| 132 |
id={ this.props.id } |
| 133 |
rows={ data['visualizer-data'] } |
| 134 |
columns={ data['visualizer-series'] } |
| 135 |
options={ data['visualizer-settings'] } |
| 136 |
/> |
| 137 |
) : ( |
| 138 |
<Chart |
| 139 |
chartType={ chart } |
| 140 |
rows={ data['visualizer-data'] } |
| 141 |
columns={ data['visualizer-series'] } |
| 142 |
options={ |
| 143 |
isValidJSON( this.props.chart['visualizer-settings'].manual ) ? |
| 144 |
merge( compact( this.props.chart['visualizer-settings']), JSON.parse( this.props.chart['visualizer-settings'].manual ) ) : |
| 145 |
compact( this.props.chart['visualizer-settings']) |
| 146 |
} |
| 147 |
height="500px" |
| 148 |
/> |
| 149 |
) |
| 150 |
} |
| 151 |
|
| 152 |
</div> |
| 153 |
</Fragment> |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
export default ChartSelect; |
| 159 |
|