Skip to content

Commit 348b368

Browse files
editing boolean results in an error and false values vanish
#733 (comment)
1 parent cfe4557 commit 348b368

File tree

3 files changed

+71
-49
lines changed

3 files changed

+71
-49
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,8 @@ public function update_chart_data( $data ) {
659659

660660
/**
661661
* Format chart data.
662+
*
663+
* Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure.
662664
*/
663665
public function format_chart_data( $data, $series ) {
664666
foreach ( $series as $i => $row ) {
@@ -671,36 +673,35 @@ public function format_chart_data( $data, $series ) {
671673
continue;
672674
}
673675

674-
if ( $row['type'] === 'number' ) {
675-
foreach ( $data as $o => $col ) {
676-
$data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
677-
}
678-
}
679-
680-
if ( $row['type'] === 'boolean' ) {
681-
foreach ( $data as $o => $col ) {
682-
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
683-
}
684-
}
685-
686-
if ( $row['type'] === 'timeofday' ) {
687-
foreach ( $data as $o => $col ) {
688-
$date = new DateTime( '1984-03-16T' . $col[ $i ] );
689-
if ( $date ) {
690-
$data[ $o ][ $i ] = array(
691-
intval( $date->format( 'H' ) ),
692-
intval( $date->format( 'i' ) ),
693-
intval( $date->format( 's' ) ),
694-
0,
695-
);
676+
switch ( $row['type'] ) {
677+
case 'number':
678+
foreach ( $data as $o => $col ) {
679+
$data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
696680
}
697-
}
698-
}
699-
700-
if ( $row['type'] === 'string' ) {
701-
foreach ( $data as $o => $col ) {
702-
$data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
703-
}
681+
break;
682+
case 'boolean':
683+
foreach ( $data as $o => $col ) {
684+
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false;
685+
}
686+
break;
687+
case 'timeofday':
688+
foreach ( $data as $o => $col ) {
689+
$date = new DateTime( '1984-03-16T' . $col[ $i ] );
690+
if ( $date ) {
691+
$data[ $o ][ $i ] = array(
692+
intval( $date->format( 'H' ) ),
693+
intval( $date->format( 'i' ) ),
694+
intval( $date->format( 's' ) ),
695+
0,
696+
);
697+
}
698+
}
699+
break;
700+
case 'string':
701+
foreach ( $data as $o => $col ) {
702+
$data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
703+
}
704+
break;
704705
}
705706
}
706707

classes/Visualizer/Gutenberg/build/block.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/Visualizer/Gutenberg/src/Editor.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -339,20 +339,39 @@ class Editor extends Component {
339339
let map = series;
340340
let fieldName = 'series';
341341

342-
if ( 'pie' === type ) {
343-
map = chartData;
344-
fieldName = 'slices';
345-
346-
// pie charts are finicky about a number being a number
347-
// and editing a number makes it a string
348-
// so let's convert it back into a number.
349-
chartData.map( ( i, index ) => {
350-
switch ( series[1].type ) {
351-
case 'number':
352-
i[1] = parseFloat( i[1]);
353-
break;
354-
}
355-
});
342+
switch ( type ) {
343+
case 'pie':
344+
map = chartData;
345+
fieldName = 'slices';
346+
347+
// pie charts are finicky about a number being a number
348+
// and editing a number makes it a string
349+
// so let's convert it back into a number.
350+
chartData.map( ( i, index ) => {
351+
switch ( series[1].type ) {
352+
case 'number':
353+
i[1] = parseFloat( i[1]);
354+
break;
355+
}
356+
});
357+
break;
358+
case 'tabular':
359+
360+
// table charts are finicky about a boolean being a boolean
361+
// and editing a boolean makes it a string
362+
// so let's convert it back into a boolean.
363+
chartData.map( ( i, index ) => {
364+
series.map( ( seriesObject, seriesIndex ) => {
365+
switch ( seriesObject.type ) {
366+
case 'boolean':
367+
if ( 'string' === typeof i[seriesIndex]) {
368+
i[seriesIndex] = 'true' === i[seriesIndex];
369+
}
370+
break;
371+
}
372+
});
373+
});
374+
break;
356375
}
357376

358377
map.map( ( i, index ) => {
@@ -362,16 +381,18 @@ class Editor extends Component {
362381

363382
const seriesIndex = 'pie' !== type ? index - 1 : index;
364383

365-
if ( settings[fieldName][seriesIndex] === undefined ) {
384+
if ( Array.isArray( settings[fieldName]) && settings[fieldName][seriesIndex] === undefined ) {
366385
settings[fieldName][seriesIndex] = {};
367386
settings[fieldName][seriesIndex].temp = 1;
368387
}
369388
});
370389

371-
settings[fieldName] = settings[fieldName].filter( ( i, index ) => {
372-
const length = -1 >= [ 'pie', 'tabular', 'dataTable' ].indexOf( type ) ? map.length - 1 : map.length;
373-
return index < length;
374-
});
390+
if ( Array.isArray( settings[fieldName]) ) {
391+
settings[fieldName] = settings[fieldName].filter( ( i, index ) => {
392+
const length = -1 >= [ 'pie', 'tabular', 'dataTable' ].indexOf( type ) ? map.length - 1 : map.length;
393+
return index < length;
394+
});
395+
}
375396

376397
chart['visualizer-source'] = source;
377398
chart['visualizer-default-data'] = 0;

0 commit comments

Comments
 (0)