| @@ -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,78 @@ | ||
| 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': |
| 44 | - $.extend( settings, { | |
| 65 | + // for smaller screens... | |
| 66 | + if(window.innerWidth < 1500){ | |
| 67 | + delete chart.settings['scrollX']; | |
| 68 | + delete chart.settings['scrollY']; | |
| 69 | + delete chart.settings['scrollY_int']; | |
| 70 | + $.extend( settings, { | |
| 45 | 71 | scrollX: 150, |
| 72 | + scrollY: (( chart.settings['responsive_bool'] === 'true' ? 0.8 : 0.5 ) * parseInt($(container).css('height').replace('px',''))), | |
| 73 | + scrollCollapse: true | |
| 74 | + } ); | |
| 75 | + }else{ | |
| 76 | + if(parseInt(chart.settings['scrollY_int']) > 180){ | |
| 77 | + chart.settings['scrollY_int']; | |
| 78 | + } | |
| 79 | + delete chart.settings['scrollX']; | |
| 80 | + $.extend( settings, { | |
| 81 | + scrollX: 150, | |
| 46 | 82 | scrollY: 180, |
| 47 | - scrollCollapse: true | |
| 48 | - } ); | |
| 83 | + } ); | |
| 84 | + } | |
| 49 | 85 | break; |
| 86 | + case 'chart': | |
| 87 | + delete chart.settings['scrollX']; // jshint ignore:line | |
| 88 | + // fall-through. | |
| 50 | 89 | case 'frontend': |
| 51 | - case 'chart': | |
| 52 | 90 | // empty. |
| 53 | 91 | break; |
| 54 | 92 | } |
| 55 | 93 | } |
| 56 | 94 | |
| 95 | + if(typeof chart.settings['scrollX'] !== 'undefined'){ | |
| 96 | + if(chart.settings['scrollX'] == 'true'){ // jshint ignore:line | |
| 97 | + $classes = $classes + ' nowrap'; | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + $('#' + id).append($('<table class="' + $classes + '"></table>')); | |
| 102 | + | |
| 57 | 103 | var select = { |
| 58 | 104 | info: false |
| 59 | 105 | }; |
| 60 | - $.extend( settings, { select } ); // jshint ignore:line | |
| 106 | + // we cannot use { select } below because https://github.com/Codeinwp/visualizer/issues/357 | |
| 107 | + $.extend( settings, { info: false } ); // jshint ignore:line | |
| 61 | 108 | |
| 62 | 109 | var stripe = ['', '']; |
| 63 | 110 | |
| 111 | + if(cssClassNames !== null){ | |
| 112 | + chart.settings['cssClassNames'] = cssClassNames; | |
| 113 | + } | |
| 114 | + | |
| 64 | 115 | // in preview mode, cssClassNames will not exist when a color is changed. |
| 65 | 116 | if(typeof chart.settings['cssClassNames'] !== 'undefined'){ |
| 117 | + cssClassNames = chart.settings['cssClassNames']; | |
| 66 | 118 | if(typeof chart.settings['cssClassNames']['oddTableRow'] !== 'undefined'){ |
| 67 | 119 | stripe[0] = chart.settings['cssClassNames']['oddTableRow']; |
| 68 | 120 | } |
| 69 | 121 | |
| @@ -77,8 +129,12 @@ | ||
| 77 | 129 | } ); |
| 78 | 130 | } |
| 79 | 131 | } |
| 80 | 132 | |
| 133 | + // remove this so that the eval does not fail. | |
| 134 | + delete chart.settings['internal_title']; | |
| 135 | + | |
| 136 | + var additional = []; | |
| 81 | 137 | for (i in chart.settings) { |
| 82 | 138 | var valoo = chart.settings[i]; |
| 83 | 139 | |
| 84 | 140 | // remove the type suffix to get the name of the setting. |
| @@ -94,8 +150,11 @@ | ||
| 94 | 150 | default: |
| 95 | 151 | if(parseInt(valoo) > 0){ |
| 96 | 152 | valoo = parseInt(valoo); |
| 97 | 153 | } |
| 154 | + if(i === 'pageLength' && (valoo === '' || parseInt(valoo) < 0)){ | |
| 155 | + valoo = 1; | |
| 156 | + } | |
| 98 | 157 | } |
| 99 | 158 | |
| 100 | 159 | // if the setting name has an '_' this means it is a sub-setting e.g. select_items means { select: { items: ... } }. |
| 101 | 160 | var array = i.split('_'); |
| @@ -101,14 +160,19 @@ | ||
| 101 | 160 | var array = i.split('_'); |
| 102 | 161 | if(array.length === 2){ |
| 103 | 162 | i = eval( array[0] ); // jshint ignore:line |
| 104 | 163 | i[ array[1] ] = valoo; |
| 164 | + additional[ array[0] ] = i; | |
| 165 | + if(array[0] === 'select' && array[1] === 'info' ){ | |
| 166 | + // enable main info or the selection info will not show. | |
| 167 | + $.extend( settings, {info: true} ); | |
| 168 | + } | |
| 169 | + } else { | |
| 170 | + settings[i] = valoo; | |
| 105 | 171 | } |
| 106 | - settings[i] = valoo; | |
| 107 | 172 | } |
| 173 | + $.extend( settings, additional ); | |
| 108 | 174 | |
| 109 | - $.extend( $.fn.dataTable.defaults, settings ); | |
| 110 | - | |
| 111 | 175 | cols = []; |
| 112 | 176 | for (j = 0; j < series.length; j++) { |
| 113 | 177 | type = series[j].type; |
| 114 | 178 | switch(type){ |
| @@ -120,9 +184,8 @@ | ||
| 120 | 184 | case 'timeofday': |
| 121 | 185 | type = 'date'; |
| 122 | 186 | break; |
| 123 | 187 | } |
| 124 | - | |
| 125 | 188 | render = addRenderer(series[j].type, settings.series, j); |
| 126 | 189 | |
| 127 | 190 | var col = { title: series[j].label, data: series[j].label, type: type, render: render }; |
| 128 | 191 | if(typeof chart.settings['cssClassNames'] !== 'undefined' && typeof chart.settings['cssClassNames']['tableCell'] !== 'undefined'){ |
| @@ -135,22 +198,36 @@ | ||
| 135 | 198 | for (i = 0; i < data.length; i++) { |
| 136 | 199 | row = []; |
| 137 | 200 | for (j = 0; j < series.length; j++) { |
| 138 | 201 | var datum = data[i][j]; |
| 202 | + // datum could be undefined for dynamic data (e.g. through json). | |
| 203 | + if(typeof datum === 'undefined'){ | |
| 204 | + datum = data[i][series[j].label]; | |
| 205 | + } | |
| 139 | 206 | row[ series[j].label ] = datum; |
| 140 | 207 | } |
| 141 | 208 | rows.push(row); |
| 142 | 209 | } |
| 143 | 210 | |
| 211 | + $.extend( settings, { | |
| 212 | + data: rows, | |
| 213 | + columns: cols, | |
| 214 | + stripeClasses: stripe, | |
| 215 | + } ); | |
| 216 | + | |
| 144 | 217 | // allow user to extend the settings. |
| 145 | 218 | $('body').trigger('visualizer:chart:settings:extend', {id: id, chart: chart, settings: settings}); |
| 146 | 219 | |
| 147 | 220 | table = $('#' + id + ' table.visualizer-data-table'); |
| 148 | - table.DataTable( { | |
| 149 | - data: rows, | |
| 150 | - columns: cols, | |
| 151 | - stripeClasses: stripe, | |
| 152 | - } ); | |
| 221 | + table.DataTable( settings ); | |
| 222 | + | |
| 223 | + // header row is handled here as the class is added dynamically to it (after the table is rendered). | |
| 224 | + if(typeof chart.settings['cssClassNames'] !== 'undefined'){ | |
| 225 | + if(typeof chart.settings['cssClassNames']['headerRow'] !== 'undefined'){ | |
| 226 | + $('#' + id + ' table thead tr').addClass( chart.settings['cssClassNames']['headerRow'] ); | |
| 227 | + } | |
| 228 | + } | |
| 229 | + | |
| 153 | 230 | $('.loader').remove(); |
| 154 | 231 | } |
| 155 | 232 | |
| 156 | 233 | function addRenderer(type, series, index){ |
| @@ -169,10 +246,13 @@ | ||
| 169 | 246 | parts[0] = series.format.thousands; |
| 170 | 247 | } |
| 171 | 248 | if(typeof series.format.decimal !== ''){ |
| 172 | 249 | parts[1] = series.format.decimal; |
| 250 | + if ( '' === series.format.precision ) { | |
| 251 | + series.format.precision = 2; | |
| 252 | + } | |
| 173 | 253 | } |
| 174 | - if(typeof series.format.precision !== ''){ | |
| 254 | + if(typeof series.format.precision !== '' && parseInt(series.format.precision) > 0){ | |
| 175 | 255 | parts[2] = series.format.precision; |
| 176 | 256 | } |
| 177 | 257 | if(typeof series.format.prefix !== ''){ |
| 178 | 258 | parts[3] = series.format.prefix; |
| @@ -188,10 +268,21 @@ | ||
| 188 | 268 | if(typeof series.format.to !== 'undefined' && typeof series.format.from !== 'undefined' && series.format.from != '' && series.format.to !== ''){ |
| 189 | 269 | render = $.fn.dataTable.render.moment(series.format.from, series.format.to); |
| 190 | 270 | } |
| 191 | 271 | break; |
| 272 | + default: | |
| 273 | + render = $.fn.dataTable.render.extra = function ( data, type, row ) { | |
| 274 | + if((data === true || data === 'true') && typeof series.format !== 'undefined' && series.format.truthy !== ''){ | |
| 275 | + data = series.format.truthy.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, ''); | |
| 276 | + } | |
| 277 | + if((data === false || data === 'false') && typeof series.format !== 'undefined' && series.format.falsy !== ''){ | |
| 278 | + data = series.format.falsy.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, ''); | |
| 279 | + } | |
| 280 | + return data; | |
| 281 | + } | |
| 192 | 282 | } |
| 193 | 283 | /* jshint ignore:end */ |
| 284 | + | |
| 194 | 285 | return render; |
| 195 | 286 | } |
| 196 | 287 | |
| 197 | 288 | function render(v) { |
| @@ -199,18 +290,16 @@ | ||
| 199 | 290 | renderChart(id, v); |
| 200 | 291 | } |
| 201 | 292 | } |
| 202 | 293 | |
| 203 | - if(typeof visualizer !== 'undefined'){ | |
| 204 | - // called while updating the chart. | |
| 205 | - visualizer.update = function(){ | |
| 206 | - renderChart('canvas', visualizer); | |
| 207 | - }; | |
| 208 | - } | |
| 209 | - | |
| 210 | 294 | $('body').on('visualizer:render:chart:start', function(event, v){ |
| 211 | 295 | all_charts = v.charts; |
| 212 | - render(v); | |
| 296 | + | |
| 297 | + if(v.is_front == true && typeof v.id !== 'undefined'){ // jshint ignore:line | |
| 298 | + renderChart(v.id, v); | |
| 299 | + } else { | |
| 300 | + render(v); | |
| 301 | + } | |
| 213 | 302 | }); |
| 214 | 303 | |
| 215 | 304 | $('body').on('visualizer:render:specificchart:start', function(event, v){ |
| 216 | 305 | renderSpecificChart(v.id, v.chart, v.v); |
| @@ -215,8 +304,25 @@ | ||
| 215 | 304 | $('body').on('visualizer:render:specificchart:start', function(event, v){ |
| 216 | 305 | renderSpecificChart(v.id, v.chart, v.v); |
| 217 | 306 | }); |
| 218 | 307 | |
| 308 | + $('body').on('visualizer:render:currentchart:update', function(event, v){ | |
| 309 | + var data = v || event.detail; | |
| 310 | + renderSpecificChart('canvas', all_charts['canvas'], data.visualizer); | |
| 311 | + }); | |
| 312 | + | |
| 313 | + // front end actions | |
| 314 | + $('body').on('visualizer:action:specificchart', function(event, v){ | |
| 315 | + switch(v.action){ | |
| 316 | + case 'print': | |
| 317 | + var id = v.id; | |
| 318 | + if(typeof rendered_charts[id] === 'undefined'){ | |
| 319 | + return; | |
| 320 | + } | |
| 321 | + $('#' + id + ' .buttons-print').trigger('click'); | |
| 322 | + break; | |
| 323 | + } | |
| 324 | + }); | |
| 219 | 325 | |
| 220 | 326 | })(jQuery); |
| 221 | 327 | |
| 222 | 328 | |