Skip to content

Commit 9cc27b2

Browse files
authored
Merge pull request #202 from contactashish13/helpscout-141153
advanced manual JSON configuration for overriding settings
2 parents 01f8f01 + 529b1b1 commit 9cc27b2

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

classes/Visualizer/Render/Sidebar.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,35 @@ protected function _renderAdvancedSettings() {
154154

155155
$this->_renderActionSettings();
156156
self::_renderGroupEnd();
157+
158+
self::_renderGroupStart( esc_html__( 'Manual Configuration', 'visualizer' ) );
159+
self::_renderSectionStart();
160+
self::_renderSectionDescription( esc_html__( 'Configure the graph by providing values.', 'visualizer' ) );
161+
self::_renderSectionEnd();
162+
163+
$example = '
164+
{
165+
vAxis: {
166+
"ticks":[5,10,15,20]
167+
"titleTextStyle":{
168+
"color":"red"
169+
},
170+
"textPosition":"in"
171+
}
172+
}';
173+
174+
self::_renderTextAreaItem(
175+
esc_html__( 'Configuration', 'visualizer' ),
176+
'manual',
177+
$this->manual,
178+
sprintf(
179+
esc_html__( 'One per line in valid JSON (key:value) format e.g. %s', 'visualizer' ), '<br><pre>' . $example . '</pre>'
180+
),
181+
'',
182+
array( 'rows' => 5 )
183+
);
184+
self::_renderGroupEnd();
185+
157186
}
158187

159188
/**
@@ -633,4 +662,22 @@ protected static function _renderCheckboxItem( $title, $name, $value, $default,
633662
echo '</div>';
634663
}
635664

665+
/**
666+
* Render a textarea item.
667+
*/
668+
protected static function _renderTextAreaItem( $title, $name, $value, $desc, $placeholder = '', $custom_attributes = array() ) {
669+
$attributes = '';
670+
if ( $custom_attributes ) {
671+
foreach ( $custom_attributes as $k => $v ) {
672+
$attributes .= ' ' . $k . '="' . esc_attr( $v ) . '"';
673+
}
674+
}
675+
echo '<div class="section-item">';
676+
echo '<a class="more-info" href="javascript:;">[?]</a>';
677+
echo '<b>', $title, '</b>';
678+
echo '<textarea class="control-text" ', $attributes, ' name="', $name, '" placeholder="', $placeholder, '">', $value, '</textarea>';
679+
echo '<p class="section-description">', $desc, '</p>';
680+
echo '</div>';
681+
}
682+
636683
}

js/preview.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* global visualizer */
2+
/* global console */
23
(function($, v) {
34
var timeout;
45

@@ -20,12 +21,23 @@
2021
}, 1000);
2122
}
2223

24+
function validateJSON() {
25+
$('#visualizer-error-manual').remove();
26+
try{
27+
var options = JSON.parse($(this).val());
28+
}catch(error){
29+
$('<div class="visualizer-error" id="visualizer-error-manual">Invalid JSON: ' + error + '</div>').insertAfter($(this));
30+
}
31+
}
32+
2333
$('.control-text').change(updateChart).keyup(updateChart);
2434
$('.control-select, .control-checkbox').change(updateChart);
2535
$('.color-picker-hex').wpColorPicker({
2636
change: updateChart,
2737
clear: updateChart
2838
});
39+
$('textarea[name="manual"]').change(validateJSON).keyup(validateJSON);
40+
2941
});
3042
})(jQuery, visualizer);
3143

js/render.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* global google */
22
/* global visualizer */
3+
/* global console */
34
(function(v, g) {
45
var gv;
56

@@ -198,9 +199,23 @@
198199
formatter.format(table, 1);
199200
}
200201

202+
v.override(settings);
203+
201204
render.draw(table, settings);
202205
};
203206

207+
v.override = function(settings) {
208+
if (settings.manual) {
209+
try{
210+
var options = JSON.parse(settings.manual);
211+
jQuery.extend(settings, options);
212+
delete settings.manual;
213+
}catch(error){
214+
console.error("Error while adding manual configuration override " + settings.manual);
215+
}
216+
}
217+
};
218+
204219
v.render = function() {
205220
for (var id in (v.charts || {})) {
206221
v.renderChart(id);

0 commit comments

Comments
 (0)