Skip to content

Commit 9155402

Browse files
can download from library too
1 parent 5e7da28 commit 9155402

File tree

7 files changed

+42
-2
lines changed

7 files changed

+42
-2
lines changed

classes/Visualizer/Render.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,31 @@ protected function get_chart_type( $with_library = false ) {
190190
$array = explode( '_', $lib_type );
191191
return end( $array );
192192
}
193+
194+
/**
195+
* Determines if the type of chart can have a particular action.
196+
*
197+
* @since ?
198+
*
199+
* @access protected
200+
* @return bool
201+
*/
202+
protected function can_chart_have_action( $action, $chart_id = null ) {
203+
$type = null;
204+
if ( ! $chart_id ) {
205+
$type = $this->get_chart_type( false );
206+
} else {
207+
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
208+
$type = ucwords( $type );
209+
}
210+
211+
switch ( $action ) {
212+
case 'image':
213+
return ! in_array( $type, array( 'Gauge', 'Tabular', 'DataTable', 'Table' ), true );
214+
}
215+
216+
return true;
217+
}
218+
219+
193220
}

classes/Visualizer/Render/Library.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ private function _renderChartBox( $placeholder_id, $chart_id ) {
336336
echo '<a class="visualizer-chart-action visualizer-chart-clone" href="', $clone_url, '" title="', esc_attr__( 'Clone', 'visualizer' ), '"></a>';
337337
echo '<a class="visualizer-chart-action visualizer-chart-edit" href="javascript:;" title="', esc_attr__( 'Edit', 'visualizer' ), '" data-chart="', $chart_id, '"></a>';
338338
echo '<a class="visualizer-chart-action visualizer-chart-export" href="javascript:;" title="', esc_attr__( 'Export', 'visualizer' ), '" data-chart="', $export_link, '"></a>';
339+
if ( $this->can_chart_have_action( 'image', $chart_id ) ) {
340+
echo '<a class="visualizer-chart-action visualizer-chart-image" href="javascript:;" title="', esc_attr__( 'Download as image', 'visualizer' ), '" data-chart="visualizer-', $chart_id, '" data-chart-title="', $title, '"></a>';
341+
}
339342
echo '<span class="visualizer-chart-shortcode" title="', esc_attr__( 'Click to select', 'visualizer' ), '">';
340343
echo '&nbsp;[visualizer id=&quot;', $chart_id, '&quot;]&nbsp;';
341344
echo '</span>';

classes/Visualizer/Render/Sidebar.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,7 @@ protected function _renderActionSettings() {
262262
);
263263

264264
// not all charts support downloading as an image.
265-
$class = $this->get_chart_type( false );
266-
$disabled = in_array( $class, array( 'Gauge', 'Tabular', 'DataTable', 'Table' ), true );
265+
$disabled = ! $this->can_chart_have_action( 'image' );
267266
self::_renderCheckboxItem(
268267
esc_html__( 'Download Image', 'visualizer' ),
269268
'actions[]',

css/library.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@
107107
background-position: -46px -64px;
108108
}
109109

110+
.visualizer-chart-image {
111+
background-position: -206px -129px;
112+
}
113+
110114
.visualizer-nochart-clone,
111115
.visualizer-nochart-delete,
112116
.visualizer-nochart-edit {

js/library.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@
139139
return false;
140140
});
141141

142+
$(".visualizer-chart-image").on("click", function () {
143+
$('body').trigger('visualizer:action:specificchart', {action: 'image', id: $(this).attr("data-chart"), data: null, dataObj: {name: $(this).attr("data-chart-title")}});
144+
return false;
145+
});
146+
142147
// if vaction=addnew is found as a GET request parameter, show the modal.
143148
if(location.href.indexOf('vaction=addnew') !== -1){
144149
$('.add-new-chart').trigger('click');

js/render-chartjs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@
406406
});
407407

408408
// front end actions
409+
// 'image' is also called from the library
409410
$('body').on('visualizer:action:specificchart', function(event, v){
410411
var id = v.id;
411412
if(typeof rendered_charts[id] === 'undefined'){

js/render-google.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ var __visualizer_chart_images = [];
432432
});
433433

434434
// front end actions
435+
// 'image' is also called from the library
435436
$('body').on('visualizer:action:specificchart', function(event, v){
436437
var id = v.id;
437438
if(typeof rendered_charts[id] === 'undefined'){

0 commit comments

Comments
 (0)