← All changes
|
classes/Visualizer/Gutenberg/src/Components/ChartSelect.js
+134
-30
3.10.2
→
3.11.7
View file →
| @@ -22,9 +22,9 @@ | ||
| 22 | 22 | import PanelButton from './PanelButton.js'; |
| 23 | 23 | |
| 24 | 24 | import merge from 'merge'; |
| 25 | 25 | |
| 26 | -import { compact, formatDate, isValidJSON, formatData } from '../utils.js'; | |
| 26 | +import { compact, formatDate, isValidJSON, formatData, googleChartPackages } from '../utils.js'; | |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * WordPress dependencies |
| 30 | 30 | */ |
| @@ -31,9 +31,15 @@ | ||
| 31 | 31 | const { startCase } = lodash; |
| 32 | 32 | |
| 33 | 33 | const { __ } = wp.i18n; |
| 34 | 34 | |
| 35 | +const { apiFetch } = wp; | |
| 36 | + | |
| 35 | 37 | const { |
| 38 | + Button | |
| 39 | +} = wp.components; | |
| 40 | + | |
| 41 | +const { | |
| 36 | 42 | Component, |
| 37 | 43 | Fragment |
| 38 | 44 | } = wp.element; |
| 39 | 45 | |
| @@ -38,8 +44,61 @@ | ||
| 38 | 44 | } = wp.element; |
| 39 | 45 | |
| 40 | 46 | const { InspectorControls } = wp.blockEditor || wp.editor; |
| 41 | 47 | |
| 48 | +let visualizerMedia, visualizerMediaView; | |
| 49 | +visualizerMedia = visualizer.media = {}; | |
| 50 | +visualizerMediaView = visualizerMedia.view = {}; | |
| 51 | + | |
| 52 | +visualizerMediaView.Chart = wp.media.view.MediaFrame.extend( | |
| 53 | + { | |
| 54 | + initialize: function() { | |
| 55 | + const self = this; | |
| 56 | + | |
| 57 | + _.defaults( | |
| 58 | + self.options, { | |
| 59 | + action: '', | |
| 60 | + id: 'visualizer', | |
| 61 | + state: 'iframe:visualizer', | |
| 62 | + title: 'Visualizer' | |
| 63 | + } | |
| 64 | + ); | |
| 65 | + | |
| 66 | + wp.media.view.MediaFrame.prototype.initialize.apply( self, arguments ); | |
| 67 | + | |
| 68 | + wp.media.view.settings.tab = 'Visualizer'; | |
| 69 | + wp.media.view.settings.tabUrl = self.options.action; | |
| 70 | + self.createIframeStates(); | |
| 71 | + }, | |
| 72 | + | |
| 73 | + createIframeStates: function( passedOptions ) { | |
| 74 | + const self = this; | |
| 75 | + wp.media.view.MediaFrame.prototype.createIframeStates.apply( self, arguments ); | |
| 76 | + | |
| 77 | + self.state( self.options.state ).set( | |
| 78 | + _.defaults( | |
| 79 | + { | |
| 80 | + tab: self.options.id, | |
| 81 | + src: self.options.action + '&tab=' + self.options.id, | |
| 82 | + title: self.options.title, | |
| 83 | + content: 'iframe', | |
| 84 | + menu: 'default' | |
| 85 | + }, passedOptions | |
| 86 | + ) | |
| 87 | + ); | |
| 88 | + | |
| 89 | + }, | |
| 90 | + | |
| 91 | + open: function() { | |
| 92 | + try { | |
| 93 | + wp.media.view.MediaFrame.prototype.open.apply( this, arguments ); | |
| 94 | + } catch ( error ) { | |
| 95 | + console.error( error ); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + } | |
| 99 | +); | |
| 100 | + | |
| 42 | 101 | class ChartSelect extends Component { |
| 43 | 102 | constructor() { |
| 44 | 103 | super( ...arguments ); |
| 45 | 104 | |
| @@ -55,11 +114,11 @@ | ||
| 55 | 114 | }; |
| 56 | 115 | } |
| 57 | 116 | |
| 58 | 117 | render() { |
| 118 | + const { legacyBlockEdit, blockEditDoc } = window.visualizerLocalize; | |
| 119 | + let chartVersion = 'undefined' !== typeof google.visualization ? google.visualization.Version : 'current'; | |
| 59 | 120 | |
| 60 | - let chartVersion = 'undefined' !== typeof google ? google.visualization.Version : 'current'; | |
| 61 | - | |
| 62 | 121 | let chart, footer; |
| 63 | 122 | |
| 64 | 123 | let data = formatDate( JSON.parse( JSON.stringify( this.props.chart ) ) ); |
| 65 | 124 | |
| @@ -80,43 +139,84 @@ | ||
| 80 | 139 | if ( data['visualizer-data-exploded']) { |
| 81 | 140 | footer = __( 'Annotations in this chart may not display here but they will display in the front end.' ); |
| 82 | 141 | } |
| 83 | 142 | |
| 143 | + const openEditChart = ( chartId ) => { | |
| 144 | + const baseURL = ( window.visualizerLocalize.chartEditUrl ) ? window.visualizerLocalize.chartEditUrl : ''; | |
| 145 | + let view = new visualizerMediaView.Chart( | |
| 146 | + { | |
| 147 | + action: `${baseURL}?action=visualizer-edit-chart&library=yes&chart=` + chartId | |
| 148 | + } | |
| 149 | + ); | |
| 150 | + const updateChartState = async() => { | |
| 151 | + await this.setState({ | |
| 152 | + isLoading: 'getChart' | |
| 153 | + }); | |
| 154 | + | |
| 155 | + let result = await apiFetch({ path: `wp/v2/visualizer/${chartId}` }); | |
| 156 | + await this.props.editSettings( result['chart_data']['visualizer-settings']); | |
| 157 | + await this.props.getChartData( chartId ); | |
| 158 | + | |
| 159 | + await this.setState({ route: 'home', chart: result['chart_data'], isModified: true, isLoading: true }); | |
| 160 | + }; | |
| 161 | + // eslint-disable-next-line camelcase | |
| 162 | + window.send_to_editor = function() { | |
| 163 | + updateChartState().then( () => { | |
| 164 | + view.close(); | |
| 165 | + }); | |
| 166 | + }; | |
| 167 | + | |
| 168 | + view.open(); | |
| 169 | + }; | |
| 170 | + | |
| 84 | 171 | return ( |
| 85 | 172 | <Fragment> |
| 86 | 173 | { 'home' === this.state.route && |
| 87 | 174 | <InspectorControls> |
| 175 | + { ! legacyBlockEdit && ( | |
| 176 | + <div className="viz-edit-chart-new"> | |
| 177 | + <Button | |
| 178 | + isPrimary={true} | |
| 179 | + onClick={ () => { | |
| 180 | + openEditChart( this.props.id ); | |
| 181 | +} }> Edit Chart </ Button> | |
| 182 | + <p dangerouslySetInnerHTML={{__html: blockEditDoc}}></p> | |
| 183 | + </div> | |
| 184 | + ) } | |
| 88 | 185 | |
| 186 | + { legacyBlockEdit && ( | |
| 187 | + <> | |
| 188 | + | |
| 189 | + <ManualData chart={ this.props.chart } editChartData={ this.props.editChartData } /> | |
| 190 | + | |
| 89 | 191 | <FileImport |
| 90 | 192 | chart={ this.props.chart } |
| 91 | 193 | readUploadedFile={ this.props.readUploadedFile } |
| 92 | 194 | /> |
| 93 | 195 | |
| 94 | - <RemoteImport | |
| 95 | - id={ this.props.id } | |
| 96 | - chart={ this.props.chart } | |
| 97 | - editURL={ this.props.editURL } | |
| 98 | - isLoading={ this.props.isLoading } | |
| 99 | - uploadData={ this.props.uploadData } | |
| 100 | - editSchedule={ this.props.editSchedule } | |
| 101 | - editJSONSchedule={ this.props.editJSONSchedule } | |
| 102 | - editJSONURL={ this.props.editJSONURL } | |
| 103 | - editJSONHeaders={ this.props.editJSONHeaders } | |
| 104 | - editJSONRoot={ this.props.editJSONRoot } | |
| 105 | - editJSONPaging={ this.props.editJSONPaging } | |
| 106 | - JSONImportData={ this.props.JSONImportData } | |
| 107 | - /> | |
| 196 | + <RemoteImport | |
| 197 | + id={ this.props.id } | |
| 198 | + chart={ this.props.chart } | |
| 199 | + editURL={ this.props.editURL } | |
| 200 | + isLoading={ this.props.isLoading } | |
| 201 | + uploadData={ this.props.uploadData } | |
| 202 | + editSchedule={ this.props.editSchedule } | |
| 203 | + editJSONSchedule={ this.props.editJSONSchedule } | |
| 204 | + editJSONURL={ this.props.editJSONURL } | |
| 205 | + editJSONHeaders={ this.props.editJSONHeaders } | |
| 206 | + editJSONRoot={ this.props.editJSONRoot } | |
| 207 | + editJSONPaging={ this.props.editJSONPaging } | |
| 208 | + JSONImportData={ this.props.JSONImportData } | |
| 209 | + /> | |
| 108 | 210 | |
| 109 | - <ChartImport getChartData={ this.props.getChartData } isLoading={ this.props.isLoading } /> | |
| 211 | + <ChartImport getChartData={ this.props.getChartData } isLoading={ this.props.isLoading } /> | |
| 110 | 212 | |
| 111 | - <DataImport | |
| 112 | - chart={ this.props.chart } | |
| 113 | - editSchedule={ this.props.editDatabaseSchedule } | |
| 114 | - databaseImportData={ this.props.databaseImportData } | |
| 115 | - /> | |
| 213 | + <DataImport | |
| 214 | + chart={ this.props.chart } | |
| 215 | + editSchedule={ this.props.editDatabaseSchedule } | |
| 216 | + databaseImportData={ this.props.databaseImportData } | |
| 217 | + /> | |
| 116 | 218 | |
| 117 | - <ManualData chart={ this.props.chart } editChartData={ this.props.editChartData } /> | |
| 118 | - | |
| 119 | 219 | <PanelButton |
| 120 | 220 | label={ __( 'Advanced Options' ) } |
| 121 | 221 | className="visualizer-advanced-options" |
| 122 | 222 | icon="admin-tools" |
| @@ -122,13 +222,15 @@ | ||
| 122 | 222 | icon="admin-tools" |
| 123 | 223 | onClick={ () => this.setState({ route: 'showAdvanced' }) } |
| 124 | 224 | /> |
| 125 | 225 | |
| 126 | - <PanelButton | |
| 127 | - label={ __( 'Chart Permissions' ) } | |
| 128 | - icon="admin-users" | |
| 129 | - onClick={ () => this.setState({ route: 'showPermissions' }) } | |
| 130 | - /> | |
| 226 | + <PanelButton | |
| 227 | + label={ __( 'Chart Permissions' ) } | |
| 228 | + icon="admin-users" | |
| 229 | + onClick={ () => this.setState({ route: 'showPermissions' }) } | |
| 230 | + /> | |
| 231 | + </> | |
| 232 | + ) } | |
| 131 | 233 | </InspectorControls> |
| 132 | 234 | } |
| 133 | 235 | |
| 134 | 236 | { ( 'showAdvanced' === this.state.route || 'showPermissions' === this.state.route ) && |
| @@ -172,8 +274,9 @@ | ||
| 172 | 274 | compact( this.props.chart['visualizer-settings']) |
| 173 | 275 | } |
| 174 | 276 | height="500px" |
| 175 | 277 | formatters={ formatData( data ) } |
| 278 | + chartPackages={ googleChartPackages } | |
| 176 | 279 | /> |
| 177 | 280 | ) : ( |
| 178 | 281 | <Chart |
| 179 | 282 | chartVersion={ chartVersion } |
| @@ -186,8 +289,9 @@ | ||
| 186 | 289 | compact( this.props.chart['visualizer-settings']) |
| 187 | 290 | } |
| 188 | 291 | height="500px" |
| 189 | 292 | formatters={ formatData( data ) } |
| 293 | + chartPackages={ googleChartPackages } | |
| 190 | 294 | /> |
| 191 | 295 | ) |
| 192 | 296 | ) } |
| 193 | 297 | |