| @@ -2,11 +2,21 @@ | ||
| 2 | 2 | /* global visualizer */ |
| 3 | 3 | |
| 4 | 4 | (function($) { |
| 5 | 5 | var all_charts; |
| 6 | + // so that we know which charts belong to our library. | |
| 7 | + var rendered_charts = []; | |
| 6 | 8 | |
| 9 | + // save the settings corresponding to cssClassNames so that when a table is being edited, they stripes etc. don't get reset | |
| 10 | + var cssClassNames; | |
| 11 | + | |
| 7 | 12 | function renderChart(id, v) { |
| 8 | - renderSpecificChart(id, all_charts[id], v); | |
| 13 | + var chart = all_charts[id]; | |
| 14 | + if(chart.library !== 'datatables'){ | |
| 15 | + return; | |
| 16 | + } | |
| 17 | + cssClassNames = null; | |
| 18 | + renderSpecificChart(id, chart, v); | |
| 9 | 19 | } |
| 10 | 20 | |
| 11 | 21 | function renderSpecificChart(id, chart, v) { |
| 12 | 22 | var render, container, series, data, table, settings, i, j, row, date, axis, property, format, formatter, type, rows, cols; |
| @@ -13,8 +23,9 @@ | ||
| 13 | 23 | |
| 14 | 24 | if(chart.library !== 'datatables'){ |
| 15 | 25 | return; |
| 16 | 26 | } |
| 27 | + rendered_charts[id] = 'yes'; | |
| 17 | 28 | |
| 18 | 29 | series = chart.series; |
| 19 | 30 | data = chart.data; |
| 20 | 31 | |
| @@ -25,9 +36,8 @@ | ||
| 25 | 36 | |
| 26 | 37 | if($('#' + id).find('table.visualizer-data-table').length > 0){ |
| 27 | 38 | $('#' + id).empty(); |
| 28 | 39 | } |
| 29 | - $('#' + id).append($('<table class="dataTable visualizer-data-table table table-striped"></table>')); | |
| 30 | 40 | |
| 31 | 41 | settings = { |
| 32 | 42 | destroy: true, |
| 33 | 43 | paging: false, |
| @@ -34,36 +44,67 @@ | ||
| 34 | 44 | searching: false, |
| 35 | 45 | ordering: true, |
| 36 | 46 | select: false, |
| 37 | 47 | lengthChange: false, |
| 48 | + buttons: { | |
| 49 | + buttons: [{ | |
| 50 | + extend: 'print', | |
| 51 | + text: '', | |
| 52 | + title: '' | |
| 53 | + }] | |
| 54 | + }, | |
| 55 | + dom: 'Bfrtip', | |
| 38 | 56 | }; |
| 39 | 57 | |
| 58 | + var $classes = 'dataTable visualizer-data-table table table-striped'; | |
| 59 | + | |
| 40 | 60 | if(typeof v.page_type !== 'undefined'){ |
| 41 | 61 | switch(v.page_type){ |
| 42 | 62 | case 'post': |
| 63 | + // fall-through. | |
| 43 | 64 | case 'library': |
| 65 | + // remove scrollY if its greater than what will fit in the box (along with the legend). | |
| 66 | + if(parseInt(chart.settings['scrollY_int']) > 180){ | |
| 67 | + chart.settings['scrollY_int']; | |
| 68 | + } | |
| 69 | + delete chart.settings['scrollX']; | |
| 44 | 70 | $.extend( settings, { |
| 45 | 71 | scrollX: 150, |
| 46 | 72 | scrollY: 180, |
| 47 | - scrollCollapse: true | |
| 48 | 73 | } ); |
| 49 | 74 | break; |
| 75 | + case 'chart': | |
| 76 | + delete chart.settings['scrollX']; // jshint ignore:line | |
| 77 | + // fall-through. | |
| 50 | 78 | case 'frontend': |
| 51 | - case 'chart': | |
| 52 | 79 | // empty. |
| 53 | 80 | break; |
| 54 | 81 | } |
| 55 | 82 | } |
| 56 | 83 | |
| 84 | + if(typeof chart.settings['scrollX'] !== 'undefined'){ | |
| 85 | + if(chart.settings['scrollX'] == 'true'){ // jshint ignore:line | |
| 86 | + $classes = $classes + ' nowrap'; | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 90 | + $('#' + id).append($('<table class="' + $classes + '"></table>')); | |
| 91 | + | |
| 57 | 92 | var select = { |
| 58 | 93 | info: false |
| 59 | 94 | }; |
| 60 | - $.extend( settings, { select } ); // jshint ignore:line | |
| 95 | + // we cannot use { select } below because https://github.com/Codeinwp/visualizer/issues/357 | |
| 96 | + $.extend( settings, { info: false } ); // jshint ignore:line | |
| 61 | 97 | |
| 62 | 98 | var stripe = ['', '']; |
| 63 | 99 | |
| 100 | + if(cssClassNames !== null){ | |
| 101 | + chart.settings['cssClassNames'] = cssClassNames; | |
| 102 | + } | |
| 103 | + | |
| 64 | 104 | // in preview mode, cssClassNames will not exist when a color is changed. |
| 65 | 105 | if(typeof chart.settings['cssClassNames'] !== 'undefined'){ |
| 106 | + cssClassNames = chart.settings['cssClassNames']; | |
| 66 | 107 | if(typeof chart.settings['cssClassNames']['oddTableRow'] !== 'undefined'){ |
| 67 | 108 | stripe[0] = chart.settings['cssClassNames']['oddTableRow']; |
| 68 | 109 | } |
| 69 | 110 | |
| @@ -77,8 +118,12 @@ | ||
| 77 | 118 | } ); |
| 78 | 119 | } |
| 79 | 120 | } |
| 80 | 121 | |
| 122 | + // remove this so that the eval does not fail. | |
| 123 | + delete chart.settings['internal_title']; | |
| 124 | + | |
| 125 | + var additional = []; | |
| 81 | 126 | for (i in chart.settings) { |
| 82 | 127 | var valoo = chart.settings[i]; |
| 83 | 128 | |
| 84 | 129 | // remove the type suffix to get the name of the setting. |
| @@ -101,14 +146,19 @@ | ||
| 101 | 146 | var array = i.split('_'); |
| 102 | 147 | if(array.length === 2){ |
| 103 | 148 | i = eval( array[0] ); // jshint ignore:line |
| 104 | 149 | i[ array[1] ] = valoo; |
| 150 | + additional[ array[0] ] = i; | |
| 151 | + if(array[0] === 'select' && array[1] === 'info' ){ | |
| 152 | + // enable main info or the selection info will not show. | |
| 153 | + $.extend( settings, {info: true} ); | |
| 154 | + } | |
| 155 | + } else { | |
| 156 | + settings[i] = valoo; | |
| 105 | 157 | } |
| 106 | - settings[i] = valoo; | |
| 107 | 158 | } |
| 159 | + $.extend( settings, additional ); | |
| 108 | 160 | |
| 109 | - $.extend( $.fn.dataTable.defaults, settings ); | |
| 110 | - | |
| 111 | 161 | cols = []; |
| 112 | 162 | for (j = 0; j < series.length; j++) { |
| 113 | 163 | type = series[j].type; |
| 114 | 164 | switch(type){ |
| @@ -140,17 +190,27 @@ | ||
| 140 | 190 | } |
| 141 | 191 | rows.push(row); |
| 142 | 192 | } |
| 143 | 193 | |
| 194 | + $.extend( settings, { | |
| 195 | + data: rows, | |
| 196 | + columns: cols, | |
| 197 | + stripeClasses: stripe, | |
| 198 | + } ); | |
| 199 | + | |
| 144 | 200 | // allow user to extend the settings. |
| 145 | 201 | $('body').trigger('visualizer:chart:settings:extend', {id: id, chart: chart, settings: settings}); |
| 146 | 202 | |
| 147 | 203 | table = $('#' + id + ' table.visualizer-data-table'); |
| 148 | - table.DataTable( { | |
| 149 | - data: rows, | |
| 150 | - columns: cols, | |
| 151 | - stripeClasses: stripe, | |
| 152 | - } ); | |
| 204 | + table.DataTable( settings ); | |
| 205 | + | |
| 206 | + // header row is handled here as the class is added dynamically to it (after the table is rendered). | |
| 207 | + if(typeof chart.settings['cssClassNames'] !== 'undefined'){ | |
| 208 | + if(typeof chart.settings['cssClassNames']['headerRow'] !== 'undefined'){ | |
| 209 | + $('#' + id + ' table thead tr').addClass( chart.settings['cssClassNames']['headerRow'] ); | |
| 210 | + } | |
| 211 | + } | |
| 212 | + | |
| 153 | 213 | $('.loader').remove(); |
| 154 | 214 | } |
| 155 | 215 | |
| 156 | 216 | function addRenderer(type, series, index){ |
| @@ -202,9 +262,9 @@ | ||
| 202 | 262 | |
| 203 | 263 | if(typeof visualizer !== 'undefined'){ |
| 204 | 264 | // called while updating the chart. |
| 205 | 265 | visualizer.update = function(){ |
| 206 | - renderChart('canvas', visualizer); | |
| 266 | + renderSpecificChart('canvas', all_charts['canvas'], visualizer); | |
| 207 | 267 | }; |
| 208 | 268 | } |
| 209 | 269 | |
| 210 | 270 | $('body').on('visualizer:render:chart:start', function(event, v){ |
| @@ -215,8 +275,20 @@ | ||
| 215 | 275 | $('body').on('visualizer:render:specificchart:start', function(event, v){ |
| 216 | 276 | renderSpecificChart(v.id, v.chart, v.v); |
| 217 | 277 | }); |
| 218 | 278 | |
| 279 | + // front end actions | |
| 280 | + $('body').on('visualizer:action:specificchart', function(event, v){ | |
| 281 | + switch(v.action){ | |
| 282 | + case 'print': | |
| 283 | + var id = v.id; | |
| 284 | + if(typeof rendered_charts[id] === 'undefined'){ | |
| 285 | + return; | |
| 286 | + } | |
| 287 | + $('#' + id + ' .buttons-print').trigger('click'); | |
| 288 | + break; | |
| 289 | + } | |
| 290 | + }); | |
| 219 | 291 | |
| 220 | 292 | })(jQuery); |
| 221 | 293 | |
| 222 | 294 | |