Skip to content

Commit 0f6d49f

Browse files
Merge pull request #685 from contactashish13/issue-677
add support for boolean data type in table charts
2 parents af20a70 + 453b1b2 commit 0f6d49f

File tree

7 files changed

+102
-30
lines changed

7 files changed

+102
-30
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ public function format_chart_data( $data, $series ) {
631631

632632
if ( $row['type'] === 'boolean' ) {
633633
foreach ( $data as $o => $col ) {
634-
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
634+
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
635635
}
636636
}
637637

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/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"react": "16.4.1",
1919
"react-google-charts": "^3.0.8",
2020
"react-json-editor-ajrm": "^2.5.8",
21+
"sprintf-js": "^1.1.2",
2122
"uuid": "^3.3.3"
2223
},
2324
"devDependencies": {

classes/Visualizer/Gutenberg/src/Components/DataTable.js

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,38 +112,51 @@ class DataTables extends Component {
112112
return data;
113113
}
114114

115-
if ( 'date' === type || 'datetime' === type || 'timeofday' === type ) {
116-
if ( settings.series[index].format && settings.series[index].format.from && settings.series[index].format.to ) {
117-
return jQuery.fn.dataTable.render.moment( settings.series[index].format.from, settings.series[index].format.to );
118-
}
119-
120-
return jQuery.fn.dataTable.render.moment( 'MM-DD-YYYY' );
121-
}
122-
123-
if ( 'num' === type ) {
124-
const parts = [ '', '', '', '', '' ];
125-
126-
if ( settings.series[index].format.thousands ) {
127-
parts[0] = settings.series[index].format.thousands;
128-
}
115+
switch ( type ) {
116+
case 'date':
117+
case 'datetime':
118+
case 'timeofday':
119+
if ( settings.series[index].format && settings.series[index].format.from && settings.series[index].format.to ) {
120+
return jQuery.fn.dataTable.render.moment( settings.series[index].format.from, settings.series[index].format.to );
121+
}
122+
return jQuery.fn.dataTable.render.moment( 'MM-DD-YYYY' );
123+
break;
124+
case 'num':
125+
const parts = [ '', '', '', '', '' ];
129126

130-
if ( settings.series[index].format.decimal ) {
131-
parts[1] = settings.series[index].format.decimal;
132-
}
127+
if ( settings.series[index].format.thousands ) {
128+
parts[0] = settings.series[index].format.thousands;
129+
}
133130

134-
if ( settings.series[index].format.precision ) {
135-
parts[2] = settings.series[index].format.precision;
136-
}
131+
if ( settings.series[index].format.decimal ) {
132+
parts[1] = settings.series[index].format.decimal;
133+
}
137134

138-
if ( settings.series[index].format.prefix ) {
139-
parts[3] = settings.series[index].format.prefix;
140-
}
135+
if ( settings.series[index].format.precision ) {
136+
parts[2] = settings.series[index].format.precision;
137+
}
141138

142-
if ( settings.series[index].format.suffix ) {
143-
parts[4] = settings.series[index].format.suffix;
144-
}
139+
if ( settings.series[index].format.prefix ) {
140+
parts[3] = settings.series[index].format.prefix;
141+
}
145142

146-
return jQuery.fn.dataTable.render.number( ...parts );
143+
if ( settings.series[index].format.suffix ) {
144+
parts[4] = settings.series[index].format.suffix;
145+
}
146+
return jQuery.fn.dataTable.render.number( ...parts );
147+
break;
148+
case 'boolean':
149+
jQuery.fn.dataTable.render.extra = function( data, type, row ) {
150+
if ( ( true === data || 'true' === data ) && 'undefined' !== typeof settings.series[index].format && '' !== settings.series[index].format.truthy ) {
151+
return settings.series[index].format.truthy.replace( /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '' );
152+
}
153+
if ( ( false === data || 'false' === data ) && 'undefined' !== typeof settings.series[index].format && '' !== settings.series[index].format.falsy ) {
154+
return settings.series[index].format.falsy.replace( /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '' );
155+
}
156+
return data;
157+
};
158+
return jQuery.fn.dataTable.render.extra;
159+
break;
147160
}
148161

149162
return data;

classes/Visualizer/Gutenberg/src/Components/Sidebar/ColumnSettings.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class ColumnSettings extends Component {
4545

4646
const series = this.props.chart['visualizer-series'];
4747

48+
const type = this.props.chart['visualizer-chart-type'];
49+
4850
return (
4951
<PanelBody
5052
title={ __( 'Column Settings' ) }
@@ -168,6 +170,36 @@ class ColumnSettings extends Component {
168170
</Fragment>
169171
) }
170172

173+
{ ( 'boolean' === series[i].type ) && ( 'dataTable' === type ) && (
174+
<Fragment>
175+
<TextControl
176+
label={ __( 'Truthy value' ) }
177+
help= { __( 'Provide the HTML entity code for the value the table should display when the value of the column is true. e.g. tick mark (Code: &#10004;) instead of true' ) }
178+
value={ settings.series[i].format ? settings.series[i].format.truthy : '' }
179+
onChange={ e => {
180+
if ( ! settings.series[i].truthy ) {
181+
settings.series[i].truthy = {};
182+
}
183+
settings.series[i].format.truthy = e;
184+
this.props.edit( settings );
185+
} }
186+
/>
187+
<TextControl
188+
label={ __( 'Falsy value' ) }
189+
help= { __( 'Provide the HTML entity code for the value the table should display when the value of the column is false. e.g. cross mark (Code: &#10006;) instead of false' ) }
190+
value={ settings.series[i].format ? settings.series[i].format.falsy : '' }
191+
onChange={ e => {
192+
if ( ! settings.series[i].falsy ) {
193+
settings.series[i].falsy = {};
194+
}
195+
settings.series[i].format.falsy = e;
196+
this.props.edit( settings );
197+
} }
198+
/>
199+
200+
</Fragment>
201+
) }
202+
171203
</PanelBody>
172204
);
173205
}

classes/Visualizer/Render/Sidebar/Type/DataTable/DataTable.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,22 @@ protected function _renderFormatField( $index = 0 ) {
477477
'YYYY-MM-DD'
478478
);
479479
break;
480+
case 'boolean':
481+
self::_renderTextItem(
482+
esc_html__( 'Truthy value', 'visualizer' ),
483+
'series[' . $index . '][format][truthy]',
484+
isset( $this->series[ $index ]['format']['truthy'] ) ? $this->series[ $index ]['format']['truthy'] : '',
485+
sprintf( esc_html__( 'Provide the HTML entity code for the value the table should display when the value of the column is true. e.g. %1$s (Code: %2$s) instead of true', 'visualizer' ), '&#10004;', '&amp;#10004;' ),
486+
''
487+
);
488+
self::_renderTextItem(
489+
esc_html__( 'Falsy value', 'visualizer' ),
490+
'series[' . $index . '][format][falsy]',
491+
isset( $this->series[ $index ]['format']['falsy'] ) ? $this->series[ $index ]['format']['falsy'] : '',
492+
sprintf( esc_html__( 'Provide the HTML entity code for the value the table should display when the value of the column is false. e.g. %1$s (Code: %2$s) instead of false', 'visualizer' ), '&#10006;', '&amp;#10006;' ),
493+
''
494+
);
495+
break;
480496
}
481497
}
482498

js/render-datatables.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@
185185
type = 'date';
186186
break;
187187
}
188-
189188
render = addRenderer(series[j].type, settings.series, j);
190189

191190
var col = { title: series[j].label, data: series[j].label, type: type, render: render };
@@ -267,8 +266,19 @@
267266
render = $.fn.dataTable.render.moment(series.format.from, series.format.to);
268267
}
269268
break;
269+
default:
270+
render = $.fn.dataTable.render.extra = function ( data, type, row ) {
271+
if((data === true || data === 'true') && typeof series.format !== 'undefined' && series.format.truthy !== ''){
272+
data = series.format.truthy.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
273+
}
274+
if((data === false || data === 'false') && typeof series.format !== 'undefined' && series.format.falsy !== ''){
275+
data = series.format.falsy.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
276+
}
277+
return data;
278+
}
270279
}
271280
/* jshint ignore:end */
281+
272282
return render;
273283
}
274284

0 commit comments

Comments
 (0)