Skip to content

Commit d07f4a7

Browse files
instance settings in gutenberg block
1 parent d5af708 commit d07f4a7

File tree

7 files changed

+110
-15
lines changed

7 files changed

+110
-15
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ public function register_block_type() {
139139
'id' => array(
140140
'type' => 'number',
141141
),
142+
'lazy' => array(
143+
'type' => 'string',
144+
),
142145
),
143146
)
144147
);
@@ -147,14 +150,39 @@ public function register_block_type() {
147150
/**
148151
* Gutenberg Block Callback Function
149152
*/
150-
public function gutenberg_block_callback( $attr ) {
151-
if ( isset( $attr['id'] ) ) {
152-
$id = $attr['id'];
153-
if ( empty( $id ) || $id === 'none' ) {
154-
return ''; // no id = no fun
155-
}
156-
return '[visualizer id="' . $id . '"]';
153+
public function gutenberg_block_callback( $atts ) {
154+
$atts = shortcode_atts(
155+
array(
156+
'id' => false,
157+
'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ),
158+
// we are deliberating excluding the class attribute from here
159+
// as this will be handled by the custom class in Gutenberg
160+
),
161+
$atts
162+
);
163+
164+
// no id, no fun.
165+
if ( ! $atts['id'] ) {
166+
return '';
167+
}
168+
169+
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
170+
if ( $atts['lazy'] == -1 || $atts['lazy'] == false ) {
171+
$atts['lazy'] = 'no';
172+
}
173+
174+
// we don't want the chart in the editor lazy-loading.
175+
if ( is_admin() ) {
176+
unset( $atts['lazy'] );
157177
}
178+
179+
$shortcode = '[visualizer';
180+
foreach ( $atts as $name => $value ) {
181+
$shortcode .= sprintf( ' %s="%s"', $name, $value );
182+
}
183+
$shortcode .= ']';
184+
185+
return $shortcode;
158186
}
159187

160188
/**

classes/Visualizer/Gutenberg/build/block.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class ChartSelect extends Component {
133133
/>
134134

135135
{ 'showAdvanced' === this.state.route &&
136-
<Sidebar chart={ this.props.chart } edit={ this.props.editSettings } />
136+
<Sidebar chart={ this.props.chart } attributes={ this.props.attributes } edit={ this.props.editSettings } />
137137
}
138138

139139
{ 'showPermissions' === this.state.route &&

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* External dependencies
33
*/
44
import GeneralSettings from './Sidebar/GeneralSettings.js';
5+
import InstanceSettings from './Sidebar/InstanceSettings.js';
56
import HorizontalAxisSettings from './Sidebar/HorizontalAxisSettings.js';
67
import VerticalAxisSettings from './Sidebar/VerticalAxisSettings.js';
78
import PieSettings from './Sidebar/PieSettings.js';
@@ -45,6 +46,8 @@ class Sidebar extends Component {
4546
return (
4647
<Fragment>
4748

49+
<InstanceSettings chart={ this.props.chart } attributes={ this.props.attributes } edit={ this.props.edit } />
50+
4851
<GeneralSettings chart={ this.props.chart } edit={ this.props.edit } />
4952

5053
{ ( -1 >= [ 'table', 'gauge', 'geo', 'pie', 'timeline', 'dataTable' ].indexOf( type ) ) && (
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
const { __ } = wp.i18n;
5+
6+
const { Component } = wp.element;
7+
8+
const {
9+
PanelBody,
10+
Notice,
11+
TextControl
12+
} = wp.components;
13+
14+
class InstanceSettings extends Component {
15+
constructor() {
16+
super( ...arguments );
17+
}
18+
19+
render() {
20+
21+
const settings = this.props.chart['visualizer-settings'];
22+
23+
return (
24+
<PanelBody
25+
title={ __( 'Instance Settings' ) }
26+
initialOpen={ true }
27+
className="visualizer-instance-panel"
28+
>
29+
30+
<Notice status="info" isDismissible={ false } >
31+
<p>
32+
{ __( 'These settings are valid only for this instance of the chart.' ) }
33+
</p>
34+
<p>
35+
{ __( 'This means that if you insert this chart again elsewhere, these values will not persist.' ) }
36+
</p>
37+
</Notice>
38+
39+
<TextControl
40+
label={ __( 'Should this instance lazy Load?' ) }
41+
help={ __( '-1: do not lazy load. Any number greater than -1 will lazy load the chart once the viewport is that many pixels away from the chart' ) }
42+
value={ this.props.attributes.lazy ? Number( this.props.attributes.lazy ) : -1 }
43+
type="number"
44+
min="-1"
45+
max="1000"
46+
step="1"
47+
onChange={ e => {
48+
this.props.attributes.lazy = e;
49+
this.props.edit( settings );
50+
} }
51+
/>
52+
53+
</PanelBody>
54+
);
55+
}
56+
}
57+
58+
export default InstanceSettings;

classes/Visualizer/Gutenberg/src/Editor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ class Editor extends Component {
101101

102102
this.props.setAttributes({
103103
id,
104-
route: 'chartSelect'
104+
route: 'chartSelect',
105+
lazy
105106
});
106107
}
107108

@@ -483,6 +484,7 @@ class Editor extends Component {
483484
{ ( 'chartSelect' === this.state.route && null !== this.state.chart ) &&
484485
<ChartSelect
485486
id={ this.props.attributes.id }
487+
attributes={ this.props.attributes }
486488
chart={ this.state.chart }
487489
editSettings={ this.editSettings }
488490
editPermissions={ this.editPermissions }

classes/Visualizer/Gutenberg/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export default registerBlockType( 'visualizer/chart', {
3939
id: {
4040
type: 'number'
4141
},
42+
lazy: {
43+
default: '-1',
44+
type: 'string'
45+
},
4246
route: {
4347
type: 'string'
4448
}

0 commit comments

Comments
 (0)