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